2 * JavaScript for Special:Preferences: Enable save button and prevent the window being accidentally
3 * closed when any form field is changed.
7 var allowCloseWindow
, saveButton
, restoreButton
,
8 oouiEnabled
= $( '#mw-prefs-form' ).hasClass( 'mw-htmlform-ooui' );
10 // Check if all of the form values are unchanged.
11 // (This function could be changed to infuse and check OOUI widgets, but that would only make it
12 // slower and more complicated. It works fine to treat them as HTML elements.)
13 function isPrefsChanged() {
14 var inputs
= $( '#mw-prefs-form :input[name]' ),
15 input
, $input
, inputType
,
19 for ( index
= 0; index
< inputs
.length
; index
++ ) {
20 input
= inputs
[ index
];
23 // Different types of inputs have different methods for accessing defaults
24 if ( $input
.is( 'select' ) ) {
25 // <select> has the property defaultSelected for each option
26 for ( optIndex
= 0; optIndex
< input
.options
.length
; optIndex
++ ) {
27 opt
= input
.options
[ optIndex
];
28 if ( opt
.selected
!== opt
.defaultSelected
) {
32 } else if ( $input
.is( 'input' ) || $input
.is( 'textarea' ) ) {
33 // <input> has defaultValue or defaultChecked
34 inputType
= input
.type
;
35 if ( inputType
=== 'radio' || inputType
=== 'checkbox' ) {
36 if ( input
.checked
!== input
.defaultChecked
) {
39 } else if ( input
.value
!== input
.defaultValue
) {
49 saveButton
= OO
.ui
.infuse( $( '#prefcontrol' ) );
50 restoreButton
= OO
.ui
.infuse( $( '#mw-prefs-restoreprefs' ) );
52 // Disable the button to save preferences unless preferences have changed
53 // Check if preferences have been changed before JS has finished loading
54 saveButton
.setDisabled( !isPrefsChanged() );
55 $( '#preferences .oo-ui-fieldsetLayout' ).on( 'change keyup mouseup', function () {
56 saveButton
.setDisabled( !isPrefsChanged() );
59 // Disable the button to save preferences unless preferences have changed
60 // Check if preferences have been changed before JS has finished loading
61 $( '#prefcontrol' ).prop( 'disabled', !isPrefsChanged() );
62 $( '#preferences > fieldset' ).on( 'change keyup mouseup', function () {
63 $( '#prefcontrol' ).prop( 'disabled', !isPrefsChanged() );
67 // Set up a message to notify users if they try to leave the page without
69 allowCloseWindow
= mw
.confirmCloseWindow( {
71 message
: mw
.msg( 'prefswarning-warning', mw
.msg( 'saveprefs' ) ),
72 namespace: 'prefswarning'
74 $( '#mw-prefs-form' ).on( 'submit', allowCloseWindow
.release
);
76 restoreButton
.on( 'click', function () {
77 allowCloseWindow
.release();
78 // The default behavior of events in OOUI is always prevented. Follow the link manually.
79 // Note that middle-click etc. still works, as it doesn't emit a OOUI 'click' event.
80 location
.href
= restoreButton
.getHref();
83 $( '#mw-prefs-restoreprefs' ).on( 'click', allowCloseWindow
.release
);
86 }( mediaWiki
, jQuery
) );