Skip to content
This repository has been archived by the owner on Nov 22, 2021. It is now read-only.

Commit

Permalink
Merge afa0341 into 1e5fa0f
Browse files Browse the repository at this point in the history
  • Loading branch information
FoxxMD committed Feb 6, 2014
2 parents 1e5fa0f + afa0341 commit 667bccf
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/auto-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
* @param {boolean=} [highlightMatchedText=true] Flag indicating that the matched text will be highlighted in the
* suggestions list.
* @param {number=} [maxResultsToShow=10] Maximum number of results to be displayed at a time.
*
* @param {boolean=} [showSuggestionOnDownkey=false] When true the suggestion box will load and become visible on down
* down key trigger.
*/
tagsInput.directive('autoComplete', function($document, $timeout, $sce, tagsInputConfig) {
function SuggestionList(loadFn, options) {
Expand Down Expand Up @@ -49,8 +52,8 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, tagsInpu
self.selected = null;
self.visible = true;
};
self.load = function(query, tags) {
if (query.length < options.minLength) {
self.load = function(query, tags, override) {
if (!override && query.length < options.minLength) {
self.reset();
return;
}
Expand Down Expand Up @@ -118,7 +121,8 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, tagsInpu
debounceDelay: [Number, 100],
minLength: [Number, 3],
highlightMatchedText: [Boolean, true],
maxResultsToShow: [Number, 10]
maxResultsToShow: [Number, 10],
showSuggestionsOnDownkey: [Boolean, false]
});

tagsInput = tagsInputCtrl.registerAutocomplete();
Expand Down Expand Up @@ -167,7 +171,8 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, tagsInpu
}
})
.on('input-keydown', function(e) {
var key, handled;
var key = e.keyCode,
handled = false;

if (hotkeys.indexOf(e.keyCode) === -1) {
return;
Expand All @@ -186,8 +191,6 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, tagsInpu
};

if (suggestionList.visible) {
key = e.keyCode;
handled = false;

if (key === KEYS.down) {
suggestionList.selectNext();
Expand All @@ -204,13 +207,19 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, tagsInpu
else if (key === KEYS.enter || key === KEYS.tab) {
handled = scope.addSuggestion();
}

if (handled) {
e.preventDefault();
e.stopImmediatePropagation();
scope.$apply();
}
else if(!suggestionList.visible){
if(key === KEYS.down && scope.options.showSuggestionsOnDownkey){
var val = e.currentTarget ? e.currentTarget.value : null;
suggestionList.load(val && val.length > 0 ? val : null, tagsInput.getTags(), true);
handled = true;
}
}
if (handled) {
e.preventDefault();
e.stopImmediatePropagation();
scope.$apply();
}
})
.on('input-blur', function() {
suggestionList.reset();
Expand Down
51 changes: 51 additions & 0 deletions test/auto-complete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,40 @@ describe('autocomplete-directive', function() {
});
});

describe('show-suggestions-on-downkey option', function(){
it('initializes the option to false', function(){
//Arrange
compile();

//Assert
expect(isolateScope.options.showSuggestionsOnDownkey).toBe(false);

});
it('shows the suggestion box when down arrow keydown is triggered', function() {
//Arrange
compile('show-suggestions-on-downkey="true"');

// Act
sendKeyDown(KEYS.down);
$timeout.flush();

// Assert
expect($scope.loadItems).toHaveBeenCalledWith(null);

});
it('does prevent the down arrow keydown event from being propagated', function() {
//Arrange
compile('show-suggestions-on-downkey="true"');

// Act
var event = sendKeyDown(KEYS.down);

// Assert
expect(event.isDefaultPrevented()).toBe(true);
expect(event.isPropagationStopped()).toBe(true);
});
});

describe('debounce-delay option', function() {
it('initializes the option to 100 milliseconds', function() {
// Arrange/Act
Expand Down Expand Up @@ -745,6 +779,23 @@ describe('autocomplete-directive', function() {
expect(getSuggestionText(1)).toBe('Item &lt;2<em>&gt;</em>');
expect(getSuggestionText(2)).toBe('Item &amp;3');
});
it('doesn\'t highlight text if input is not null but min-length is 0', function() {

//Arrange
compile('highlight-matched-text="true"', 'min-length="0"');

//Act
loadSuggestions(['a', 'ab', 'ba', 'aba', 'bab'], '');

//Assert
// Assert
expect(getSuggestionText(0)).toBe('a');
expect(getSuggestionText(1)).toBe('ab');
expect(getSuggestionText(2)).toBe('ba');
expect(getSuggestionText(3)).toBe('aba');
expect(getSuggestionText(4)).toBe('bab');

});

});

Expand Down

0 comments on commit 667bccf

Please sign in to comment.