var searchCompanyApp = angular.module('searchCompanyApp', ["commonsApp"]); searchCompanyApp.directive('searchCompany',['$document','$timeout','Constants','commonsAppServices','mySharedService','$sce','$rootScope', function( $document,$timeout,Constants,commonsAppServices,sharedService,$sce,$rootScope) { return { restrict: "E", replace: true, scope: { searchCompany : '=', multiSelect : '@', callbackFn : '&', calltoFn : '&', wantToAdd : '@', clickToFollow : '=', loading : '=', txtPlaceHolder : '@', showFollow : '@', wishListText : '@', ignoreAdded:'@', clearText:'@', hideAddCompMsg:'@', maxLimitText : '@', limitCompanies : '=' }, link: function (scope, element, attrs) { scope.isPopupVisible = false; scope.newCompanyAdded = false; scope.maxCompanyAdded = false; scope.showMaxCompanyAddedText = false; scope.callbackCompanyList={}; scope.companyArray={}; scope.companyList={}; scope.newCompanyList={}; scope.search=""; scope.search.text=""; scope.clearText = false; scope.hideAddCompMsg = false; scope.focusIndex = -1; var ignoreIndexList=new Array(); attrs.$observe('showFollow', function(value){ if(!value){ scope.showFollow=true; } }); attrs.$observe('ignoreAdded', function(value){ if(!value){ scope.ignoreAdded=false; } }); attrs.$observe('clearText', function(value){ if(value){ scope.clearText = true; } }); attrs.$observe('hideAddCompMsg', function(value){ if(value){ scope.hideAddCompMsg = true; } }); attrs.$observe('multiSelect', function(value){ if(!value){ scope.multiSelect=true; }else if(value=="false"){ scope.multiSelect=false; }else{ scope.multiSelect=true; } }); attrs.$observe('wantToAdd', function(value){ if(!value){ scope.wantToAdd=true; }else if(value=="false"){ scope.wantToAdd=false; }else{ scope.wantToAdd=true; } }); attrs.$observe('wishListText', function(value){ if(!value){ scope.wishListText="Company Added to request list. Will appear in the list after approval"; } }); attrs.$observe('maxLimitText', function(value){ if(!value){ scope.maxLimitText="Oops! You have reached your max. You can't add more companies"; } }); attrs.$observe('calltoFn', function(value){ if(!value){ scope.calltoFn=function(){}; } }); $document.bind('click', function(event){ var isClickedElementChildOfPopup = element.find(event.target).length > 0; if (isClickedElementChildOfPopup) if(scope.multiSelect){ return; } // $("#searchAndManage").val(""); // scope.search.text=""; scope.isPopupVisible = false; scope.$apply(); }); scope.clearSearchBox = function(){ if(scope.clearText){ $rootScope.$broadcast('showHideCPTopCompThanksMsg'); } } scope.$on('maxCompanyAdded', function(){ scope.maxCompanyAdded = true; }); scope.$on('resetMaxCompanyAdded', function(){ scope.maxCompanyAdded = false; }); var headerWatermark = "Search for a company"; var selectedItem=null; function searchAndManageCompany() { var searchBox = document.getElementById("searchAndManageCompany"); if(searchBox != null){ var winWidth = (window.innerWidth > 0) ? window.innerWidth : screen.width; if(winWidth < 640) { var searchTerm = document.querySelector(".mobile-search #searchAndManageCompany").value; }else{ var searchTerm = document.querySelector("#searchAndManageCompany").value; } if (searchTerm == headerWatermark) { //user haven't entered anything //1) nothing to search //2) autocomplete menu won't be available return; } location.href = HOST_WITH_SSL+"browsecompanyprofiles.htm?searchTerm=" + searchTerm; } return false; }; scope.handleKeyPress = function(e) { var keycode; if (e) keycode = e.which; else return true; if (keycode == 13) { if(scope.focusIndex != -1){ scope.followCompany(scope.companyList[scope.focusIndex], !scope.companyList[scope.focusIndex].following) } }else if(keycode == 40) { e.preventDefault(); scope.focusIndex++; scope.focusIndex = (scope.focusIndex + scope.companyList.length) % scope.companyList.length; updateListSelection(scope.focusIndex); } else if(keycode == 38) { e.preventDefault(); if(scope.focusIndex == - 1 ) { scope.focusIndex = (scope.focusIndex + scope.companyList.length) % scope.companyList.length; }else{ scope.focusIndex--; scope.focusIndex = (scope.focusIndex + scope.companyList.length) % scope.companyList.length; } updateListSelection(scope.focusIndex); } } function updateListSelection(liID) { var list = document.getElementById("searchAndManageCompanySuggestionBox"); var targetLi = document.getElementById("searchAndManageCompanySuggestionBox-"+liID); list.scrollTop = (targetLi.offsetTop - 63); }; scope.updateSearchResult = function(searchTerm) { scope.focusIndex = -1; searchTerm = searchTerm.replace("https://", ''); searchTerm = searchTerm.replace("http://", ''); searchTerm = searchTerm.replace("www.", ''); if(searchTerm != "" && searchTerm.length > 1) { scope.isPopupVisible = true; commonsAppServices.searchCompanies(searchTerm).then(function(result) { scope.companyArray = result.data.results; searchTerm = getSearchTerm(searchTerm); if(searchTerm != "" && scope.companyArray != null && scope.companyArray != '') { scope.newCompanyAdded = false; scope.callbackCompanyList = scope.callbackFn(); var tempList=new Array(); angular.forEach(scope.companyArray, function(item) { var label = item.shortName + " - " + item.primaryDomain + getTickerIfNotNull(item); var lSearchTerm = searchTerm.toLowerCase(); var displayString = label; var lLabel = label.toLowerCase(); var startIndex = 0; while (lLabel.indexOf(lSearchTerm, startIndex) > -1) { var index = lLabel.indexOf(lSearchTerm, startIndex); startIndex = index + lSearchTerm.length + 13; var startString = displayString.substring(0, index); var endString = displayString.substring(index + lSearchTerm.length); var searchString = displayString.substring(index, index + lSearchTerm.length); searchString = "" + searchString + ""; displayString = startString + searchString + endString; lLabel = displayString.toLowerCase(); } var linkString = displayString; var following = false; var overrideFollow=false; var ignoreEntry=false; angular.forEach(scope.callbackCompanyList, function(company) { if (company.id+'' === item.id) { if(scope.ignoreAdded){ ignoreEntry=true; return; } if(company.overrideFollow==undefined){ following = true; }else{ overrideFollow=true; following = company.follow == undefined || company.follow; } } }); if(!ignoreEntry){ tempList.push(item); } item["overrideFollow"]=overrideFollow; item["resultString"] = linkString; item["following"] = following; }); scope.companyList = tempList; } else { scope.companyList = null; if(scope.wantToAdd && ValidURL(searchTerm)) { var company = []; scope.newCompanyList = []; company["url"] = searchTerm; scope.newCompanyList.push(company); } else { scope.newCompanyList = null; scope.companyList = null; } } }); } else { scope.isPopupVisible = false; scope.companyList=null; } }; scope.addNewCompanyToWatchlist = function(website) { commonsAppServices.addNewCompanyToWatchlist(website).then(function(result) { scope.isPopupVisible = false; scope.newCompanyAdded = true; if(scope.hideAddCompMsg){ $("#searchAndManage").val(""); $timeout(function(){ scope.newCompanyAdded = false; }, 2000); } }); } scope.followCompany = function(company, following) { $("#searchAndManage").val(""); if(scope.limitCompanies && scope.maxCompanyAdded){ scope.showMaxCompanyAddedText = true; $timeout(function(){ scope.showMaxCompanyAddedText = false; }, 2000); } else { if(scope.clickToFollow){ sharedService.showLoading(); commonsAppServices.followCompany(company.id, following).then(function(result) { scope.following = result.data; if(scope.following == "true") { sharedService.addCompany(company); if(scope.loading == false) { sharedService.hideLoading(); } } else if(scope.following == "false") { sharedService.removeCompany(company); sharedService.hideLoading(); } }); } else{ scope.calltoFn({company:company,following:following}); angular.forEach(scope.companyArray, function(item) { if(company.id === item.id){ item["following"] = following; } }); scope.companyList = scope.companyArray; } scope.isPopupVisible = false; } }; scope.$on('companyAdded', function() { scope.addedCompany = sharedService.addedCompany ; angular.forEach(scope.companyArray, function(company) { if (company.id === scope.addedCompany.id) { company.following = true; return false; } }); }); scope.$on('companyRemoved', function() { scope.removedCompany = sharedService.removedCompany ; angular.forEach(scope.companyArray, function(company) { if (company.id === scope.removedCompany.id) { company.following = false; return false; } }); }); scope.getHTMLvalue = function(name){ return $sce.trustAsHtml(name); }; function getTickerIfNotNull(item){ var ticker = item.tickerSymbol; if(ticker != null){ return " - " +item.tickerSymbol; }else{ return ""; }; } function getSearchTerm(str){ return stripDomain(str); } function ValidURL(str) { var strValue = str; var patt = /^[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,})$/; var result = patt.test(strValue); if(result) { return true; } else { return false; } } commonsAppServices.getTemplateLoader('searchCompany.html',Constants.cdnFullPath + 'scripts/angular/commons/modules/searchcompany/searchCompany.html')(element,scope); } }; }]);