Skip to content
This repository was archived by the owner on Apr 21, 2019. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
318 changes: 174 additions & 144 deletions js/timeout-dialog.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
* timeout-dialog.js v1.0.1, 01-03-2012
* timeout-dialog.js v1.0.2, 01-03-2012
*
* @author: Rodrigo Neri (@rigoneri)
* @contributor: Jason Ogaard (@jasonogaard)
*
* (The MIT License)
*
Expand All @@ -24,157 +25,186 @@
* THE SOFTWARE.
*/


/* String formatting, you might want to remove this if you already use it.
* Example:
*
* var location = 'World';
* alert('Hello {0}'.format(location));
*/
String.prototype.format = function() {
var s = this,
i = arguments.length;
var s = this, i = arguments.length;

while (i--) {
s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
}
return s;
while (i--) {
s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
}
return s;
};

!function($) {
$.timeoutDialog = function(options) {

var settings = {
timeout: 1200,
countdown: 60,
title : 'Your session is about to expire!',
message : 'You will be logged out in {0} seconds.',
question: 'Do you want to stay signed in?',
keep_alive_button_text: 'Yes, Keep me signed in',
sign_out_button_text: 'No, Sign me out',
keep_alive_url: '/keep-alive',
logout_url: null,
logout_redirect_url: '/',
restart_on_yes: true,
dialog_width: 350
}

$.extend(settings, options);

var TimeoutDialog = {
init: function () {
this.setupDialogTimer();
},

setupDialogTimer: function() {
var self = this;
window.setTimeout(function() {
self.setupDialog();
}, (settings.timeout - settings.countdown) * 1000);
},

setupDialog: function() {
var self = this;
self.destroyDialog();

$('<div id="timeout-dialog">' +
'<p id="timeout-message">' + settings.message.format('<span id="timeout-countdown">' + settings.countdown + '</span>') + '</p>' +
'<p id="timeout-question">' + settings.question + '</p>' +
'</div>')
.appendTo('body')
.dialog({
modal: true,
width: settings.dialog_width,
minHeight: 'auto',
zIndex: 10000,
closeOnEscape: false,
draggable: false,
resizable: false,
dialogClass: 'timeout-dialog',
title: settings.title,
buttons : {
'keep-alive-button' : {
text: settings.keep_alive_button_text,
id: "timeout-keep-signin-btn",
click: function() {
self.keepAlive();
}
},
'sign-out-button' : {
text: settings.sign_out_button_text,
id: "timeout-sign-out-button",
click: function() {
self.signOut(true);
}
}
}
});

self.startCountdown();
},

destroyDialog: function() {
if ($("#timeout-dialog").length) {
$(this).dialog("close");
$('#timeout-dialog').remove();
}
},

startCountdown: function() {
var self = this,
counter = settings.countdown;

this.countdown = window.setInterval(function() {
counter -= 1;
$("#timeout-countdown").html(counter);

if (counter <= 0) {
window.clearInterval(self.countdown);
self.signOut(false);
}

}, 1000);
},

keepAlive: function() {
var self = this;
this.destroyDialog();
window.clearInterval(this.countdown);

$.get(settings.keep_alive_url, function(data) {
if (data == "OK") {
if (settings.restart_on_yes) {
self.setupDialogTimer();
}
}
else {
self.signOut(false);
}
});
},

signOut: function(is_forced) {
var self = this;
this.destroyDialog();

if (settings.logout_url != null) {
$.post(settings.logout_url, function(data){
self.redirectLogout(is_forced);
});
}
else {
self.redirectLogout(is_forced);
}
},

redirectLogout: function(is_forced){
var target = settings.logout_redirect_url + '?next=' + encodeURIComponent(window.location.pathname + window.location.search);
if (!is_forced)
target += '&timeout=t';
window.location = target;
}
};

TimeoutDialog.init();
};
}(window.jQuery);
$.timeoutDialog = function(options) {

var settings = {
timeout : 1200,
countdown : 60,
title : 'Your session is about to expire!',
message : 'You will be logged out in {0} ',
question : 'Do you want to stay signed in?',
keep_alive_button_text : 'Yes, Keep me signed in',
sign_out_button_text : 'No, Sign me out',
keep_alive_url : '/keep-alive',
logout_url : null,
logout_redirect_url : '/',
restart_on_yes : true,
dialog_width : 350,
display_minutes_and_seconds : false
}

$.extend(settings, options);

var TimeoutDialog = {
init : function() {
this.setupDialogTimer();
},

setupDialogTimer : function() {
var self = this;
window.setTimeout(function() {
self.setupDialog();
}, (settings.timeout - settings.countdown) * 1000);
},

setupDialog : function() {
var self = this;
self.destroyDialog();

var initialTimeDisplay = settings.countdown;
if(settings.display_minutes_and_seconds) {
seconds = settings.countdown%60;
if(seconds === 0) {
seconds = "00";
}
initialTimeDisplay = Math.floor(settings.countdown/60) + ":" + seconds;
}

$(
'<div id="timeout-dialog">'
+ '<p id="timeout-message">'
+ settings.message
.format('<span id="timeout-countdown">'
+ initialTimeDisplay
+ '</span>') + '</p>'
+ '<p id="timeout-question">'
+ settings.question + '</p>' + '</div>')
.appendTo('body').dialog({
modal : true,
width : settings.dialog_width,
minHeight : 'auto',
zIndex : 10000,
closeOnEscape : false,
draggable : false,
resizable : false,
dialogClass : 'timeout-dialog',
title : settings.title,
buttons : {
'keep-alive-button' : {
text : settings.keep_alive_button_text,
id : "timeout-keep-signin-btn",
click : function() {
self.keepAlive();
}
},
'sign-out-button' : {
text : settings.sign_out_button_text,
id : "timeout-sign-out-button",
click : function() {
self.signOut(true);
}
}
}
});

self.startCountdown();
},

minutes : function() {
Math.floor(settings.countdown/60);
},

destroyDialog : function() {
if ($("#timeout-dialog").length) {
$("#timeout-dialog").dialog("close");
$('#timeout-dialog').remove();
}
},

startCountdown : function() {
var self = this,
counter = settings.countdown;

this.countdown = window.setInterval(function() {
counter -= 1;

if(settings.display_minutes_and_seconds) {
displayMinutes = Math.floor(counter/60),
displaySeconds = counter%60;
if(displaySeconds < 10) {
displaySeconds = "0" + displaySeconds;
}
$("#timeout-countdown").html(displayMinutes + ":" + displaySeconds);
}
else {
$("#timeout-countdown").html(counter);
}


if (counter <= 0) {
window.clearInterval(self.countdown);
self.signOut(false);
}

}, 1000);
},

keepAlive : function() {
var self = this;
this.destroyDialog();
window.clearInterval(this.countdown);

$.get(settings.keep_alive_url, function(data) {
if (data == "OK") {
if (settings.restart_on_yes) {
self.setupDialogTimer();
}
} else {
self.signOut(false);
}
});
},

signOut : function(is_forced) {
var self = this;
this.destroyDialog();

if (settings.logout_url != null) {
$.post(settings.logout_url, function(data) {
self.redirectLogout(is_forced);
});
} else {
self.redirectLogout(is_forced);
}
},

redirectLogout : function(is_forced) {
var target = settings.logout_redirect_url
+ '?next='
+ encodeURIComponent(window.location.pathname
+ window.location.search);
if (!is_forced)
target += '&timeout=t';
window.location = target;
}
};

TimeoutDialog.init();
};
}(window.jQuery);