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