2 * JavaScript for Special:Preferences
4 jQuery( function ( $ ) {
5 var $preftoc, $preferences, $fieldsets, $legends,
7 $tzSelect, $tzTextbox, $localtimeHolder, servertime;
9 labelFunc = function () {
10 return this.id.replace( /^mw-prefsection/g, 'preftab' );
13 $( '#prefsubmit' ).attr( 'id', 'prefcontrol' );
14 $preftoc = $('<ul id="preftoc"></ul>')
15 .attr( 'role', 'tablist' );
16 $preferences = $( '#preferences' )
17 .addClass( 'jsprefs' )
19 $fieldsets = $preferences.children( 'fieldset' )
23 'aria-hidden': 'true',
24 'aria-labelledby': labelFunc
26 .addClass( 'prefsection' );
29 .addClass( 'mainLegend' );
31 // Make sure the accessibility tip is selectable so that screen reader users take notice,
32 // but hide it per default to reduce interface clutter. Also make sure it becomes visible
33 // when selected. Similar to jquery.mw-jump
34 $( '<div>' ).addClass( 'mw-navigation-hint' )
35 .text( mediaWiki.msg( 'prefs-tabs-navigation-hint' ) )
36 .attr( 'tabIndex', 0 )
37 .on( 'focus blur', function( e ) {
38 if ( e.type === 'blur' || e.type === 'focusout' ) {
39 $( this ).css( 'height', '0' );
41 $( this ).css( 'height', 'auto' );
43 } ).insertBefore( $preftoc );
46 * It uses document.getElementById for security reasons (HTML injections in $()).
48 * @param String name: the name of a tab without the prefix ("mw-prefsection-")
49 * @param String mode: [optional] A hash will be set according to the current
50 * open section. Set mode 'noHash' to surpress this.
52 function switchPrefTab( name, mode ) {
54 // Handle hash manually to prevent jumping,
55 // therefore save and restore scrollTop to prevent jumping.
56 scrollTop = $( window ).scrollTop();
57 if ( mode !== 'noHash' ) {
58 window.location.hash = '#mw-prefsection-' + name;
60 $( window ).scrollTop( scrollTop );
62 $preftoc.find( 'li' ).removeClass( 'selected' )
65 'aria-selected': 'false'
68 $tab = $( document.getElementById( 'preftab-' + name ) );
72 'aria-selected': 'true'
75 .parent().addClass( 'selected' );
77 $preferences.children( 'fieldset' ).hide().attr( 'aria-hidden', 'true' );
78 $( document.getElementById( 'mw-prefsection-' + name ) ).show().attr( 'aria-hidden', 'false' );
82 // Populate the prefToc
83 $legends.each( function ( i, legend ) {
84 var $legend = $(legend),
87 $legend.parent().show();
89 ident = $legend.parent().attr( 'id' );
92 .attr( 'role', 'presentation' )
93 .addClass( i === 0 ? 'selected' : '' );
96 id: ident.replace( 'mw-prefsection', 'preftab' ),
99 tabIndex: i === 0 ? 0 : -1,
100 'aria-selected': i === 0 ? 'true' : 'false',
101 'aria-controls': ident
103 .text( $legend.text() );
105 $preftoc.append( $li );
108 // Enable keyboard users to use left and right keys to switch tabs
109 $preftoc.on( 'keydown', function( event ) {
114 if( event.keyCode === keyLeft ) {
115 $el = $( '#preftoc li.selected' ).prev().find( 'a' );
116 } else if ( event.keyCode === keyRight ) {
117 $el = $( '#preftoc li.selected' ).next().find( 'a' );
121 if ( $el.length > 0 ) {
122 switchPrefTab( $el.attr( 'href' ).replace( '#mw-prefsection-', '' ) );
126 // If we've reloaded the page or followed an open-in-new-window,
127 // make the selected tab visible.
128 hash = window.location.hash;
129 if ( hash.match( /^#mw-prefsection-[\w\-]+/ ) ) {
130 switchPrefTab( hash.replace( '#mw-prefsection-' , '' ) );
133 // In browsers that support the onhashchange event we will not bind click
134 // handlers and instead let the browser do the default behavior (clicking the
135 // <a href="#.."> will naturally set the hash, handled by onhashchange.
136 // But other things that change the hash will also be catched (e.g. using
137 // the Back and Forward browser navigation).
138 // Note the special check for IE "compatibility" mode.
139 if ( 'onhashchange' in window &&
140 ( document.documentMode === undefined || document.documentMode >= 8 )
142 $(window).on( 'hashchange' , function () {
143 var hash = window.location.hash;
144 if ( hash.match( /^#mw-prefsection-[\w\-]+/ ) ) {
145 switchPrefTab( hash.replace( '#mw-prefsection-', '' ) );
146 } else if ( hash === '' ) {
147 switchPrefTab( 'personal', 'noHash' );
150 // In older browsers we'll bind a click handler as fallback.
151 // We must not have onhashchange *and* the click handlers, other wise
152 // the click handler calls switchPrefTab() which sets the hash value,
153 // which triggers onhashcange and calls switchPrefTab() again.
155 $preftoc.on( 'click', 'li a', function ( e ) {
156 switchPrefTab( $( this ).attr( 'href' ).replace( '#mw-prefsection-', '' ) );
162 * Timezone functions.
163 * 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( 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 );