//ask requirejs to load dashboard scripts. requirejs([ 'dashboard/DashboardHome', 'dashboard/DashboardProfile', 'dashboard/DashboardFinancialDetails', 'dashboard/DashboardInsuredItems', 'dashboard/DashboardReunderwriting', 'dashboard/DashboardClaims', 'dashboard/DashboardServiceProviders', 'dashboard/DashboardVehicleInspection', 'dashboard/DashboardSavedQuotes', 'dashboard/DashboardNotifications', 'dashboard/DashboardConfirmDetails', 'dashboard/DashboardDocuments', 'dashboard/DashboardHouseholdMembers', 'dashboard/DashboardApp' ]); define(['angular', 'lib/json-formatter', 'utilities', 'validators'], function (require) { return angular.module('dashboard', ['utilities', 'validators', 'jsonFormatter', 'ui.router']) .run(['$anchorScroll', function ($anchorScroll) { $anchorScroll.yOffset = function () { var header = document.getElementsByTagName('header'); return header[0].offsetHeight + 15; }; }]) .factory('dashboardRedirectInterceptor', ['$q', '$window', function ($q, $window) { var dashboardRedirectInterceptor = { 'request': function (config) { //Attach a header to requests so the server is aware when requests are coming from within the dashboard config.headers['DashboardRequest'] = 'true'; return config; }, 'responseError': function (rejection) { /* If the request gets rejected Much like my own :( Reload the page. This will prevent the login page from loading inside the ui-view */ if (rejection.status == 403) { if (typeof IsBroker !== 'undefined' && IsBroker !== null && IsBroker == true) { window.location = '/brokers/dashboard'; } else { window.location = '/login'; } } else if (rejection.status == 500) { window.location = '/500'; } return $q.reject(rejection); } }; return dashboardRedirectInterceptor; }]) .config(['$stateProvider', '$urlRouterProvider', '$httpProvider', function ($stateProvider, $urlRouterProvider, $httpProvider) { var isDashboard = ('' + window.location.pathname).match(/[dD]ashboard/); if (isDashboard) { var policyMatch = ('' + window.location).match(/\/(\d+|guest)/); var originalPolicy = policyMatch && policyMatch[1]; var isVehicleInspection = ('' + window.location.pathname).match(/vehicleinspection/); var isReferred = ('' + window.location.pathname).match(/referred-to-agent/); if (typeof originalPolicy !== 'undefined' && originalPolicy !== null && isVehicleInspection === null && isReferred === null) { $stateProvider .state('dashboard', { url: '', abstract: true, template: '' }); if (typeof VisibleStates !== 'undefined' && VisibleStates !== null && VisibleStates !== '') { for (i = 0; i < VisibleStates.length; i++) { var url = '/' + VisibleStates[i].Name; if (typeof (VisibleStates[i].Parameters) !== 'undefined' && VisibleStates[i].Parameters !== null) { for (var x = 0; x < VisibleStates[i].Parameters.length; x++) { url += '/:' + VisibleStates[i].Parameters[x]; } } if (VisibleStates[i].Parent === null && VisibleStates[i].View === null) { var stateDetails = { parent: 'dashboard', url: url, abstract: true, template: '' }; $stateProvider.state('dashboard.' + VisibleStates[i].Name, stateDetails); } else if (VisibleStates[i].Parent === null && VisibleStates[i].View !== null) { var stateDetails = { parent: 'dashboard', url: url, templateUrl: '/Dashboard/' + originalPolicy + '/' + VisibleStates[i].View }; $stateProvider.state('dashboard.' + VisibleStates[i].Name, stateDetails); } else if (VisibleStates[i].Parent !== null && VisibleStates[i].View !== null) { var stateDetails = { parent: 'dashboard.' + VisibleStates[i].Parent, url: url, templateUrl: '/Dashboard/' + originalPolicy + '/' + VisibleStates[i].View }; $stateProvider.state('dashboard.' + VisibleStates[i].Parent + '.' + VisibleStates[i].Name, stateDetails); } } } $urlRouterProvider.otherwise('/home'); $httpProvider.interceptors.push('dashboardRedirectInterceptor'); } } }]) .controller('Dashboard', ['$scope', '$http', '$q', '$rootScope', '$state', '$window', 'Helpers', '$mdDialog', function ($scope, $http, $q, $rootScope, $state, $window, Helpers, $mdDialog) { $scope.goToState = function (state, stateParams) { if (typeof state !== 'undefined' && state !== null) { $state.go(state, stateParams); $scope.Dashboard.MenuOpen = false; } }; $scope.goToAddInsured = function () { $scope.goToState('dashboard.insureditems', { scrollToAddCard: true }); } $scope.goBack = function () { $window.history.back(); }; $scope.DashboardPost = function (url, data, paramObj) { if ($scope.Dashboard.Policy.ReferenceNumber) { url = "/dashboard/" + encodeURIComponent($scope.Dashboard.Policy.ReferenceNumber.match(/^[^\|]+/)[0]) + '/' + url; } else if ($scope.Dashboard.IsGuest) { url = "/dashboard/guest/" + url; } else { url = "/dashboard/" + url; } //Goes through and adds additional parameters to the url if (paramObj) { url += "?"; var count = 0; for (var x in paramObj) { if (count++ > 0) url += "&"; url += encodeURIComponent(x) + "=" + encodeURIComponent(paramObj[x]) } } //do the http call return $http({ method: 'POST', url: url, data: data }); }; $scope.DashboardGetActionUrl = function (uri, paramObj) { var url = ''; if ($scope.Dashboard.Policy.ReferenceNumber) { url = "/dashboard/" + encodeURIComponent($scope.Dashboard.Policy.ReferenceNumber.match(/^[^\|]+/)[0]) + '/' + uri; } else if ($scope.Dashboard.IsGuest) { url = "/dashboard/guest/" + url; } else { url = "/dashboard/" + uri; } //Goes through and adds additional parameters to the url if (paramObj) { url += "?"; var count = 0; for (var x in paramObj) { if (count++ > 0) { url += "&"; } url += encodeURIComponent(x) + "=" + encodeURIComponent(paramObj[x]) } } return url; } //this method will initialize the quote state scope variable in its own special way $scope.InitializePolicy = function (policy) { if (!policy) policy = {}; prop($scope.Dashboard, 'Policy', function (a, b, c, d, e) { return policy; }, function (newValue) { throw 'Dashboard policy may only be initialized by InitializePolicy'; }); } var getChildStates = function (state) { var children = []; for (var j = 0; j < VisibleStates.length; j++) { if (VisibleStates[j].Parent === state) { var menuItem = { ToState: 'dashboard.' + state + '.' + VisibleStates[j].Name }; children.push(menuItem); } } return children; } var filterMenuItems = function () { var menuItems = []; for (var i = 0; i < VisibleStates.length; i++) { if (VisibleStates[i].Parent === null && VisibleStates[i].Icon !== null) { menuItem = null; if (VisibleStates[i].View === null) { menuItem = { Children: getChildStates(VisibleStates[i].Name) }; menuItem.ToState = menuItem.Children[0].ToState; menuItem.ParentState = 'dashboard.' + VisibleStates[i].Name; } else { menuItem = { Children: null, ToState: 'dashboard.' + VisibleStates[i].Name }; } menuItem.Icon = VisibleStates[i].Icon; menuItem.Description = VisibleStates[i].Description; menuItems.push(menuItem); } } return menuItems; }; $scope.StateIsActive = function (state) { var stateName = state.ToState; if (state.Children !== null && state.Children.length > 0) { stateName = state.ParentState } return $scope.Dashboard.CurrentState === stateName; }; $scope.StateIsLoading = function (state) { var stateName = state.ToState; if (state.Children !== null && state.Children.length > 0) { stateName = state.ParentState } return $scope.Dashboard.LoadingState === stateName; }; $scope.Dashboard = { CurrentState: 'dashboard.home', VisibleStates: filterMenuItems(), ActiveVdn: ActiveVdn, AmendmentSuccessMessage: AmendmentSuccessMessage }; $scope.InitializePolicy(GetPolicy()); //Populate IsGuest if (typeof IsGuest !== 'undefined' && IsGuest !== null && IsGuest == true) { $scope.Dashboard.IsGuest = true; } else { $scope.Dashboard.IsGuest = false; } var policyMatch = ('' + window.location).match(/\/(\d+)/); var originalPolicy = policyMatch && policyMatch[1]; var dayRange = []; //Days in a month for (var i = 1; i <= 31; i++) { dayRange.push(i); } $scope.dayRange = dayRange; $scope.isInDayRange = function (value) { var iValue = 0; if (typeof value === 'number') { iValue = value; } else if (typeof value === 'string') { iValue = parseInt(value); } return ($scope.dayRange.indexOf(iValue) != -1); } $scope.Nav = { CurrentPolicy: originalPolicy, ChangePolicy: function ChangePolicy(policy) { if (originalPolicy) { $scope.Dashboard.LoadingState = true; window.location = ('' + window.location).replace(originalPolicy, policy); } else { $scope.Dashboard.LoadingState = true; window.location = '/dashboard/' + policy; } } }; $scope.getVehicleCarCount = function () { var vehicles = $scope.Dashboard.Policy.RiskItemDetails.Vehicles; var count = 0; for (i = 0; i < vehicles.length; i++) if (vehicles[i].VehicleType === 'Car') count++; return count; }; $scope.hasHomeContents = function () { return typeof $scope.Dashboard.Policy.RiskItemDetails.Home !== 'undefined' && $scope.Dashboard.Policy.RiskItemDetails.Home !== null && typeof $scope.Dashboard.Policy.RiskItemDetails.Home.Description !== 'undefined' && $scope.Dashboard.Policy.RiskItemDetails.Home.Description !== null && $scope.Dashboard.Policy.RiskItemDetails.Home.Description !== ''; }; $scope.hasBuilding = function () { return typeof $scope.Dashboard.Policy.RiskItemDetails.Building !== 'undefined' != null && $scope.Dashboard.Policy.RiskItemDetails.Building !== null && typeof $scope.Dashboard.Policy.RiskItemDetails.Building.Description !== 'undefined' && $scope.Dashboard.Policy.RiskItemDetails.Building.Description !== null && $scope.Dashboard.Policy.RiskItemDetails.Building.Description !== ''; }; $scope.hasRegularDrivers = function () { return typeof $scope.Dashboard.Policy.RegularDrivers !== 'undefined' && $scope.Dashboard.Policy.RegularDrivers !== null && $scope.Dashboard.Policy.RegularDrivers.length > 0 }; $scope.hasHouseholdMembers = function () { return typeof $scope.Dashboard.Policy.HouseholdMembers !== 'undefined' && $scope.Dashboard.Policy.HouseholdMembers !== null && $scope.Dashboard.Policy.HouseholdMembers.length > 0 }; $scope.hasFirstName = function () { return typeof $scope.Dashboard.Policy.AccountDetails.PolicyHolder.FirstName !== 'undefined' && $scope.Dashboard.Policy.AccountDetails.PolicyHolder.FirstName !== null && $scope.Dashboard.Policy.AccountDetails.PolicyHolder.FirstName !== '' }; $scope.hasRiskAddress = function () { return typeof $scope.Dashboard.Policy.AccountDetails.ResidentialAddress !== 'undefined' && $scope.Dashboard.Policy.AccountDetails.ResidentialAddress !== null && $scope.Dashboard.Policy.AccountDetails.ResidentialAddress.Address1 !== '' }; $scope.cashbackMessage = function () { //function to calculate the months and days until cashback, not working correctly, just using the cashback time from deepvision, as used in telematics //var diff = $scope.Dashboard.Policy.FinancialDetails.PaymentDetails.CashbackBonusDaysRemaining; // The string we're working with to create the representation //var str = ''; //// Map lengths of `diff` to different time periods //var values = [[' month', 30], [' day', 1]]; //// Iterate over the values... //for (var i = 0; i < values.length; i++) { // var amount = Math.floor(diff / values[i][1]); // // ... and find the largest time value that fits into the diff // if (amount >= 1) { // // If we match, add to the string ('s' is for pluralization) // str += amount + values[i][0] + (amount > 1 ? 's' : '') + ' '; // // and subtract from the diff // diff -= amount * values[i][1]; // } //} //return str; return timeLeft = $scope.Dashboard.Policy.FinancialDetails.PaymentDetails.CashbackBonusDescription; }; $scope.Documents = null; $scope.InitDocuments = function () { //GET the documents from the controller,bypass viewmodel $scope.DashboardPost("DashboardDocumentsRequest/ProcessGetIndex/") .then(function (success) { $scope.Documents = { GeneralMessages: success.data.GeneralMessages, AdhocLetters: success.data.AdHocLetters, AdhocProcess: { Steps: $scope.InitDocumentSteps(), ForwardTo: '', TaxLetter: { CoverItems: [] } }, Documents: success.data.Documents, PolicyStartDate: success.data.PolicyStartDate, ActivePolicy: success.data.ActivePolicy, FinanceCompanies: [], MortgageCompanies: [], AvailableDrivers: [], AddressList: [], TaxCoverItems: [], SelectedCoverItems: [], SelectedRegisteredAddress: {}, Today: new Date().getDateOnly(), DateArrivalMin: new Date().getDateOnly(), DateArrivalMax: null, CADateValid: true, ActiveDriver: null, RiskItemsRequiringInspection: [] }; }, function (failure) { console.log(failure) }); }; $scope.InitDocumentSteps = function () { return { ChooseLetter: true, ChooseRiskItem: false, CaptureData: false, ForwardLetter: false, Confirmation: false, TaxLetter: false, }; } $scope.BrokerPrefix = ""; if (window.location.href.indexOf("brokers") > 0) $scope.BrokerPrefix = "/brokers"; //AMENDFUNCTION BEGION $scope.getVehicleAmendEditUrl = function (riskItem) { var url = $scope.BrokerPrefix+'/quote/?quoteType=amendment&amendmentType=' + $scope.getAmendType(riskItem) + '&amendmentMode=edit&referenceNumber=' + $scope.Dashboard.Policy.PolicyNumber + '&sequenceNumber=' + riskItem.SequenceNumber return url; } $scope.getPortableAmendUrl = function (portableItem) { var url = $scope.BrokerPrefix +'/quote/?quoteType=amendment&amendmentType=' + $scope.getPortableAmendType(portableItem) + '&amendmentMode=edit&referenceNumber=' + $scope.Dashboard.Policy.PolicyNumber; return url; } $scope.BoostCar = function (APIVehicle) { APIVehicle.BoostAmendLoading = false; if (!APIVehicle.PaybackBoosterIncluded) { APIVehicle.BoostAmendLoading = true; window.location = $scope.BrokerPrefix +'/quote/?quoteType=amendment&amendmentType=car&amendmentMode=edit&sequenceNumber=' + APIVehicle.VehicleSequenceNumber + '&referenceNumber=' + APIVehicle.PolicyNumber + '&addBooster=true'; } } $scope.getAmendType = function (riskItem) { var amendType = 'car'; // default to car switch (riskItem.VehicleType) { case 'Car': amendType = 'car'; break; case 'Motorcycle': amendType = 'motorcycle'; break; case 'Trailer': amendType = 'trailer'; break; case 'Caravan': amendType = 'caravan'; break; case 'GolfCart': amendType = 'golfcart'; break; case 'Catamaran': case 'Dingy': case 'JetSki': case 'MotorBoat': case 'RubberDuck': case 'Yacht': amendType = 'watercraft'; break; } return amendType; } $scope.getPortableAmendType = function (portableItem) { var amendType = ''; if (portableItem.RiskType == "SB" || portableItem.RiskType == "SP") { if ($scope.hasHomeContents()) { amendType = 'home'; } else { amendType = 'building'; } } else { amendType = 'portable'; } return amendType; } //AMENDFUNCTION END $rootScope.$on('$stateChangeError', function (event, toState, toParams, fromState, fromParams, error) { console.error(error); }); $rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams, error) { $scope.Dashboard.CurrentState = (toState.parent !== 'dashboard') ? toState.parent : toState.name; $scope.Dashboard.LoadingState = null; window.scroll(0, 0); if (toState.parent == 'dashboard.documents' && $scope.Documents == null) { $scope.InitDocuments(); } }); $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams, error) { var reUnderwriteState = 'dashboard.reunderwriting'; //Handle HasReUnderwritingPolicy restrictions if (typeof HasReUnderwritingPolicy !== 'undefined' && HasReUnderwritingPolicy !== null && HasReUnderwritingPolicy == true && toState.name != reUnderwriteState) { //event.preventDefault(); //$scope.Dashboard.LoadingState = reUnderwriteState; //$scope.goToState('dashboard.reunderwriting'); $scope.HasReUnderwritingPolicy = true; $scope.Dashboard.Policy.UnderwritingRequired = true; return; } else if (typeof HasReUnderwritingPolicy !== 'undefined' && HasReUnderwritingPolicy !== null && HasReUnderwritingPolicy == false && toState.name == reUnderwriteState) { event.preventDefault(); $scope.goToState('dashboard.home'); return; } $scope.Dashboard.LoadingState = toState.name; $scope.Dashboard.LoadingState = (toState.parent !== 'dashboard') ? toState.parent : toState.name; if (typeof RedirectState !== 'undefined' && RedirectState !== null && RedirectState !== '') { event.preventDefault(); var state = 'dashboard.'; if (RedirectState.indexOf('claims') >= 0) { RedirectState = RedirectState.replace('claims', ''); state += 'claims.'; } state += RedirectState; RedirectState = null; $scope.goToState(state); return; } }); $scope.submitClaim = function (riskItem) { var selectedItemSeqNumber = riskItem.VehicleSequenceNumber; var itemType = riskItem.VehicleRiskType; if (!selectedItemSeqNumber) selectedItemSeqNumber = riskItem.SequenceNumber; if (!itemType) itemType = riskItem.RiskType; $scope.goToState('dashboard.claims.submit', { selectedItem: selectedItemSeqNumber, itemType: itemType }); }; }]); });