c092682984760428a3f651c5a594988727136f5a
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 // Attach capturing event handlers to the document, to catch events inside OOUI dropdowns:
56 // * Use capture because OO.ui.SelectWidget also does, and it stops event propagation,
57 // so the event is not fired on descendant elements
58 // * Attach to the document because the dropdowns are in the .oo-ui-defaultOverlay element
59 // (and it doesn't exist yet at this point, so we can't attach them to it)
60 [ 'change', 'keyup', 'mouseup' ].forEach( function ( eventType
) {
61 document
.addEventListener( eventType
, function () {
62 // Make sure SelectWidget's event handlers run first
63 setTimeout( function () {
64 saveButton
.setDisabled( !isPrefsChanged() );
69 // Disable the button to save preferences unless preferences have changed
70 // Check if preferences have been changed before JS has finished loading
71 $( '#prefcontrol' ).prop( 'disabled', !isPrefsChanged() );
72 $( '#preferences > fieldset' ).on( 'change keyup mouseup', function () {
73 $( '#prefcontrol' ).prop( 'disabled', !isPrefsChanged() );
77 // Set up a message to notify users if they try to leave the page without
79 allowCloseWindow
= mw
.confirmCloseWindow( {
81 message
: mw
.msg( 'prefswarning-warning', mw
.msg( 'saveprefs' ) ),
82 namespace: 'prefswarning'
84 $( '#mw-prefs-form' ).on( 'submit', allowCloseWindow
.release
);
86 restoreButton
.on( 'click', function () {
87 allowCloseWindow
.release();
88 // The default behavior of events in OOUI is always prevented. Follow the link manually.
89 // Note that middle-click etc. still works, as it doesn't emit a OOUI 'click' event.
90 location
.href
= restoreButton
.getHref();
93 $( '#mw-prefs-restoreprefs' ).on( 'click', allowCloseWindow
.release
);
96 }( mediaWiki
, jQuery
) );