Improve "selfmove" message's wording
[lhc/web/wiklou.git] / resources / src / mediawiki.special / mediawiki.special.preferences.js
1 /*!
2 * JavaScript for Special:Preferences
3 */
4 ( function ( mw, $ ) {
5 $( function () {
6 var $preftoc, $preferences, $fieldsets, labelFunc, previousTab,
7 $tzSelect, $tzTextbox, $localtimeHolder, servertime, allowCloseWindow,
8 convertmessagebox = require( 'mediawiki.notification.convertmessagebox' );
9
10 labelFunc = function () {
11 return this.id.replace( /^mw-prefsection/g, 'preftab' );
12 };
13
14 $( '#prefsubmit' ).attr( 'id', 'prefcontrol' );
15 $preftoc = $( '#preftoc' );
16 $preferences = $( '#preferences' );
17
18 $fieldsets = $preferences.children( 'fieldset' )
19 .attr( {
20 role: 'tabpanel',
21 'aria-labelledby': labelFunc
22 } );
23 $fieldsets.not( '#mw-prefsection-personal' )
24 .hide()
25 .attr( 'aria-hidden', 'true' );
26
27 // T115692: The following is kept for backwards compatibility with older skins
28 $preferences.addClass( 'jsprefs' );
29 $fieldsets.addClass( 'prefsection' );
30 $fieldsets.children( 'legend' ).addClass( 'mainLegend' );
31
32 // Make sure the accessibility tip is selectable so that screen reader users take notice,
33 // but hide it per default to reduce interface clutter. Also make sure it becomes visible
34 // when selected. Similar to jquery.mw-jump
35 $( '<div>' ).addClass( 'mw-navigation-hint' )
36 .text( mw.msg( 'prefs-tabs-navigation-hint' ) )
37 .attr( 'tabIndex', 0 )
38 .on( 'focus blur', function ( e ) {
39 if ( e.type === 'blur' || e.type === 'focusout' ) {
40 $( this ).css( 'height', '0' );
41 } else {
42 $( this ).css( 'height', 'auto' );
43 }
44 } ).insertBefore( $preftoc );
45
46 /**
47 * It uses document.getElementById for security reasons (HTML injections in $()).
48 *
49 * @ignore
50 * @param {string} name the name of a tab without the prefix ("mw-prefsection-")
51 * @param {string} [mode] A hash will be set according to the current
52 * open section. Set mode 'noHash' to surpress this.
53 */
54 function switchPrefTab( name, mode ) {
55 var $tab, scrollTop;
56 // Handle hash manually to prevent jumping,
57 // therefore save and restore scrollTop to prevent jumping.
58 scrollTop = $( window ).scrollTop();
59 if ( mode !== 'noHash' ) {
60 location.hash = '#mw-prefsection-' + name;
61 }
62 $( window ).scrollTop( scrollTop );
63
64 $preftoc.find( 'li' ).removeClass( 'selected' )
65 .find( 'a' ).attr( {
66 tabIndex: -1,
67 'aria-selected': 'false'
68 } );
69
70 $tab = $( document.getElementById( 'preftab-' + name ) );
71 if ( $tab.length ) {
72 $tab.attr( {
73 tabIndex: 0,
74 'aria-selected': 'true'
75 } )
76 .focus()
77 .parent().addClass( 'selected' );
78
79 $preferences.children( 'fieldset' ).hide().attr( 'aria-hidden', 'true' );
80 $( document.getElementById( 'mw-prefsection-' + name ) ).show().attr( 'aria-hidden', 'false' );
81 }
82 }
83
84 // Check for successbox to replace with notifications
85 convertmessagebox();
86
87 // Enable keyboard users to use left and right keys to switch tabs
88 $preftoc.on( 'keydown', function ( event ) {
89 var keyLeft = 37,
90 keyRight = 39,
91 $el;
92
93 if ( event.keyCode === keyLeft ) {
94 $el = $( '#preftoc li.selected' ).prev().find( 'a' );
95 } else if ( event.keyCode === keyRight ) {
96 $el = $( '#preftoc li.selected' ).next().find( 'a' );
97 } else {
98 return;
99 }
100 if ( $el.length > 0 ) {
101 switchPrefTab( $el.attr( 'href' ).replace( '#mw-prefsection-', '' ) );
102 }
103 } );
104
105 // Jump to correct section as indicated by the hash.
106 // This function is called onload and onhashchange.
107 function detectHash() {
108 var hash = location.hash,
109 matchedElement, parentSection;
110 if ( hash.match( /^#mw-prefsection-[\w\-]+/ ) ) {
111 mw.storage.session.remove( 'mwpreferences-prevTab' );
112 switchPrefTab( hash.replace( '#mw-prefsection-', '' ) );
113 } else if ( hash.match( /^#mw-[\w\-]+/ ) ) {
114 matchedElement = document.getElementById( hash.slice( 1 ) );
115 parentSection = $( matchedElement ).closest( '.prefsection' );
116 if ( parentSection.length ) {
117 mw.storage.session.remove( 'mwpreferences-prevTab' );
118 // Switch to proper tab and scroll to selected item.
119 switchPrefTab( parentSection.attr( 'id' ).replace( 'mw-prefsection-', '' ), 'noHash' );
120 matchedElement.scrollIntoView();
121 }
122 }
123 }
124
125 // In browsers that support the onhashchange event we will not bind click
126 // handlers and instead let the browser do the default behavior (clicking the
127 // <a href="#.."> will naturally set the hash, handled by onhashchange.
128 // But other things that change the hash will also be caught (e.g. using
129 // the Back and Forward browser navigation).
130 // Note the special check for IE "compatibility" mode.
131 if ( 'onhashchange' in window &&
132 ( document.documentMode === undefined || document.documentMode >= 8 )
133 ) {
134 $( window ).on( 'hashchange', function () {
135 var hash = location.hash;
136 if ( hash.match( /^#mw-[\w\-]+/ ) ) {
137 detectHash();
138 } else if ( hash === '' ) {
139 switchPrefTab( 'personal', 'noHash' );
140 }
141 } )
142 // Run the function immediately to select the proper tab on startup.
143 .trigger( 'hashchange' );
144 // In older browsers we'll bind a click handler as fallback.
145 // We must not have onhashchange *and* the click handlers, otherwise
146 // the click handler calls switchPrefTab() which sets the hash value,
147 // which triggers onhashchange and calls switchPrefTab() again.
148 } else {
149 $preftoc.on( 'click', 'li a', function ( e ) {
150 switchPrefTab( $( this ).attr( 'href' ).replace( '#mw-prefsection-', '' ) );
151 e.preventDefault();
152 } );
153 // If we've reloaded the page or followed an open-in-new-window,
154 // make the selected tab visible.
155 detectHash();
156 }
157
158 // Timezone functions.
159 // Guesses Timezone from browser and updates fields onchange.
160
161 $tzSelect = $( '#mw-input-wptimecorrection' );
162 $tzTextbox = $( '#mw-input-wptimecorrection-other' );
163 $localtimeHolder = $( '#wpLocalTime' );
164 servertime = parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
165
166 function minutesToHours( min ) {
167 var tzHour = Math.floor( Math.abs( min ) / 60 ),
168 tzMin = Math.abs( min ) % 60,
169 tzString = ( ( min >= 0 ) ? '' : '-' ) + ( ( tzHour < 10 ) ? '0' : '' ) + tzHour +
170 ':' + ( ( tzMin < 10 ) ? '0' : '' ) + tzMin;
171 return tzString;
172 }
173
174 function hoursToMinutes( hour ) {
175 var minutes,
176 arr = hour.split( ':' );
177
178 arr[ 0 ] = parseInt( arr[ 0 ], 10 );
179
180 if ( arr.length === 1 ) {
181 // Specification is of the form [-]XX
182 minutes = arr[ 0 ] * 60;
183 } else {
184 // Specification is of the form [-]XX:XX
185 minutes = Math.abs( arr[ 0 ] ) * 60 + parseInt( arr[ 1 ], 10 );
186 if ( arr[ 0 ] < 0 ) {
187 minutes *= -1;
188 }
189 }
190 // Gracefully handle non-numbers.
191 if ( isNaN( minutes ) ) {
192 return 0;
193 } else {
194 return minutes;
195 }
196 }
197
198 function updateTimezoneSelection() {
199 var minuteDiff, localTime,
200 type = $tzSelect.val();
201
202 if ( type === 'other' ) {
203 // User specified time zone manually in <input>
204 // Grab data from the textbox, parse it.
205 minuteDiff = hoursToMinutes( $tzTextbox.val() );
206 } else {
207 // Time zone not manually specified by user
208 if ( type === 'guess' ) {
209 // Get browser timezone & fill it in
210 minuteDiff = -( new Date().getTimezoneOffset() );
211 $tzTextbox.val( minutesToHours( minuteDiff ) );
212 $tzSelect.val( 'other' );
213 $tzTextbox.prop( 'disabled', false );
214 } else {
215 // Grab data from the $tzSelect value
216 minuteDiff = parseInt( type.split( '|' )[ 1 ], 10 ) || 0;
217 $tzTextbox.val( minutesToHours( minuteDiff ) );
218 }
219
220 // Set defaultValue prop on the generated box so we don't trigger the
221 // unsaved preferences check
222 $tzTextbox.prop( 'defaultValue', $tzTextbox.val() );
223 }
224
225 // Determine local time from server time and minutes difference, for display.
226 localTime = servertime + minuteDiff;
227
228 // Bring time within the [0,1440) range.
229 localTime = ( ( localTime % 1440 ) + 1440 ) % 1440;
230
231 $localtimeHolder.text( mw.language.convertNumber( minutesToHours( localTime ) ) );
232 }
233
234 if ( $tzSelect.length && $tzTextbox.length ) {
235 $tzSelect.change( updateTimezoneSelection );
236 $tzTextbox.blur( updateTimezoneSelection );
237 updateTimezoneSelection();
238 }
239
240 // Restore the active tab after saving the preferences
241 previousTab = mw.storage.session.get( 'mwpreferences-prevTab' );
242 if ( previousTab ) {
243 switchPrefTab( previousTab, 'noHash' );
244 // Deleting the key, the tab states should be reset until we press Save
245 mw.storage.session.remove( 'mwpreferences-prevTab' );
246 }
247
248 $( '#mw-prefs-form' ).on( 'submit', function () {
249 var value = $( $preftoc ).find( 'li.selected a' ).attr( 'id' ).replace( 'preftab-', '' );
250 mw.storage.session.set( 'mwpreferences-prevTab', value );
251 } );
252
253 // Check if all of the form values are unchanged
254 function isPrefsChanged() {
255 var inputs = $( '#mw-prefs-form :input[name]' ),
256 input, $input, inputType,
257 index, optIndex,
258 opt;
259
260 for ( index = 0; index < inputs.length; index++ ) {
261 input = inputs[ index ];
262 $input = $( input );
263
264 // Different types of inputs have different methods for accessing defaults
265 if ( $input.is( 'select' ) ) {
266 // <select> has the property defaultSelected for each option
267 for ( optIndex = 0; optIndex < input.options.length; optIndex++ ) {
268 opt = input.options[ optIndex ];
269 if ( opt.selected !== opt.defaultSelected ) {
270 return true;
271 }
272 }
273 } else if ( $input.is( 'input' ) ) { // <input> has defaultValue or defaultChecked
274 inputType = input.type;
275 if ( inputType === 'radio' || inputType === 'checkbox' ) {
276 if ( input.checked !== input.defaultChecked ) {
277 return true;
278 }
279 } else if ( input.value !== input.defaultValue ) {
280 return true;
281 }
282 }
283 }
284
285 return false;
286 }
287
288 // Disable the button to save preferences unless preferences have changed
289 // Check if preferences have been changed before JS has finished loading
290 if ( !isPrefsChanged() ) {
291 $( '#prefcontrol' ).prop( 'disabled', true );
292 $( '#preferences > fieldset' ).one( 'change keydown mousedown', function () {
293 $( '#prefcontrol' ).prop( 'disabled', false );
294 } );
295 }
296
297 // Set up a message to notify users if they try to leave the page without
298 // saving.
299 allowCloseWindow = mw.confirmCloseWindow( {
300 test: isPrefsChanged,
301 message: mw.msg( 'prefswarning-warning', mw.msg( 'saveprefs' ) ),
302 namespace: 'prefswarning'
303 } );
304 $( '#mw-prefs-form' ).submit( $.proxy( allowCloseWindow, 'release' ) );
305 $( '#mw-prefs-restoreprefs' ).click( $.proxy( allowCloseWindow, 'release' ) );
306 } );
307 }( mediaWiki, jQuery ) );