/* Assuming the autocomplete custom template fields as * id, title, subTitle, name, image, etc. Use as necessary * ( response should get convert into the above fields as an object ). * */ angular.module('kalgudiApp').directive('autocompleteSearch',function() { return { templateUrl: !IS_MOBILE_DEVICE ? 'webapp/themes/html/search/template/autocompleteSearchTemplate.html' : 'app/themes/mobile_app/basicApp/autoCompleteSearchTemplate.html', scope : { searchKey :'=searchKey', // search keyword variable searchProp : '=searchProp', // properties object having minChar, keyPressDelay etc. callService : '&callService', // service function which gets autocomplete result searchTypeAutoComplete : '=searchTypeAutoComplete', // type of document like products, business etc showAutoComplete: '=visible', //after clicking button hide or show template hideInternalTemplate: '=hideInternalTemplate', //to make the template hide from outside click secondArg:'=secondArg' //to return second argument also }, link : function(scope, element, attrs){ scope.autoCompletes= [];//autocomplete result array var timeoutId = 0; // interval reference scope.selectedWord = ""; // selected word from the autocomplete list. /* Watches the changes made in 'searchKey' and calls * the service after given interval. */ scope.$watch('searchKey', function (a) { //if selectedWord equals searchKey then don't do service call // 'showAutoComplete' also facilitates to toggle the service call //console.log(scope.searchKey); //console.log(scope.searchTypeAutoComplete); if(scope.searchKey && scope.searchKey.length >= scope.searchProp.minChar && scope.selectedWord != scope.searchKey && scope.showAutoComplete){ clearTimeout(timeoutId); timeoutId = setTimeout(function(){ var s = scope.callService(); if(s != null){ //if passed function not returned null s.then(function(data) { if(typeof(data) == "string"){ data = JSON.parse(data); } //console.log(data); scope.filterResult(data); }); } else { return; } }, scope.searchProp.keyPressDelay); } else { clearTimeout(timeoutId); scope.autoCompletes = []; } }); /* converting autocomplete result into custom template model */ scope.filterResult = function(results){ scope.autoCompletes = []; scope.showTemplate = true; switch(scope.searchTypeAutoComplete){ case 'Products':{ scope.productNameFlag = false; scope.productDetailsFlag = false; var prodNameArr = results.productNames; for(var i in prodNameArr){ scope.productNameFlag = true; var singleObj = {}; singleObj.name = prodNameArr[i]; singleObj.subTitle = "productName"; scope.autoCompletes.push(singleObj); } var prodDetArr = results.productDetails; for(var j in prodDetArr){ scope.productDetailsFlag = true; var singleObj = {}; singleObj.name = prodDetArr[j]; singleObj.subTitle = "productDetails"; scope.autoCompletes.push(singleObj); } break; } case 'Network':{ for(var i in results){ var singleObj = {}; singleObj.name = results[i]; scope.autoCompletes.push(singleObj); } break; } case 'Location':{ for(var i in results){ var obj = results[i] var singleObj = {}; singleObj.name = obj.doc.locationName; singleObj.id = obj.doc.VProductId; scope.autoCompletes.push(singleObj); } break; } case 'vproductfull':{ for(var i in results){ var obj = results[i] var singleObj = {}; singleObj.name = obj.doc.VProductName; singleObj.id = obj.doc.VProductId; scope.autoCompletes.push(singleObj); } break; } }; } /* after selecting word, assigning the word and hiding the autocomplete list. */ scope.selectWord = function(word,secondarg){ console.log("selectWord",word,secondarg) scope.selectedWord = word; scope.searchKey = word; scope.showTemplate = false; scope.secondArg = secondarg; }; //this is to hide the template scope.$watch('hideInternalTemplate', function (a) { if(scope.autoCompletes.length > 0){ scope.showTemplate = false; }; }); } /*, controller : function ($scope){ console.log("productForwardSearch",$scope.item); }*/ } });