LT-107: Add countdown dialog
- Remove ui from meteor-stale-session package
This commit is contained in:
1 parent
d18a66d513
commit
5ec3cace94
14 files changed
+158
-74
No files matched your search
@@ -9,74 +9,84 @@
|
||||
var heartbeatInterval = Meteor.settings && Meteor.settings.public && Meteor.settings.public.staleSessionHeartbeatInterval || (3*60*1000); // 3mins
|
||||
var activityEvents = Meteor.settings && Meteor.settings.public && Meteor.settings.public.staleSessionActivityEvents || 'mousemove click keydown';
|
||||
var inactivityTimeout = Meteor.settings && Meteor.settings.public && Meteor.settings.public.staleSessionInactivityTimeout || (30*60*1000); // 30mins
|
||||
var countdownDialogTime = Meteor.settings && Meteor.settings.public && Meteor.settings.public.countdownDialogTime || (15*1000); // 30second
|
||||
var showCountdownDialog = Meteor.settings && Meteor.settings.public && Meteor.settings.public.showCountdownDialog || true;
|
||||
|
||||
var dialogTimeout = Meteor.settings && Meteor.settings.public && Meteor.settings.public.dialogTimeout || (30*1000); // 30secs
|
||||
var showCountdownDialog = Meteor.settings && Meteor.settings.public && Meteor.settings.public.showCountdownDialog || false;
|
||||
var countdownHeartbeatInterval = (heartbeatInterval < dialogTimeout)? heartbeatInterval : dialogTimeout;
|
||||
|
||||
var activityDetected = false;
|
||||
var activityDetectedTime = new Date();
|
||||
var lastActivityDetectedTime = new Date();
|
||||
var dialogIsOpen = false;
|
||||
|
||||
Meteor.startup(function() {
|
||||
// Add countdown dialog to body
|
||||
$("body").append('<div id="staleSessionCountdownModal" title="Session will expire!"></div>');
|
||||
|
||||
//Initialize the dialog
|
||||
$("#staleSessionCountdownModal").dialog({
|
||||
autoOpen: false,
|
||||
open: function(){
|
||||
$('.ui-widget-overlay').bind('click',function(){
|
||||
$('#staleSessionCountdownModal').dialog('close');
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
//
|
||||
// periodically send a heartbeat if activity has been detected within the interval
|
||||
//
|
||||
Meteor.setInterval(function() {
|
||||
if (Meteor.userId() && activityDetected) {
|
||||
Meteor.call('heartbeat');
|
||||
activityDetected = false;
|
||||
}
|
||||
}, heartbeatInterval);
|
||||
|
||||
// Detect the time when countdown dialog will be shown
|
||||
if (showCountdownDialog) {
|
||||
Meteor.setInterval(function() {
|
||||
if (!activityDetected) {
|
||||
var lastHeartbeatTime = activityDetectedTime.getTime();
|
||||
var now = new Date().getTime();
|
||||
var overdueTimestamp = now - lastHeartbeatTime;
|
||||
var dialogTime = inactivityTimeout - countdownDialogTime;
|
||||
if(dialogTime <= overdueTimestamp && inactivityTimeout >= overdueTimestamp) {
|
||||
var sec = Math.round((inactivityTimeout - overdueTimestamp) / 1000);
|
||||
console.log(sec);
|
||||
var dialogStr = "You will be log out in "+sec+" seconds.";
|
||||
if (sec === 0) {
|
||||
$("#staleSessionCountdownModal").dialog('close');
|
||||
} else {
|
||||
if(sec === 1) {
|
||||
dialogStr = "You will be log out in "+sec+" second.";
|
||||
}
|
||||
$('#staleSessionCountdownModal').html(dialogStr);
|
||||
$("#staleSessionCountdownModal").dialog('open');
|
||||
|
||||
// Remove border of close button
|
||||
$(".ui-button:focus").css("outline", "none");
|
||||
}
|
||||
if (Meteor.userId()) {
|
||||
if (activityDetected) {
|
||||
Meteor.call('heartbeat', function(error, heartbeatTime) {
|
||||
lastActivityDetectedTime = heartbeatTime;
|
||||
activityDetected = false;
|
||||
// Event to close dialog
|
||||
$.event.trigger('TriggerCloseTimeoutCountdownDialog');
|
||||
dialogIsOpen = false;
|
||||
});
|
||||
|
||||
} else {
|
||||
$("#staleSessionCountdownModal").dialog('close');
|
||||
var overdueTimestamp = new Date().getTime() - lastActivityDetectedTime;
|
||||
// Ignore min differences
|
||||
overdueTimestamp = overdueTimestamp - (overdueTimestamp % 1000);
|
||||
console.log(overdueTimestamp);
|
||||
var startTime = inactivityTimeout - dialogTimeout;
|
||||
if (overdueTimestamp <= inactivityTimeout) {
|
||||
var nextIntervalTime = overdueTimestamp + countdownHeartbeatInterval;
|
||||
if (nextIntervalTime <= inactivityTimeout && nextIntervalTime >= startTime) {
|
||||
if (Math.abs(startTime - overdueTimestamp) <= Math.abs(nextIntervalTime - startTime) && !dialogIsOpen) {
|
||||
// Open dialog
|
||||
var leftTime = Math.round((inactivityTimeout - overdueTimestamp) / 1000);
|
||||
$.event.trigger('TriggerOpenTimeoutCountdownDialog', leftTime);
|
||||
dialogIsOpen = true;
|
||||
}
|
||||
} else {
|
||||
// Event to close dialog
|
||||
$.event.trigger('TriggerCloseTimeoutCountdownDialog');
|
||||
dialogIsOpen = false;
|
||||
}
|
||||
|
||||
} else {
|
||||
// Event to close dialog
|
||||
$.event.trigger('TriggerCloseTimeoutCountdownDialog');
|
||||
dialogIsOpen = false;
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
// Event to close dialog
|
||||
$.event.trigger('TriggerCloseTimeoutCountdownDialog');
|
||||
dialogIsOpen = false;
|
||||
}
|
||||
}, 1000);
|
||||
}, countdownHeartbeatInterval);
|
||||
|
||||
} else{
|
||||
|
||||
Meteor.setInterval(function() {
|
||||
if (Meteor.userId() && activityDetected) {
|
||||
Meteor.call('heartbeat');
|
||||
activityDetected = false;
|
||||
}
|
||||
}, heartbeatInterval);
|
||||
}
|
||||
|
||||
//
|
||||
// detect activity and mark it as detected on any of the following events
|
||||
//
|
||||
$(document).on(activityEvents, function(event) {
|
||||
$(document).on(activityEvents, function() {
|
||||
activityDetected = true;
|
||||
activityDetectedTime = new Date();
|
||||
$("#staleSessionCountdownModal").dialog('close');
|
||||
// Event to close dialog
|
||||
$.event.trigger('TriggerCloseTimeoutCountdownDialog');
|
||||
dialogIsOpen = false;
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user