registration: Only allow one extension to set a specific config setting
[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 } ).focus()
76 .parent().addClass( 'selected' );
77
78 $preferences.children( 'fieldset' ).hide().attr( 'aria-hidden', 'true' );
79 $( document.getElementById( 'mw-prefsection-' + name ) ).show().attr( 'aria-hidden', 'false' );
80 }
81 }
82
83 // Check for successbox to replace with notifications
84 convertmessagebox();
85
86 // Enable keyboard users to use left and right keys to switch tabs
87 $preftoc.on( 'keydown', function ( event ) {
88 var keyLeft = 37,
89 keyRight = 39,
90 $el;
91
92 if ( event.keyCode === keyLeft ) {
93 $el = $( '#preftoc li.selected' ).prev().find( 'a' );
94 } else if ( event.keyCode === keyRight ) {
95 $el = $( '#preftoc li.selected' ).next().find( 'a' );
96 } else {
97 return;
98 }
99 if ( $el.length > 0 ) {
100 switchPrefTab( $el.attr( 'href' ).replace( '#mw-prefsection-', '' ) );
101 }
102 } );
103
104 // Jump to correct section as indicated by the hash.
105 // This function is called onload and onhashchange.
106 function detectHash() {
107 var hash = location.hash,
108 matchedElement, parentSection;
109 if ( hash.match( /^#mw-prefsection-[\w-]+/ ) ) {
110 mw.storage.session.remove( 'mwpreferences-prevTab' );
111 switchPrefTab( hash.replace( '#mw-prefsection-', '' ) );
112 } else if ( hash.match( /^#mw-[\w-]+/ ) ) {
113 matchedElement = document.getElementById( hash.slice( 1 ) );
114 parentSection = $( matchedElement ).closest( '.prefsection' );
115 if ( parentSection.length ) {
116 mw.storage.session.remove( 'mwpreferences-prevTab' );
117 // Switch to proper tab and scroll to selected item.
118 switchPrefTab( parentSection.attr( 'id' ).replace( 'mw-prefsection-', '' ), 'noHash' );
119 matchedElement.scrollIntoView();
120 }
121 }
122 }
123
124 // In browsers that support the onhashchange event we will not bind click
125 // handlers and instead let the browser do the default behavior (clicking the
126 // <a href="#.."> will naturally set the hash, handled by onhashchange.
127 // But other things that change the hash will also be caught (e.g. using
128 // the Back and Forward browser navigation).
129 // Note the special check for IE "compatibility" mode.
130 if ( 'onhashchange' in window &&
131 ( document.documentMode === undefined || document.documentMode >= 8 )
132 ) {
133 $( window ).on( 'hashchange', function () {
134 var hash = location.hash;
135 if ( hash.match( /^#mw-[\w-]+/ ) ) {
136 detectHash();
137 } else if ( hash === '' ) {
138 switchPrefTab( 'personal', 'noHash' );
139 }
140 } )
141 // Run the function immediately to select the proper tab on startup.
142 .trigger( 'hashchange' );
143 // In older browsers we'll bind a click handler as fallback.
144 // We must not have onhashchange *and* the click handlers, otherwise
145 // the click handler calls switchPrefTab() which sets the hash value,
146 // which triggers onhashchange and calls switchPrefTab() again.
147 } else {
148 $preftoc.on( 'click', 'li a', function ( e ) {
149 switchPrefTab( $( this ).attr( 'href' ).replace( '#mw-prefsection-', '' ) );
150 e.preventDefault();
151 } );
152 // If we've reloaded the page or followed an open-in-new-window,
153 // make the selected tab visible.
154 detectHash();
155 }
156
157 // Timezone functions.
158 // Guesses Timezone from browser and updates fields onchange.
159
160 $tzSelect = $( '#mw-input-wptimecorrection' );
161 $tzTextbox = $( '#mw-input-wptimecorrection-other' );
162 $localtimeHolder = $( '#wpLocalTime' );
163 servertime = parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
164
165 function minutesToHours( min ) {
166 var tzHour = Math.floor( Math.abs( min ) / 60 ),
167 tzMin = Math.abs( min ) % 60,
168 tzString = ( ( min >= 0 ) ? '' : '-' ) + ( ( tzHour < 10 ) ? '0' : '' ) + tzHour +
169 ':' + ( ( tzMin < 10 ) ? '0' : '' ) + tzMin;
170 return tzString;
171 }
172
173 function hoursToMinutes( hour ) {
174 var minutes,
175 arr = hour.split( ':' );
176
177 arr[ 0 ] = parseInt( arr[ 0 ], 10 );
178
179 if ( arr.length === 1 ) {
180 // Specification is of the form [-]XX
181 minutes = arr[ 0 ] * 60;
182 } else {
183 // Specification is of the form [-]XX:XX
184 minutes = Math.abs( arr[ 0 ] ) * 60 + parseInt( arr[ 1 ], 10 );
185 if ( arr[ 0 ] < 0 ) {
186 minutes *= -1;
187 }
188 }
189 // Gracefully handle non-numbers.
190 if ( isNaN( minutes ) ) {
191 return 0;
192 } else {
193 return minutes;
194 }
195 }
196
197 function updateTimezoneSelection() {
198 var minuteDiff, localTime,
199 type = $tzSelect.val();
200
201 if ( type === 'other' ) {
202 // User specified time zone manually in <input>
203 // Grab data from the textbox, parse it.
204 minuteDiff = hoursToMinutes( $tzTextbox.val() );
205 } else {
206 // Time zone not manually specified by user
207 if ( type === 'guess' ) {
208 // Get browser timezone & fill it in
209 minuteDiff = -( new Date().getTimezoneOffset() );
210 $tzTextbox.val( minutesToHours( minuteDiff ) );
211 $tzSelect.val( 'other' );
212 $tzTextbox.prop( 'disabled', false );
213 } else {
214 // Grab data from the $tzSelect value
215 minuteDiff = parseInt( type.split( '|' )[ 1 ], 10 ) || 0;
216 $tzTextbox.val( minutesToHours( minuteDiff ) );
217 }
218
219 // Set defaultValue prop on the generated box so we don't trigger the
220 // unsaved preferences check
221 $tzTextbox.prop( 'defaultValue', $tzTextbox.val() );
222 }
223
224 // Determine local time from server time and minutes difference, for display.
225 localTime = servertime + minuteDiff;
226
227 // Bring time within the [0,1440) range.
228 localTime = ( ( localTime % 1440 ) + 1440 ) % 1440;
229
230 $localtimeHolder.text( mw.language.convertNumber( minutesToHours( localTime ) ) );
231 }
232
233 if ( $tzSelect.length && $tzTextbox.length ) {
234 $tzSelect.change( updateTimezoneSelection );
235 $tzTextbox.blur( updateTimezoneSelection );
236 updateTimezoneSelection();
237 }
238
239 // Restore the active tab after saving the preferences
240 previousTab = mw.storage.session.get( 'mwpreferences-prevTab' );
241 if ( previousTab ) {
242 switchPrefTab( previousTab, 'noHash' );
243 // Deleting the key, the tab states should be reset until we press Save
244 mw.storage.session.remove( 'mwpreferences-prevTab' );
245 }
246
247 $( '#mw-prefs-form' ).on( 'submit', function () {
248 var value = $( $preftoc ).find( 'li.selected a' ).attr( 'id' ).replace( 'preftab-', '' );
249 mw.storage.session.set( 'mwpreferences-prevTab', value );
250 } );
251
252 // Check if all of the form values are unchanged
253 function isPrefsChanged() {
254 var inputs = $( '#mw-prefs-form :input[name]' ),
255 input, $input, inputType,
256 index, optIndex,
257 opt;
258
259 for ( index = 0; index < inputs.length; index++ ) {
260 input = inputs[ index ];
261 $input = $( input );
262
263 // Different types of inputs have different methods for accessing defaults
264 if ( $input.is( 'select' ) ) {
265 // <select> has the property defaultSelected for each option
266 for ( optIndex = 0; optIndex < input.options.length; optIndex++ ) {
267 opt = input.options[ optIndex ];
268 if ( opt.selected !== opt.defaultSelected ) {
269 return true;
270 }
271 }
272 } else if ( $input.is( 'input' ) ) { // <input> has defaultValue or defaultChecked
273 inputType = input.type;
274 if ( inputType === 'radio' || inputType === 'checkbox' ) {
275 if ( input.checked !== input.defaultChecked ) {
276 return true;
277 }
278 } else if ( input.value !== input.defaultValue ) {
279 return true;
280 }
281 }
282 }
283
284 return false;
285 }
286
287 // Disable the button to save preferences unless preferences have changed
288 // Check if preferences have been changed before JS has finished loading
289 if ( !isPrefsChanged() ) {
290 $( '#prefcontrol' ).prop( 'disabled', true );
291 $( '#preferences > fieldset' ).one( 'change keydown mousedown', function () {
292 $( '#prefcontrol' ).prop( 'disabled', false );
293 } );
294 }
295
296 // Set up a message to notify users if they try to leave the page without
297 // saving.
298 allowCloseWindow = mw.confirmCloseWindow( {
299 test: isPrefsChanged,
300 message: mw.msg( 'prefswarning-warning', mw.msg( 'saveprefs' ) ),
301 namespace: 'prefswarning'
302 } );
303 $( '#mw-prefs-form' ).submit( $.proxy( allowCloseWindow, 'release' ) );
304 $( '#mw-prefs-restoreprefs' ).click( $.proxy( allowCloseWindow, 'release' ) );
305 } );
306 }( mediaWiki, jQuery ) );