From 6ff15b65334966ac30cdf2fda18046b5771a9f69 Mon Sep 17 00:00:00 2001 From: 0x4C4A Date: Wed, 3 May 2017 15:30:28 +0300 Subject: [PATCH] Added blur_on_select option. --- massautocomplete.js | 4 ++++ massautocomplete.min.js | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/massautocomplete.js b/massautocomplete.js index deb279b..1e43e5c 100644 --- a/massautocomplete.js +++ b/massautocomplete.js @@ -321,6 +321,10 @@ angular.module('MassAutoComplete', []) update_model_value(selected.value); hide_autocomplete(); + if(current_options.blur_on_select){ + current_element[0].blur(); + } + if (current_options.on_select) { current_options.on_select(selected); } diff --git a/massautocomplete.min.js b/massautocomplete.min.js index d78f80d..864fec1 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,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.blur_on_select&&b[0].blur(),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