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

Commit f5b986b

Browse files
committed
refactor(uiSelectChoices) only compile template once
Previously the template was compiled, edited, and then recompiled. This was unnecessary as the changes we unrelated to the scope and so could be made before the link function was run. This change, moves the DOM transformations to the compile function so the directive does not have to be compiled manually. Fixes #1000
1 parent c0d039b commit f5b986b

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

Diff for: src/uiSelectChoicesDirective.js

+35-33
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,46 @@ uis.directive('uiSelectChoices',
2020

2121
if (!tAttrs.repeat) throw uiSelectMinErr('repeat', "Expected 'repeat' expression.");
2222

23-
return function link(scope, element, attrs, $select, transcludeFn) {
24-
25-
// var repeat = RepeatParser.parse(attrs.repeat);
26-
var groupByExp = attrs.groupBy;
27-
var groupFilterExp = attrs.groupFilter;
28-
23+
// var repeat = RepeatParser.parse(attrs.repeat);
24+
var groupByExp = tAttrs.groupBy;
25+
var groupFilterExp = tAttrs.groupFilter;
26+
27+
if (groupByExp) {
28+
var groups = tElement.querySelectorAll('.ui-select-choices-group');
29+
if (groups.length !== 1) throw uiSelectMinErr('rows', "Expected 1 .ui-select-choices-group but got '{0}'.", groups.length);
30+
groups.attr('ng-repeat', RepeatParser.getGroupNgRepeatExpression());
31+
}
32+
33+
var parserResult = RepeatParser.parse(tAttrs.repeat);
34+
35+
var choices = tElement.querySelectorAll('.ui-select-choices-row');
36+
if (choices.length !== 1) {
37+
throw uiSelectMinErr('rows', "Expected 1 .ui-select-choices-row but got '{0}'.", choices.length);
38+
}
39+
40+
choices.attr('ng-repeat', parserResult.repeatExpression(groupByExp))
41+
.attr('ng-if', '$select.open'); //Prevent unnecessary watches when dropdown is closed
42+
43+
44+
var rowsInner = tElement.querySelectorAll('.ui-select-choices-row-inner');
45+
if (rowsInner.length !== 1) {
46+
throw uiSelectMinErr('rows', "Expected 1 .ui-select-choices-row-inner but got '{0}'.", rowsInner.length);
47+
}
48+
rowsInner.attr('uis-transclude-append', ''); //Adding uisTranscludeAppend directive to row element after choices element has ngRepeat
49+
50+
// If IE8 then need to target rowsInner to apply the ng-click attr as choices will not capture the event.
51+
var clickTarget = $window.document.addEventListener ? choices : rowsInner;
52+
clickTarget.attr('ng-click', '$select.select(' + parserResult.itemName + ',$select.skipFocusser,$event)');
53+
54+
return function link(scope, element, attrs, $select) {
55+
56+
2957
$select.parseRepeatAttr(attrs.repeat, groupByExp, groupFilterExp); //Result ready at $select.parserResult
3058

3159
$select.disableChoiceExpression = attrs.uiDisableChoice;
3260
$select.onHighlightCallback = attrs.onHighlight;
3361

34-
$select.dropdownPosition = attrs.position ? attrs.position.toLowerCase() : uiSelectConfig.dropdownPosition;
35-
36-
if(groupByExp) {
37-
var groups = element.querySelectorAll('.ui-select-choices-group');
38-
if (groups.length !== 1) throw uiSelectMinErr('rows', "Expected 1 .ui-select-choices-group but got '{0}'.", groups.length);
39-
groups.attr('ng-repeat', RepeatParser.getGroupNgRepeatExpression());
40-
}
41-
42-
var choices = element.querySelectorAll('.ui-select-choices-row');
43-
if (choices.length !== 1) {
44-
throw uiSelectMinErr('rows', "Expected 1 .ui-select-choices-row but got '{0}'.", choices.length);
45-
}
46-
47-
choices.attr('ng-repeat', $select.parserResult.repeatExpression(groupByExp))
48-
.attr('ng-if', '$select.open'); //Prevent unnecessary watches when dropdown is closed
49-
if ($window.document.addEventListener) { //crude way to exclude IE8, specifically, which also cannot capture events
50-
choices.attr('ng-click', '$select.select(' + $select.parserResult.itemName + ',$select.skipFocusser,$event)');
51-
}
52-
53-
var rowsInner = element.querySelectorAll('.ui-select-choices-row-inner');
54-
if (rowsInner.length !== 1) throw uiSelectMinErr('rows', "Expected 1 .ui-select-choices-row-inner but got '{0}'.", rowsInner.length);
55-
rowsInner.attr('uis-transclude-append', ''); //Adding uisTranscludeAppend directive to row element after choices element has ngRepeat
56-
if (!$window.document.addEventListener) { //crude way to target IE8, specifically, which also cannot capture events - so event bindings must be here
57-
rowsInner.attr('ng-click', '$select.select(' + $select.parserResult.itemName + ',$select.skipFocusser,$event)');
58-
}
59-
60-
$compile(element, transcludeFn)(scope); //Passing current transcludeFn to be able to append elements correctly from uisTranscludeAppend
62+
$select.dropdownPosition = attrs.position ? attrs.position.toLowerCase() : uiSelectConfig.dropdownPosition;
6163

6264
scope.$on('$destroy', function() {
6365
choices.remove();

0 commit comments

Comments
 (0)