var companyInfoApp = angular.module('companyInfoApp', ["commonsApp"]); companyInfoApp.directive('companyInfo',['$document','$timeout','$compile','Constants','commonsAppServices','mySharedService', function($document,$timeout,$compile,Constants,commonsAppServices,sharedService) { return { restrict: "E", templateUrl: Constants.cdnFullPath+'scripts/angular/commons/modules/companyinfo/companyInfo.html', replace: false, scope: { companyId : '=', top : '=', left : '=', arrow : '=', showAcquiredFollowButton : '=', showAcquiredUnfollowButton : '=', infocontrol : '=', hoveredCompany : '@', followAndAssign : '@', dynamic : '@', profileNewTab : '@', showDownArrow : '@', hideButtons : '@', showAddress : '@' }, link:function(scope, element, attrs) { scope.companyInfo = {}; scope.companyInfo.loading = true; scope.companyInfo.showInfo = false; scope.companyInfo.mouseHovered = false; scope.companyInfo.followCompanyButtons = true; scope.companyInfo.showFollowButton = true; scope.companyInfo.showUnfollowButton = false; scope.showCloseButton = false; scope.showAddresses = false; scope.address = ''; scope.infocontrol.getCompanyInfo = function(event, company, isFollowing) { scope.showCloseButton = false; scope.followAndAssign = false; if(company != null && company != undefined) { if (company.id != null && company.id != "") { scope.companyId = company.id; } else { scope.companyId = company.companyId; } } scope.companyInfo.loading = true; scope.companyInfo.mouseHovered = false; commonsAppServices.getCompanyInfo(scope.companyId).then(function(result) { scope.companyInfo = result.data; if(scope.showAddress && (scope.companyInfo.address != null && scope.companyInfo.address != undefined)){ if(scope.companyInfo.address.city != null && scope.companyInfo.address.city != undefined && scope.companyInfo.address.state != null && scope.companyInfo.address.state != undefined){ scope.showAddresses = true; scope.address = scope.companyInfo.address.city + ', ' + scope.companyInfo.address.state; } } scope.companyInfo.loading = false; scope.companyInfo.showInfo = true; if (!scope.hideButtons || scope.hideButtons == undefined) { scope.companyInfo.followCompanyButtons = true; scope.companyInfo.showUnfollowButton = false; scope.companyInfo.showFollowButton = false; if(isFollowing) { scope.companyInfo.showUnfollowButton = true; } else { scope.companyInfo.showFollowButton = true; } } }); if(scope.dynamic == "true") { var parentPos = document.getElementById('channelCompanyList').getBoundingClientRect(); var childrenPos = event.currentTarget.getBoundingClientRect(); var x = childrenPos.left - parentPos.left; var y = childrenPos.top - parentPos.top; if (x > parentPos.width / 2) { scope.arrow = "arrow_right"; } if (x < parentPos.width / 2) { scope.arrow = "arrow_left"; } var body = document.body; var docElem = document.documentElement; var scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop; var scrollLeft = window.pageXOffset || docElem.scrollLeft || body.scrollLeft; var clientTop = docElem.clientTop; var clientLeft = docElem.clientLeft; var popupWidth = 260; var popupHeight = 282; var top = y + scrollTop - clientTop - popupHeight/4 + 10; var left = x + scrollLeft - clientLeft; var right = x + scrollLeft - clientLeft + childrenPos.width; var mousex = event.pageX + 20; //Get X coodrinates var mousey = event.pageY + 20; //Get Y coordinates var popupVisX = mousex - parentPos.left; //Distance of element from the right edge of viewport var popupVisY = (mousey + popupHeight) - parentPos.height; //Distance of element from the bottom of viewport if (popupVisX < parentPos.width / 2) { //If tooltip exceeds the X coordinate of viewport scope.left = right + "px"; scope.top = top + "px"; } else { scope.left = left - popupWidth + "px"; scope.top = top + "px"; } } else { if(scope.showDownArrow) { var rawDom = event.currentTarget; var toolTipPopup = document.getElementById('companyInfo'); rawDom.appendChild(toolTipPopup); } else { var rawDom = event.currentTarget; var _x = 0; var _y = 0; var body = document.documentElement || document.body; var scrollX = window.pageXOffset || body.scrollLeft; var scrollY = window.pageYOffset || body.scrollTop; _x = rawDom.getBoundingClientRect().left + scrollX + 75; _y = rawDom.getBoundingClientRect().top + scrollY - 110; scope.left = _x + "px"; scope.top = _y + "px"; scope.arrow = "arrow_left"; } } scope.hoveredCompany = company; }; scope.infocontrol.getCompanyInfoForMobile = function(event,company,isFollowing){ scope.showCloseButton = true; scope.followAndAssign = false; if(company.id != null && company.id != "") { scope.companyId = company.id; } else { scope.companyId = company.companyId; } scope.companyInfo.loading = true; //scope.companyInfo.mouseHovered = false; commonsAppServices.getCompanyInfo(scope.companyId).then(function(result) { scope.companyInfo = result.data; scope.companyInfo.loading = false; scope.companyInfo.showInfo = true; scope.companyInfo.followCompanyButtons = true; scope.companyInfo.showUnfollowButton = false; scope.companyInfo.showFollowButton = false; if(isFollowing) { scope.companyInfo.showUnfollowButton = true; } else { scope.companyInfo.showFollowButton = true; } }); scope.left = "15%"; scope.top = "10%"; scope.hoveredCompany = company; } scope.infocontrol.getSuggCompanyInfo = function(event, company, isFollowing) { scope.followAndAssign = true; if(company.id != null && company.id != "") { scope.companyId = company.id; } else { scope.companyId = company.companyId; } scope.companyInfo.loading = true; scope.companyInfo.mouseHovered = false; commonsAppServices.getCompanyInfo(scope.companyId).then(function(result) { scope.companyInfo = result.data; scope.companyInfo.loading = false; scope.companyInfo.showInfo = true; scope.companyInfo.followCompanyButtons = true; scope.companyInfo.showFollowButton = true; scope.companyInfo.showUnfollowButton = false; scope.companyInfo.showFollowButton = false; if(isFollowing) { scope.companyInfo.showUnfollowButton = true; } else { scope.companyInfo.showFollowButton = true; } }); var rawDom = event.target; var _x = 0; var _y = 0; var body = document.documentElement || document.body; var scrollX = window.pageXOffset || body.scrollLeft; var scrollY = window.pageYOffset || body.scrollTop; _x = rawDom.getBoundingClientRect().left + scrollX - 250; _y = rawDom.getBoundingClientRect().top + scrollY - 110; scope.left = _x + "px"; scope.top = _y + "px"; scope.arrow = "arrow_right"; scope.hoveredCompany = company; }; scope.infocontrol.getAcquiredCompanyInfo = function(event, companyId, isFollowing) { scope.followAndAssign = false; scope.companyInfo.followCompanyButtons = false; scope.acquired_company_id = companyId; scope.companyInfo.loading = true; scope.companyInfo.mouseHovered = false; commonsAppServices.getCompanyInfo(companyId).then(function(result) { scope.companyInfo = result.data; scope.companyInfo.loading = false; scope.companyInfo.showInfo = true; scope.companyInfo.followCompanyButtons = false; scope.companyInfo.showFollowButton = true; scope.companyInfo.showUnfollowButton = false; scope.companyInfo.showFollowButton = false; if(isFollowing) { scope.companyInfo.showUnfollowButton = true; } else { scope.companyInfo.showFollowButton = true; } }); var rawDom = event.target; var _x = 0; var _y = 0; var body = document.documentElement || document.body; var scrollX = window.pageXOffset || body.scrollLeft; var scrollY = window.pageYOffset || body.scrollTop; _x = rawDom.getBoundingClientRect().left + scrollX + 75; _y = rawDom.getBoundingClientRect().top + scrollY - 110; scope.left = _x + "px"; scope.top = _y + "px"; scope.arrow = "arrow_left"; }; scope.infocontrol.hideCompanyInfo = function() { if(!scope.companyInfo.mouseHovered) { scope.companyInfo.showInfo = false; } else { scope.companyInfo.showInfo = true; } }; // bind to mouse over element.bind("mouseenter", function() { scope.companyInfo.mouseHovered = true; scope.companyInfo.showInfo = true; scope.$apply(); }); // and mouse out element.bind("mouseleave", function() { scope.companyInfo.showInfo = false; scope.$apply(); }); $document.bind('click', function(event){ if(event.target.classList != undefined && event.target.classList != null){ if(event.target.classList[0] == "channel-company-logo" || event.target.classList[1] == "mobile-click-track") return; } var isClickedElementChildOfPopup = element .find(event.target) .length > 0; if (isClickedElementChildOfPopup) return; scope.companyInfo.showInfo = false; scope.$apply(); }); scope.followCompany = function(companyId) { scope.companyInfo.loading = true; if(scope.followAndAssign === true) { $timeout(function() { scope.companyInfo.showInfo = false; // $(element).hide(); angular.element('#'+companyId).trigger('click'); scope.$apply(); }, 100); scope.companyInfo.loading = false; } else { commonsAppServices.followCompany(companyId, true).then(function(result) { sharedService.addCompany(scope.hoveredCompany); scope.companyInfo.loading = false; scope.companyInfo.showUnfollowButton = true; scope.companyInfo.showFollowButton = false; scope.companyInfo.showInfo = false; }); } }; scope.unFollowCompany = function(companyId) { scope.companyInfo.loading = true; commonsAppServices.followCompany(companyId, false).then(function(result) { sharedService.removeCompany(scope.hoveredCompany); scope.companyInfo.loading = false; scope.companyInfo.showUnfollowButton = false; scope.companyInfo.showFollowButton = true; scope.companyInfo.showInfo = false; }); }; scope.closeButton = function(){ scope.companyInfo.showInfo = false; } scope.followAcquiredCompany = function(companyId) { scope.companyInfo.loading = true; commonsAppServices.followCompany(companyId, true).then(function(result) { sharedService.addAcquiredCompany(companyId); scope.companyInfo.loading = false; scope.companyInfo.showUnfollowButton = true; scope.companyInfo.showFollowButton = false; scope.companyInfo.showInfo = false; }); }; scope.unFollowAcquiredCompany = function(companyId) { scope.companyInfo.loading = true; commonsAppServices.followCompany(companyId, false).then(function(result) { sharedService.removeAcquiredCompany(companyId); scope.companyInfo.loading = false; scope.companyInfo.showUnfollowButton = false; scope.companyInfo.showFollowButton = true; scope.companyInfo.showInfo = false; }); }; } }; }]);