Merge "Fix deprecated hooks not having a non-deprecated alternative"
[lhc/web/wiklou.git] / resources / src / mediawiki.action / mediawiki.action.edit.editWarning.js
1 /*
2 * Javascript for module editWarning
3 */
4 ( function ( mw, $ ) {
5 'use strict';
6
7 $( function () {
8 var savedWindowOnBeforeUnload,
9 $wpTextbox1 = $( '#wpTextbox1' ),
10 $wpSummary = $( '#wpSummary' );
11 // Check if EditWarning is enabled and if we need it
12 if ( $wpTextbox1.length === 0 ) {
13 return true;
14 }
15 // Get the original values of some form elements
16 $wpTextbox1.add( $wpSummary ).each( function () {
17 $( this ).data( 'origtext', $( this ).val() );
18 } );
19 $( window )
20 .on( 'beforeunload.editwarning', function () {
21 var retval;
22
23 // Check if the current values of some form elements are the same as
24 // the original values
25 if (
26 mw.config.get( 'wgAction' ) === 'submit' ||
27 $wpTextbox1.data( 'origtext' ) !== $wpTextbox1.textSelection( 'getContents' ) ||
28 $wpSummary.data( 'origtext' ) !== $wpSummary.textSelection( 'getContents' )
29 ) {
30 // Return our message
31 retval = mw.msg( 'editwarning-warning' );
32 }
33
34 // Unset the onbeforeunload handler so we don't break page caching in Firefox
35 savedWindowOnBeforeUnload = window.onbeforeunload;
36 window.onbeforeunload = null;
37 if ( retval !== undefined ) {
38 // ...but if the user chooses not to leave the page, we need to rebind it
39 setTimeout( function () {
40 window.onbeforeunload = savedWindowOnBeforeUnload;
41 }, 1 );
42 return retval;
43 }
44 } )
45 .on( 'pageshow.editwarning', function () {
46 // Re-add onbeforeunload handler
47 if ( !window.onbeforeunload ) {
48 window.onbeforeunload = savedWindowOnBeforeUnload;
49 }
50 } );
51
52 // Add form submission handler
53 $( '#editform' ).submit( function () {
54 // Unbind our handlers
55 $( window ).off( '.editwarning' );
56 } );
57 } );
58
59 }( mediaWiki, jQuery ) );