Skip to content
This repository was archived by the owner on Nov 22, 2021. It is now read-only.
Open
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
8 changes: 6 additions & 2 deletions src/auto-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
* The expression is provided with the current match as $match, its index as $index and its state as $selected. The result
* of the evaluation must be one of the values supported by the ngClass directive (either a string, an array or an object).
* See https://docs.angularjs.org/api/ng/directive/ngClass for more information.
* @param {string=} [openPattern=\S] Pattern for open suggestion list.
* @param {string=} [closePattern=^$] Pattern for close suggestion list.
*/
tagsInput.directive('autoComplete', function($document, $timeout, $sce, $q, tagsInputConfig, tiUtil) {
function SuggestionList(loadFn, options, events) {
Expand Down Expand Up @@ -154,7 +156,9 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, $q, tags
loadOnEmpty: [Boolean, false],
loadOnFocus: [Boolean, false],
selectFirstMatch: [Boolean, true],
displayProperty: [String, '']
displayProperty: [String, ''],
openPattern: [RegExp, /\S/],
closePattern: [RegExp, /^$/]
});

$scope.suggestionList = new SuggestionList($scope.source, $scope.options, $scope.events);
Expand All @@ -181,7 +185,7 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, $q, tags
options.tagsInput = tagsInput.getOptions();

shouldLoadSuggestions = function(value) {
return value && value.length >= options.minLength || !value && options.loadOnEmpty;
return value && value.length >= options.minLength && options.openPattern.test(value) && !options.closePattern.test(value) || !value && options.loadOnEmpty;
};

scope.templateScope = tagsInput.getTemplateScope();
Expand Down
58 changes: 58 additions & 0 deletions test/auto-complete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1372,4 +1372,62 @@ describe('autoComplete directive', function() {
});
});
});

describe('open-pattern option', function() {
it('initialize the option to /\S/', function(){
// Arrange/Act
compile();

// Assert
expect(isolateScope.options.openPattern).toEqual(/\S/);
});

it('calls the load function when the input field is some pattern', function(){
// Arrange
compile('open-pattern="@$"');

// Act
changeInputValue('ABC@');
$timeout.flush();

// Assert
expect($scope.loadItems).toHaveBeenCalledWith('ABC@');
});

it('doesn\'t call the load function when the input field not contains some pattern', function(){
// Arrange
compile('open-pattern="@$"');

// Act
changeInputValue('ABCDEF');
$timeout.flush();

// Assert
expect($scope.loadItems).not.toHaveBeenCalled();
});
});

describe('close-pattern option', function() {
it('initialize the option to /^$/', function(){
// Arrange/Act
compile();

// Assert
expect(isolateScope.options.closePattern).toEqual(/^$/);
});

it('calls the load function when the input field is some pattern', function(){
// Arrange
compile('close-pattern="\\s$"');
changeInputValue('foobar');
suggestionList.show();
$scope.$digest();

// Act
changeInputValue('ABC ');

// Assert
expect(isSuggestionsBoxVisible()).toBe(false);
});
});
});
8 changes: 8 additions & 0 deletions test/tags-input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2132,6 +2132,14 @@ describe('tags-input directive', function() {
expect(autocompleteObj.getOptions()).toEqual({ option1: 1, option2: 2, option3: true });
});

it('return current scope', function() {
// Arrange
isolateScope.templateScope = $scope;

// Act/Assert
expect(autocompleteObj.getTemplateScope()).toEqual($scope);
});

it('subscribe for an event', function() {
// Arrange
var callback = angular.noop;
Expand Down