forked from mauriciogentile/angular-livesearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
liveSearch.min.js
1 lines (1 loc) · 2.17 KB
/
liveSearch.min.js
1
"use strict";angular.module("LiveSearch",["ng"]).directive("liveSearch",["$compile","$timeout",function(e,t){return{restrict:"E",replace:true,scope:{liveSearchCallback:"=",liveSearchSelect:"=?",liveSearchItemTemplate:"@",liveSearchWaitTimeout:"=?",liveSearchMaxResultSize:"=?"},template:"<input type='text' />",link:function(n,r,i,s){var o;n.results=[];n.visible=false;n.selectedIndex=-1;n.select=function(e){n.selectedIndex=e;n.visible=false};n.isSelected=function(e){return n.selectedIndex===e};n.$watch("selectedIndex",function(e,t){var s=n.results[e];if(s){if(i.liveSearchSelect){r.val(s[i.liveSearchSelect])}else{r.val(s)}}if("undefined"!==r.controller("ngModel")){r.controller("ngModel").$setViewValue(r.val())}});n.$watch("visible",function(e,t){if(e==false){return}n.width=r[0].clientWidth;var i=u(r[0]);n.top=i.y+r[0].clientHeight+1;n.left=i.x});r[0].onkeydown=function(e){if(e.keyCode==40){if(n.selectedIndex+1===n.results.length){n.selectedIndex=0}else{n.selectedIndex++}}else if(e.keyCode==38){if(n.selectedIndex==0){n.selectedIndex=n.results.length-1}else if(n.selectedIndex==-1){n.selectedIndex=0}else n.selectedIndex--}if(e.keyCode==13){n.visible=false}n.$apply()};r[0].onkeyup=function(e){if(e.keyCode==13||e.keyCode==37||e.keyCode==38||e.keyCode==39||e.keyCode==40){return false}var i=r;t.cancel(o);var s=i.val().split(",");var u=s[s.length-1].trim();if(u.length<3||u.length>9){n.visible=false;n.$apply();return}o=t(function(){var e=[];var t=n.liveSearchCallback.call(null,{query:u});t.then(function(t){if(t){e=t.slice(0,(n.liveSearchMaxResultSize||20)-1)}n.visible=true});t.finally(function(){n.selectedIndex=-1;n.results=e.filter(function(t,n){return e.indexOf(t)==n})})},n.liveSearchWaitTimeout||100)};var u=function(e){var t=0;var n=0;while(e){t+=e.offsetLeft-e.scrollLeft+e.clientLeft;n+=e.offsetTop-e.scrollTop+e.clientTop;e=e.offsetParent}return{x:t,y:n}};var a=r.attr("live-search-item-template")||"{{result}}";var f="<ul ng-show='visible' style='top:{{top}}px;left:{{left}}px;width:{{width}}' class='searchresultspopup'><li ng-class=\"{ 'selected' : isSelected($index) }\" ng-click='select($index)' ng-repeat='result in results'>"+a+"</li></ul>";var l=e(f)(n);document.body.appendChild(l[0])}}}])