2 * JavaScript for Special:Preferences: Enable save button and prevent the window being accidentally
3 * closed when any form field is changed.
9 // Check if all of the form values are unchanged
10 function isPrefsChanged() {
11 var inputs = $( '#mw-prefs-form :input[name]' ),
12 input, $input, inputType,
16 for ( index = 0; index < inputs.length; index++ ) {
17 input = inputs[ index ];
20 // Different types of inputs have different methods for accessing defaults
21 if ( $input.is( 'select' ) ) {
22 // <select> has the property defaultSelected for each option
23 for ( optIndex = 0; optIndex < input.options.length; optIndex++ ) {
24 opt = input.options[ optIndex ];
25 if ( opt.selected !== opt.defaultSelected ) {
29 } else if ( $input.is( 'input' ) ) { // <input> has defaultValue or defaultChecked
30 inputType = input.type;
31 if ( inputType === 'radio' || inputType === 'checkbox' ) {
32 if ( input.checked !== input.defaultChecked ) {
35 } else if ( input.value !== input.defaultValue ) {
44 // Disable the button to save preferences unless preferences have changed
45 // Check if preferences have been changed before JS has finished loading
46 if ( !isPrefsChanged() ) {
47 $( '#prefcontrol' ).prop( 'disabled', true );
48 $( '#preferences > fieldset' ).one( 'change keydown mousedown', function () {
49 $( '#prefcontrol' ).prop( 'disabled', false );
53 // Set up a message to notify users if they try to leave the page without
55 allowCloseWindow = mw.confirmCloseWindow( {
57 message: mw.msg( 'prefswarning-warning', mw.msg( 'saveprefs' ) ),
58 namespace: 'prefswarning'
60 $( '#mw-prefs-form' ).submit( $.proxy( allowCloseWindow, 'release' ) );
61 $( '#mw-prefs-restoreprefs' ).click( $.proxy( allowCloseWindow, 'release' ) );
63 }( mediaWiki, jQuery ) );