summaryrefslogtreecommitdiff
path: root/resources/src/mediawiki.action/mediawiki.action.edit.editWarning.js
blob: b5654400f7d0f882622ea8e851b8ed33ea08e1d6 (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
/*
 * Javascript for module editWarning
 */
( function ( mw, $ ) {
	'use strict';

	$( function () {
		var savedWindowOnBeforeUnload,
			$wpTextbox1 = $( '#wpTextbox1' ),
			$wpSummary = $( '#wpSummary' );
		// Check if EditWarning is enabled and if we need it
		if ( $wpTextbox1.length === 0 ) {
			return true;
		}
		// Get the original values of some form elements
		$wpTextbox1.add( $wpSummary ).each( function () {
			$( this ).data( 'origtext', $( this ).val() );
		} );
		$( window )
			.on( 'beforeunload.editwarning', function () {
				var retval;

				// Check if the current values of some form elements are the same as
				// the original values
				if (
					mw.config.get( 'wgAction' ) === 'submit' ||
						$wpTextbox1.data( 'origtext' ) !== $wpTextbox1.textSelection( 'getContents' ) ||
						$wpSummary.data( 'origtext' ) !== $wpSummary.textSelection( 'getContents' )
				) {
					// Return our message
					retval = mw.msg( 'editwarning-warning' );
				}

				// Unset the onbeforeunload handler so we don't break page caching in Firefox
				savedWindowOnBeforeUnload = window.onbeforeunload;
				window.onbeforeunload = null;
				if ( retval !== undefined ) {
					// ...but if the user chooses not to leave the page, we need to rebind it
					setTimeout( function () {
						window.onbeforeunload = savedWindowOnBeforeUnload;
					}, 1 );
					return retval;
				}
			} )
			.on( 'pageshow.editwarning', function () {
				// Re-add onbeforeunload handler
				if ( !window.onbeforeunload ) {
					window.onbeforeunload = savedWindowOnBeforeUnload;
				}
			} );

		// Add form submission handler
		$( '#editform' ).submit( function () {
			// Unbind our handlers
			$( window ).off( '.editwarning' );
		} );
	} );

}( mediaWiki, jQuery ) );