From 693bb8954f962477bb35c068fe761ad1ef90b93d Mon Sep 17 00:00:00 2001 From: Nico Date: Wed, 9 Sep 2015 18:13:46 +0200 Subject: [PATCH 1/8] Add focusFirst attribute. fixes #92 Allow the use of `focus-first="true"` attribute to automatically select the first matching result. --- angucomplete-alt.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/angucomplete-alt.js b/angucomplete-alt.js index 8d5156ca..21b13339 100644 --- a/angucomplete-alt.js +++ b/angucomplete-alt.js @@ -473,7 +473,7 @@ function initResults() { scope.showDropdown = displaySearching; - scope.currentIndex = -1; + scope.currentIndex = scope.focusFirst ? 0 : -1; scope.results = []; } @@ -769,7 +769,8 @@ autoMatch: '@', focusOut: '&', focusIn: '&', - inputName: '@' + inputName: '@', + focusFirst: '@' }, templateUrl: function(element, attrs) { return attrs.templateUrl || TEMPLATE_URL; From 02f38389f1b8f3510e7236c1f74cfaf79e5060a5 Mon Sep 17 00:00:00 2001 From: Nico Date: Wed, 9 Sep 2015 18:29:21 +0200 Subject: [PATCH 2/8] Update README.md add `focus-first` attribute to the attribute table --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index cdaff0f7..f0725e27 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,7 @@ It expects the returned results from remote API to have a root object. In the ab | focus-out | A function or expression to be called when input field lose focus. [example](http://ghiden.github.io/angucomplete-alt/#example12) | No | focusOut() | | disable-input | A model to control disable/enable of input field. [example page](http://ghiden.github.io/angucomplete-alt/#example13) | No | disableInput | | template-url | Customize the markup of the autocomplete template. [example page](http://ghiden.github.io/angucomplete-alt/#example14) | No | "/my-custom-template.html" | +| focus-first | Automatically select the first match from the result list. | No | true | ### Scrollbar From b6a99ae4f5c3a635f3be04abb05bedaab9f3ee1a Mon Sep 17 00:00:00 2001 From: Freezystem Date: Thu, 10 Sep 2015 00:25:13 +0200 Subject: [PATCH 3/8] set scope.currentIndex to 0 at init if focusFirst is true --- angucomplete-alt.js | 2 +- dist/angucomplete-alt.min.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/angucomplete-alt.js b/angucomplete-alt.js index 21b13339..0e15120f 100644 --- a/angucomplete-alt.js +++ b/angucomplete-alt.js @@ -95,7 +95,7 @@ } }); - scope.currentIndex = null; + scope.currentIndex = scope.focusFirst ? 0 : null; scope.searching = false; unbindInitialValue = scope.$watch('initialValue', function(newval, oldval) { diff --git a/dist/angucomplete-alt.min.js b/dist/angucomplete-alt.min.js index 4a01ecd8..41138188 100644 --- a/dist/angucomplete-alt.min.js +++ b/dist/angucomplete-alt.min.js @@ -1,2 +1,2 @@ /*! Copyright (c) 2014 Hidenari Nozaki and contributors | Licensed under the MIT license */ -"use strict";!function(a,b){"undefined"!=typeof module&&module.exports?module.exports=b(require("angular")):"function"==typeof define&&define.amd?define(["angular"],b):b(a.angular)}(window,function(a){a.module("angucomplete-alt",[]).directive("angucompleteAlt",["$q","$parse","$http","$sce","$timeout","$templateCache","$interpolate",function(a,b,c,d,e,f,g){function h(b,f,g,h){function w(a){la=null,b.hideResults(a),document.body.removeEventListener("click",w)}function x(a){return a.which?a.which:a.keyCode}function y(a){"function"==typeof b.selectedObject?b.selectedObject(a):b.selectedObject=a,E(a?!0:!1)}function z(a){return function(c){return b[a]?b[a](c):c}}function A(a){y({originalObject:a}),b.clearSelected&&(b.searchStr=null),T()}function B(a){return b.titleField.split(",").map(function(b){return C(a,b)}).join(" ")}function C(a,b){var c,d;if(b){c=b.split("."),d=a;for(var e=0;e'+f[0]+""):a,d.trustAsHtml(e)):void 0}function E(a){b.notEmpty=a,ha=b.searchStr,b.fieldRequired&&h&&h.$setValidity(ga,a)}function F(a){var c=x(a);if(c!==l&&c!==j)if(c===k||c===n)a.preventDefault();else if(c===i)a.preventDefault(),!b.showDropdown&&b.searchStr&&b.searchStr.length>=ea&&(U(),b.searching=!0,X(b.searchStr));else if(c===m)T(),b.$apply(function(){da.val(b.searchStr)});else{if(0===ea&&!b.searchStr)return;b.searchStr&&""!==b.searchStr?b.searchStr.length>=ea&&(U(),fa&&e.cancel(fa),b.searching=!0,fa=e(function(){X(b.searchStr)},b.pause)):b.showDropdown=!1,ha&&ha!==b.searchStr&&!b.clearSelected&&b.$apply(function(){y()})}}function G(a){!b.overrideSuggestions||b.selectedObject&&b.selectedObject.originalObject===b.searchStr||(a&&a.preventDefault(),A(b.searchStr))}function H(a){var b=getComputedStyle(a);return a.offsetHeight+parseInt(b.marginTop,10)+parseInt(b.marginBottom,10)}function I(){return ja.getBoundingClientRect().top+parseInt(getComputedStyle(ja).maxHeight,10)}function J(){return f[0].querySelectorAll(".angucomplete-row")[b.currentIndex]}function K(){return J().getBoundingClientRect().top-(ja.getBoundingClientRect().top+parseInt(getComputedStyle(ja).paddingTop,10))}function L(a){ja.scrollTop=ja.scrollTop+a}function M(){var a=b.results[b.currentIndex];b.matchClass?da.val(B(a.originalObject)):da.val(a.title)}function N(a){var c=x(a),d=null,e=null;c===n&&b.results?(b.currentIndex>=0&&b.currentIndex=1?(b.$apply(function(){b.currentIndex--,M()}),ka&&(e=K(),0>e&&L(e-1))):0===b.currentIndex&&b.$apply(function(){b.currentIndex=-1,da.val(b.searchStr)})):c===o&&(b.results&&b.results.length>0&&b.showDropdown?-1===b.currentIndex&&b.overrideSuggestions?G():(-1===b.currentIndex&&(b.currentIndex=0),b.selectResult(b.results[b.currentIndex]),b.$digest()):b.searchStr&&b.searchStr.length>0&&G())}function O(a){return function(c,d,e,f){d||e||f||(c=c.data),b.searching=!1,Y(C(_(c),b.remoteUrlDataField),a)}}function P(a,c,d,e){c||d||e||(c=a.status),0!==c&&(b.remoteUrlErrorCallback?b.remoteUrlErrorCallback(a,c,d,e):console&&console.error&&console.error("http error"))}function Q(){ia&&ia.resolve()}function R(d){var e={},f=b.remoteUrl+encodeURIComponent(d);b.remoteUrlRequestFormatter&&(e={params:b.remoteUrlRequestFormatter(d)},f=b.remoteUrl),b.remoteUrlRequestWithCredentials&&(e.withCredentials=!0),Q(),ia=a.defer(),e.timeout=ia.promise,c.get(f,e).success(O(d)).error(P)}function S(c){Q(),ia=a.defer(),b.remoteApiHandler(c,ia.promise).then(O(c))["catch"](P)}function T(){b.showDropdown=!1,b.results=[],ja&&(ja.scrollTop=0)}function U(){b.showDropdown=ba,b.currentIndex=-1,b.results=[]}function V(a){var c,d,e,f,g=b.searchFields.split(","),h=[];for(c=0;c=0;d&&(h[h.length]=b.localData[c])}b.searching=!1,Y(h,a)}function W(a,c,d){if(d)for(var e in c)if(c[e].toLowerCase()===d.toLowerCase())return void b.selectResult(a)}function X(a){!a||a.length0)for(b.results=[],d=0;d0?b.searchStr=a:console&&console.error&&console.error("Tried to set initial value of angucomplete to",a,"which is an invalid value"),E(!0))}),b.$on("angucomplete-alt:clearInput",function(a,c){c&&c!==b.id||(b.searchStr=null,y(),E(!1),T())}),b.onFocusHandler=function(){b.focusIn&&b.focusIn(),0!==ea||b.searchStr&&0!==b.searchStr.length||(b.showDropdown=!0,Z())},b.hideResults=function(a){la&&(la===b.id+"_dropdown"||la.indexOf("angucomplete")>=0)?la=null:($=e(function(){T(),b.$apply(function(){b.searchStr&&b.searchStr.length>0&&da.val(b.searchStr)})},s),Q(),b.focusOut&&b.focusOut(),b.overrideSuggestions&&b.searchStr&&b.searchStr.length>0&&-1===b.currentIndex&&G())},b.resetHideResults=function(){$&&e.cancel($)},b.hoverRow=function(a){b.currentIndex=a},b.selectResult=function(a){b.matchClass&&(a.title=B(a.originalObject),a.description=C(a.originalObject,b.descriptionField)),b.clearSelected?b.searchStr=null:b.searchStr=a.title,y(a),T()},b.inputChangeHandler=function(a){return a.length
{{ result.title }}
{{result.description}}
'),{restrict:"EA",require:"^?form",scope:{selectedObject:"=",disableInput:"=",initialValue:"=",localData:"=",remoteUrlRequestFormatter:"=",remoteUrlRequestWithCredentials:"@",remoteUrlResponseFormatter:"=",remoteUrlErrorCallback:"=",remoteApiHandler:"=",id:"@",type:"@",placeholder:"@",remoteUrl:"@",remoteUrlDataField:"@",titleField:"@",descriptionField:"@",imageField:"@",inputClass:"@",pause:"@",searchFields:"@",minlength:"@",matchClass:"@",clearSelected:"@",overrideSuggestions:"@",fieldRequired:"@",fieldRequiredClass:"@",inputChanged:"=",autoMatch:"@",focusOut:"&",focusIn:"&",inputName:"@"},templateUrl:function(a,b){return b.templateUrl||w},compile:function(a,b){var c=g.startSymbol(),d=g.endSymbol();if("{{"!==c||"}}"!==d){var e=a.html().replace(/\{\{/g,c).replace(/\}\}/g,d);a.html(e)}return h}}}])}); \ No newline at end of file +"use strict";!function(a,b){"undefined"!=typeof module&&module.exports?module.exports=b(require("angular")):"function"==typeof define&&define.amd?define(["angular"],b):b(a.angular)}(window,function(a){a.module("angucomplete-alt",[]).directive("angucompleteAlt",["$q","$parse","$http","$sce","$timeout","$templateCache","$interpolate",function(a,b,c,d,e,f,g){function h(b,f,g,h){function w(a){la=null,b.hideResults(a),document.body.removeEventListener("click",w)}function x(a){return a.which?a.which:a.keyCode}function y(a){"function"==typeof b.selectedObject?b.selectedObject(a):b.selectedObject=a,E(a?!0:!1)}function z(a){return function(c){return b[a]?b[a](c):c}}function A(a){y({originalObject:a}),b.clearSelected&&(b.searchStr=null),T()}function B(a){return b.titleField.split(",").map(function(b){return C(a,b)}).join(" ")}function C(a,b){var c,d;if(b){c=b.split("."),d=a;for(var e=0;e'+f[0]+""):a,d.trustAsHtml(e)):void 0}function E(a){b.notEmpty=a,ha=b.searchStr,b.fieldRequired&&h&&h.$setValidity(ga,a)}function F(a){var c=x(a);if(c!==l&&c!==j)if(c===k||c===n)a.preventDefault();else if(c===i)a.preventDefault(),!b.showDropdown&&b.searchStr&&b.searchStr.length>=ea&&(U(),b.searching=!0,X(b.searchStr));else if(c===m)T(),b.$apply(function(){da.val(b.searchStr)});else{if(0===ea&&!b.searchStr)return;b.searchStr&&""!==b.searchStr?b.searchStr.length>=ea&&(U(),fa&&e.cancel(fa),b.searching=!0,fa=e(function(){X(b.searchStr)},b.pause)):b.showDropdown=!1,ha&&ha!==b.searchStr&&!b.clearSelected&&b.$apply(function(){y()})}}function G(a){!b.overrideSuggestions||b.selectedObject&&b.selectedObject.originalObject===b.searchStr||(a&&a.preventDefault(),A(b.searchStr))}function H(a){var b=getComputedStyle(a);return a.offsetHeight+parseInt(b.marginTop,10)+parseInt(b.marginBottom,10)}function I(){return ja.getBoundingClientRect().top+parseInt(getComputedStyle(ja).maxHeight,10)}function J(){return f[0].querySelectorAll(".angucomplete-row")[b.currentIndex]}function K(){return J().getBoundingClientRect().top-(ja.getBoundingClientRect().top+parseInt(getComputedStyle(ja).paddingTop,10))}function L(a){ja.scrollTop=ja.scrollTop+a}function M(){var a=b.results[b.currentIndex];b.matchClass?da.val(B(a.originalObject)):da.val(a.title)}function N(a){var c=x(a),d=null,e=null;c===n&&b.results?(b.currentIndex>=0&&b.currentIndex=1?(b.$apply(function(){b.currentIndex--,M()}),ka&&(e=K(),0>e&&L(e-1))):0===b.currentIndex&&b.$apply(function(){b.currentIndex=-1,da.val(b.searchStr)})):c===o&&(b.results&&b.results.length>0&&b.showDropdown?-1===b.currentIndex&&b.overrideSuggestions?G():(-1===b.currentIndex&&(b.currentIndex=0),b.selectResult(b.results[b.currentIndex]),b.$digest()):b.searchStr&&b.searchStr.length>0&&G())}function O(a){return function(c,d,e,f){d||e||f||(c=c.data),b.searching=!1,Y(C(_(c),b.remoteUrlDataField),a)}}function P(a,c,d,e){c||d||e||(c=a.status),0!==c&&(b.remoteUrlErrorCallback?b.remoteUrlErrorCallback(a,c,d,e):console&&console.error&&console.error("http error"))}function Q(){ia&&ia.resolve()}function R(d){var e={},f=b.remoteUrl+encodeURIComponent(d);b.remoteUrlRequestFormatter&&(e={params:b.remoteUrlRequestFormatter(d)},f=b.remoteUrl),b.remoteUrlRequestWithCredentials&&(e.withCredentials=!0),Q(),ia=a.defer(),e.timeout=ia.promise,c.get(f,e).success(O(d)).error(P)}function S(c){Q(),ia=a.defer(),b.remoteApiHandler(c,ia.promise).then(O(c))["catch"](P)}function T(){b.showDropdown=!1,b.results=[],ja&&(ja.scrollTop=0)}function U(){b.showDropdown=ba,b.currentIndex=b.focusFirst?0:-1,b.results=[]}function V(a){var c,d,e,f,g=b.searchFields.split(","),h=[];for(c=0;c=0;d&&(h[h.length]=b.localData[c])}b.searching=!1,Y(h,a)}function W(a,c,d){if(d)for(var e in c)if(c[e].toLowerCase()===d.toLowerCase())return void b.selectResult(a)}function X(a){!a||a.length0)for(b.results=[],d=0;d0?b.searchStr=a:console&&console.error&&console.error("Tried to set initial value of angucomplete to",a,"which is an invalid value"),E(!0))}),b.$on("angucomplete-alt:clearInput",function(a,c){c&&c!==b.id||(b.searchStr=null,y(),E(!1),T())}),b.onFocusHandler=function(){b.focusIn&&b.focusIn(),0!==ea||b.searchStr&&0!==b.searchStr.length||(b.showDropdown=!0,Z())},b.hideResults=function(a){la&&(la===b.id+"_dropdown"||la.indexOf("angucomplete")>=0)?la=null:($=e(function(){T(),b.$apply(function(){b.searchStr&&b.searchStr.length>0&&da.val(b.searchStr)})},s),Q(),b.focusOut&&b.focusOut(),b.overrideSuggestions&&b.searchStr&&b.searchStr.length>0&&-1===b.currentIndex&&G())},b.resetHideResults=function(){$&&e.cancel($)},b.hoverRow=function(a){b.currentIndex=a},b.selectResult=function(a){b.matchClass&&(a.title=B(a.originalObject),a.description=C(a.originalObject,b.descriptionField)),b.clearSelected?b.searchStr=null:b.searchStr=a.title,y(a),T()},b.inputChangeHandler=function(a){return a.length
{{ result.title }}
{{result.description}}
'),{restrict:"EA",require:"^?form",scope:{selectedObject:"=",disableInput:"=",initialValue:"=",localData:"=",remoteUrlRequestFormatter:"=",remoteUrlRequestWithCredentials:"@",remoteUrlResponseFormatter:"=",remoteUrlErrorCallback:"=",remoteApiHandler:"=",id:"@",type:"@",placeholder:"@",remoteUrl:"@",remoteUrlDataField:"@",titleField:"@",descriptionField:"@",imageField:"@",inputClass:"@",pause:"@",searchFields:"@",minlength:"@",matchClass:"@",clearSelected:"@",overrideSuggestions:"@",fieldRequired:"@",fieldRequiredClass:"@",inputChanged:"=",autoMatch:"@",focusOut:"&",focusIn:"&",inputName:"@",focusFirst:"@"},templateUrl:function(a,b){return b.templateUrl||w},compile:function(a,b){var c=g.startSymbol(),d=g.endSymbol();if("{{"!==c||"}}"!==d){var e=a.html().replace(/\{\{/g,c).replace(/\}\}/g,d);a.html(e)}return h}}}])}); \ No newline at end of file From 02ffc620e835691ba4ad2a725ab89ead2a884df3 Mon Sep 17 00:00:00 2001 From: Nicolas Polizzo Date: Thu, 10 Sep 2015 14:10:07 +0200 Subject: [PATCH 4/8] reset selectedIndex to 0 on refocus when focusFirst is true --- angucomplete-alt.js | 1 + dist/angucomplete-alt.min.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/angucomplete-alt.js b/angucomplete-alt.js index 0e15120f..d2a35d8e 100644 --- a/angucomplete-alt.js +++ b/angucomplete-alt.js @@ -593,6 +593,7 @@ scope.focusIn(); } if (minlength === 0 && (!scope.searchStr || scope.searchStr.length === 0)) { + scope.currentIndex = scope.focusFirst ? 0 : scope.currentIndex; scope.showDropdown = true; showAll(); } diff --git a/dist/angucomplete-alt.min.js b/dist/angucomplete-alt.min.js index 41138188..7bcd8859 100644 --- a/dist/angucomplete-alt.min.js +++ b/dist/angucomplete-alt.min.js @@ -1,2 +1,2 @@ /*! Copyright (c) 2014 Hidenari Nozaki and contributors | Licensed under the MIT license */ -"use strict";!function(a,b){"undefined"!=typeof module&&module.exports?module.exports=b(require("angular")):"function"==typeof define&&define.amd?define(["angular"],b):b(a.angular)}(window,function(a){a.module("angucomplete-alt",[]).directive("angucompleteAlt",["$q","$parse","$http","$sce","$timeout","$templateCache","$interpolate",function(a,b,c,d,e,f,g){function h(b,f,g,h){function w(a){la=null,b.hideResults(a),document.body.removeEventListener("click",w)}function x(a){return a.which?a.which:a.keyCode}function y(a){"function"==typeof b.selectedObject?b.selectedObject(a):b.selectedObject=a,E(a?!0:!1)}function z(a){return function(c){return b[a]?b[a](c):c}}function A(a){y({originalObject:a}),b.clearSelected&&(b.searchStr=null),T()}function B(a){return b.titleField.split(",").map(function(b){return C(a,b)}).join(" ")}function C(a,b){var c,d;if(b){c=b.split("."),d=a;for(var e=0;e'+f[0]+""):a,d.trustAsHtml(e)):void 0}function E(a){b.notEmpty=a,ha=b.searchStr,b.fieldRequired&&h&&h.$setValidity(ga,a)}function F(a){var c=x(a);if(c!==l&&c!==j)if(c===k||c===n)a.preventDefault();else if(c===i)a.preventDefault(),!b.showDropdown&&b.searchStr&&b.searchStr.length>=ea&&(U(),b.searching=!0,X(b.searchStr));else if(c===m)T(),b.$apply(function(){da.val(b.searchStr)});else{if(0===ea&&!b.searchStr)return;b.searchStr&&""!==b.searchStr?b.searchStr.length>=ea&&(U(),fa&&e.cancel(fa),b.searching=!0,fa=e(function(){X(b.searchStr)},b.pause)):b.showDropdown=!1,ha&&ha!==b.searchStr&&!b.clearSelected&&b.$apply(function(){y()})}}function G(a){!b.overrideSuggestions||b.selectedObject&&b.selectedObject.originalObject===b.searchStr||(a&&a.preventDefault(),A(b.searchStr))}function H(a){var b=getComputedStyle(a);return a.offsetHeight+parseInt(b.marginTop,10)+parseInt(b.marginBottom,10)}function I(){return ja.getBoundingClientRect().top+parseInt(getComputedStyle(ja).maxHeight,10)}function J(){return f[0].querySelectorAll(".angucomplete-row")[b.currentIndex]}function K(){return J().getBoundingClientRect().top-(ja.getBoundingClientRect().top+parseInt(getComputedStyle(ja).paddingTop,10))}function L(a){ja.scrollTop=ja.scrollTop+a}function M(){var a=b.results[b.currentIndex];b.matchClass?da.val(B(a.originalObject)):da.val(a.title)}function N(a){var c=x(a),d=null,e=null;c===n&&b.results?(b.currentIndex>=0&&b.currentIndex=1?(b.$apply(function(){b.currentIndex--,M()}),ka&&(e=K(),0>e&&L(e-1))):0===b.currentIndex&&b.$apply(function(){b.currentIndex=-1,da.val(b.searchStr)})):c===o&&(b.results&&b.results.length>0&&b.showDropdown?-1===b.currentIndex&&b.overrideSuggestions?G():(-1===b.currentIndex&&(b.currentIndex=0),b.selectResult(b.results[b.currentIndex]),b.$digest()):b.searchStr&&b.searchStr.length>0&&G())}function O(a){return function(c,d,e,f){d||e||f||(c=c.data),b.searching=!1,Y(C(_(c),b.remoteUrlDataField),a)}}function P(a,c,d,e){c||d||e||(c=a.status),0!==c&&(b.remoteUrlErrorCallback?b.remoteUrlErrorCallback(a,c,d,e):console&&console.error&&console.error("http error"))}function Q(){ia&&ia.resolve()}function R(d){var e={},f=b.remoteUrl+encodeURIComponent(d);b.remoteUrlRequestFormatter&&(e={params:b.remoteUrlRequestFormatter(d)},f=b.remoteUrl),b.remoteUrlRequestWithCredentials&&(e.withCredentials=!0),Q(),ia=a.defer(),e.timeout=ia.promise,c.get(f,e).success(O(d)).error(P)}function S(c){Q(),ia=a.defer(),b.remoteApiHandler(c,ia.promise).then(O(c))["catch"](P)}function T(){b.showDropdown=!1,b.results=[],ja&&(ja.scrollTop=0)}function U(){b.showDropdown=ba,b.currentIndex=b.focusFirst?0:-1,b.results=[]}function V(a){var c,d,e,f,g=b.searchFields.split(","),h=[];for(c=0;c=0;d&&(h[h.length]=b.localData[c])}b.searching=!1,Y(h,a)}function W(a,c,d){if(d)for(var e in c)if(c[e].toLowerCase()===d.toLowerCase())return void b.selectResult(a)}function X(a){!a||a.length0)for(b.results=[],d=0;d0?b.searchStr=a:console&&console.error&&console.error("Tried to set initial value of angucomplete to",a,"which is an invalid value"),E(!0))}),b.$on("angucomplete-alt:clearInput",function(a,c){c&&c!==b.id||(b.searchStr=null,y(),E(!1),T())}),b.onFocusHandler=function(){b.focusIn&&b.focusIn(),0!==ea||b.searchStr&&0!==b.searchStr.length||(b.showDropdown=!0,Z())},b.hideResults=function(a){la&&(la===b.id+"_dropdown"||la.indexOf("angucomplete")>=0)?la=null:($=e(function(){T(),b.$apply(function(){b.searchStr&&b.searchStr.length>0&&da.val(b.searchStr)})},s),Q(),b.focusOut&&b.focusOut(),b.overrideSuggestions&&b.searchStr&&b.searchStr.length>0&&-1===b.currentIndex&&G())},b.resetHideResults=function(){$&&e.cancel($)},b.hoverRow=function(a){b.currentIndex=a},b.selectResult=function(a){b.matchClass&&(a.title=B(a.originalObject),a.description=C(a.originalObject,b.descriptionField)),b.clearSelected?b.searchStr=null:b.searchStr=a.title,y(a),T()},b.inputChangeHandler=function(a){return a.length
{{ result.title }}
{{result.description}}
'),{restrict:"EA",require:"^?form",scope:{selectedObject:"=",disableInput:"=",initialValue:"=",localData:"=",remoteUrlRequestFormatter:"=",remoteUrlRequestWithCredentials:"@",remoteUrlResponseFormatter:"=",remoteUrlErrorCallback:"=",remoteApiHandler:"=",id:"@",type:"@",placeholder:"@",remoteUrl:"@",remoteUrlDataField:"@",titleField:"@",descriptionField:"@",imageField:"@",inputClass:"@",pause:"@",searchFields:"@",minlength:"@",matchClass:"@",clearSelected:"@",overrideSuggestions:"@",fieldRequired:"@",fieldRequiredClass:"@",inputChanged:"=",autoMatch:"@",focusOut:"&",focusIn:"&",inputName:"@",focusFirst:"@"},templateUrl:function(a,b){return b.templateUrl||w},compile:function(a,b){var c=g.startSymbol(),d=g.endSymbol();if("{{"!==c||"}}"!==d){var e=a.html().replace(/\{\{/g,c).replace(/\}\}/g,d);a.html(e)}return h}}}])}); \ No newline at end of file +"use strict";!function(a,b){"undefined"!=typeof module&&module.exports?module.exports=b(require("angular")):"function"==typeof define&&define.amd?define(["angular"],b):b(a.angular)}(window,function(a){a.module("angucomplete-alt",[]).directive("angucompleteAlt",["$q","$parse","$http","$sce","$timeout","$templateCache","$interpolate",function(a,b,c,d,e,f,g){function h(b,f,g,h){function w(a){la=null,b.hideResults(a),document.body.removeEventListener("click",w)}function x(a){return a.which?a.which:a.keyCode}function y(a){"function"==typeof b.selectedObject?b.selectedObject(a):b.selectedObject=a,E(a?!0:!1)}function z(a){return function(c){return b[a]?b[a](c):c}}function A(a){y({originalObject:a}),b.clearSelected&&(b.searchStr=null),T()}function B(a){return b.titleField.split(",").map(function(b){return C(a,b)}).join(" ")}function C(a,b){var c,d;if(b){c=b.split("."),d=a;for(var e=0;e'+f[0]+""):a,d.trustAsHtml(e)):void 0}function E(a){b.notEmpty=a,ha=b.searchStr,b.fieldRequired&&h&&h.$setValidity(ga,a)}function F(a){var c=x(a);if(c!==l&&c!==j)if(c===k||c===n)a.preventDefault();else if(c===i)a.preventDefault(),!b.showDropdown&&b.searchStr&&b.searchStr.length>=ea&&(U(),b.searching=!0,X(b.searchStr));else if(c===m)T(),b.$apply(function(){da.val(b.searchStr)});else{if(0===ea&&!b.searchStr)return;b.searchStr&&""!==b.searchStr?b.searchStr.length>=ea&&(U(),fa&&e.cancel(fa),b.searching=!0,fa=e(function(){X(b.searchStr)},b.pause)):b.showDropdown=!1,ha&&ha!==b.searchStr&&!b.clearSelected&&b.$apply(function(){y()})}}function G(a){!b.overrideSuggestions||b.selectedObject&&b.selectedObject.originalObject===b.searchStr||(a&&a.preventDefault(),A(b.searchStr))}function H(a){var b=getComputedStyle(a);return a.offsetHeight+parseInt(b.marginTop,10)+parseInt(b.marginBottom,10)}function I(){return ja.getBoundingClientRect().top+parseInt(getComputedStyle(ja).maxHeight,10)}function J(){return f[0].querySelectorAll(".angucomplete-row")[b.currentIndex]}function K(){return J().getBoundingClientRect().top-(ja.getBoundingClientRect().top+parseInt(getComputedStyle(ja).paddingTop,10))}function L(a){ja.scrollTop=ja.scrollTop+a}function M(){var a=b.results[b.currentIndex];b.matchClass?da.val(B(a.originalObject)):da.val(a.title)}function N(a){var c=x(a),d=null,e=null;c===n&&b.results?(b.currentIndex>=0&&b.currentIndex=1?(b.$apply(function(){b.currentIndex--,M()}),ka&&(e=K(),0>e&&L(e-1))):0===b.currentIndex&&b.$apply(function(){b.currentIndex=-1,da.val(b.searchStr)})):c===o&&(b.results&&b.results.length>0&&b.showDropdown?-1===b.currentIndex&&b.overrideSuggestions?G():(-1===b.currentIndex&&(b.currentIndex=0),b.selectResult(b.results[b.currentIndex]),b.$digest()):b.searchStr&&b.searchStr.length>0&&G())}function O(a){return function(c,d,e,f){d||e||f||(c=c.data),b.searching=!1,Y(C(_(c),b.remoteUrlDataField),a)}}function P(a,c,d,e){c||d||e||(c=a.status),0!==c&&(b.remoteUrlErrorCallback?b.remoteUrlErrorCallback(a,c,d,e):console&&console.error&&console.error("http error"))}function Q(){ia&&ia.resolve()}function R(d){var e={},f=b.remoteUrl+encodeURIComponent(d);b.remoteUrlRequestFormatter&&(e={params:b.remoteUrlRequestFormatter(d)},f=b.remoteUrl),b.remoteUrlRequestWithCredentials&&(e.withCredentials=!0),Q(),ia=a.defer(),e.timeout=ia.promise,c.get(f,e).success(O(d)).error(P)}function S(c){Q(),ia=a.defer(),b.remoteApiHandler(c,ia.promise).then(O(c))["catch"](P)}function T(){b.showDropdown=!1,b.results=[],ja&&(ja.scrollTop=0)}function U(){b.showDropdown=ba,b.currentIndex=b.focusFirst?0:-1,b.results=[]}function V(a){var c,d,e,f,g=b.searchFields.split(","),h=[];for(c=0;c=0;d&&(h[h.length]=b.localData[c])}b.searching=!1,Y(h,a)}function W(a,c,d){if(d)for(var e in c)if(c[e].toLowerCase()===d.toLowerCase())return void b.selectResult(a)}function X(a){!a||a.length0)for(b.results=[],d=0;d0?b.searchStr=a:console&&console.error&&console.error("Tried to set initial value of angucomplete to",a,"which is an invalid value"),E(!0))}),b.$on("angucomplete-alt:clearInput",function(a,c){c&&c!==b.id||(b.searchStr=null,y(),E(!1),T())}),b.onFocusHandler=function(){b.focusIn&&b.focusIn(),0!==ea||b.searchStr&&0!==b.searchStr.length||(b.currentIndex=b.focusFirst?0:b.currentIndex,b.showDropdown=!0,Z())},b.hideResults=function(a){la&&(la===b.id+"_dropdown"||la.indexOf("angucomplete")>=0)?la=null:($=e(function(){T(),b.$apply(function(){b.searchStr&&b.searchStr.length>0&&da.val(b.searchStr)})},s),Q(),b.focusOut&&b.focusOut(),b.overrideSuggestions&&b.searchStr&&b.searchStr.length>0&&-1===b.currentIndex&&G())},b.resetHideResults=function(){$&&e.cancel($)},b.hoverRow=function(a){b.currentIndex=a},b.selectResult=function(a){b.matchClass&&(a.title=B(a.originalObject),a.description=C(a.originalObject,b.descriptionField)),b.clearSelected?b.searchStr=null:b.searchStr=a.title,y(a),T()},b.inputChangeHandler=function(a){return a.length
{{ result.title }}
{{result.description}}
'),{restrict:"EA",require:"^?form",scope:{selectedObject:"=",disableInput:"=",initialValue:"=",localData:"=",remoteUrlRequestFormatter:"=",remoteUrlRequestWithCredentials:"@",remoteUrlResponseFormatter:"=",remoteUrlErrorCallback:"=",remoteApiHandler:"=",id:"@",type:"@",placeholder:"@",remoteUrl:"@",remoteUrlDataField:"@",titleField:"@",descriptionField:"@",imageField:"@",inputClass:"@",pause:"@",searchFields:"@",minlength:"@",matchClass:"@",clearSelected:"@",overrideSuggestions:"@",fieldRequired:"@",fieldRequiredClass:"@",inputChanged:"=",autoMatch:"@",focusOut:"&",focusIn:"&",inputName:"@",focusFirst:"@"},templateUrl:function(a,b){return b.templateUrl||w},compile:function(a,b){var c=g.startSymbol(),d=g.endSymbol();if("{{"!==c||"}}"!==d){var e=a.html().replace(/\{\{/g,c).replace(/\}\}/g,d);a.html(e)}return h}}}])}); \ No newline at end of file From 68b7defb43c818ca3d5c11a9295d3a96ea2ade11 Mon Sep 17 00:00:00 2001 From: Nicolas Polizzo Date: Fri, 11 Sep 2015 14:44:28 +0200 Subject: [PATCH 5/8] write karma test for focusFirst attribute --- dist/angucomplete-alt.min.js | 2 -- test/angucomplete-alt.spec.js | 25 +++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) delete mode 100644 dist/angucomplete-alt.min.js diff --git a/dist/angucomplete-alt.min.js b/dist/angucomplete-alt.min.js deleted file mode 100644 index 7bcd8859..00000000 --- a/dist/angucomplete-alt.min.js +++ /dev/null @@ -1,2 +0,0 @@ -/*! Copyright (c) 2014 Hidenari Nozaki and contributors | Licensed under the MIT license */ -"use strict";!function(a,b){"undefined"!=typeof module&&module.exports?module.exports=b(require("angular")):"function"==typeof define&&define.amd?define(["angular"],b):b(a.angular)}(window,function(a){a.module("angucomplete-alt",[]).directive("angucompleteAlt",["$q","$parse","$http","$sce","$timeout","$templateCache","$interpolate",function(a,b,c,d,e,f,g){function h(b,f,g,h){function w(a){la=null,b.hideResults(a),document.body.removeEventListener("click",w)}function x(a){return a.which?a.which:a.keyCode}function y(a){"function"==typeof b.selectedObject?b.selectedObject(a):b.selectedObject=a,E(a?!0:!1)}function z(a){return function(c){return b[a]?b[a](c):c}}function A(a){y({originalObject:a}),b.clearSelected&&(b.searchStr=null),T()}function B(a){return b.titleField.split(",").map(function(b){return C(a,b)}).join(" ")}function C(a,b){var c,d;if(b){c=b.split("."),d=a;for(var e=0;e'+f[0]+""):a,d.trustAsHtml(e)):void 0}function E(a){b.notEmpty=a,ha=b.searchStr,b.fieldRequired&&h&&h.$setValidity(ga,a)}function F(a){var c=x(a);if(c!==l&&c!==j)if(c===k||c===n)a.preventDefault();else if(c===i)a.preventDefault(),!b.showDropdown&&b.searchStr&&b.searchStr.length>=ea&&(U(),b.searching=!0,X(b.searchStr));else if(c===m)T(),b.$apply(function(){da.val(b.searchStr)});else{if(0===ea&&!b.searchStr)return;b.searchStr&&""!==b.searchStr?b.searchStr.length>=ea&&(U(),fa&&e.cancel(fa),b.searching=!0,fa=e(function(){X(b.searchStr)},b.pause)):b.showDropdown=!1,ha&&ha!==b.searchStr&&!b.clearSelected&&b.$apply(function(){y()})}}function G(a){!b.overrideSuggestions||b.selectedObject&&b.selectedObject.originalObject===b.searchStr||(a&&a.preventDefault(),A(b.searchStr))}function H(a){var b=getComputedStyle(a);return a.offsetHeight+parseInt(b.marginTop,10)+parseInt(b.marginBottom,10)}function I(){return ja.getBoundingClientRect().top+parseInt(getComputedStyle(ja).maxHeight,10)}function J(){return f[0].querySelectorAll(".angucomplete-row")[b.currentIndex]}function K(){return J().getBoundingClientRect().top-(ja.getBoundingClientRect().top+parseInt(getComputedStyle(ja).paddingTop,10))}function L(a){ja.scrollTop=ja.scrollTop+a}function M(){var a=b.results[b.currentIndex];b.matchClass?da.val(B(a.originalObject)):da.val(a.title)}function N(a){var c=x(a),d=null,e=null;c===n&&b.results?(b.currentIndex>=0&&b.currentIndex=1?(b.$apply(function(){b.currentIndex--,M()}),ka&&(e=K(),0>e&&L(e-1))):0===b.currentIndex&&b.$apply(function(){b.currentIndex=-1,da.val(b.searchStr)})):c===o&&(b.results&&b.results.length>0&&b.showDropdown?-1===b.currentIndex&&b.overrideSuggestions?G():(-1===b.currentIndex&&(b.currentIndex=0),b.selectResult(b.results[b.currentIndex]),b.$digest()):b.searchStr&&b.searchStr.length>0&&G())}function O(a){return function(c,d,e,f){d||e||f||(c=c.data),b.searching=!1,Y(C(_(c),b.remoteUrlDataField),a)}}function P(a,c,d,e){c||d||e||(c=a.status),0!==c&&(b.remoteUrlErrorCallback?b.remoteUrlErrorCallback(a,c,d,e):console&&console.error&&console.error("http error"))}function Q(){ia&&ia.resolve()}function R(d){var e={},f=b.remoteUrl+encodeURIComponent(d);b.remoteUrlRequestFormatter&&(e={params:b.remoteUrlRequestFormatter(d)},f=b.remoteUrl),b.remoteUrlRequestWithCredentials&&(e.withCredentials=!0),Q(),ia=a.defer(),e.timeout=ia.promise,c.get(f,e).success(O(d)).error(P)}function S(c){Q(),ia=a.defer(),b.remoteApiHandler(c,ia.promise).then(O(c))["catch"](P)}function T(){b.showDropdown=!1,b.results=[],ja&&(ja.scrollTop=0)}function U(){b.showDropdown=ba,b.currentIndex=b.focusFirst?0:-1,b.results=[]}function V(a){var c,d,e,f,g=b.searchFields.split(","),h=[];for(c=0;c=0;d&&(h[h.length]=b.localData[c])}b.searching=!1,Y(h,a)}function W(a,c,d){if(d)for(var e in c)if(c[e].toLowerCase()===d.toLowerCase())return void b.selectResult(a)}function X(a){!a||a.length0)for(b.results=[],d=0;d0?b.searchStr=a:console&&console.error&&console.error("Tried to set initial value of angucomplete to",a,"which is an invalid value"),E(!0))}),b.$on("angucomplete-alt:clearInput",function(a,c){c&&c!==b.id||(b.searchStr=null,y(),E(!1),T())}),b.onFocusHandler=function(){b.focusIn&&b.focusIn(),0!==ea||b.searchStr&&0!==b.searchStr.length||(b.currentIndex=b.focusFirst?0:b.currentIndex,b.showDropdown=!0,Z())},b.hideResults=function(a){la&&(la===b.id+"_dropdown"||la.indexOf("angucomplete")>=0)?la=null:($=e(function(){T(),b.$apply(function(){b.searchStr&&b.searchStr.length>0&&da.val(b.searchStr)})},s),Q(),b.focusOut&&b.focusOut(),b.overrideSuggestions&&b.searchStr&&b.searchStr.length>0&&-1===b.currentIndex&&G())},b.resetHideResults=function(){$&&e.cancel($)},b.hoverRow=function(a){b.currentIndex=a},b.selectResult=function(a){b.matchClass&&(a.title=B(a.originalObject),a.description=C(a.originalObject,b.descriptionField)),b.clearSelected?b.searchStr=null:b.searchStr=a.title,y(a),T()},b.inputChangeHandler=function(a){return a.length
{{ result.title }}
{{result.description}}
'),{restrict:"EA",require:"^?form",scope:{selectedObject:"=",disableInput:"=",initialValue:"=",localData:"=",remoteUrlRequestFormatter:"=",remoteUrlRequestWithCredentials:"@",remoteUrlResponseFormatter:"=",remoteUrlErrorCallback:"=",remoteApiHandler:"=",id:"@",type:"@",placeholder:"@",remoteUrl:"@",remoteUrlDataField:"@",titleField:"@",descriptionField:"@",imageField:"@",inputClass:"@",pause:"@",searchFields:"@",minlength:"@",matchClass:"@",clearSelected:"@",overrideSuggestions:"@",fieldRequired:"@",fieldRequiredClass:"@",inputChanged:"=",autoMatch:"@",focusOut:"&",focusIn:"&",inputName:"@",focusFirst:"@"},templateUrl:function(a,b){return b.templateUrl||w},compile:function(a,b){var c=g.startSymbol(),d=g.endSymbol();if("{{"!==c||"}}"!==d){var e=a.html().replace(/\{\{/g,c).replace(/\}\}/g,d);a.html(e)}return h}}}])}); \ No newline at end of file diff --git a/test/angucomplete-alt.spec.js b/test/angucomplete-alt.spec.js index b09a6f8e..c2a2cd5b 100644 --- a/test/angucomplete-alt.spec.js +++ b/test/angucomplete-alt.spec.js @@ -1608,5 +1608,30 @@ describe('angucomplete-alt', function() { $timeout.flush(); expect(element.find('.angucomplete-row').length).toBe(3); }); + + describe('focus first attribute', function() { + it('should focus on first row when focusFirst attribute is present', function() { + var element = angular.element('
'); + $scope.selectedPerson = undefined; + $scope.people = [ + {name: 'Jim Beam', email: 'jbeam@aol.com'}, + {name: 'Elvis Presly', email: 'theking@gmail.com'}, + {name: 'John Elway', email: 'elway@nfl.com'} + ]; + $compile(element)($scope); + $scope.$digest(); + expect($scope.currentIndex).toEqual(0); + + var inputField = element.find('#ex1_value'); + var y = $.Event('keyup'); + y.which = 121; + + inputField.val('El'); + inputField.trigger('input'); + inputField.trigger(y); + $timeout.flush(); + expect($scope.currentIndex).toEqual(0); + }); + }); }); }); From 48c7eebc058b3c65bb34ddbee52843ab5865f4ab Mon Sep 17 00:00:00 2001 From: Nicolas Polizzo Date: Fri, 11 Sep 2015 15:02:48 +0200 Subject: [PATCH 6/8] move test block to the proper place --- test/angucomplete-alt.spec.js | 42 +++++++++++++++++------------------ 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/test/angucomplete-alt.spec.js b/test/angucomplete-alt.spec.js index c2a2cd5b..7fb46246 100644 --- a/test/angucomplete-alt.spec.js +++ b/test/angucomplete-alt.spec.js @@ -1608,30 +1608,30 @@ describe('angucomplete-alt', function() { $timeout.flush(); expect(element.find('.angucomplete-row').length).toBe(3); }); + }); - describe('focus first attribute', function() { - it('should focus on first row when focusFirst attribute is present', function() { - var element = angular.element('
'); - $scope.selectedPerson = undefined; - $scope.people = [ - {name: 'Jim Beam', email: 'jbeam@aol.com'}, - {name: 'Elvis Presly', email: 'theking@gmail.com'}, - {name: 'John Elway', email: 'elway@nfl.com'} - ]; - $compile(element)($scope); - $scope.$digest(); - expect($scope.currentIndex).toEqual(0); + describe('focus first attribute', function() { + it('should focus on first row when focusFirst attribute is present', function() { + var element = angular.element('
'); + $scope.selectedPerson = undefined; + $scope.people = [ + {name: 'Jim Beam', email: 'jbeam@aol.com'}, + {name: 'Elvis Presly', email: 'theking@gmail.com'}, + {name: 'John Elway', email: 'elway@nfl.com'} + ]; + $compile(element)($scope); + $scope.$digest(); + expect($scope.currentIndex).toEqual(0); - var inputField = element.find('#ex1_value'); - var y = $.Event('keyup'); - y.which = 121; + var inputField = element.find('#ex1_value'); + var y = $.Event('keyup'); + y.which = 121; - inputField.val('El'); - inputField.trigger('input'); - inputField.trigger(y); - $timeout.flush(); - expect($scope.currentIndex).toEqual(0); - }); + inputField.val('El'); + inputField.trigger('input'); + inputField.trigger(y); + $timeout.flush(); + expect($scope.currentIndex).toEqual(0); }); }); }); From 44010ec4eafde2d14fc396af6f116909ec8262f6 Mon Sep 17 00:00:00 2001 From: Freezystem Date: Sat, 12 Sep 2015 12:58:12 +0200 Subject: [PATCH 7/8] fix bad written test for focusFirst attribute --- test/angucomplete-alt.spec.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/test/angucomplete-alt.spec.js b/test/angucomplete-alt.spec.js index 7fb46246..3da1cfc0 100644 --- a/test/angucomplete-alt.spec.js +++ b/test/angucomplete-alt.spec.js @@ -1611,7 +1611,7 @@ describe('angucomplete-alt', function() { }); describe('focus first attribute', function() { - it('should focus on first row when focusFirst attribute is present', function() { + it('should focus on first row when focusFirst attribute is set to true', function() { var element = angular.element('
'); $scope.selectedPerson = undefined; $scope.people = [ @@ -1621,17 +1621,19 @@ describe('angucomplete-alt', function() { ]; $compile(element)($scope); $scope.$digest(); - expect($scope.currentIndex).toEqual(0); + + expect(element.isolateScope().focusFirst).toBeTruthy(); + expect(element.isolateScope().currentIndex).toBe(0); var inputField = element.find('#ex1_value'); - var y = $.Event('keyup'); - y.which = 121; + var e = $.Event('keyup'); - inputField.val('El'); + e.which = 'l'.charCodeAt(0); + inputField.val('l'); inputField.trigger('input'); - inputField.trigger(y); + inputField.trigger(e); $timeout.flush(); - expect($scope.currentIndex).toEqual(0); + expect(element.isolateScope().currentIndex).toBe(0); }); }); }); From 57038998f7d54e1164222915632e333a783f4981 Mon Sep 17 00:00:00 2001 From: Freezystem Date: Sun, 13 Sep 2015 00:04:51 +0200 Subject: [PATCH 8/8] split and improve tests for focusFirst attribute --- test/angucomplete-alt.spec.js | 56 ++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/test/angucomplete-alt.spec.js b/test/angucomplete-alt.spec.js index 3da1cfc0..53a0a366 100644 --- a/test/angucomplete-alt.spec.js +++ b/test/angucomplete-alt.spec.js @@ -1611,8 +1611,8 @@ describe('angucomplete-alt', function() { }); describe('focus first attribute', function() { - it('should focus on first row when focusFirst attribute is set to true', function() { - var element = angular.element('
'); + it('should be handled by angucomplete-alt directive', function() { + var element = angular.element('
'); $scope.selectedPerson = undefined; $scope.people = [ {name: 'Jim Beam', email: 'jbeam@aol.com'}, @@ -1623,7 +1623,19 @@ describe('angucomplete-alt', function() { $scope.$digest(); expect(element.isolateScope().focusFirst).toBeTruthy(); - expect(element.isolateScope().currentIndex).toBe(0); + expect(element.isolateScope().currentIndex).toEqual(0); + }); + + it('should force the focus on first match after match list update', function() { + var element = angular.element('
'); + $scope.selectedPerson = undefined; + $scope.people = [ + {name: 'Jim Beam', email: 'jbeam@aol.com'}, + {name: 'Elvis Presly', email: 'theking@gmail.com'}, + {name: 'John Elway', email: 'elway@nfl.com'} + ]; + $compile(element)($scope); + $scope.$digest(); var inputField = element.find('#ex1_value'); var e = $.Event('keyup'); @@ -1633,7 +1645,43 @@ describe('angucomplete-alt', function() { inputField.trigger('input'); inputField.trigger(e); $timeout.flush(); - expect(element.isolateScope().currentIndex).toBe(0); + expect(element.isolateScope().searchStr).toEqual('l'); + expect(element.find('.angucomplete-row').length).toEqual(2); + expect(element.isolateScope().currentIndex).toEqual(0); + }); + + it('should force the focus on first match after input is blured and refocused', function() { + var element = angular.element('
'); + $scope.selectedPerson = undefined; + $scope.people = [ + {name: 'Jim Beam', email: 'jbeam@aol.com'}, + {name: 'Elvis Presly', email: 'theking@gmail.com'}, + {name: 'John Elway', email: 'elway@nfl.com'} + ]; + $compile(element)($scope); + $scope.$digest(); + + var inputField = element.find('#ex1_value'); + var e = $.Event('keyup'); + + e.which = 'l'.charCodeAt(0); + inputField.val('l'); + inputField.trigger('input'); + inputField.trigger(e); + + element.isolateScope().currentIndex = 1; + $timeout.flush(); + expect(element.find('.angucomplete-row').length).toEqual(2); + + inputField.blur(); + $timeout.flush(); + expect(element.find('#ex1_dropdown').hasClass('ng-hide')).toBeTruthy(); + + inputField.focus(); + $timeout(function(){ + expect(element.find('#ex1_dropdown').hasClass('ng-hide')).toBeFalsy(); + expect(element.isolateScope().currentIndex).toEqual(0); + },0); }); }); });