This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ngTranscludeSelect): add ng-transclude-select directive
The ng-translude-select directive allows users to transclude multiple templates into their directive.
- Loading branch information
Kara Erickson
committed
Jun 2, 2015
1 parent
d7dc14d
commit def1619
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict'; | ||
|
||
/** | ||
* @ngdoc directive | ||
* @name ngTranscludeSelect | ||
* @restrict EAC | ||
* | ||
* @description | ||
* | ||
* @element ANY | ||
* | ||
*/ | ||
var ngTranscludeSelectDirective = ngDirective({ | ||
restrict: 'EAC', | ||
link: function($scope, $element, $attrs, controller, $transclude) { | ||
checkTranscludeOption(); | ||
$transclude(transcludeSelection); | ||
|
||
function transcludeSelection(clone) { | ||
var selector = $attrs.ngTranscludeSelect; | ||
var selectedElements = getSelectedElements(clone, selector); | ||
if (selectedElements.length) $element.append(selectedElements); | ||
} | ||
|
||
function getSelectedElements(clone, selector) { | ||
var wrapper = wrapClone(clone); | ||
var selectedElements = wrapper[0].querySelectorAll(selector); | ||
wrapper.remove(); | ||
return selectedElements; | ||
} | ||
|
||
function wrapClone(clone) { | ||
var wrapper = jqLite("<div>"); | ||
wrapper.append(clone); | ||
return wrapper; | ||
} | ||
|
||
function checkTranscludeOption() { | ||
if (!$transclude) { | ||
throw minErr('ngTransclude')('orphan', | ||
'Illegal use of ngTranscludeSelect directive in the template! ' + | ||
'No parent directive that requires a transclusion found. ' + | ||
'Element: {0}', | ||
startingTag($element)); | ||
} | ||
} | ||
} | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
|
||
|
||
describe('ngTranscludeSelect', function() { | ||
it('should append matching clone elements to ng-transclude-select elements', function() { | ||
module(function($compileProvider) { | ||
$compileProvider.directive('transclude', valueFn({ | ||
transclude: true, | ||
scope: {}, | ||
template: '<div ng-transclude-select="[top]"></div><div ng-transclude-select="[bottom]"></div>' | ||
})); | ||
}); | ||
inject(function($compile, $rootScope) { | ||
var topTarget, bottomTarget; | ||
var element = $compile( | ||
'<div transclude><div bottom>In bottom.</div><div top>In top.</div></div></div>' | ||
)($rootScope); | ||
topTarget = jqLite(element[0].querySelector('[ng-transclude-select="[top]"]')); | ||
bottomTarget = jqLite(element[0].querySelector('[ng-transclude-select="[bottom]"]')); | ||
expect(topTarget.text()).toEqual('In top.'); | ||
expect(bottomTarget.text()).toEqual('In bottom.'); | ||
}); | ||
}); | ||
it('should throw on an ng-transclude-select element inside no transclusion directive', function() { | ||
inject(function($rootScope, $compile) { | ||
try { | ||
$compile('<div><div ng-transclude-select="[top]"></div></div>')($rootScope); | ||
} catch (e) { | ||
expect(e.message).toMatch(new RegExp( | ||
'^\\[ngTransclude:orphan\\] ' + | ||
'Illegal use of ngTranscludeSelect directive in the template! ' + | ||
'No parent directive that requires a transclusion found\\. ' + | ||
'Element: <div ng-transclude-select.+')); | ||
} | ||
}); | ||
}); | ||
}); |