2022-08-08 18:24:50 +08:00
|
|
|
/* globals I18n */
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
2022-11-24 03:08:26 +08:00
|
|
|
var expireOn;
|
2022-08-08 18:24:50 +08:00
|
|
|
var timeoutID;
|
2022-11-24 03:08:26 +08:00
|
|
|
const oneSecondTimeout = 1000;
|
|
|
|
const expireLimit = 900000; // 15min in miliseconds
|
2022-08-08 18:24:50 +08:00
|
|
|
|
2022-11-24 03:08:26 +08:00
|
|
|
function padTimeStr(i) {
|
2022-08-08 18:24:50 +08:00
|
|
|
var s = ('0' + Math.floor(i));
|
|
|
|
return s.substring(s.length - 2);
|
2022-11-24 03:08:26 +08:00
|
|
|
}
|
2022-08-08 18:24:50 +08:00
|
|
|
|
2022-11-24 03:08:26 +08:00
|
|
|
function newTimerStr(expirationTime) {
|
2022-08-08 18:24:50 +08:00
|
|
|
var m = (expirationTime / 60) % 60;
|
|
|
|
var s = (expirationTime % 60);
|
2022-11-24 03:08:26 +08:00
|
|
|
return [m, s].map(padTimeStr).join(':');
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCurrentTime() {
|
|
|
|
var dateNow = new Date();
|
|
|
|
return dateNow.getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
function sessionExpireIn() {
|
|
|
|
expireOn = window.localStorage.getItem('sessionEnd');
|
|
|
|
return expireOn - getCurrentTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
function setSessionTimeout(functionCallback, timeoutTime) {
|
|
|
|
if (timeoutID) {
|
|
|
|
clearTimeout(timeoutID);
|
|
|
|
}
|
|
|
|
timeoutID = setTimeout(functionCallback, timeoutTime);
|
|
|
|
}
|
2022-08-08 18:24:50 +08:00
|
|
|
|
2022-11-24 03:08:26 +08:00
|
|
|
function toogleDocumentTitle(timeString = null) {
|
|
|
|
var sleepEmoticon = String.fromCodePoint(0x1F62A);
|
|
|
|
var originalTitle = document.title.split(sleepEmoticon).pop().trim();
|
|
|
|
|
|
|
|
if (timeString) {
|
|
|
|
document.title = timeString + ' ' + sleepEmoticon + ' ' + originalTitle;
|
|
|
|
} else {
|
|
|
|
document.title = originalTitle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function initializeSessionCountdown() {
|
|
|
|
var expirationUrl = $('meta[name=\'expiration-url\']').attr('content');
|
|
|
|
var expireIn;
|
2022-08-08 18:24:50 +08:00
|
|
|
if (expirationUrl) {
|
|
|
|
$.get(expirationUrl, function(data) {
|
2022-11-24 03:08:26 +08:00
|
|
|
expireOn = Math.max(window.localStorage.getItem('sessionEnd'), getCurrentTime() + parseInt(data, 10));
|
|
|
|
window.localStorage.setItem('sessionEnd', expireOn);
|
|
|
|
expireIn = sessionExpireIn();
|
|
|
|
|
|
|
|
if (expireIn <= 0) {
|
2022-08-08 18:24:50 +08:00
|
|
|
$('#session-finished').modal();
|
2022-11-24 03:08:26 +08:00
|
|
|
} else if (expireIn <= expireLimit + oneSecondTimeout) {
|
2022-08-08 18:24:50 +08:00
|
|
|
// eslint-disable-next-line no-use-before-define
|
2022-11-24 03:08:26 +08:00
|
|
|
setSessionTimeout(sessionCountdown, oneSecondTimeout);
|
2022-08-08 18:24:50 +08:00
|
|
|
} else {
|
2022-11-24 03:08:26 +08:00
|
|
|
setSessionTimeout(initializeSessionCountdown, (expireIn - expireLimit));
|
2022-08-08 18:24:50 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-24 03:08:26 +08:00
|
|
|
function reviveSession() {
|
|
|
|
$.post($('meta[name=\'revive-url\']').attr('content'));
|
|
|
|
toogleDocumentTitle();
|
|
|
|
window.localStorage.removeItem('sessionEnd');
|
|
|
|
setSessionTimeout(initializeSessionCountdown, oneSecondTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
function initializeSessionReviveCallbacks() {
|
|
|
|
$('#session-expire').modal().off('hide.bs.modal').on('hide.bs.modal', function() {
|
|
|
|
if (sessionExpireIn() > 0) {
|
|
|
|
reviveSession();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// for manual page reload
|
|
|
|
$(window).off('beforeunload').on('beforeunload', function() {
|
|
|
|
if (sessionExpireIn() > 0) {
|
|
|
|
reviveSession();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function sessionCountdown() {
|
2022-09-16 16:39:09 +08:00
|
|
|
var timeString;
|
2022-11-24 03:08:26 +08:00
|
|
|
var expireIn = sessionExpireIn();
|
|
|
|
|
|
|
|
if (!expireOn) {
|
|
|
|
initializeSessionCountdown();
|
|
|
|
} else if (expireIn > 0 && expireIn <= expireLimit) {
|
|
|
|
timeString = newTimerStr(expireIn / 1000);
|
|
|
|
toogleDocumentTitle(timeString);
|
2022-09-16 16:41:51 +08:00
|
|
|
$('.expiring').text(I18n.t('devise.sessions.expire_modal.session_end_in.header', { time: timeString }));
|
2022-11-24 03:08:26 +08:00
|
|
|
|
2022-08-08 18:24:50 +08:00
|
|
|
if (!$('#session-expire').hasClass('in')) {
|
2022-11-24 03:08:26 +08:00
|
|
|
initializeSessionReviveCallbacks();
|
2022-08-08 18:24:50 +08:00
|
|
|
}
|
2022-11-24 03:08:26 +08:00
|
|
|
|
|
|
|
setSessionTimeout(sessionCountdown, oneSecondTimeout);
|
|
|
|
} else if (expireIn <= 0) {
|
|
|
|
toogleDocumentTitle();
|
2022-08-08 18:24:50 +08:00
|
|
|
$('#session-expire').modal('hide');
|
|
|
|
$('#session-finished').modal();
|
|
|
|
}
|
|
|
|
}
|
2022-11-24 03:08:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
window.localStorage.removeItem('sessionEnd');
|
|
|
|
setSessionTimeout(initializeSessionCountdown, oneSecondTimeout);
|
2022-08-08 18:24:50 +08:00
|
|
|
|
|
|
|
$(document).on('click', '.session-login', function() {
|
|
|
|
window.location.reload();
|
|
|
|
});
|
2022-11-24 03:08:26 +08:00
|
|
|
|
|
|
|
$(window).on('storage', (event) => {
|
|
|
|
if (event.originalEvent.storageArea !== localStorage) return;
|
|
|
|
if (event.originalEvent.key === 'sessionEnd') {
|
|
|
|
if (!expireOn && event.originalEvent.newValue) {
|
|
|
|
if ($('#session-finished').hasClass('in')) {
|
|
|
|
$('#session-finished').modal('hide');
|
|
|
|
}
|
|
|
|
setSessionTimeout(initializeSessionCountdown, oneSecondTimeout);
|
|
|
|
} else if (expireOn && !event.originalEvent.newValue) {
|
|
|
|
toogleDocumentTitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
expireOn = event.originalEvent.newValue;
|
|
|
|
$('#session-expire').modal().off('hide.bs.modal').modal('hide');
|
|
|
|
}
|
|
|
|
});
|
2022-08-08 18:24:50 +08:00
|
|
|
}());
|