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 $( '#prefcontrol' ).prop( 'disabled', !isPrefsChanged() );
47 $( '#preferences > fieldset' ).on( 'change keyup mouseup', function () {
48 $( '#prefcontrol' ).prop( 'disabled', !isPrefsChanged() );
51 // Set up a message to notify users if they try to leave the page without
53 allowCloseWindow
= mw
.confirmCloseWindow( {
55 message
: mw
.msg( 'prefswarning-warning', mw
.msg( 'saveprefs' ) ),
56 namespace: 'prefswarning'
58 $( '#mw-prefs-form' ).submit( $.proxy( allowCloseWindow
, 'release' ) );
59 $( '#mw-prefs-restoreprefs' ).click( $.proxy( allowCloseWindow
, 'release' ) );
61 }( mediaWiki
, jQuery
) );