/**
* Author : Larry Li
* Date : 2013-12-5
* Email : larry.li@aicent.com
*/
var Session = function() {
var defaults = {
title : 'Session Notification',
message : 'Your session is about to expire.',
keepAliveUrl : '/admin/session/keep-alive',
redirUrl : '/account/timed-out',
logoutUrl : '/account/logout',
warnAfter : 900000, // 15 minutes
redirAfter : 1200000 // 20 minutes
};
var o = defaults, dialogTimer, redirTimer;
var controlRedirTimer = function(action) {
switch(action) {
case 'start':
// Dialog has been shown, if no action taken during redir period, redirect
redirTimer = setTimeout(function(){
window.location = o.redirUrl;
}, o.redirAfter - o.warnAfter);
break;
case 'stop':
clearTimeout(redirTimer);
break;
case 'restart':
clearTimeout(redirTimer);
redirTimer = setTimeout(function(){
window.location = o.redirUrl;
}, o.redirAfter - o.warnAfter);
break;
}
};
var controlDialogTimer = function(action) {
switch(action) {
case 'start':
dialogTimer = setTimeout(function(){
$('#sessionTimeout-dialog').modal('show');
controlRedirTimer('start');
}, o.warnAfter);
break;
case 'stop':
clearTimeout(dialogTimer);
break;
case 'restart':
clearTimeout(dialogTimer);
dialogTimer = setTimeout(function(){
$('#sessionTimeout-dialog').modal('show');
controlRedirTimer('restart');
}, o.warnAfter);
break;
}
};
var doKeepAlive = function() {
$.ajax({
type: 'POST',
url: o.keepAliveUrl,
success: function() {
// Stop redirect timer and restart warning timer
controlRedirTimer('restart');
controlDialogTimer('restart');
}
});
};
return {
sessionTimeout: function(options) {
if ( options ) { o = $.extend( defaults, options ); }
var warning_dialog = '<div class="modal fade" id="sessionTimeout-dialog">'
+' <div class="modal-dialog modal-small">'
+' <div class="modal-content">'
+' <div class="modal-header">'
+' <button id="_close" type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>'
+' <h4 class="modal-title">'+ o.title +'</h4>'
+' </div>'
+' <div class="modal-body">'+ o.message +'</div>'
+' <div class="modal-footer">'
+' <button id="sessionTimeout-dialog-logout" type="button" class="btn btn-default">Logout</button>'
+' <button id="sessionTimeout-dialog-keepalive" type="button" class="btn btn-primary" data-dismiss="modal">Stay Connected</button>'
+' </div>'
+' </div>'
+' </div>'
+'</div>';
$('body').append(warning_dialog);
$('#sessionTimeout-dialog-logout').on('click', function () { window.location = o.logoutUrl; });
$('#_close,#sessionTimeout-dialog-keepalive').click(function() {
doKeepAlive();
});
// Begin warning period
controlDialogTimer('start');
},
sessionTimeoutKeepAlive: function() {
controlRedirTimer('restart');
controlDialogTimer('restart');
}
};
}();
$(function() {
Session.sessionTimeout({
title: 'Session Timeout Notification',
message: 'Your session is about to expire.',
keepAliveUrl: contextPath + '/admin/session/keep-alive',
redirUrl: contextPath + '/account/login',
logoutUrl: contextPath + '/account/logout',
warnAfter: 1500000,
redirAfter: 1780000
});
$(document).ajaxComplete(function() {
Session.sessionTimeoutKeepAlive();
});
});