-
Notifications
You must be signed in to change notification settings - Fork 64
/
massautocomplete.min.js
1 lines (1 loc) · 5.26 KB
/
massautocomplete.min.js
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){if("undefined"!=typeof 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"}},e.CLASSES={container:"ac-container",menu:"ac-menu",menu_item:"ac-menu-item",menu_item_focus:"ac-state-focus"},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:'<span ng-transclude></span><div class="'+e.CLASSES.container+'"aria-autocomplete="list" role="listbox" ng-show="show_autocomplete"><ul class="'+e.CLASSES.menu+'"> <li ng-repeat="result in results" ng-if="$index > 0" class="'+e.CLASSES.menu_item+'" role="option" id="{{result.id}}" ng-class="$index == selected_index ? \''+e.CLASSES.menu_item_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]),e.container[0].style.position="absolute"},controller:["$scope",function(a){function c(){a.show_autocomplete=!0}function u(){a.show_autocomplete=!1,p()}function l(e,n,o){var i;return function(){var a=this,c=arguments,u=function(){i=null,o||e.apply(a,c)},l=o&&!i;t.cancel(i),i=t(u,n),l&&e.apply(a,c)}}function s(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(v.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(),v.auto_select_first&&f(1)):(a.results=[],u()))},function(e){u(),v.on_error&&v.on_error(e)})["finally"](function(){a.waiting_for_suggestion=!1}):(a.waiting_for_suggestion=!1,u(),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,S=e,v=n,N=e.$viewValue,A=s(t),a.container[0].setAttribute("aria-labelledby",b.id),a.results=[],a.selected_index=-1,_(),w=a.$watch(function(){return e.$modelValue},function(e){e!==y&&(r(),x(e,b))})))}function m(e){S.$modelValue!==e&&(S.$setViewValue(e),S.$render())}function p(){a.selected_index=-1,a.container[0].removeAttribute("aria-activedescendant"),y=void 0}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,V),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?(u(),a.$apply()):b.val(N);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()),u(),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,S,v,N,w,y,A,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};a.show_autocomplete=!1;var V=l(r,$.debounce_position),x=l(d,$.debounce_suggest);g.attach=l(E,$.debounce_attach),g.detach=function(){if(b){var t=b.val();m(t),v.on_detach&&v.on_detach(t),b.unbind(e.EVENTS.KEYDOWN,h[e.EVENTS.KEYDOWN]),b.unbind(e.EVENTS.BLUR,h[e.EVENTS.BLUR]),A&&b[0].removeAttribute("id")}u(),a.container[0].removeAttribute("aria-labelledby"),angular.element(n).unbind(e.EVENTS.RESIZE,h[e.EVENTS.RESIZE]),w&&w(),a.selected_index=a.results=void 0,S=b=N=void 0},a.apply_selection=function(e){if(b[0].focus(),!(!a.show_autocomplete||e>a.results.length||e<0)){var t=f(e);y=t.value,m(t.value),u(),v.on_select&&v.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)})}}})}();