diff --git a/massautocomplete.js b/massautocomplete.js index deb279b..3adf840 100644 --- a/massautocomplete.js +++ b/massautocomplete.js @@ -101,7 +101,7 @@ angular.module('MassAutoComplete', []) current_options, previous_value, value_watch, - last_selected_value, + last_selected_value = null, current_element_random_id_set; $scope.show_autocomplete = false; @@ -154,7 +154,8 @@ angular.module('MassAutoComplete', []) $scope.selected_index = 0; $scope.waiting_for_suggestion = true; - if (typeof(term) === 'string' && term.length > 0) { + if ((typeof(term) === 'string' || (term === undefined && current_options.suggest_when_empty)) && + (current_options.suggest_when_empty || term.length > 0)) { $q.when(current_options.suggest(term), function suggest_succeeded(suggestions) { // Make sure the suggestion we are processing is of the current element. diff --git a/massautocomplete.min.js b/massautocomplete.min.js index d78f80d..4041588 100644 --- a/massautocomplete.min.js +++ b/massautocomplete.min.js @@ -1 +1 @@ -!function(){"use strict";angular.module("MassAutoComplete",[]).provider("massAutocompleteConfig",function(){var e=this;e.KEYS={TAB:9,ESC:27,ENTER:13,UP:38,DOWN:40},e.EVENTS={KEYDOWN:"keydown",RESIZE:"resize",BLUR:"blur"},e.DEBOUNCE={position:150,attach:300,suggest:200,blur:150},e.generate_random_id=function(e){return e+"_"+Math.random().toString().substring(2)},e.position_autocomplete=function(e,t){var n=t[0].getBoundingClientRect(),o=document.body.scrollTop||document.documentElement.scrollTop||window.pageYOffset,i=document.body.scrollLeft||document.documentElement.scrollLeft||window.pageXOffset;e[0].style.top=n.top+n.height+o+"px",e[0].style.left=n.left+i+"px",e[0].style.width=n.width+"px"},this.$get=function(){return e}}).directive("massAutocomplete",["massAutocompleteConfig","$timeout","$window","$document","$q",function(e,t,n,o,i){return{restrict:"A",scope:{options:"&massAutocomplete"},transclude:!0,template:'
',link:function(e,t){e.container=angular.element(t[0].getElementsByClassName("ac-container")[0]),e.container[0].style.position="absolute"},controller:["$scope",function(a){function c(){a.show_autocomplete=!0}function l(){a.show_autocomplete=!1,f()}function s(e,n,o){var i;return function(){var a=this,c=arguments,l=function(){i=null,o||e.apply(a,c)},s=o&&!i;t.cancel(i),i=t(l,n),s&&e.apply(a,c)}}function u(t){return(!t.id||""===t.id)&&(t.id=e.generate_random_id("ac_element"),!0)}function r(){e.position_autocomplete(a.container,b)}function d(t,n){a.selected_index=0,a.waiting_for_suggestion=!0,"string"==typeof t&&t.length>0?i.when(N.suggest(t),function(o){b&&b===n&&(o&&o.length>0?(o.forEach(function(t){t.id||(t.id=e.generate_random_id("ac_item"))}),a.results=[{value:t,label:"",id:""}].concat(o),c(),N.auto_select_first&&m(1)):(a.results=[],l()))},function(e){l(),N.on_error&&N.on_error(e)})["finally"](function(){a.waiting_for_suggestion=!1}):(a.waiting_for_suggestion=!1,l(),a.$apply())}function E(e,t,n){b!==t&&(b&&g.detach(),t[0]===o[0].activeElement&&(n.on_attach&&n.on_attach(),b=t,v=e,N=n,w=e.$viewValue,T=u(t),a.container[0].setAttribute("aria-labelledby",b.id),a.results=[],a.selected_index=-1,_(),S=a.$watch(function(){return e.$modelValue},function(e){e!==y&&(r(),A(e,b))})))}function p(e){v.$modelValue!==e&&(v.$setViewValue(e),v.$render())}function f(){a.selected_index=-1,a.container[0].removeAttribute("aria-activedescendant")}function m(e){var t=a.results[e];return b.val(t.value),a.selected_index=e,a.container[0].setAttribute("aria-activedescendant",t.id),t}function _(){angular.element(n).bind(e.EVENTS.RESIZE,x),h[e.EVENTS.BLUR]=function(){t(function(){b&&b[0]===o[0].activeElement||g.detach()},V.debounce_blur)},b.bind(e.EVENTS.BLUR,h[e.EVENTS.BLUR]),h[e.EVENTS.KEYDOWN]=function(t){if(!t.shiftKey)switch(t.keyCode){case e.KEYS.ESC:a.show_autocomplete?(l(),a.$apply()):b.val(w);break;case e.KEYS.ENTER:a.show_autocomplete&&a.selected_index>0&&!a.waiting_for_suggestion&&(a.apply_selection(a.selected_index),t.stopPropagation(),t.preventDefault()),l(),a.$apply();break;case e.KEYS.TAB:if(!a.show_autocomplete)break;t.preventDefault();case e.KEYS.DOWN:a.results.length>0&&(a.show_autocomplete?m(a.selected_index+1>a.results.length-1?0:a.selected_index+1):(c(),m(0)),a.$apply());break;case e.KEYS.UP:a.show_autocomplete&&(t.preventDefault(),m(a.selected_index-1>=0?a.selected_index-1:a.results.length-1),a.$apply())}},b.bind(e.EVENTS.KEYDOWN,h[e.EVENTS.KEYDOWN])}var g=this,h={};h[e.EVENTS.BLUR]=null,h[e.EVENTS.KEYDOWN]=null,h[e.EVENTS.RESIZE]=null;var b,v,N,w,S,y,T,$=a.options()||{},V={debounce_position:$.debounce_position||e.DEBOUNCE.position,debounce_attach:$.debounce_attach||e.DEBOUNCE.attach,debounce_suggest:$.debounce_suggest||e.DEBOUNCE.suggest,debounce_blur:$.debounce_blur||e.DEBOUNCE.blur};a.show_autocomplete=!1;var x=s(r,V.debounce_position),A=s(d,V.debounce_suggest);g.attach=s(E,V.debounce_attach),g.detach=function(){if(b){var t=b.val();p(t),N.on_detach&&N.on_detach(t),b.unbind(e.EVENTS.KEYDOWN,h[e.EVENTS.KEYDOWN]),b.unbind(e.EVENTS.BLUR,h[e.EVENTS.BLUR]),T&&b[0].removeAttribute("id")}l(),a.container[0].removeAttribute("aria-labelledby"),angular.element(n).unbind(e.EVENTS.RESIZE,h[e.EVENTS.RESIZE]),S&&S(),a.selected_index=a.results=void 0,v=b=w=void 0},a.apply_selection=function(e){if(b[0].focus(),!(!a.show_autocomplete||e>a.results.length||e<0)){var t=m(e);y=t.value,p(t.value),l(),N.on_select&&N.on_select(t)}},a.$on("$destroy",function(){g.detach(),a.container.remove()})}]}}]).directive("massAutocompleteItem",function(){return{restrict:"A",require:["^massAutocomplete","ngModel"],scope:{massAutocompleteItem:"&"},link:function(e,t,n,o){n.$set("autocomplete","off");var i=o[0],a=o[1];t.bind("focus",function(){var n=e.massAutocompleteItem();if(!n)throw new Error("Invalid options");i.attach(a,t,n)})}}})}(); \ No newline at end of file +!function(){"use strict";angular.module("MassAutoComplete",[]).provider("massAutocompleteConfig",function(){var e=this;e.KEYS={TAB:9,ESC:27,ENTER:13,UP:38,DOWN:40},e.EVENTS={KEYDOWN:"keydown",RESIZE:"resize",BLUR:"blur"},e.DEBOUNCE={position:150,attach:300,suggest:200,blur:150},e.generate_random_id=function(e){return e+"_"+Math.random().toString().substring(2)},e.position_autocomplete=function(e,t){var n=t[0].getBoundingClientRect(),o=document.body.scrollTop||document.documentElement.scrollTop||window.pageYOffset,i=document.body.scrollLeft||document.documentElement.scrollLeft||window.pageXOffset;e[0].style.top=n.top+n.height+o+"px",e[0].style.left=n.left+i+"px",e[0].style.width=n.width+"px"},this.$get=function(){return e}}).directive("massAutocomplete",["massAutocompleteConfig","$timeout","$window","$document","$q",function(e,t,n,o,i){return{restrict:"A",scope:{options:"&massAutocomplete"},transclude:!0,template:'
',link:function(e,t){e.container=angular.element(t[0].getElementsByClassName("ac-container")[0]),e.container[0].style.position="absolute"},controller:["$scope",function(a){function c(){a.show_autocomplete=!0}function l(){a.show_autocomplete=!1,m()}function s(e,n,o){var i;return function(){var a=this,c=arguments,l=function(){i=null,o||e.apply(a,c)},s=o&&!i;t.cancel(i),i=t(l,n),s&&e.apply(a,c)}}function u(t){return(!t.id||""===t.id)&&(t.id=e.generate_random_id("ac_element"),!0)}function r(){e.position_autocomplete(a.container,b)}function d(t,n){a.selected_index=0,a.waiting_for_suggestion=!0,("string"==typeof t||void 0===t&&N.suggest_when_empty)&&(N.suggest_when_empty||t.length>0)?i.when(N.suggest(t),function(o){b&&b===n&&(o&&o.length>0?(o.forEach(function(t){t.id||(t.id=e.generate_random_id("ac_item"))}),a.results=[{value:t,label:"",id:""}].concat(o),c(),N.auto_select_first&&f(1)):(a.results=[],l()))},function(e){l(),N.on_error&&N.on_error(e)})["finally"](function(){a.waiting_for_suggestion=!1}):(a.waiting_for_suggestion=!1,l(),a.$apply())}function p(e,t,n){b!==t&&(b&&g.detach(),t[0]===o[0].activeElement&&(n.on_attach&&n.on_attach(),b=t,v=e,N=n,w=e.$viewValue,y=u(t),a.container[0].setAttribute("aria-labelledby",b.id),a.results=[],a.selected_index=-1,_(),S=a.$watch(function(){return e.$modelValue},function(e){e!==V&&(r(),A(e,b))})))}function E(e){v.$modelValue!==e&&(v.$setViewValue(e),v.$render())}function m(){a.selected_index=-1,a.container[0].removeAttribute("aria-activedescendant")}function f(e){var t=a.results[e];return b.val(t.value),a.selected_index=e,a.container[0].setAttribute("aria-activedescendant",t.id),t}function _(){angular.element(n).bind(e.EVENTS.RESIZE,x),h[e.EVENTS.BLUR]=function(){t(function(){b&&b[0]===o[0].activeElement||g.detach()},$.debounce_blur)},b.bind(e.EVENTS.BLUR,h[e.EVENTS.BLUR]),h[e.EVENTS.KEYDOWN]=function(t){if(!t.shiftKey)switch(t.keyCode){case e.KEYS.ESC:a.show_autocomplete?(l(),a.$apply()):b.val(w);break;case e.KEYS.ENTER:a.show_autocomplete&&a.selected_index>0&&!a.waiting_for_suggestion&&(a.apply_selection(a.selected_index),t.stopPropagation(),t.preventDefault()),l(),a.$apply();break;case e.KEYS.TAB:if(!a.show_autocomplete)break;t.preventDefault();case e.KEYS.DOWN:a.results.length>0&&(a.show_autocomplete?f(a.selected_index+1>a.results.length-1?0:a.selected_index+1):(c(),f(0)),a.$apply());break;case e.KEYS.UP:a.show_autocomplete&&(t.preventDefault(),f(a.selected_index-1>=0?a.selected_index-1:a.results.length-1),a.$apply())}},b.bind(e.EVENTS.KEYDOWN,h[e.EVENTS.KEYDOWN])}var g=this,h={};h[e.EVENTS.BLUR]=null,h[e.EVENTS.KEYDOWN]=null,h[e.EVENTS.RESIZE]=null;var b,v,N,w,S,y,T=a.options()||{},$={debounce_position:T.debounce_position||e.DEBOUNCE.position,debounce_attach:T.debounce_attach||e.DEBOUNCE.attach,debounce_suggest:T.debounce_suggest||e.DEBOUNCE.suggest,debounce_blur:T.debounce_blur||e.DEBOUNCE.blur},V=null;a.show_autocomplete=!1;var x=s(r,$.debounce_position),A=s(d,$.debounce_suggest);g.attach=s(p,$.debounce_attach),g.detach=function(){if(b){var t=b.val();E(t),N.on_detach&&N.on_detach(t),b.unbind(e.EVENTS.KEYDOWN,h[e.EVENTS.KEYDOWN]),b.unbind(e.EVENTS.BLUR,h[e.EVENTS.BLUR]),y&&b[0].removeAttribute("id")}l(),a.container[0].removeAttribute("aria-labelledby"),angular.element(n).unbind(e.EVENTS.RESIZE,h[e.EVENTS.RESIZE]),S&&S(),a.selected_index=a.results=void 0,v=b=w=void 0},a.apply_selection=function(e){if(b[0].focus(),!(!a.show_autocomplete||e>a.results.length||e<0)){var t=f(e);V=t.value,E(t.value),l(),N.on_select&&N.on_select(t)}},a.$on("$destroy",function(){g.detach(),a.container.remove()})}]}}]).directive("massAutocompleteItem",function(){return{restrict:"A",require:["^massAutocomplete","ngModel"],scope:{massAutocompleteItem:"&"},link:function(e,t,n,o){n.$set("autocomplete","off");var i=o[0],a=o[1];t.bind("focus",function(){var n=e.massAutocompleteItem();if(!n)throw new Error("Invalid options");i.attach(a,t,n)})}}})}(); \ No newline at end of file