var $preftoc, $preferences, $fieldsets, $legends,
hash, labelFunc,
$tzSelect, $tzTextbox, $localtimeHolder, servertime,
- $checkBoxes;
+ $checkBoxes, savedWindowOnBeforeUnload;
labelFunc = function () {
return this.id.replace( /^mw-prefsection/g, 'preftab' );
// therefore save and restore scrollTop to prevent jumping.
scrollTop = $( window ).scrollTop();
if ( mode !== 'noHash' ) {
- window.location.hash = '#mw-prefsection-' + name;
+ location.hash = '#mw-prefsection-' + name;
}
$( window ).scrollTop( scrollTop );
// If we've reloaded the page or followed an open-in-new-window,
// make the selected tab visible.
- hash = window.location.hash;
+ hash = location.hash;
if ( hash.match( /^#mw-prefsection-[\w\-]+/ ) ) {
switchPrefTab( hash.replace( '#mw-prefsection-', '' ) );
}
( document.documentMode === undefined || document.documentMode >= 8 )
) {
$( window ).on( 'hashchange', function () {
- var hash = window.location.hash;
+ var hash = location.hash;
if ( hash.match( /^#mw-prefsection-[\w\-]+/ ) ) {
switchPrefTab( hash.replace( '#mw-prefsection-', '' ) );
} else if ( hash === '' ) {
$( '#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' );
+ } );
} );