Merge "rdbms: make LBFactory "cliMode" check for phpdbg"
[lhc/web/wiklou.git] / resources / src / mediawiki.special / mediawiki.special.preferences.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 ( mw, $ ) {
6 $( function () {
7 var allowCloseWindow;
8
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,
13 index, optIndex,
14 opt;
15
16 for ( index = 0; index < inputs.length; index++ ) {
17 input = inputs[ index ];
18 $input = $( input );
19
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 ) {
26 return true;
27 }
28 }
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 ) {
33 return true;
34 }
35 } else if ( input.value !== input.defaultValue ) {
36 return true;
37 }
38 }
39 }
40
41 return false;
42 }
43
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() );
49 } );
50
51 // Set up a message to notify users if they try to leave the page without
52 // saving.
53 allowCloseWindow = mw.confirmCloseWindow( {
54 test: isPrefsChanged,
55 message: mw.msg( 'prefswarning-warning', mw.msg( 'saveprefs' ) ),
56 namespace: 'prefswarning'
57 } );
58 $( '#mw-prefs-form' ).submit( $.proxy( allowCloseWindow, 'release' ) );
59 $( '#mw-prefs-restoreprefs' ).click( $.proxy( allowCloseWindow, 'release' ) );
60 } );
61 }( mediaWiki, jQuery ) );