var app = angular.module("questionnaire", ['ngResource', 'ngAnimate', 'ui.router', 'ui.sortable', 'ui.bootstrap']); app.provider('sessionManager', function () { var timeoutTime; return { setTimeoutSession: function (value) { // in minutes timeoutTime = value * 1000 * 60; }, $get: function ($rootScope) { var timer, beforeSessionTimeout = timeoutTime * 0.95, leftToTimeout = (timeoutTime - beforeSessionTimeout) * 0.9; // it gets -10% buffer return { startCountdown: function () { valideBeforeStart(); if (timer) { clearTimeout(timer); } // Ocuurs 5% before the session ends. timer = setTimeout(function () { $rootScope.$broadcast('SESSION_BEFORE_TIMEOUT', { leftToTimeout: leftToTimeout }); }, beforeSessionTimeout); } }; } }; function valideBeforeStart() { if (!timeoutTime) { throw new Error('Timeout is not defined in sessionManager.'); } } }); app.config(function ($stateProvider, $urlRouterProvider, sessionManagerProvider) { sessionManagerProvider.setTimeoutSession(30); $stateProvider .state('tabs', { url: '/tabs/:tabOrder', templateUrl: 'app/Views/tabs.html', resolve: { questionnaire: function(QuestionnaireInfo) { return QuestionnaireInfo.get().$promise; }, tab: function($stateParams, TabService) { return TabService.get({ id: $stateParams.tabOrder }).$promise; } }, controller: 'tabsController' }) .state('tabs.mitham', { url: '/mitham/:mithamId', templateUrl: 'app/Views/mitham.html', controller: 'mithamGroupController' }) .state('tabs.previousTab', { url: '/previousTab', onEnter: function($modal) { $modal.open({ templateUrl: 'app/templates/confirmationModal.html', controller: 'confirmationModalController', keyboard: false, backdrop: 'static' }); } }) .state('tabs.previousTabDenied', { url: '/previousTabDenied', onEnter: function($modal) { $modal.open({ templateUrl: 'app/templates/previousTabDeniedModal.html', controller: 'confirmationModalController' }); } }) .state('tabs.mitham.previousTabDenied', { url: '/previousTabDenied', onEnter: function($modal) { $modal.open({ templateUrl: 'app/templates/previousTabDeniedModal.html', controller: 'confirmationModalController' }); } }) .state('error', { url: '/error/:statusCode', templateUrl: 'app/Views/error.html', controller: 'errorController' }) .state('IENotSupported', { url: '/IENotSupported', templateUrl: 'app/Views/IENotSupported.html', controller: function($scope, GIYUS_SITE) { $scope.url = { home: GIYUS_SITE }; } }); $urlRouterProvider.when('/tabs/2', '/tabs/2/mitham/'); }); app.run(function ($rootScope, $state, $window, QuestionnaireInfo, professionDescriptionService, GIYUS_SITE) { $rootScope.initialLoading = true; function goToCurrentTab() { if (isValidBrowser()) { return QuestionnaireInfo.get().$promise.then(function (response) { return $state.go('tabs', { tabOrder: response.CurrentTabOrder }); }); } else { return $state.go('IENotSupported'); } } // 'finally' is a reserved word in IE8. goToCurrentTab().then(function () { $rootScope.initialLoading = false; }, function (response) { $rootScope.initialLoading = false; redirectByStatusCode(response); }); professionDescriptionService.get(); $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) { redirectByStatusCode(error); }); function redirectByStatusCode(error) { if (error.status === 401) { $window.location.href = GIYUS_SITE + 'Login.aspx?moduleToGoTo=17'; } else if (error.status === 404) { $window.location.href = GIYUS_SITE; } else if (error.status === 403) { goToCurrentTab(); } else { $state.go('error', { exceptionId: error.data.ExceptionId }); } } function isValidBrowser() { if (navigator.appName == 'Microsoft Internet Explorer') { return !(navigator.userAgent.contains('MSIE 8.0') || navigator.userAgent.contains('MSIE 7.0') || navigator.userAgent.contains('MSIE 6.0')); } return true; } }); app.run(function (authenticationService, $rootScope, sessionManager, $modal, $window, GIYUS_SITE, $q) { $rootScope.$on('SESSION_BEFORE_TIMEOUT', function (event, data) { var dialog = $modal.open({ templateUrl: 'app/templates/sessionTimeoutModal.html', keyboard: false, backdrop: 'static', controller: function ($modalInstance, $scope) { $scope.leftToTimeout = convertToViewTime(data.leftToTimeout); var interval = setInterval(function () { data.leftToTimeout -= 1000; if (data.leftToTimeout >= 0) { $scope.leftToTimeout = convertToViewTime(data.leftToTimeout); $scope.$digest(); } else { // rejecting. $scope.go(false); } }, 1000); $scope.go = function(isStay) { if (isStay) { $scope.$close(); } else { $scope.$dismiss(); } clearInterval(interval); }; function convertToViewTime(milliseconds) { var minutes = Math.floor(milliseconds / 1000 / 60), seconds = Math.floor(milliseconds / 1000 % 60); seconds = seconds.toString().length == 1 ? '0' + seconds : seconds; return minutes + ':' + seconds; } } }); dialog.result.then(function () { return authenticationService.ping().then(function () { sessionManager.startCountdown(); }, function () { // The user is already disconnced for any reason / internal server error. return $q.reject(); }); }, function () { return authenticationService.disconnect().then(function () { return $q.reject(); }); }).then(null, function () { $window.location = GIYUS_SITE; }); }); }); app.constant('AUTH_SERVICE', 'https://www.aka.idf.il/main/giyus/AuthenticationService.asmx'); app.constant('GIYUS_SITE', 'https://www.aka.idf.il/main/giyus/');