Skip to content
This repository was archived by the owner on May 21, 2025. It is now read-only.

Commit 293306f

Browse files
committed
Fix #28
1 parent 9100b60 commit 293306f

2 files changed

Lines changed: 59 additions & 19 deletions

File tree

angucomplete-alt.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ angular.module('angucomplete-alt', [] ).directive('angucompleteAlt', ['$q', '$pa
7878
var inputField = elem.find('input');
7979
var minlength = MIN_LENGTH;
8080
var searchTimer = null;
81-
var lastSearchTerm = null;
8281
var hideTimer;
8382
var requiredClassName = REQUIRED_CLASS;
8483
var responseFormatter;
@@ -120,12 +119,7 @@ angular.module('angucomplete-alt', [] ).directive('angucompleteAlt', ['$q', '$pa
120119
if (scope.clearSelected) {
121120
scope.searchStr = null;
122121
}
123-
scope.showDropdown = false;
124-
scope.results = [];
125-
}
126-
127-
function isNewSearchNeeded(newTerm, oldTerm) {
128-
return newTerm.length >= minlength && newTerm !== oldTerm;
122+
clearResults();
129123
}
130124

131125
function extractTitle(data) {
@@ -179,12 +173,8 @@ angular.module('angucomplete-alt', [] ).directive('angucompleteAlt', ['$q', '$pa
179173
else {
180174
if (!scope.searchStr || scope.searchStr === '') {
181175
scope.showDropdown = false;
182-
lastSearchTerm = null;
183-
} else if (isNewSearchNeeded(scope.searchStr, lastSearchTerm)) {
184-
lastSearchTerm = scope.searchStr;
185-
scope.showDropdown = true;
186-
scope.currentIndex = -1;
187-
scope.results = [];
176+
} else if (scope.searchStr.length >= minlength) {
177+
initResults();
188178

189179
if (searchTimer) {
190180
$timeout.cancel(searchTimer);
@@ -294,6 +284,17 @@ angular.module('angucomplete-alt', [] ).directive('angucompleteAlt', ['$q', '$pa
294284
.error(httpErrorCallback);
295285
}
296286

287+
function clearResults() {
288+
scope.showDropdown = false;
289+
scope.results = [];
290+
}
291+
292+
function initResults() {
293+
scope.showDropdown = true;
294+
scope.currentIndex = -1;
295+
scope.results = [];
296+
}
297+
297298
function getLocalResults(str) {
298299
var i, match, s,
299300
searchFields = scope.searchFields.split(','),
@@ -394,13 +395,21 @@ angular.module('angucomplete-alt', [] ).directive('angucompleteAlt', ['$q', '$pa
394395
scope.searchStr = null;
395396
}
396397
else {
397-
scope.searchStr = lastSearchTerm = result.title;
398+
scope.searchStr = result.title;
398399
}
399400
callOrAssign(result);
400-
scope.showDropdown = false;
401-
scope.results = [];
401+
clearResults();
402402
};
403403

404+
scope.inputChangeHandler = function(str) {
405+
if (str.length < minlength) {
406+
clearResults();
407+
}
408+
if (scope.inputChanged) {
409+
str = scope.inputChanged(str);
410+
}
411+
return str;
412+
};
404413

405414
// check required
406415
if (scope.fieldRequiredClass && scope.fieldRequiredClass !== '') {
@@ -449,9 +458,6 @@ angular.module('angucomplete-alt', [] ).directive('angucompleteAlt', ['$q', '$pa
449458

450459
// set response formatter
451460
responseFormatter = callFunctionOrIdentity('remoteUrlResponseFormatter');
452-
453-
// set response formatter
454-
scope.inputChangeHandler = callFunctionOrIdentity('inputChanged');
455461
}
456462
};
457463
}]);

test/angucomplete-alt.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,40 @@ describe('angucomplete-alt', function() {
9393
expect(element.find('.angucomplete-row').length).toBeGreaterThan(0);
9494
});
9595

96+
it('should show search results after 2 letters are entered and hide results when a letter is deleted', function() {
97+
var element = angular.element('<div angucomplete-alt id="ex1" placeholder="Search countries" selected-object="selectedCountry" local-data="countries" search-fields="name" title-field="name" minlength="2"/>');
98+
$scope.selectedCountry = undefined;
99+
$scope.countries = [
100+
{name: 'Afghanistan', code: 'AF'},
101+
{name: 'Aland Islands', code: 'AX'},
102+
{name: 'Albania', code: 'AL'}
103+
];
104+
$compile(element)($scope);
105+
$scope.$digest();
106+
var inputField = element.find('#ex1_value');
107+
var e = $.Event('keyup');
108+
109+
e.which = 97; // letter: a
110+
inputField.val('a');
111+
inputField.trigger('input');
112+
inputField.trigger(e);
113+
expect(element.find('.angucomplete-row').length).toBe(0);
114+
115+
e.which = 108; // letter: l
116+
inputField.val('al');
117+
inputField.trigger('input');
118+
inputField.trigger(e);
119+
$timeout.flush();
120+
expect(element.find('.angucomplete-row').length).toBe(2);
121+
122+
// delete a char
123+
e.which = KEY_DEL;
124+
inputField.val('a');
125+
inputField.trigger('input');
126+
inputField.trigger(e);
127+
expect(element.find('.angucomplete-row').length).toBe(0);
128+
});
129+
96130
it('should reset selectedObject to undefined when input changes', function() {
97131
var element = angular.element('<div angucomplete-alt id="ex1" placeholder="Search countries" selected-object="selectedCountry" local-data="countries" search-fields="name" title-field="name" minlength="1"/>');
98132
$scope.selectedCountry = undefined;

0 commit comments

Comments
 (0)