Merge "jquery.lengthLimit: Fix 'cut'/'paste' event handling"
[lhc/web/wiklou.git] / resources / src / 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, saveButton, restoreButton,
8 oouiEnabled = $( '#mw-prefs-form' ).hasClass( 'mw-htmlform-ooui' );
9
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,
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 if ( oouiEnabled ) {
49 saveButton = OO.ui.infuse( $( '#prefcontrol' ) );
50 restoreButton = OO.ui.infuse( $( '#mw-prefs-restoreprefs' ) );
51
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 $( '#preferences .oo-ui-fieldsetLayout' ).on( 'change keyup mouseup', function () {
56 saveButton.setDisabled( !isPrefsChanged() );
57 } );
58 } else {
59 // Disable the button to save preferences unless preferences have changed
60 // Check if preferences have been changed before JS has finished loading
61 $( '#prefcontrol' ).prop( 'disabled', !isPrefsChanged() );
62 $( '#preferences > fieldset' ).on( 'change keyup mouseup', function () {
63 $( '#prefcontrol' ).prop( 'disabled', !isPrefsChanged() );
64 } );
65 }
66
67 // Set up a message to notify users if they try to leave the page without
68 // saving.
69 allowCloseWindow = mw.confirmCloseWindow( {
70 test: isPrefsChanged,
71 message: mw.msg( 'prefswarning-warning', mw.msg( 'saveprefs' ) ),
72 namespace: 'prefswarning'
73 } );
74 $( '#mw-prefs-form' ).on( 'submit', $.proxy( allowCloseWindow, 'release' ) );
75 if ( oouiEnabled ) {
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 } else {
83 $( '#mw-prefs-restoreprefs' ).on( 'click', $.proxy( allowCloseWindow, 'release' ) );
84 }
85 } );
86 }( mediaWiki, jQuery ) );