2 * JavaScript for Special:Preferences: Enable save button and prevent the window being accidentally
3 * closed when any form field is changed.
7 // Check if all of the form values are unchanged.
8 // (This function could be changed to infuse and check OOUI widgets, but that would only make it
9 // slower and more complicated. It works fine to treat them as HTML elements.)
10 function isPrefsChanged() {
11 // eslint-disable-next-line no-jquery/no-sizzle
12 const $inputs = $( '#mw-prefs-form :input[name]' );
14 for ( let index = 0; index < $inputs.length; index++ ) {
15 const input = $inputs[ index ];
16 const $input = $( input );
18 // Different types of inputs have different methods for accessing defaults
19 if ( $input.is( 'select' ) ) {
20 // <select> has the property defaultSelected for each option
21 for ( let optIndex = 0; optIndex < input.options.length; optIndex++ ) {
22 const opt = input.options[ optIndex ];
23 if ( opt.selected !== opt.defaultSelected ) {
27 } else if ( $input.is( 'input' ) || $input.is( 'textarea' ) ) {
28 // <input> has defaultValue or defaultChecked
29 const inputType = input.type;
30 if ( inputType === 'radio' || inputType === 'checkbox' ) {
31 if ( input.checked !== input.defaultChecked ) {
34 } else if ( input.value !== input.defaultValue ) {
43 const saveButton = OO.ui.infuse( $( '#prefcontrol' ) );
45 // Disable the button to save preferences unless preferences have changed
46 // Check if preferences have been changed before JS has finished loading
47 saveButton.setDisabled( !isPrefsChanged() );
48 // Attach capturing event handlers to the document, to catch events inside OOUI dropdowns:
49 // * Use capture because OO.ui.SelectWidget also does, and it stops event propagation,
50 // so the event is not fired on descendant elements
51 // * Attach to the document because the dropdowns are in the .oo-ui-defaultOverlay element
52 // (and it doesn't exist yet at this point, so we can't attach them to it)
53 [ 'change', 'keyup', 'mouseup' ].forEach( ( eventType ) => {
54 document.addEventListener( eventType, () => {
55 // Make sure SelectWidget's event handlers run first
57 saveButton.setDisabled( !isPrefsChanged() );
62 // Prompt users if they try to leave the page without saving.
63 const allowCloseWindow = mw.confirmCloseWindow( {
66 $( '#mw-prefs-form' ).on( 'submit', allowCloseWindow.release );