Merge "Add first letter data for bn collation (Standard and Traditional)"
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.confirmCloseWindow.js
1 ( function ( mw, $ ) {
2 /**
3 * @method confirmCloseWindow
4 * @member mw
5 *
6 * Prevent the closing of a window with a confirm message (the onbeforeunload event seems to
7 * work in most browsers.)
8 *
9 * This supersedes any previous onbeforeunload handler. If there was a handler before, it is
10 * restored when you execute the returned release() function.
11 *
12 * var allowCloseWindow = mw.confirmCloseWindow();
13 * // ... do stuff that can't be interrupted ...
14 * allowCloseWindow.release();
15 *
16 * The second function returned is a trigger function to trigger the check and an alert
17 * window manually, e.g.:
18 *
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)
23 * } else {
24 * // do whatever you wanted to do
25 * }
26 *
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
34 */
35 mw.confirmCloseWindow = function ( options ) {
36 var savedUnloadHandler,
37 mainEventName = 'beforeunload',
38 showEventName = 'pageshow',
39 message;
40
41 options = $.extend( {
42 message: mw.message( 'mwe-prevent-close' ).text(),
43 test: function () { return true; }
44 }, options );
45
46 if ( options.namespace ) {
47 mainEventName += '.' + options.namespace;
48 showEventName += '.' + options.namespace;
49 }
50
51 if ( $.isFunction( options.message ) ) {
52 message = options.message();
53 } else {
54 message = options.message;
55 }
56
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;
65 }, 1 );
66
67 // show an alert with this message
68 return message;
69 }
70 } ).on( showEventName, function () {
71 // Re-add onbeforeunload handler
72 if ( !window.onbeforeunload && savedUnloadHandler ) {
73 window.onbeforeunload = savedUnloadHandler;
74 }
75 } );
76
77 /**
78 * Return the object with functions to release and manually trigger the confirm alert
79 *
80 * @ignore
81 */
82 return {
83 /**
84 * Remove all event listeners and don't show an alert anymore, if the user wants to leave
85 * the page.
86 *
87 * @ignore
88 */
89 release: function () {
90 $( window ).off( mainEventName + ' ' + showEventName );
91 },
92 /**
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.
96 *
97 * @ignore
98 * @return {boolean}
99 */
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
104 return false;
105 }
106 // otherwise return true
107 return true;
108 }
109 };
110 };
111 }( mediaWiki, jQuery ) );