"use strict"; (function () { 'use strict'; angular.module('myitsmApp') .controller('MainController', ["$scope", "$state", "$filter", "l10nModel", "authModel", "ccsModel", "userModel", "searchModel", "$window", "AUTH_EVENTS", "$timeout", "chatModel", "emailModel", "mobileClientCheckerService", "systemAlertService", "i18nService", "$rootScope", "$http", "$modal", "$cookies", function ($scope, $state, $filter, l10nModel, authModel, ccsModel, userModel, searchModel, $window, AUTH_EVENTS, $timeout, chatModel, emailModel, mobileClientCheckerService, systemAlertService, i18nService, $rootScope, $http, $modal, $cookies) { 'ngInject'; var APP_STATES = { INDETERMINATE: -1, SHOW_LOGIN: 0, SHOW_MAIN: 1, SHOW_WINDOW: 2 }, ANCHORS = { DashBoardWithHash: '#/', 'Ticket Console': 'ticket-console', 'Asset Console': 'asset-console', 'Knowledge Console': 'knowledge-console', 'Smart Recorder': 'create/smart-recorder' }; $scope.APP_STATES = APP_STATES; $scope.appState = APP_STATES.INDETERMINATE; $scope.state = $state; var unbind = $rootScope.$on('i18nResourcesUpdated', function () { mobileClientCheckerService.checkForMobileDevice(); }); $scope.$on('$destroy', function () { console.log('destroy root scope events bound from here'); unbind(); }); $(document).ready(function () { if (window.localStorage.tabCount) { window.localStorage.tabCount++; } else { window.localStorage.tabCount = 1; } }); $(window).bind('beforeunload', function (evt) { if (window.localStorage.tabCount) { window.localStorage.tabCount--; } if (window.localStorage.tabCount == 0 && window.localStorage.sessionActive == 'true') { var request = new XMLHttpRequest(); request.open('POST', '/smartit/rest/arlicense/timeout/register', false); request.send(); } }); /** * Public functions */ $scope.onLogoutClick = function () { $scope.appState = APP_STATES.INDETERMINATE; authModel.logout(); }; $scope.onAboutClick = function () { $modal.open({ templateUrl: 'views/about.html', windowClass: 'bmc-system-about-modal', size: 'lg' }); }; $scope.enableAccessibility = function () { userModel.isAccessibleUser = !userModel.isAccessibleUser; var expDate = new Date(); expDate.setDate(expDate.getDate() + 365); $cookies.put('accessibility', userModel.isAccessibleUser, { expires: expDate }); userModel.updateUserPreferences('interface', { id: userModel.isAccessibleUserPreferenceId, name: 'accessibility', value: userModel.isAccessibleUser }); }; $scope.setFocus = function (id) { $('#' + id).focus(); }; $scope.$on(AUTH_EVENTS.LOGIN_SUCCESS, function () { console.log('$scope.$on(AUTH_EVENTS.LOGIN_SUCCESS)'); if (ANCHORS.DashBoardWithHash === window.location.hash) { var ccsPropertiesPromise = ccsModel.getCCSParameters(); ccsPropertiesPromise.then(function (response) { $scope.ccsProperties = response; if ($scope.ccsProperties && $scope.ccsProperties.landingPage && ANCHORS[$scope.ccsProperties.landingPage]) { console.log('Landing page :: Diverting to === ' + ANCHORS[$scope.ccsProperties.landingPage]); window.location.hash = ANCHORS[$scope.ccsProperties.landingPage]; } window.location.reload(false); }); } else { window.location.reload(false); } }); $scope.$on(AUTH_EVENTS.LOGOUT_SUCCESS, function (event, eventData) { console.log('$scope.$on(AUTH_EVENTS.LOGOUT_SUCCESS)'); localStorage.removeItem('tabCount'); window.localStorage.sessionActive = false; if (authModel.isSSOEnabled()) { if (eventData && eventData.postLogoutUrl) { var url = eventData.postLogoutUrl; if (url.indexOf('?') > -1) { url += '&callback=JSON_CALLBACK'; } else { url += '?callback=JSON_CALLBACK'; } $http.jsonp(url) .finally(function () { $window.location.href = './?' + moment().valueOf(); }); } else if (eventData && eventData.redirectUrl) { $window.location.href = eventData.redirectUrl; } else { $window.location.href = './?' + moment().valueOf(); } } else { $window.location.href = './'; } }); $scope.$on(AUTH_EVENTS.SESSION_ACTIVE, function () { console.log('$scope.$on(AUTH_EVENTS.SESSION_ACTIVE)'); window.localStorage.sessionActive = true; if (window.localStorage.tabCount == 1) { authModel.deRegister(); } $rootScope.$emit('$stateChangeStart', $scope.state.current); loadApplicationData(); }); $scope.$on(AUTH_EVENTS.NOT_AUTHENTICATED, function () { console.log('$scope.$on(AUTH_EVENTS.NOT_AUTHENTICATED)'); $scope.appState = APP_STATES.SHOW_LOGIN; }); $scope.$on(AUTH_EVENTS.AR_CONNECTION_FAILED, function (event, response) { console.log('$scope.$on(AUTH_EVENTS.AR_CONNECTION_FAILED)'); systemAlertService.error({ text: response.data.error }); }); $scope.$on(AUTH_EVENTS.SESSION_EXPIRED, function () { console.log('$scope.$on(AUTH_EVENTS.SESSION_EXPIRED)'); window.localStorage.sessionActive = false; localStorage.removeItem('tabCount'); if (authModel.isSSOEnabled()) { $scope.appState = APP_STATES.INDETERMINATE; //For RSSO show session expired alert var modalInstance = systemAlertService.modal({ title: $filter('i18n')('error.sessionExpired.title'), text: $filter('i18n')('error.sessionExpired.message'), backdrop: 'static', buttons: [ { text: i18nService.getLocalizedString('common.button.signInAgain'), data: true } ] }); modalInstance.result.then(function (data) { if (data) { $window.location.reload(); } }); } else { $scope.appState = APP_STATES.SHOW_LOGIN; } }); /** * Private functions */ /** * Loads all data required for application */ function loadApplicationData() { $scope.appState = APP_STATES.INDETERMINATE; // show loading spinner userModel.getUserDataForCurrentSession(); l10nModel.loadSystemMessages(); init(); } /** * Entry point */ function init() { $scope.showMeridian = window.showMeridian; searchModel.init(); $scope.chatModel = chatModel; $scope.userModel = userModel; $scope.emailModel = { emailInstances: emailModel.emailInstances }; if ($scope.state.$current.data && $scope.state.$current.data.separateWindowMode) { $scope.appState = APP_STATES.SHOW_WINDOW; } else { $scope.appState = APP_STATES.SHOW_MAIN; var accessibilityCookie = $cookies.get('accessibility') ? JSON.parse($cookies.get('accessibility')) : false; userModel.getUserPreferences('interface').then(function (accessibleConfig) { if (_.isObject(accessibleConfig[0]) || accessibilityCookie) { userModel.isAccessibleUser = _.isObject(accessibleConfig[0]) ? accessibleConfig[0].value : false; userModel.isAccessibleUserPreferenceId = _.isObject(accessibleConfig[0]) ? accessibleConfig[0].id : null; if (userModel.isAccessibleUser !== accessibilityCookie) { $scope.enableAccessibility(); } } }); } $scope.$on('$viewContentLoaded', function () { if (!chatModel.connected) { chatModel.openChatConnection(); } }); } }]); })();