forked from hakib/MassAutocomplete
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmassautocomplete.min.js
More file actions
1 lines (1 loc) · 5 KB
/
Copy pathmassautocomplete.min.js
File metadata and controls
1 lines (1 loc) · 5 KB
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();e[0].style.left=0,e[0].style.top=n.height+"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:'<span ng-transclude></span><div class="ac-container" aria-autocomplete="list" role="listbox" ng-show="show_autocomplete" style="position:absolute;"><ul class="ac-menu"> <li ng-repeat="result in results" ng-if="$index > 0" class="ac-menu-item" role="option" id="{{result.id}}" 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","$element",function(a,s){function c(){a.show_autocomplete=!0}function l(){a.show_autocomplete=!1,f()}function u(e,t,n){var o;return function(){var i=this,a=arguments,s=function(){o=null,n||e.apply(i,a)},c=n&&!o;clearTimeout(o),o=setTimeout(s,t),c&&e.apply(i,a)}}function r(t){return(!t.id||""===t.id)&&(t.id=e.generate_random_id("ac_element"),!0)}function d(){e.position_autocomplete(a.container,v)}function E(t,n){a.selected_index=0,a.waiting_for_suggestion=!0,"string"==typeof t&&t.length>0?i.when(S.suggest(t),function(o){v&&v===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(),S.auto_select_first&&m(1)):(a.results=[],l()))},function(e){l(),S.on_error&&S.on_error(e)})["finally"](function(){a.waiting_for_suggestion=!1}):(a.waiting_for_suggestion=!1,l(),a.$apply())}function p(e,t,n){v!==t&&(v&&h.detach(),t[0]===o[0].activeElement&&(n.on_attach&&n.on_attach(),v=t,N=e,S=n,w=e.$viewValue,T=r(t),a.container[0].setAttribute("aria-labelledby",v.id),a.results=[],a.selected_index=-1,g(),y=a.$watch(function(){return e.$modelValue},function(e){e!==$&&(d(),D(e,v))})))}function _(e){N.$modelValue!==e&&(N.$setViewValue(e),N.$render())}function f(){a.selected_index=-1,a.container[0].removeAttribute("aria-activedescendant")}function m(e){var t=a.results[e];return v.val(t.value),a.selected_index=e,a.container[0].setAttribute("aria-activedescendant",t.id),t}function g(){angular.element(n).bind(e.EVENTS.RESIZE,A),b[e.EVENTS.BLUR]=function(){t(function(){v&&v[0]===o[0].activeElement||h.detach()},x.debounce_blur)},v.bind(e.EVENTS.BLUR,b[e.EVENTS.BLUR]),b[e.EVENTS.KEYDOWN]=function(t){if(!t.shiftKey)switch(t.keyCode){case e.KEYS.ESC:a.show_autocomplete?(l(),a.$apply()):v.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())}},v.bind(e.EVENTS.KEYDOWN,b[e.EVENTS.KEYDOWN])}var h=this,b={};b[e.EVENTS.BLUR]=null,b[e.EVENTS.KEYDOWN]=null,b[e.EVENTS.RESIZE]=null;var v,N,S,w,y,$,T,V=a.options()||{},x={debounce_position:V.debounce_position||e.DEBOUNCE.position,debounce_attach:V.debounce_attach||e.DEBOUNCE.attach,debounce_suggest:V.debounce_suggest||e.DEBOUNCE.suggest,debounce_blur:V.debounce_blur||e.DEBOUNCE.blur};a.show_autocomplete=!1;var A=u(d,x.debounce_position),D=u(E,x.debounce_suggest);h.attach=u(p,x.debounce_attach),h.detach=function(){if(v){var t=v.val();_(t),S.on_detach&&S.on_detach(t),v.unbind(e.EVENTS.KEYDOWN,b[e.EVENTS.KEYDOWN]),v.unbind(e.EVENTS.BLUR,b[e.EVENTS.BLUR]),T&&v[0].removeAttribute("id")}l(),a.container[0].removeAttribute("aria-labelledby"),angular.element(n).unbind(e.EVENTS.RESIZE,b[e.EVENTS.RESIZE]),y&&y(),a.selected_index=a.results=void 0,N=v=w=void 0},a.apply_selection=function(e){if(v[0].focus(),!(!a.show_autocomplete||e>a.results.length||e<0)){var t=m(e);$=t.value,_(t.value),l(),S.on_select&&S.on_select(t)}},a.$on("$destroy",function(){h.detach(),a.container.remove()}),["fixed","absolute","sticky","relative"].indexOf(s.css("position"))===-1&&s.css("position","relative")}]}}]).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)})}}})}();