-
Notifications
You must be signed in to change notification settings - Fork 7
/
epilgrim.sessionTimeoutHandler.js
184 lines (175 loc) · 5.74 KB
/
epilgrim.sessionTimeoutHandler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
(function($, window, undefined){
$.widget( "epilgrim.sessionTimeoutHandler", {
options: {
// default options
title : 'Session Timeout',
message : 'Your session is about to expire.',
keepAliveUrl : 'keepAlive.php',
retrieveTimeLeftUrl : 'retrieveTimeLeft.php',
redirUrl : false,
logoutUrl : 'logout.php',
defaultSessionTime: false,
warnWhenLeft : 300000,
checkTimeBeforeRedirect: true,
checkTimeBeforePopup: false,
errorGettingSessionTimeLeft: 'There was an error retrieving session information from the server. Session could be timed out',
modalId: 'sessionTimeout-dialog',
// callbacks
logOutNow: function(event, data) {
window.location = data.plugin.options.logoutUrl;
},
redirectNow: function(event, data) {
window.location = data.plugin.options.redirUrl;
},
stayConnected: function(event, data) {
data.plugin.modal.dialog('close');
data.plugin._trigger(
'getSessionTimeLeft',
null,
{
plugin: data.plugin,
updateSession: true,
callback: $.proxy(
function(time){
this.initializeTimers(time);
}, data.plugin
)
}
);
},
getSessionTimeLeft: function (event, data) {
var action = data.updateSession ? data.plugin.options.keepAliveUrl : data.plugin.options.retrieveTimeLeftUrl;
var self = data.plugin;
$.ajax({
context: self,
dataType: "json",
url: action,
success: function (time){
var timeLeft = time * 1000;
data.callback.call(self, timeLeft);
if (timeLeft > this.options.warnWhenLeft) {
this.modal.dialog('close');
}
},
error: function(){
alert(self.options.errorGettingSessionTimeLeft);
}
});
}
},
// the constructor
_create: function() {
// Create timeout warning dialog
this.dialogTimer = null;
// timer for redirecting
this.redirectTimer = null;
if (this.options.redirUrl == false){
this.options.redirUrl = this.options.logoutUrl;
}
this._createModal();
this.initializeTimers(this.options.defaultSessionTime);
},
_createModal: function(){
var self = this;
this.modal = jQuery('<div/>', {
id: this.options.modalId,
title: this.options.title,
text: this.options.message
});
this.modal.appendTo(this.element);
this.modal.dialog({
autoOpen: false,
width: 400,
modal: true,
closeOnEscape: false,
open: function(event, ui) {
$(".ui-dialog-titlebar-close", this.modal).hide();
},
buttons: {
// Button one - takes user to logout URL
"Log Out Now": function() {
self._trigger('logOutNow', null, { plugin: self });
},
// Button two - closes dialog and makes call to keep-alive URL
"Stay Connected": function() {
self._trigger('stayConnected', null, { plugin: self });
}
}
});
},
_destroy: function() {
// remove generated elements
this.modal.remove();
},
_dialogTimer: function(action, time ){
switch(action) {
case 'start':
this.dialogTimer = setTimeout($.proxy(this._dialogHandler, this), time);
break;
case 'stop':
clearTimeout(this.dialogTimer);
break;
case 'restart':
this._dialogTimer('stop');
this._dialogTimer('start', time);
break;
}
},
_redirectTimer: function(action, time ){
switch(action) {
case 'start':
this.redirectTimer = setTimeout($.proxy(this._redirectHandler, this), time);
break;
case 'stop':
clearTimeout(this.redirectTimer);
break;
case 'restart':
this._redirectTimer('stop');
this._redirectTimer('start', time);
break;
}
},
_dialogHandler: function(){
if(this.options.checkTimeBeforePopup){
this._trigger('getSessionTimeLeft', null, {plugin: this, updateSession: false, callback: function(timeLeft){
if (timeLeft <= this.options.warnWhenLeft){
this.modal.dialog('open');
} else {
this.initializeTimers(timeLeft);
}
}});
}else{
this.modal.dialog('open');
}
},
_redirectHandler: function(){
if (this.options.checkTimeBeforeRedirect){
this._trigger('getSessionTimeLeft', null, {plugin: this, updateSession: false, callback: function(time){
if (time <= 0){
this._trigger('redirectNow', null, {plugin: this});
} else {
this.initializeTimers(time);
}
}});
} else {
this._trigger('redirectNow', null, {plugin: this});
}
},
_setTimers: function (time){
var action;
action = this.dialogTimer === null ? 'start' : 'restart';
this._dialogTimer( action, (time - this.options.warnWhenLeft));
action = this.redirectTimer === null ? 'start' : 'restart';
this._redirectTimer ( action, time);
},
initializeTimers: function (time){
if ( time === false){
// if no time variable was passed, get it from the server
this._trigger('getSessionTimeLeft', null, {plugin: this, updateSession: false, callback: this._setTimers});
} else {
// when time variable passed, set it
this._setTimers(time);
}
}
});
})(jQuery, window);