2 * JavaScript for Special:Preferences
4 jQuery( function ( $ ) {
5 var $preftoc, $preferences, $fieldsets, $legends,
7 $tzSelect, $tzTextbox, $localtimeHolder, servertime,
8 $checkBoxes, savedWindowOnBeforeUnload;
10 labelFunc = function () {
11 return this.id.replace( /^mw-prefsection/g, 'preftab' );
14 $( '#prefsubmit' ).attr( 'id', 'prefcontrol' );
15 $preftoc = $( '<ul id="preftoc"></ul>' )
16 .attr( 'role', 'tablist' );
17 $preferences = $( '#preferences' )
18 .addClass( 'jsprefs' )
20 $fieldsets = $preferences.children( 'fieldset' )
24 'aria-hidden': 'true',
25 'aria-labelledby': labelFunc
27 .addClass( 'prefsection' );
30 .addClass( 'mainLegend' );
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( mediaWiki.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' );
42 $( this ).css( 'height', 'auto' );
44 } ).insertBefore( $preftoc );
47 * It uses document.getElementById for security reasons (HTML injections in $()).
50 * @param String name: the name of a tab without the prefix ("mw-prefsection-")
51 * @param String mode: [optional] A hash will be set according to the current
52 * open section. Set mode 'noHash' to surpress this.
54 function switchPrefTab( name, mode ) {
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;
62 $( window ).scrollTop( scrollTop );
64 $preftoc.find( 'li' ).removeClass( 'selected' )
67 'aria-selected': 'false'
70 $tab = $( document.getElementById( 'preftab-' + name ) );
74 'aria-selected': 'true'
77 .parent().addClass( 'selected' );
79 $preferences.children( 'fieldset' ).hide().attr( 'aria-hidden', 'true' );
80 $( document.getElementById( 'mw-prefsection-' + name ) ).show().attr( 'aria-hidden', 'false' );
84 // Populate the prefToc
85 $legends.each( function ( i, legend ) {
86 var $legend = $( legend ),
89 $legend.parent().show();
91 ident = $legend.parent().attr( 'id' );
94 .attr( 'role', 'presentation' )
95 .addClass( i === 0 ? 'selected' : '' );
98 id: ident.replace( 'mw-prefsection', 'preftab' ),
101 tabIndex: i === 0 ? 0 : -1,
102 'aria-selected': i === 0 ? 'true' : 'false',
103 'aria-controls': ident
105 .text( $legend.text() );
107 $preftoc.append( $li );
110 // Enable keyboard users to use left and right keys to switch tabs
111 $preftoc.on( 'keydown', function ( event ) {
116 if ( event.keyCode === keyLeft ) {
117 $el = $( '#preftoc li.selected' ).prev().find( 'a' );
118 } else if ( event.keyCode === keyRight ) {
119 $el = $( '#preftoc li.selected' ).next().find( 'a' );
123 if ( $el.length > 0 ) {
124 switchPrefTab( $el.attr( 'href' ).replace( '#mw-prefsection-', '' ) );
128 // If we've reloaded the page or followed an open-in-new-window,
129 // make the selected tab visible.
130 hash = location.hash;
131 if ( hash.match( /^#mw-prefsection-[\w\-]+/ ) ) {
132 switchPrefTab( hash.replace( '#mw-prefsection-', '' ) );
135 // In browsers that support the onhashchange event we will not bind click
136 // handlers and instead let the browser do the default behavior (clicking the
137 // <a href="#.."> will naturally set the hash, handled by onhashchange.
138 // But other things that change the hash will also be catched (e.g. using
139 // the Back and Forward browser navigation).
140 // Note the special check for IE "compatibility" mode.
141 if ( 'onhashchange' in window &&
142 ( document.documentMode === undefined || document.documentMode >= 8 )
144 $( window ).on( 'hashchange', function () {
145 var hash = location.hash;
146 if ( hash.match( /^#mw-prefsection-[\w\-]+/ ) ) {
147 switchPrefTab( hash.replace( '#mw-prefsection-', '' ) );
148 } else if ( hash === '' ) {
149 switchPrefTab( 'personal', 'noHash' );
152 // In older browsers we'll bind a click handler as fallback.
153 // We must not have onhashchange *and* the click handlers, other wise
154 // the click handler calls switchPrefTab() which sets the hash value,
155 // which triggers onhashcange and calls switchPrefTab() again.
157 $preftoc.on( 'click', 'li a', function ( e ) {
158 switchPrefTab( $( this ).attr( 'href' ).replace( '#mw-prefsection-', '' ) );
163 // Timezone functions.
164 // Guesses Timezone from browser and updates fields onchange.
166 $tzSelect = $( '#mw-input-wptimecorrection' );
167 $tzTextbox = $( '#mw-input-wptimecorrection-other' );
168 $localtimeHolder = $( '#wpLocalTime' );
169 servertime = parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
171 function minutesToHours( min ) {
172 var tzHour = Math.floor( Math.abs( min ) / 60 ),
173 tzMin = Math.abs( min ) % 60,
174 tzString = ( ( min >= 0 ) ? '' : '-' ) + ( ( tzHour < 10 ) ? '0' : '' ) + tzHour +
175 ':' + ( ( tzMin < 10 ) ? '0' : '' ) + tzMin;
179 function hoursToMinutes( hour ) {
181 arr = hour.split( ':' );
183 arr[0] = parseInt( arr[0], 10 );
185 if ( arr.length === 1 ) {
186 // Specification is of the form [-]XX
187 minutes = arr[0] * 60;
189 // Specification is of the form [-]XX:XX
190 minutes = Math.abs( arr[0] ) * 60 + parseInt( arr[1], 10 );
195 // Gracefully handle non-numbers.
196 if ( isNaN( minutes ) ) {
203 function updateTimezoneSelection() {
204 var minuteDiff, localTime,
205 type = $tzSelect.val();
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 if ( type === 'other' ) {
214 // Grab data from the textbox, parse it.
215 minuteDiff = hoursToMinutes( $tzTextbox.val() );
217 // Grab data from the $tzSelect value
218 minuteDiff = parseInt( type.split( '|' )[1], 10 ) || 0;
219 $tzTextbox.val( minutesToHours( minuteDiff ) );
222 // Determine local time from server time and minutes difference, for display.
223 localTime = servertime + minuteDiff;
225 // Bring time within the [0,1440) range.
226 while ( localTime < 0 ) {
229 while ( localTime >= 1440 ) {
232 $localtimeHolder.text( mediaWiki.language.convertNumber( minutesToHours( localTime ) ) );
235 if ( $tzSelect.length && $tzTextbox.length ) {
236 $tzSelect.change( updateTimezoneSelection );
237 $tzTextbox.blur( updateTimezoneSelection );
238 updateTimezoneSelection();
241 // Preserve the tab after saving the preferences
242 // Not using cookies, because their deletion results are inconsistent.
243 // Not using jStorage due to its enormous size (for this feature)
244 if ( window.sessionStorage ) {
245 if ( sessionStorage.getItem( 'mediawikiPreferencesTab' ) !== null ) {
246 switchPrefTab( sessionStorage.getItem( 'mediawikiPreferencesTab' ), 'noHash' );
248 // Deleting the key, the tab states should be reset until we press Save
249 sessionStorage.removeItem( 'mediawikiPreferencesTab' );
251 $( '#mw-prefs-form' ).submit( function () {
252 var storageData = $( $preftoc ).find( 'li.selected a' ).attr( 'id' ).replace( 'preftab-', '' );
253 sessionStorage.setItem( 'mediawikiPreferencesTab', storageData );
257 // To disable all 'namespace' checkboxes in Search preferences
258 // when 'Search in all namespaces' checkbox is ticked.
259 $checkBoxes = $( '#mw-htmlform-advancedsearchoptions input[id^=mw-input-wpsearchnamespaces]' );
260 if ( $( '#mw-input-wpsearcheverything' ).prop( 'checked' ) ) {
261 $checkBoxes.prop( 'disabled', true );
263 $( '#mw-input-wpsearcheverything' ).change( function () {
264 $checkBoxes.prop( 'disabled', $( this ).prop( 'checked' ) );
267 // Set up a message to notify users if they try to leave the page without
269 $( '#mw-prefs-form' ).data( 'origdata', $( '#mw-prefs-form' ).serialize() );
271 .on( 'beforeunload.prefswarning', function () {
274 // Check if anything changed
275 if ( $( '#mw-prefs-form' ).serialize() !== $( '#mw-prefs-form' ).data( 'origdata' ) ) {
276 // Return our message
277 retval = mediaWiki.msg( 'prefswarning-warning', mediaWiki.msg( 'saveprefs' ) );
280 // Unset the onbeforeunload handler so we don't break page caching in Firefox
281 savedWindowOnBeforeUnload = window.onbeforeunload;
282 window.onbeforeunload = null;
283 if ( retval !== undefined ) {
284 // ...but if the user chooses not to leave the page, we need to rebind it
285 setTimeout( function () {
286 window.onbeforeunload = savedWindowOnBeforeUnload;
291 .on( 'pageshow.prefswarning', function () {
292 // Re-add onbeforeunload handler
293 if ( !window.onbeforeunload ) {
294 window.onbeforeunload = savedWindowOnBeforeUnload;
297 $( '#mw-prefs-form' ).submit( function () {
298 // Unbind our beforeunload handler
299 $( window ).off( '.prefswarning' );
301 $( '#mw-prefs-restoreprefs' ).click( function () {
302 // Unbind our beforeunload handler
303 $( window ).off( '.prefswarning' );