summaryrefslogtreecommitdiff
path: root/resources/src/mediawiki/mediawiki.confirmCloseWindow.js
blob: 4d0c1352de13cf66c4d8823ff4590d92370ec06a (plain)
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
/* jshint devel: true */
( function ( mw, $ ) {
	/**
	 * @method confirmCloseWindow
	 * @member mw
	 *
	 * Prevent the closing of a window with a confirm message (the onbeforeunload event seems to
	 * work in most browsers.)
	 *
	 * This supersedes any previous onbeforeunload handler. If there was a handler before, it is
	 * restored when you execute the returned release() function.
	 *
	 *     var allowCloseWindow = mw.confirmCloseWindow();
	 *     // ... do stuff that can't be interrupted ...
	 *     allowCloseWindow.release();
	 *
	 * The second function returned is a trigger function to trigger the check and an alert
	 * window manually, e.g.:
	 *
	 *     var allowCloseWindow = mw.confirmCloseWindow();
	 *     // ... do stuff that can't be interrupted ...
	 *     if ( allowCloseWindow.trigger() ) {
	 *         // don't do anything (e.g. destroy the input field)
	 *     } else {
	 *         // do whatever you wanted to do
	 *     }
	 *
	 * @param {Object} [options]
	 * @param {string} [options.namespace] Namespace for the event registration
	 * @param {string} [options.message]
	 * @param {string} options.message.return The string message to show in the confirm dialog.
	 * @param {Function} [options.test]
	 * @param {boolean} [options.test.return=true] Whether to show the dialog to the user.
	 * @return {Object} An object of functions to work with this module
	 */
	mw.confirmCloseWindow = function ( options ) {
		var savedUnloadHandler,
			mainEventName = 'beforeunload',
			showEventName = 'pageshow',
			message;

		options = $.extend( {
			message: mw.message( 'mwe-prevent-close' ).text(),
			test: function () { return true; }
		}, options );

		if ( options.namespace ) {
			mainEventName += '.' + options.namespace;
			showEventName += '.' + options.namespace;
		}

		if ( $.isFunction( options.message ) ) {
			message = options.message();
		} else {
			message = options.message;
		}

		$( window ).on( mainEventName, function () {
			if ( options.test() ) {
				// remove the handler while the alert is showing - otherwise breaks caching in Firefox (3?).
				// but if they continue working on this page, immediately re-register this handler
				savedUnloadHandler = window.onbeforeunload;
				window.onbeforeunload = null;
				setTimeout( function () {
					window.onbeforeunload = savedUnloadHandler;
				}, 1 );

				// show an alert with this message
				return message;
			}
		} ).on( showEventName, function () {
			// Re-add onbeforeunload handler
			if ( !window.onbeforeunload && savedUnloadHandler ) {
				window.onbeforeunload = savedUnloadHandler;
			}
		} );

		/**
		 * Return the object with functions to release and manually trigger the confirm alert
		 *
		 * @ignore
		 */
		return {
			/**
			 * Remove all event listeners and don't show an alert anymore, if the user wants to leave
			 * the page.
			 *
			 * @ignore
			 */
			release: function () {
				$( window ).off( mainEventName + ' ' + showEventName );
			},
			/**
			 * Trigger the module's function manually: Check, if options.test() returns true and show
			 * an alert to the user if he/she want to leave this page. Returns false, if options.test() returns
			 * false or the user cancelled the alert window (~don't leave the page), true otherwise.
			 *
			 * @ignore
			 * @return {boolean}
			 */
			trigger: function () {
				// use confirm to show the message to the user (if options.text() is true)
				if ( options.test() && !confirm( message ) ) {
					// the user want to keep the actual page
					return false;
				}
				// otherwise return true
				return true;
			}
		};
	};
} )( mediaWiki, jQuery );