Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[lhc/web/wiklou.git] / resources / src / mediawiki.special.preferences.ooui / confirmClose.js
1 /*!
2 * JavaScript for Special:Preferences: Enable save button and prevent the window being accidentally
3 * closed when any form field is changed.
4 */
5 ( function () {
6 $( function () {
7 var allowCloseWindow, saveButton, restoreButton;
8
9 // Check if all of the form values are unchanged.
10 // (This function could be changed to infuse and check OOUI widgets, but that would only make it
11 // slower and more complicated. It works fine to treat them as HTML elements.)
12 function isPrefsChanged() {
13 // eslint-disable-next-line no-jquery/no-sizzle
14 var inputs = $( '#mw-prefs-form :input[name]' ),
15 input, $input, inputType,
16 index, optIndex,
17 opt;
18
19 for ( index = 0; index < inputs.length; index++ ) {
20 input = inputs[ index ];
21 $input = $( input );
22
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 ) {
29 return true;
30 }
31 }
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 ) {
37 return true;
38 }
39 } else if ( input.value !== input.defaultValue ) {
40 return true;
41 }
42 }
43 }
44
45 return false;
46 }
47
48 saveButton = OO.ui.infuse( $( '#prefcontrol' ) );
49 restoreButton = OO.ui.infuse( $( '#mw-prefs-restoreprefs' ) );
50
51 // Disable the button to save preferences unless preferences have changed
52 // Check if preferences have been changed before JS has finished loading
53 saveButton.setDisabled( !isPrefsChanged() );
54 // Attach capturing event handlers to the document, to catch events inside OOUI dropdowns:
55 // * Use capture because OO.ui.SelectWidget also does, and it stops event propagation,
56 // so the event is not fired on descendant elements
57 // * Attach to the document because the dropdowns are in the .oo-ui-defaultOverlay element
58 // (and it doesn't exist yet at this point, so we can't attach them to it)
59 [ 'change', 'keyup', 'mouseup' ].forEach( function ( eventType ) {
60 document.addEventListener( eventType, function () {
61 // Make sure SelectWidget's event handlers run first
62 setTimeout( function () {
63 saveButton.setDisabled( !isPrefsChanged() );
64 } );
65 }, true );
66 } );
67
68 // Set up a message to notify users if they try to leave the page without
69 // saving.
70 allowCloseWindow = mw.confirmCloseWindow( {
71 test: isPrefsChanged,
72 message: mw.msg( 'prefswarning-warning', mw.msg( 'saveprefs' ) ),
73 namespace: 'prefswarning'
74 } );
75 $( '#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();
81 } );
82 } );
83 }() );