/********************************* About *******************************
* This script governed by the GNU Lesser General Public License
* by Nathaniel Fitzgerald-Hood Central Queensland University
* based on JavaScript WebVoyage(TM) Timer v2.0
* by Jim Robinson, http://library.tccd.edu/code/
*
* The timer.js script requires the following files: timer.css
***********************************************************************/
function SessionTimer( sessionTime, displayType ) {
// the number of seconds until timeout
this.sessionIdleTime = sessionTime;
// remaining number of seconds when the popup window appears
this.remindTime = 55;
// the relative URI of the login page / page to request in order to reset session timer
this.loginUrl = "loginPage.do";
this.sessionRefreshRequest = "signin.do";
// warning text
this.warningText = "Your session will end soon.
To keep your work, click Continue Session
To ignore click Cancel";
this.alertWarningText = "Your session will end soon.\nTo keep your work, click 'OK' to continue \nor to ignore click 'Cancel'";
// timed-out text
this.timeoutText = "Your session has ended.
You will need to login again to continue.";
this.status = "IN_SESSION"; //IN_SESSION, WARN, EXPIRED
this.displayType = displayType; //one of: ALERT, DIALOG
this.secondsRemaining = 0;
this.seconds = 0;
this.ignoreWarning = false;
this.msgBox;
this.timeoutID;
this.interval = 1; // 1 second intervals
}
SessionTimer.prototype.init = function( ) {
//insert popup into document
if (this.displayType == "DIALOG") {
if (!document.getElementById) return false;
msgBox = document.createElement("div");
msgBox.id = "warningDiv";
msgBox.innerHTML = this.getWarning();
body = document.getElementsByTagName("body")[0];
body.insertBefore( msgBox, body.firstChild);
this.msgBox = msgBox;
}
this.reset();
this.tick();
}
SessionTimer.prototype.reset = function() {
//set seconds, (re)init warning boxes
this.secondsRemaining = this.sessionIdleTime;
this.status = 'IN_SESSION';
}
SessionTimer.prototype.tick = function() {
fmtMinutes = Math.floor(this.secondsRemaining/60);
seconds = this.secondsRemaining%60;
if(seconds < 10) {
seconds = "0" + seconds; // format seconds for display
}
if (this.secondsRemaining < 0) {
timeRemainingMsg = this.formatTime("Session has timed out");
//if(this.displayType == "ALERT" || (this.displayType == "DIALOG" && !this.ignoreWarning)) {
this.setExpiredDialog();
this.showDialog();
this.status = 'EXPIRED'
//}
} else if(this.secondsRemaining > 0 && this.secondsRemaining <= this.remindTime) {
if (!this.ignoreWarning) {
this.setWarningDialog();
this.showDialog();
}
timeRemainingMsg = this.formatTime("Session timeout in: " + fmtMinutes + " minutes, " + seconds + " seconds");
} else {
timeRemainingMsg = this.formatTime("Session timeout in: " + fmtMinutes + " minutes, " + seconds + " seconds");
}
if (this.secondsRemaining >= -1 && !this.ignoreWarning) {
this.timeoutId = setTimeout("timer.tick()", 1000*this.interval); // restart the timer for another second
}
if (this.displayType =="DIALOG" && this.msgBox.style.display == "block") {
document.getElementById("timeRemaining").value = timeRemainingMsg;
}
this.secondsRemaining -= this.interval;
//alert( this.secondsRemaining
}
SessionTimer.prototype.formatTime = function(x) { // Format the time
var pattern = new RegExp("([0-9]+);");
while ((result = pattern.exec(x)) != null) {
x = x.replace(result[0], String.fromCharCode(RegExp.$1));
}
x = x.replace(/ /g, ' ');
return x;
}
SessionTimer.prototype.showDialog = function() {
if (this.displayType == "DIALOG") {
if (this.msgBox.style.display != "block") {
this.msgBox.style.display = "block";
}
} else {
if (this.status == "WARN") {
result = confirm(this.alertWarningText);
if (result) { //OK
this.refreshPage();
} else { //cancel
this.cancelWarning();
}
} else if (this.status == "EXPIRED") {
confirm( this.timeoutText );
this.login();
}
}
}
SessionTimer.prototype.hideDialog = function() {
if (this.displayType == "DIALOG" && this.msgBox.style.display == "block") {
this.msgBox.style.display = "none";
}
}
SessionTimer.prototype.setMessage = function( message ) {
if (this.displayType == "DIALOG") {
document.getElementById("warningHeader").innerHTML = message;
}
}
SessionTimer.prototype.setButtons = function( buttons ) {
if (this.displayType == "DIALOG") {
document.getElementById("actionButtons").innerHTML = buttons;
}
}
SessionTimer.prototype.setWarningDialog = function() {
if (this.status != "WARN") {
this.setMessage( this.warningText );
button1 = '';
button2 = '';
this.setButtons( button1 + button2 );
this.status = "WARN";
}
}
SessionTimer.prototype.setExpiredDialog = function() {
if (this.status != "EXPIRED") {
this.setMessage( this.timeoutText );
button1 = '';
this.setButtons( button1 );
this.status = "EXPIRED";
}
}
/**** actions.. should eventually be converted to events ****/
// loads an image which restarts the session clock and the timer
// Hides the warning
SessionTimer.prototype.refreshPage = function() {
//load an uncached image and close box
var oImage = new Image;
oImage.src = this.sessionRefreshRequest + "?" + Math.random(); // need <- better random #
this.hideDialog();
window.status = "Session refreshed";
this.reset();
//what's happening to ticks?
}
SessionTimer.prototype.login = function() {
if (parent.frames.length != 0) {
// loaded in frames
top.location = this.loginUrl;
}
else {
// not loaded frames
window.location.href = this.loginUrl;
}
}
SessionTimer.prototype.cancelWarning = function() {
this.ignoreWarning = true;
this.hideDialog();
}
SessionTimer.prototype.getWarning = function() {
var myDiv = '