forked from hakib/MassAutocomplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
massautocomplete.min.js
1 lines (1 loc) · 4.09 KB
/
massautocomplete.min.js
1
angular.module("MassAutoComplete",[]).directive("massAutocomplete",["$timeout","$window","$document","$q",function(e,t,n,o){"use strict";return{restrict:"A",scope:{options:"&massAutocomplete"},transclude:!0,template:'<span ng-transclude></span><div class="ac-container" ng-show="show_autocomplete && results.length > 0" style="position:absolute;"><ul class="ac-menu"><li ng-repeat="result in results" ng-if="$index > 0" class="ac-menu-item" ng-class="$index == selected_index ? \'ac-state-focus\': \'\'"><a href ng-click="apply_selection($index)" ng-bind-html="result.label"></a></li></ul></div>',link:function(e,t){e.container=angular.element(t[0].getElementsByClassName("ac-container")[0])},controller:["$scope",function(l){function s(e,t,n){var o;return function(){var l=this,s=arguments,c=function(){o=null,n||e.apply(l,s)},a=n&&!o;clearTimeout(o),o=setTimeout(c,t),a&&e.apply(l,s)}}function c(){var e=h[0].getBoundingClientRect(),o=n[0].body.scrollTop||n[0].documentElement.scrollTop||t.pageYOffset,s=n[0].body.scrollLeft||n[0].documentElement.scrollLeft||t.pageXOffset,c=l.container[0];c.style.top=e.top+e.height+o+"px",c.style.left=e.left+s+"px",c.style.width=e.width+"px"}function a(e,t,o){h!==t&&(h&&p.detach(),t[0]===n[0].activeElement&&(o.on_attach&&o.on_attach(),h=t,g=e,b=o,v=e.$viewValue,l.results=[],l.selected_index=-1,d(),w=l.$watch(function(){return e.$modelValue},function(e){e!==E&&(c(),R(e,h))})))}function u(e,t){l.selected_index=0,l.waiting_for_suggestion=!0,"string"==typeof e&&e.length>0?o.when(b.suggest(e),function(n){h&&h===t&&(n&&n.length>0?(l.results=[{value:e,label:""}].concat(n),l.show_autocomplete=!0,b.auto_select_first&&r(1)):l.results=[])},function(e){l.show_autocomplete=!1,b.on_error&&b.on_error(e)}).finally(function(){l.waiting_for_suggestion=!1}):(l.waiting_for_suggestion=!1,l.show_autocomplete=!1,l.$apply())}function i(e){g.$modelValue!==e&&(g.$setViewValue(e),g.$render())}function r(e){var t=l.results[e];return h.val(t.value),l.selected_index=e,t}function d(){angular.element(t).bind(m.RESIZE,x),f[m.BLUR]=function(){e(function(){h&&h[0]===n[0].activeElement||p.detach()},$.debounce_blur)},h.bind(m.BLUR,f[m.BLUR]),f[m.KEYDOWN]=function(e){if(!e.shiftKey)switch(e.keyCode){case _.ESC:l.show_autocomplete?(l.show_autocomplete=!1,l.$apply()):h.val(v);break;case _.ENTER:l.show_autocomplete&&l.selected_index>0&&!l.waiting_for_suggestion&&(l.apply_selection(l.selected_index),e.stopPropagation(),e.preventDefault()),l.show_autocomplete=!1,l.$apply();break;case _.TAB:if(!l.show_autocomplete)break;e.preventDefault();case _.DOWN:l.results.length>0&&(l.show_autocomplete?r(l.selected_index+1>l.results.length-1?0:l.selected_index+1):(l.show_autocomplete=!0,l.selected_index=0),l.$apply());break;case _.UP:l.show_autocomplete&&(e.preventDefault(),r(l.selected_index-1>=0?l.selected_index-1:l.results.length-1),l.$apply())}},h.bind(m.KEYDOWN,f[m.KEYDOWN])}var p=this,_={TAB:9,ESC:27,ENTER:13,UP:38,DOWN:40},m={KEYDOWN:"keydown",RESIZE:"resize",BLUR:"blur"},f={};f[m.BLUR]=null,f[m.KEYDOWN]=null,f[m.RESIZE]=null;var h,g,b,v,w,E,y=l.options()||{},$={debounce_position:y.debounce_position||150,debounce_attach:y.debounce_attach||300,debounce_suggest:y.debounce_suggest||200,debounce_blur:y.debounce_blur||150};l.show_autocomplete=!1;var x=s(c,$.debounce_position);p.attach=s(a,$.debounce_attach);var R=s(u,$.debounce_suggest);p.detach=function(){if(h){var e=h.val();i(e),b.on_detach&&b.on_detach(e),h.unbind(m.KEYDOWN,f[m.KEYDOWN]),h.unbind(m.BLUR,f[m.BLUR])}l.show_autocomplete=!1,angular.element(t).unbind(m.RESIZE,f[m.RESIZE]),w&&w(),l.selected_index=l.results=void 0,g=h=v=void 0},l.apply_selection=function(e){if(h[0].focus(),!(!l.show_autocomplete||e>l.results.length||0>e)){var t=r(e);E=t.value,i(t.value),l.show_autocomplete=!1,b.on_select&&b.on_select(t)}},l.$on("$destroy",function(){p.detach(),l.container.remove()})}]}}]).directive("massAutocompleteItem",function(){"use strict";return{restrict:"A",require:["^massAutocomplete","ngModel"],scope:{massAutocompleteItem:"&"},link:function(e,t,n,o){n.$set("autocomplete","off"),t.bind("focus",function(){var n=e.massAutocompleteItem();if(!n)throw"Invalid options";o[0].attach(o[1],t,n)})}}});