Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Commit fb71e22

Browse files
committed
feat(transclude): use directive instead of class to mark injection point
1 parent 441f8d2 commit fb71e22

File tree

4 files changed

+38
-36
lines changed

4 files changed

+38
-36
lines changed

src/select.js

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -354,27 +354,6 @@ angular.module('ui.select', [])
354354
$document.off('click', onDocumentClick);
355355
});
356356

357-
// Move transcluded elements to their correct position in main template
358-
transcludeFn(scope, function(clone) {
359-
// See Transclude in AngularJS http://blog.omkarpatil.com/2012/11/transclude-in-angularjs.html
360-
361-
// One day jqLite will be replaced by jQuery and we will be able to write:
362-
// var transcludedElement = clone.filter('.my-class')
363-
// instead of creating a hackish DOM element:
364-
var transcluded = angular.element('<div>').append(clone);
365-
366-
var transcludedMatch = transcluded.querySelectorAll('.ui-select-match');
367-
if (transcludedMatch.length !== 1) {
368-
throw uiSelectMinErr('transcluded', "Expected 1 .ui-select-match but got '{0}'.", transcludedMatch.length);
369-
}
370-
element.querySelectorAll('.ui-select-match').replaceWith(transcludedMatch);
371-
372-
var transcludedChoices = transcluded.querySelectorAll('.ui-select-choices');
373-
if (transcludedChoices.length !== 1) {
374-
throw uiSelectMinErr('transcluded', "Expected 1 .ui-select-choices but got '{0}'.", transcludedChoices.length);
375-
}
376-
element.querySelectorAll('.ui-select-choices').replaceWith(transcludedChoices);
377-
});
378357
}
379358
};
380359
}])
@@ -406,17 +385,8 @@ angular.module('ui.select', [])
406385
rows.attr('ng-repeat', RepeatParser.getNgRepeatExpression(repeat.lhs, '$select.items', repeat.trackByExp))
407386
.attr('ng-mouseenter', '$select.activeIndex = $index')
408387
.attr('ng-click', '$select.select(' + repeat.lhs + ')');
409-
410-
411-
transcludeFn(function(clone) {
412-
var rowsInner = element.querySelectorAll('.ui-select-choices-row-inner');
413-
if (rowsInner.length !== 1)
414-
throw uiSelectMinErr('rows', "Expected 1 .ui-select-choices-row-inner but got '{0}'.", rowsInner.length);
415-
416-
rowsInner.append(clone);
417-
$compile(element)(scope);
418-
});
419-
388+
389+
$compile(element)(scope);
420390
$select.parseRepeatAttr(attrs.repeat);
421391

422392
scope.$watch('$select.search', function() {
@@ -453,6 +423,38 @@ angular.module('ui.select', [])
453423
};
454424
}])
455425

426+
427+
.directive( 'transinject', function($compile, uiSelectMinErr) {
428+
return {
429+
link: function( scope, element, attrs, controller, transcludeFn ) {
430+
431+
if (!transcludeFn){
432+
return;
433+
}
434+
435+
transcludeFn( scope, function( clone ) {
436+
437+
var injecting = clone;
438+
439+
if (attrs.transinject){
440+
var cloneContainer = angular.element('<div>').append(clone);
441+
var cloneFiltered = cloneContainer.querySelectorAll(attrs.transinject);
442+
443+
if (cloneFiltered.length !== 1) {
444+
throw uiSelectMinErr('transcluded', "Expected 1 .xxx but got '{0}'.", cloneFiltered.length);
445+
}
446+
447+
injecting = cloneFiltered;
448+
}
449+
450+
element.empty();
451+
element.append( injecting );
452+
453+
});
454+
}
455+
};
456+
})
457+
456458
/**
457459
* Highlights text that matches $select.search.
458460
*

src/select2/choices.tpl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<ul class="ui-select-choices ui-select-choices-content select2-results">
22
<li class="ui-select-choices-row" ng-class="{'select2-highlighted': $select.activeIndex === $index}">
3-
<div class="select2-result-label ui-select-choices-row-inner"></div>
3+
<div class="select2-result-label" transinject></div>
44
</li>
55
</ul>

src/select2/match.tpl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
ng-class="{'select2-default': $select.selected === undefined}"
88
ng-click="$select.activate()">
99
<span ng-hide="$select.selected !== undefined" class="select2-chosen">{{$select.placeholder}}</span>
10-
<span ng-show="$select.selected !== undefined" class="select2-chosen" ng-transclude></span>
10+
<span ng-show="$select.selected !== undefined" class="select2-chosen" transinject></span>
1111
<span class="select2-arrow"><b></b></span>
1212
</a>

src/select2/select.tpl.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<div class="select2 select2-container"
22
ng-class="{'select2-container-active select2-dropdown-open': $select.open,
33
'select2-container-disabled': $select.disabled}">
4-
<div class="ui-select-match"></div>
4+
<div transinject=".ui-select-match"></div>
55
<div class="select2-drop select2-with-searchbox select2-drop-active"
66
ng-class="{'select2-display-none': !$select.open}">
77
<div class="select2-search">
88
<input type="text" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"
99
class="ui-select-search select2-input"
1010
ng-model="$select.search">
1111
</div>
12-
<div class="ui-select-choices"></div>
12+
<div transinject=".ui-select-choices"></div>
1313
</div>
1414
</div>

0 commit comments

Comments
 (0)