From 1f3c2aa5fe42bee6f0d2647c2dc0249e33b77392 Mon Sep 17 00:00:00 2001 From: weihanchen Date: Wed, 15 Feb 2017 23:38:02 +0800 Subject: [PATCH] feat(autocomplete): Add open/close pattern option --- src/auto-complete.js | 8 ++++-- test/auto-complete.spec.js | 58 ++++++++++++++++++++++++++++++++++++++ test/tags-input.spec.js | 8 ++++++ 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/src/auto-complete.js b/src/auto-complete.js index a5696ffc..b1f27470 100644 --- a/src/auto-complete.js +++ b/src/auto-complete.js @@ -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) { @@ -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); @@ -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(); diff --git a/test/auto-complete.spec.js b/test/auto-complete.spec.js index febbb67e..b48f4a70 100644 --- a/test/auto-complete.spec.js +++ b/test/auto-complete.spec.js @@ -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); + }); + }); }); diff --git a/test/tags-input.spec.js b/test/tags-input.spec.js index 3a72d514..700168ea 100644 --- a/test/tags-input.spec.js +++ b/test/tags-input.spec.js @@ -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;