Merge "ParserTestRunner: Fix some documentation types"
[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.is( 'textarea' ) ) {
30 // <input> has defaultValue or defaultChecked
31 inputType = input.type;
32 if ( inputType === 'radio' || inputType === 'checkbox' ) {
33 if ( input.checked !== input.defaultChecked ) {
34 return true;
35 }
36 } else if ( input.value !== input.defaultValue ) {
37 return true;
38 }
39 }
40 }
41
42 return false;
43 }
44
45 // Disable the button to save preferences unless preferences have changed
46 // Check if preferences have been changed before JS has finished loading
47 $( '#prefcontrol' ).prop( 'disabled', !isPrefsChanged() );
48 $( '#preferences > fieldset' ).on( 'change keyup mouseup', function () {
49 $( '#prefcontrol' ).prop( 'disabled', !isPrefsChanged() );
50 } );
51
52 // Set up a message to notify users if they try to leave the page without
53 // saving.
54 allowCloseWindow = mw.confirmCloseWindow( {
55 test: isPrefsChanged,
56 message: mw.msg( 'prefswarning-warning', mw.msg( 'saveprefs' ) ),
57 namespace: 'prefswarning'
58 } );
59 $( '#mw-prefs-form' ).submit( $.proxy( allowCloseWindow, 'release' ) );
60 $( '#mw-prefs-restoreprefs' ).click( $.proxy( allowCloseWindow, 'release' ) );
61 } );
62 }( mediaWiki, jQuery ) );