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

fix minimum-input-length with refresh-delay #1898 #1905

Merged
merged 2 commits into from Apr 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
/.idea
/.tmp
.DS_Store
npm-debug.log
*~
104 changes: 0 additions & 104 deletions npm-debug.log

This file was deleted.

11 changes: 6 additions & 5 deletions src/uiSelectChoicesDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,26 @@ uis.directive('uiSelectChoices',

choices.attr('ng-repeat', parserResult.repeatExpression(groupByExp))
.attr('ng-if', '$select.open'); //Prevent unnecessary watches when dropdown is closed


var rowsInner = tElement.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 IE8 then need to target rowsInner to apply the ng-click attr as choices will not capture the event.
// If IE8 then need to target rowsInner to apply the ng-click attr as choices will not capture the event.
var clickTarget = $window.document.addEventListener ? choices : rowsInner;
clickTarget.attr('ng-click', '$select.select(' + parserResult.itemName + ',$select.skipFocusser,$event)');

return function link(scope, element, attrs, $select) {


$select.parseRepeatAttr(attrs.repeat, groupByExp, groupFilterExp); //Result ready at $select.parserResult
$select.disableChoiceExpression = attrs.uiDisableChoice;
$select.onHighlightCallback = attrs.onHighlight;
$select.dropdownPosition = attrs.position ? attrs.position.toLowerCase() : uiSelectConfig.dropdownPosition;
$select.minimumInputLength = parseInt(attrs.minimumInputLength) || 0;
$select.dropdownPosition = attrs.position ? attrs.position.toLowerCase() : uiSelectConfig.dropdownPosition;

scope.$watch('$select.search', function(newValue) {
if(newValue && !$select.open && $select.multiple) $select.activate(false, true);
Expand Down
17 changes: 10 additions & 7 deletions src/uiSelectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,16 @@ uis.controller('uiSelectCtrl',
$timeout.cancel(_refreshDelayPromise);
}
_refreshDelayPromise = $timeout(function() {
var refreshPromise = $scope.$eval(refreshAttr);
if (refreshPromise && angular.isFunction(refreshPromise.then) && !ctrl.refreshing) {
ctrl.refreshing = true;
refreshPromise.finally(function() {
ctrl.refreshing = false;
});
}}, ctrl.refreshDelay);
if ($scope.$select.search.length >= $scope.$select.minimumInputLength) {
var refreshPromise = $scope.$eval(refreshAttr);
if (refreshPromise && angular.isFunction(refreshPromise.then) && !ctrl.refreshing) {
ctrl.refreshing = true;
refreshPromise.finally(function() {
ctrl.refreshing = false;
});
}
}
}, ctrl.refreshDelay);
}
};

Expand Down
40 changes: 40 additions & 0 deletions test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,46 @@ describe('ui-select tests', function() {
expect(scope.fetchFromServer).toHaveBeenCalledWith('red');
});


it('should call refresh function respecting minimum input length option with given refresh-delay', function () {

var el = compileTemplate(
'<ui-select ng-model="selection.selected"> \
<ui-select-match> \
</ui-select-match> \
<ui-select-choices repeat="person in people | filter: $select.search" \
refresh="fetchFromServer($select.search)" refresh-delay="1" minimum-input-length="3"> \
<div ng-bind-html="person.name | highlight: $select.search"></div> \
<div ng-if="person.name==\'Wladimir\'"> \
<span class="only-once">I should appear only once</span>\
</div> \
</ui-select-choices> \
</ui-select>'
);

scope.fetchFromServer = function(){};

spyOn(scope, 'fetchFromServer');

el.scope().$select.search = 'redd';
scope.$digest();
$timeout.flush();
expect(scope.fetchFromServer).toHaveBeenCalledWith('redd');


el.scope().$select.search = 'red';
scope.$digest();
el.scope().$select.search = 're';
scope.$digest();
el.scope().$select.search = 'r';
scope.$digest();
$timeout.flush();
expect(scope.fetchFromServer).not.toHaveBeenCalledWith('r');


});


it('should format view value correctly when using single property binding and refresh function', function () {

var el = compileTemplate(
Expand Down