(function(){ "use strict"; var MODAL_TIMEOUT_SECONDS = 60 * 2; // 2 minutes angular.module('shared') .factory('SessionTimeoutModal', [ 'LazModalService', function(LazModalService){ function show(){ return LazModalService .showModal({ templateUrl: '/shared/js/angular/auth/session-timeout-modal.html', controllerAs: '$ctrl', controller: 'SessionTimeoutController', hideCloseButton: 'true', overrideClass: 'js-none' }) .then(function(modal){ return modal.close; }); } return { getModalTimeoutSeconds: function() { return MODAL_TIMEOUT_SECONDS; }, show: show } }]) .controller('SessionTimeoutController', ['close', '$interval', '$scope', function(closeExternal, $interval, $scope){ var ctrl = this; ctrl.endTime = nowInSeconds() + MODAL_TIMEOUT_SECONDS; updateTimeRemaining(); var timeRemainingInterval = $interval(updateTimeRemaining, 200); $scope.$on("$destroy", stopTimeRemainingInterval); ctrl.accept = function(){ close('accept'); }; ctrl.reject = function(){ close('reject'); }; ctrl.close = ctrl.accept; function updateTimeRemaining(){ ctrl.timeRemaining = Math.max(ctrl.endTime - nowInSeconds(), 0); if(ctrl.timeRemaining === 0){ timeout(); } } function nowInSeconds(){ return Math.floor(Date.now() / 1000); } function timeout(){ close('timeout'); } function close(value){ stopTimeRemainingInterval(); closeExternal(value); } function stopTimeRemainingInterval(){ if(timeRemainingInterval){ $interval.cancel(timeRemainingInterval); } timeRemainingInterval = null; } }]); })();