Merge "Improve "selfmove" message's wording"
[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 if ( !isPrefsChanged() ) {
47 $( '#prefcontrol' ).prop( 'disabled', true );
48 $( '#preferences > fieldset' ).one( 'change keydown mousedown', function () {
49 $( '#prefcontrol' ).prop( 'disabled', false );
50 } );
51 }
52
53 // Set up a message to notify users if they try to leave the page without
54 // saving.
55 allowCloseWindow = mw.confirmCloseWindow( {
56 test: isPrefsChanged,
57 message: mw.msg( 'prefswarning-warning', mw.msg( 'saveprefs' ) ),
58 namespace: 'prefswarning'
59 } );
60 $( '#mw-prefs-form' ).submit( $.proxy( allowCloseWindow, 'release' ) );
61 $( '#mw-prefs-restoreprefs' ).click( $.proxy( allowCloseWindow, 'release' ) );
62 } );
63 }( mediaWiki, jQuery ) );