From 96790804af77c04d522fc2cf867240fb047c2114 Mon Sep 17 00:00:00 2001 From: Avien Date: Thu, 25 Feb 2016 11:31:17 +0200 Subject: [PATCH 1/3] Improve performance, add filter emit event --- dist/select.js | 79 ++++++++++++++++---------------------------------- 1 file changed, 25 insertions(+), 54 deletions(-) diff --git a/dist/select.js b/dist/select.js index 5fdbd0df3..57a3d0507 100644 --- a/dist/select.js +++ b/dist/select.js @@ -1,8 +1,9 @@ /*! * ui-select * http://github.com/angular-ui/ui-select - * Version: 0.14.8 - 2016-02-18T22:01:43.792Z + * Version: 0.14.7.1 - 2016-02-18T21:01:36.893Z * License: MIT + * Forked: Avien */ @@ -175,8 +176,8 @@ var uis = angular.module('ui.select', []) }]); uis.directive('uiSelectChoices', - ['uiSelectConfig', 'uisRepeatParser', 'uiSelectMinErr', '$compile', '$window', - function(uiSelectConfig, RepeatParser, uiSelectMinErr, $compile, $window) { + ['uiSelectConfig', 'uisRepeatParser', 'uiSelectMinErr', '$compile', + function(uiSelectConfig, RepeatParser, uiSelectMinErr, $compile) { return { restrict: 'EA', @@ -221,19 +222,12 @@ uis.directive('uiSelectChoices', } choices.attr('ng-repeat', $select.parserResult.repeatExpression(groupByExp)) - .attr('ng-if', '$select.open'); //Prevent unnecessary watches when dropdown is closed - if ($window.document.addEventListener) { //crude way to exclude IE8, specifically, which also cannot capture events - choices.attr('ng-mouseenter', '$select.setActiveItem('+$select.parserResult.itemName +')') - .attr('ng-click', '$select.select(' + $select.parserResult.itemName + ',false,$event)'); - } + .attr('ng-if', '$select.open') //Prevent unnecessary watches when dropdown is closed + .attr('ng-click', '$select.select(' + $select.parserResult.itemName + ',false,$event)'); var rowsInner = element.querySelectorAll('.ui-select-choices-row-inner'); if (rowsInner.length !== 1) throw uiSelectMinErr('rows', "Expected 1 .ui-select-choices-row-inner but got '{0}'.", rowsInner.length); rowsInner.attr('uis-transclude-append', ''); //Adding uisTranscludeAppend directive to row element after choices element has ngRepeat - if (!$window.document.addEventListener) { //crude way to target IE8, specifically, which also cannot capture events - so event bindings must be here - rowsInner.attr('ng-mouseenter', '$select.setActiveItem('+$select.parserResult.itemName +')') - .attr('ng-click', '$select.select(' + $select.parserResult.itemName + ',false,$event)'); - } $compile(element, transcludeFn)(scope); //Passing current transcludeFn to be able to append elements correctly from uisTranscludeAppend @@ -380,29 +374,14 @@ uis.controller('uiSelectCtrl', ctrl.activeIndex = 0; } - var container = $element.querySelectorAll('.ui-select-choices-content'); - if (ctrl.$animate && ctrl.$animate.enabled(container[0])) { - ctrl.$animate.on('enter', container[0], function (elem, phase) { - if (phase === 'close') { - // Only focus input after the animation has finished - $timeout(function () { - ctrl.focusSearchInput(initSearchValue); - }); - } - }); - } else { - $timeout(function () { - ctrl.focusSearchInput(initSearchValue); - }); - } - } - }; - - ctrl.focusSearchInput = function (initSearchValue) { - ctrl.search = initSearchValue || ctrl.search; - ctrl.searchInput[0].focus(); - if(!ctrl.tagging.isActivated && ctrl.items.length > 1) { - _ensureHighlightVisible(); + // Give it time to appear before focus + $timeout(function() { + ctrl.search = initSearchValue || ctrl.search; + ctrl.searchInput[0].focus(); + if(!ctrl.tagging.isActivated && ctrl.items.length > 1) { + _ensureHighlightVisible(); + } + }); } }; @@ -492,7 +471,7 @@ uis.controller('uiSelectCtrl', }; // See https://github.com/angular/angular.js/blob/v1.2.15/src/ng/directive/ngRepeat.js#L259 - $scope.$watchCollection(ctrl.parserResult.source, function(items) { + $scope.$watchCollection(ctrl.parserResult.source, function(items,oldItems) { if (items === undefined || items === null) { // If the user specifies undefined or null => reset the collection // Special case: items can be undefined if the user did not initialized the collection on the scope @@ -506,6 +485,9 @@ uis.controller('uiSelectCtrl', //TODO Should add a test ctrl.refreshItems(items); ctrl.ngModel.$modelValue = null; //Force scope model value and ngModel value to be out of sync to re-run formatters + if(ctrl.search =="" && items != oldItems) { //don't broadcast the event while searching + $scope.$emit('uis:filter'); + } } } }); @@ -750,11 +732,6 @@ uis.controller('uiSelectCtrl', var key = e.which; - if (~[KEY.ENTER,KEY.ESC].indexOf(key)){ - e.preventDefault(); - e.stopPropagation(); - } - // if(~[KEY.ESC,KEY.TAB].indexOf(key)){ // //TODO: SEGURO? // ctrl.close(); @@ -1144,9 +1121,6 @@ uis.directive('uiSelect', element[0].style.left = ''; element[0].style.top = ''; element[0].style.width = originalWidth; - - // Set focus back on to the moved element - $select.setFocus(); } // Hold on to a reference to the .ui-select-dropdown element for direction support. @@ -1197,7 +1171,7 @@ uis.directive('uiSelect', dropdown[0].style.opacity = 0; // Delay positioning the dropdown until all choices have been added so its height is correct. - $timeout(function(){ + //$timeout(function(){ if ($select.dropdownPosition === 'up'){ //Go UP @@ -1226,7 +1200,7 @@ uis.directive('uiSelect', // Display the dropdown once it has been positioned. dropdown[0].style.opacity = 1; - }); + //}); } else { if (dropdown === null || dropdown.length === 0) { return; @@ -1290,10 +1264,7 @@ uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', function(uiSelec $select = $scope.$select, ngModel; - if (angular.isUndefined($select.selected)) - $select.selected = []; - - //Wait for link fn to inject it + //Wait for link fn to inject it $scope.$evalAsync(function(){ ngModel = $scope.ngModel; }); ctrl.activeMatchIndex = -1; @@ -1305,7 +1276,7 @@ uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', function(uiSelec ctrl.refreshComponent = function(){ //Remove already selected items - //e.g. When user clicks on a selection, the selected array changes and + //e.g. When user clicks on a selection, the selected array changes and //the dropdown should remove that item $select.refreshItems(); $select.sizeSearchInput(); @@ -1407,7 +1378,7 @@ uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', function(uiSelec }; if (!inputValue) return resultMultiple; //If ngModel was undefined for (var k = inputValue.length - 1; k >= 0; k--) { - //Check model array of currently selected items + //Check model array of currently selected items if (!checkFnMultiple($select.selected, inputValue[k])){ //Check model array of all items available if (!checkFnMultiple(data, inputValue[k])){ @@ -1418,8 +1389,8 @@ uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', function(uiSelec } return resultMultiple; }); - - //Watch for external model changes + + //Watch for external model changes scope.$watchCollection(function(){ return ngModel.$modelValue; }, function(newValue, oldValue) { if (oldValue != newValue){ ngModel.$modelValue = null; //Force scope model value and ngModel value to be out of sync to re-run formatters From e581dcb57f696ae6acbe3c730a02c4dfbd86c6bd Mon Sep 17 00:00:00 2001 From: Avien Date: Thu, 25 Feb 2016 11:33:29 +0200 Subject: [PATCH 2/3] Update version --- dist/select.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dist/select.js b/dist/select.js index 57a3d0507..fadeb3671 100644 --- a/dist/select.js +++ b/dist/select.js @@ -1,7 +1,7 @@ /*! * ui-select * http://github.com/angular-ui/ui-select - * Version: 0.14.7.1 - 2016-02-18T21:01:36.893Z + * Version: 0.14.9.1 - 2016-02-25 * License: MIT * Forked: Avien */ From 79838e97875f1d319f2087cd1cb14329deefa6a5 Mon Sep 17 00:00:00 2001 From: Avien Date: Thu, 25 Feb 2016 12:10:25 +0200 Subject: [PATCH 3/3] Improve performance, add filter emit event --- src/uiSelectController.js | 57 +++++++++++++++------------------------ src/uiSelectDirective.js | 4 +-- 2 files changed, 24 insertions(+), 37 deletions(-) diff --git a/src/uiSelectController.js b/src/uiSelectController.js index 6814ff5b3..c252535a2 100644 --- a/src/uiSelectController.js +++ b/src/uiSelectController.js @@ -105,47 +105,32 @@ uis.controller('uiSelectCtrl', } // When the user clicks on ui-select, displays the dropdown list - ctrl.activate = function(initSearchValue, avoidReset) { - if (!ctrl.disabled && !ctrl.open) { - if(!avoidReset) _resetSearchInput(); + ctrl.activate = function(initSearchValue, avoidReset) { + if (!ctrl.disabled && !ctrl.open) { + if(!avoidReset) _resetSearchInput(); - $scope.$broadcast('uis:activate'); + $scope.$broadcast('uis:activate'); - ctrl.open = true; + ctrl.open = true; - ctrl.activeIndex = ctrl.activeIndex >= ctrl.items.length ? 0 : ctrl.activeIndex; + ctrl.activeIndex = ctrl.activeIndex >= ctrl.items.length ? 0 : ctrl.activeIndex; - // ensure that the index is set to zero for tagging variants - // that where first option is auto-selected - if ( ctrl.activeIndex === -1 && ctrl.taggingLabel !== false ) { - ctrl.activeIndex = 0; - } + // ensure that the index is set to zero for tagging variants + // that where first option is auto-selected + if ( ctrl.activeIndex === -1 && ctrl.taggingLabel !== false ) { + ctrl.activeIndex = 0; + } - var container = $element.querySelectorAll('.ui-select-choices-content'); - if (ctrl.$animate && ctrl.$animate.enabled(container[0])) { - ctrl.$animate.on('enter', container[0], function (elem, phase) { - if (phase === 'close') { - // Only focus input after the animation has finished - $timeout(function () { - ctrl.focusSearchInput(initSearchValue); - }); + // Give it time to appear before focus + $timeout(function() { + ctrl.search = initSearchValue || ctrl.search; + ctrl.searchInput[0].focus(); + if(!ctrl.tagging.isActivated && ctrl.items.length > 1) { + _ensureHighlightVisible(); } }); - } else { - $timeout(function () { - ctrl.focusSearchInput(initSearchValue); - }); } - } - }; - - ctrl.focusSearchInput = function (initSearchValue) { - ctrl.search = initSearchValue || ctrl.search; - ctrl.searchInput[0].focus(); - if(!ctrl.tagging.isActivated && ctrl.items.length > 1) { - _ensureHighlightVisible(); - } - }; + }; ctrl.findGroupByName = function(name) { return ctrl.groups && ctrl.groups.filter(function(group) { @@ -233,7 +218,7 @@ uis.controller('uiSelectCtrl', }; // See https://github.com/angular/angular.js/blob/v1.2.15/src/ng/directive/ngRepeat.js#L259 - $scope.$watchCollection(ctrl.parserResult.source, function(items) { + $scope.$watchCollection(ctrl.parserResult.source, function(items,oldItems) { if (items === undefined || items === null) { // If the user specifies undefined or null => reset the collection // Special case: items can be undefined if the user did not initialized the collection on the scope @@ -247,10 +232,12 @@ uis.controller('uiSelectCtrl', //TODO Should add a test ctrl.refreshItems(items); ctrl.ngModel.$modelValue = null; //Force scope model value and ngModel value to be out of sync to re-run formatters + if(ctrl.search =="" && items != oldItems) { //don't broadcast the event while searching + $scope.$emit('uis:filter'); + } } } }); - }; var _refreshDelayPromise; diff --git a/src/uiSelectDirective.js b/src/uiSelectDirective.js index 20dbcde7f..3d984399b 100644 --- a/src/uiSelectDirective.js +++ b/src/uiSelectDirective.js @@ -318,7 +318,7 @@ uis.directive('uiSelect', dropdown[0].style.opacity = 0; // Delay positioning the dropdown until all choices have been added so its height is correct. - $timeout(function(){ + //$timeout(function(){ if ($select.dropdownPosition === 'up'){ //Go UP @@ -347,7 +347,7 @@ uis.directive('uiSelect', // Display the dropdown once it has been positioned. dropdown[0].style.opacity = 1; - }); + //}); } else { if (dropdown === null || dropdown.length === 0) { return;