/
autocomplete.min.js
1 lines (1 loc) · 2.17 KB
/
autocomplete.min.js
1
"use strict";angular.module("jackrabbitsgroup.angular-autocomplete",[]).directive("jrgAutocomplete",["$timeout","$filter",function($timeout,$filter){return{restrict:"A",scope:{ngModel:"=",vals:"=",config:"=?",ngChange:"&?"},template:function(element,attrs){var defaultsAttrs={placeholder:"Type to search.."};for(var xx in defaultsAttrs)void 0===attrs[xx]&&(attrs[xx]=defaultsAttrs[xx]);var html="<div class='jrg-autocomplete'><input type='text' ng-model='ngModel' ng-change='filterVals({})' placeholder='"+attrs.placeholder+"' class='jrg-autocomplete-input' /><div ng-show='visible.dropdown' class='jrg-autocomplete-dropdown'><div ng-repeat='val in filteredVals' class='jrg-autocomplete-dropdown-val' ng-click='clickVal(val, {})'>{{val}}</div></div></div>";return html},link:function(){},controller:function($scope,$element){function init(){$scope.filterVals({})}function hideDropdown(){$scope.visible.dropdown=!1}function ngChange(){timeouts.ngChange&&$timeout.cancel(timeouts.ngChange),void 0!==$scope.ngChange&&$timeout(function(){$scope.ngChange()},50)}$scope.visible={dropdown:!1},$scope.filteredVals=[];var triggers={clickDropdown:!1},timers={resetClickDropdown:400,blurDropdown:350,ngChange:750},timeouts={ngChange:!1},keycodes={enter:13,tab:9},ele=angular.element($element).find("input");ele.bind("keyup",function(evt){evt.keyCode==keycodes.enter?$scope.filteredVals.length>0&&$scope.clickVal($scope.filteredVals[0],{}):($scope.filterVals({}),$scope.visible.dropdown=!0,timeouts.ngChange&&$timeout.cancel(timeouts.ngChange),timeouts.ngChange=$timeout(function(){ngChange({})},timers.ngChange)),$scope.$$phase||$scope.$apply()}),ele.bind("focus",function(){$scope.visible.dropdown=!0,$scope.$$phase||$scope.$apply()}),ele.bind("blur",function(){$timeout(function(){triggers.clickDropdown||hideDropdown({})},timers.blurDropdown),$scope.$$phase||$scope.$apply()}),$scope.clickDropdown=function(){triggers.clickDropdown=!0,$timeout(function(){triggers.clickDropdown=!1},timers.resetClickDropdown)},$scope.clickVal=function(val){$scope.clickDropdown({}),$scope.ngModel=val,hideDropdown({}),ngChange({})},$scope.filterVals=function(){$scope.filteredVals=$filter("filter")($scope.vals,$scope.ngModel)},init({})}}}]);