3 * @method confirmCloseWindow
6 * Prevent the closing of a window with a confirm message (the onbeforeunload event seems to
7 * work in most browsers.)
9 * This supersedes any previous onbeforeunload handler. If there was a handler before, it is
10 * restored when you execute the returned release() function.
12 * var allowCloseWindow = mw.confirmCloseWindow();
13 * // ... do stuff that can't be interrupted ...
14 * allowCloseWindow.release();
16 * The second function returned is a trigger function to trigger the check and an alert
17 * window manually, e.g.:
19 * var allowCloseWindow = mw.confirmCloseWindow();
20 * // ... do stuff that can't be interrupted ...
21 * if ( allowCloseWindow.trigger() ) {
22 * // don't do anything (e.g. destroy the input field)
24 * // do whatever you wanted to do
27 * @param {Object} [options]
28 * @param {string} [options.namespace] Namespace for the event registration
29 * @param {string} [options.message]
30 * @param {string} options.message.return The string message to show in the confirm dialog.
31 * @param {Function} [options.test]
32 * @param {boolean} [options.test.return=true] Whether to show the dialog to the user.
33 * @return {Object} An object of functions to work with this module
35 mw
.confirmCloseWindow = function ( options
) {
36 var savedUnloadHandler
,
37 mainEventName
= 'beforeunload',
38 showEventName
= 'pageshow',
42 message
: mw
.message( 'mwe-prevent-close' ).text(),
43 test: function () { return true; }
46 if ( options
.namespace ) {
47 mainEventName
+= '.' + options
.namespace;
48 showEventName
+= '.' + options
.namespace;
51 if ( $.isFunction( options
.message
) ) {
52 message
= options
.message();
54 message
= options
.message
;
57 $( window
).on( mainEventName
, function () {
58 if ( options
.test() ) {
59 // remove the handler while the alert is showing - otherwise breaks caching in Firefox (3?).
60 // but if they continue working on this page, immediately re-register this handler
61 savedUnloadHandler
= window
.onbeforeunload
;
62 window
.onbeforeunload
= null;
63 setTimeout( function () {
64 window
.onbeforeunload
= savedUnloadHandler
;
67 // show an alert with this message
70 } ).on( showEventName
, function () {
71 // Re-add onbeforeunload handler
72 if ( !window
.onbeforeunload
&& savedUnloadHandler
) {
73 window
.onbeforeunload
= savedUnloadHandler
;
78 * Return the object with functions to release and manually trigger the confirm alert
84 * Remove all event listeners and don't show an alert anymore, if the user wants to leave
89 release: function () {
90 $( window
).off( mainEventName
+ ' ' + showEventName
);
93 * Trigger the module's function manually: Check, if options.test() returns true and show
94 * an alert to the user if he/she want to leave this page. Returns false, if options.test() returns
95 * false or the user cancelled the alert window (~don't leave the page), true otherwise.
100 trigger: function () {
101 // use confirm to show the message to the user (if options.text() is true)
102 if ( options
.test() && !confirm( message
) ) {
103 // the user want to keep the actual page
106 // otherwise return true
111 }( mediaWiki
, jQuery
) );