bootstrap-session-timeout.js
9.86 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* bootstrap-session-timeout
* www.orangehilldev.com
*
* Copyright (c) 2014 Vedran Opacic
* Licensed under the MIT license.
*/
(function($) {
/*jshint multistr: true */
'use strict';
$.sessionTimeout = function(options) {
var defaults = {
title: 'Your Session is About to Expire!',
message: 'Your session is about to expire.',
logoutButton: 'Logout',
keepAliveButton: 'Stay Connected',
keepAliveUrl: '/keep-alive',
ajaxType: 'POST',
ajaxData: '',
redirUrl: '/timed-out',
logoutUrl: '/log-out',
warnAfter: 900000, // 15 minutes
redirAfter: 1200000, // 20 minutes
keepAliveInterval: 5000,
keepAlive: true,
ignoreUserActivity: false,
onStart: false,
onWarn: false,
onRedir: false,
countdownMessage: false,
countdownBar: false,
countdownSmart: false
};
var opt = defaults,
timer,
countdown = {};
// Extend user-set options over defaults
if (options) {
opt = $.extend(defaults, options);
}
// Some error handling if options are miss-configured
if (opt.warnAfter >= opt.redirAfter) {
console.error('Bootstrap-session-timeout plugin is miss-configured. Option "redirAfter" must be equal or greater than "warnAfter".');
return false;
}
// Unless user set his own callback function, prepare bootstrap modal elements and events
if (typeof opt.onWarn !== 'function') {
// If opt.countdownMessage is defined add a coundown timer message to the modal dialog
var countdownMessage = opt.countdownMessage ?
'<p>' + opt.countdownMessage.replace(/{timer}/g, '<span class="countdown-holder"></span>') + '</p>' : '';
var coundownBarHtml = opt.countdownBar ?
'<div class="progress"> \
<div class="progress-bar progress-bar-striped countdown-bar active" role="progressbar" style="min-width: 15px; width: 100%;"> \
<span class="countdown-holder"></span> \
</div> \
</div>' : '';
// Create timeout warning dialog
$('body').append('<div class="modal fade" id="session-timeout-dialog"> \
<div class="modal-dialog"> \
<div class="modal-content"> \
<div class="modal-header"> \
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> \
<h4 class="modal-title">' + opt.title + '</h4> \
</div> \
<div class="modal-body"> \
<p>' + opt.message + '</p> \
' + countdownMessage + ' \
' + coundownBarHtml + ' \
</div> \
<div class="modal-footer"> \
<button id="session-timeout-dialog-logout" type="button" class="btn btn-default">' + opt.logoutButton + '</button> \
<button id="session-timeout-dialog-keepalive" type="button" class="btn btn-primary" data-dismiss="modal">' + opt.keepAliveButton + '</button> \
</div> \
</div> \
</div> \
</div>');
// "Logout" button click
$('#session-timeout-dialog-logout').on('click', function() {
window.location = opt.logoutUrl;
});
// "Stay Connected" button click
$('#session-timeout-dialog').on('hide.bs.modal', function() {
// Restart session timer
startSessionTimer();
});
}
// Reset timer on any of these events
if (!opt.ignoreUserActivity) {
var mousePosition = [-1, -1];
$(document).on('keyup mouseup mousemove touchend touchmove', function(e) {
if (e.type === 'mousemove') {
// Solves mousemove even when mouse not moving issue on Chrome:
// https://code.google.com/p/chromium/issues/detail?id=241476
if (e.clientX === mousePosition[0] && e.clientY === mousePosition[1]) {
return;
}
mousePosition[0] = e.clientX;
mousePosition[1] = e.clientY;
}
startSessionTimer();
// If they moved the mouse not only reset the counter
// but remove the modal too!
if ($('#session-timeout-dialog').length > 0 &&
$('#session-timeout-dialog').data('bs.modal') &&
$('#session-timeout-dialog').data('bs.modal').isShown) {
// http://stackoverflow.com/questions/11519660/twitter-bootstrap-modal-backdrop-doesnt-disappear
$('#session-timeout-dialog').modal('hide');
$('body').removeClass('modal-open');
$('div.modal-backdrop').remove();
}
});
}
// Keeps the server side connection live, by pingin url set in keepAliveUrl option.
// KeepAlivePinged is a helper var to ensure the functionality of the keepAliveInterval option
var keepAlivePinged = false;
function keepAlive() {
if (!keepAlivePinged) {
// Ping keepalive URL using (if provided) data and type from options
$.ajax({
type: opt.ajaxType,
url: opt.keepAliveUrl,
data: opt.ajaxData
});
keepAlivePinged = true;
setTimeout(function() {
keepAlivePinged = false;
}, opt.keepAliveInterval);
}
}
function startSessionTimer() {
// Clear session timer
clearTimeout(timer);
if (opt.countdownMessage || opt.countdownBar) {
startCountdownTimer('session', true);
}
if (typeof opt.onStart === 'function') {
opt.onStart(opt);
}
// If keepAlive option is set to "true", ping the "keepAliveUrl" url
if (opt.keepAlive) {
keepAlive();
}
// Set session timer
timer = setTimeout(function() {
// Check for onWarn callback function and if there is none, launch dialog
if (typeof opt.onWarn !== 'function') {
$('#session-timeout-dialog').modal('show');
} else {
opt.onWarn(opt);
}
// Start dialog timer
startDialogTimer();
}, opt.warnAfter);
}
function startDialogTimer() {
// Clear session timer
clearTimeout(timer);
if (!$('#session-timeout-dialog').hasClass('in') && (opt.countdownMessage || opt.countdownBar)) {
// If warning dialog is not already open and either opt.countdownMessage
// or opt.countdownBar are set start countdown
startCountdownTimer('dialog', true);
}
// Set dialog timer
timer = setTimeout(function() {
// Check for onRedir callback function and if there is none, launch redirect
if (typeof opt.onRedir !== 'function') {
window.location = opt.redirUrl;
} else {
opt.onRedir(opt);
}
}, (opt.redirAfter - opt.warnAfter));
}
function startCountdownTimer(type, reset) {
// Clear countdown timer
clearTimeout(countdown.timer);
if (type === 'dialog' && reset) {
// If triggered by startDialogTimer start warning countdown
countdown.timeLeft = Math.floor((opt.redirAfter - opt.warnAfter) / 1000);
} else if (type === 'session' && reset) {
// If triggered by startSessionTimer start full countdown
// (this is needed if user doesn't close the warning dialog)
countdown.timeLeft = Math.floor(opt.redirAfter / 1000);
}
// If opt.countdownBar is true, calculate remaining time percentage
if (opt.countdownBar && type === 'dialog') {
countdown.percentLeft = Math.floor(countdown.timeLeft / ((opt.redirAfter - opt.warnAfter) / 1000) * 100);
} else if (opt.countdownBar && type === 'session') {
countdown.percentLeft = Math.floor(countdown.timeLeft / (opt.redirAfter / 1000) * 100);
}
// Set countdown message time value
var countdownEl = $('.countdown-holder');
var secondsLeft = countdown.timeLeft >= 0 ? countdown.timeLeft : 0;
if (opt.countdownSmart) {
var minLeft = Math.floor(secondsLeft / 60);
var secRemain = secondsLeft % 60;
var countTxt = minLeft > 0 ? minLeft + 'm' : '';
if (countTxt.length > 0) {
countTxt += ' ';
}
countTxt += secRemain + 's';
countdownEl.text(countTxt);
} else {
countdownEl.text(secondsLeft + "s");
}
// Set countdown message time value
if (opt.countdownBar) {
$('.countdown-bar').css('width', countdown.percentLeft + '%');
}
// Countdown by one second
countdown.timeLeft = countdown.timeLeft - 1;
countdown.timer = setTimeout(function() {
// Call self after one second
startCountdownTimer(type);
}, 1000);
}
// Start session timer
startSessionTimer();
};
})(jQuery);