Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add callback control for buttons, control over modal show/hide, control over timer reset. #50

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ Default: `'Logout'`

This is the text shown to user via Bootstrap warning dialog after warning period in the logout button.

**logoutButtonCallback**<br>

Type: `Boolean` or `function`

Default: `false`

This is a way to set a custom callback behavior for the logout button.

**keepAliveButton**<br>

Type: `String`
Expand All @@ -56,6 +64,14 @@ Default: `'Stay Connected'`

This is the text shown to user via Bootstrap warning dialog after warning period in the Keep Alive button.

**keepAliveButtonCallback**<br>

Type: `Boolean` or `function`

Default: `false`

This is a way to set a custom callback behavior for the keep alive button.

**keepAliveUrl**

Type: `String`
Expand Down Expand Up @@ -188,6 +204,31 @@ Default: `false`

Custom callback you can use instead of redirecting the user to `redirUrl`. Takes options object as the only argument.

**activityHidesModal**

Type: `Boolean`

Default: `true`

If `false`, the modal persists on the screen regardless of mouse or keyboard activity.

**resetTimerWhileModalOpen**

Type: `Boolean`

Default: `true`

If `false`, the timer is not reset on any mouse or keyboard activity.

**isModalStatic**

Type: `Boolean`

Default: `false`

If `true`, the modal persists on the screen even when user clicks outside of the modal element or presses the escape key.
To dismiss the modal the user must use the buttons.

## Examples

You can play around with the examples in the `/examples` directory.
Expand Down
67 changes: 52 additions & 15 deletions dist/bootstrap-session-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
title: 'Your Session is About to Expire!',
message: 'Your session is about to expire.',
logoutButton: 'Logout',
// allow control of logoutButton callback
logoutButtonCallback: false,
keepAliveButton: 'Stay Connected',
// allow control of keepAliveButton callback
keepAliveButtonCallback: false,
keepAliveUrl: '/keep-alive',
ajaxType: 'POST',
ajaxData: '',
Expand All @@ -30,7 +34,13 @@
onRedir: false,
countdownMessage: false,
countdownBar: false,
countdownSmart: false
countdownSmart: false,
// allow control over show/hide modal on mouse or keyboard activity
activityHidesModal: true,
// allow control over timer reset while modal is shown
resetTimerWhileModalOpen: true,
// allow ability to set modal as static.
isModalStatic: false
};

var opt = defaults,
Expand Down Expand Up @@ -60,8 +70,11 @@
</div> \
</div>' : '';

// if isModalStatic is true, set modal data attributes to make the modal static
var modalStaticAttributes = opt.isModalStatic ? 'data-backdrop="static" data-keyboard="false"' : '';

// Create timeout warning dialog
$('body').append('<div class="modal fade" id="session-timeout-dialog"> \
$('body').append('<div class="modal fade" id="session-timeout-dialog" ' + modalStaticAttributes + '> \
<div class="modal-dialog"> \
<div class="modal-content"> \
<div class="modal-header"> \
Expand All @@ -82,14 +95,30 @@
</div>');

// "Logout" button click
$('#session-timeout-dialog-logout').on('click', function() {
window.location = opt.logoutUrl;
});
// Allow for callback execution.
if (typeof opt.logoutButtonCallback !== 'function') {
$('#session-timeout-dialog-logout').on('click', function () {
window.location = opt.logoutUrl;
});
} else {
$('#session-timeout-dialog-logout').on('click', function () {
opt.logoutButtonCallback();
});
}

// "Stay Connected" button click
$('#session-timeout-dialog').on('hide.bs.modal', function() {
// Restart session timer
startSessionTimer();
});
// Allow for callback execution.
if (typeof opt.keepAliveButtonCallback !== 'function') {
$('#session-timeout-dialog').on('hide.bs.modal', function () {
// Restart session timer
startSessionTimer();
});
} else {
$('#session-timeout-dialog').on('hide.bs.modal', function () {
opt.keepAliveButtonCallback();
startSessionTimer();
});
}
}

// Reset timer on any of these events
Expand All @@ -105,23 +134,31 @@
mousePosition[0] = e.clientX;
mousePosition[1] = e.clientY;
}
startSessionTimer();

var isModalOpen = $('#session-timeout-dialog').length > 0 &&
$('#session-timeout-dialog').data('bs.modal') &&
$('#session-timeout-dialog').data('bs.modal').isShown;

// Reset timer if the modal is not open
// OR
// if the modal is open AND the option is set to true.
if (!isModalOpen || (isModalOpen && opt.resetTimerWhileModalOpen)) {
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) {
// But only hide the modal if activityHidesModal is true.
if (opt.activityHidesModal === true && isModalOpen) {
// 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.
// Keeps the server side connection live, by pinging url set in keepAliveUrl option.
// KeepAlivePinged is a helper var to ensure the functionality of the keepAliveInterval option
var keepAlivePinged = false;

Expand Down
2 changes: 1 addition & 1 deletion dist/bootstrap-session-timeout.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.