Merge "Fix some issues with Microsoft SQL Server Driver"
[lhc/web/wiklou.git] / resources / src / mediawiki.special / mediawiki.special.preferences.js
index e553f44..1f6429b 100644 (file)
@@ -5,7 +5,7 @@ jQuery( function ( $ ) {
        var $preftoc, $preferences, $fieldsets, $legends,
                hash, labelFunc,
                $tzSelect, $tzTextbox, $localtimeHolder, servertime,
-               $checkBoxes;
+               $checkBoxes, savedWindowOnBeforeUnload;
 
        labelFunc = function () {
                return this.id.replace( /^mw-prefsection/g, 'preftab' );
@@ -263,4 +263,43 @@ jQuery( function ( $ ) {
        $( '#mw-input-wpsearcheverything' ).change( function () {
                $checkBoxes.prop( 'disabled', $( this ).prop( 'checked' ) );
        } );
+
+       // Set up a message to notify users if they try to leave the page without
+       // saving.
+       $( '#mw-prefs-form' ).data( 'origdata', $( '#mw-prefs-form' ).serialize() );
+       $( window )
+               .on( 'beforeunload.prefswarning', function () {
+                       var retval;
+
+                       // Check if anything changed
+                       if ( $( '#mw-prefs-form' ).serialize() !== $( '#mw-prefs-form' ).data( 'origdata' ) ) {
+                               // Return our message
+                               retval = mediaWiki.msg( 'prefswarning-warning', mediaWiki.msg( 'saveprefs' ) );
+                       }
+
+                       // 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.prefswarning', function () {
+                       // Re-add onbeforeunload handler
+                       if ( !window.onbeforeunload ) {
+                               window.onbeforeunload = savedWindowOnBeforeUnload;
+                       }
+               } );
+       $( '#mw-prefs-form' ).submit( function () {
+               // Unbind our beforeunload handler
+               $( window ).off( '.prefswarning' );
+       } );
+       $( '#mw-prefs-restoreprefs' ).click( function () {
+               // Unbind our beforeunload handler
+               $( window ).off( '.prefswarning' );
+       } );
 } );