2 * JavaScript for Special:Preferences
4 jQuery( function ( $ ) {
5 var $preftoc
, $preferences
, $fieldsets
, $legends
,
7 $tzSelect
, $tzTextbox
, $localtimeHolder
, servertime
,
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 $()).
49 * @param String name: the name of a tab without the prefix ("mw-prefsection-")
50 * @param String mode: [optional] A hash will be set according to the current
51 * open section. Set mode 'noHash' to surpress this.
53 function switchPrefTab( name
, mode
) {
55 // Handle hash manually to prevent jumping,
56 // therefore save and restore scrollTop to prevent jumping.
57 scrollTop
= $( window
).scrollTop();
58 if ( mode
!== 'noHash' ) {
59 window
.location
.hash
= '#mw-prefsection-' + name
;
61 $( window
).scrollTop( scrollTop
);
63 $preftoc
.find( 'li' ).removeClass( 'selected' )
66 'aria-selected': 'false'
69 $tab
= $( document
.getElementById( 'preftab-' + name
) );
73 'aria-selected': 'true'
76 .parent().addClass( 'selected' );
78 $preferences
.children( 'fieldset' ).hide().attr( 'aria-hidden', 'true' );
79 $( document
.getElementById( 'mw-prefsection-' + name
) ).show().attr( 'aria-hidden', 'false' );
83 // Populate the prefToc
84 $legends
.each( function ( i
, legend
) {
85 var $legend
= $( legend
),
88 $legend
.parent().show();
90 ident
= $legend
.parent().attr( 'id' );
93 .attr( 'role', 'presentation' )
94 .addClass( i
=== 0 ? 'selected' : '' );
97 id
: ident
.replace( 'mw-prefsection', 'preftab' ),
100 tabIndex
: i
=== 0 ? 0 : -1,
101 'aria-selected': i
=== 0 ? 'true' : 'false',
102 'aria-controls': ident
104 .text( $legend
.text() );
106 $preftoc
.append( $li
);
109 // Enable keyboard users to use left and right keys to switch tabs
110 $preftoc
.on( 'keydown', function ( event
) {
115 if ( event
.keyCode
=== keyLeft
) {
116 $el
= $( '#preftoc li.selected' ).prev().find( 'a' );
117 } else if ( event
.keyCode
=== keyRight
) {
118 $el
= $( '#preftoc li.selected' ).next().find( 'a' );
122 if ( $el
.length
> 0 ) {
123 switchPrefTab( $el
.attr( 'href' ).replace( '#mw-prefsection-', '' ) );
127 // If we've reloaded the page or followed an open-in-new-window,
128 // make the selected tab visible.
129 hash
= window
.location
.hash
;
130 if ( hash
.match( /^#mw-prefsection-[\w\-]+/ ) ) {
131 switchPrefTab( hash
.replace( '#mw-prefsection-', '' ) );
134 // In browsers that support the onhashchange event we will not bind click
135 // handlers and instead let the browser do the default behavior (clicking the
136 // <a href="#.."> will naturally set the hash, handled by onhashchange.
137 // But other things that change the hash will also be catched (e.g. using
138 // the Back and Forward browser navigation).
139 // Note the special check for IE "compatibility" mode.
140 if ( 'onhashchange' in window
&&
141 ( document
.documentMode
=== undefined || document
.documentMode
>= 8 )
143 $( window
).on( 'hashchange', function () {
144 var hash
= window
.location
.hash
;
145 if ( hash
.match( /^#mw-prefsection-[\w\-]+/ ) ) {
146 switchPrefTab( hash
.replace( '#mw-prefsection-', '' ) );
147 } else if ( hash
=== '' ) {
148 switchPrefTab( 'personal', 'noHash' );
151 // In older browsers we'll bind a click handler as fallback.
152 // We must not have onhashchange *and* the click handlers, other wise
153 // the click handler calls switchPrefTab() which sets the hash value,
154 // which triggers onhashcange and calls switchPrefTab() again.
156 $preftoc
.on( 'click', 'li a', function ( e
) {
157 switchPrefTab( $( this ).attr( 'href' ).replace( '#mw-prefsection-', '' ) );
163 * Timezone functions.
164 * Guesses Timezone from browser and updates fields onchange
167 $tzSelect
= $( '#mw-input-wptimecorrection' );
168 $tzTextbox
= $( '#mw-input-wptimecorrection-other' );
169 $localtimeHolder
= $( '#wpLocalTime' );
170 servertime
= parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
172 function minutesToHours( min
) {
173 var tzHour
= Math
.floor( Math
.abs( min
) / 60 ),
174 tzMin
= Math
.abs( min
) % 60,
175 tzString
= ( ( min
>= 0 ) ? '' : '-' ) + ( ( tzHour
< 10 ) ? '0' : '' ) + tzHour
+
176 ':' + ( ( tzMin
< 10 ) ? '0' : '' ) + tzMin
;
180 function hoursToMinutes( hour
) {
182 arr
= hour
.split( ':' );
184 arr
[0] = parseInt( arr
[0], 10 );
186 if ( arr
.length
=== 1 ) {
187 // Specification is of the form [-]XX
188 minutes
= arr
[0] * 60;
190 // Specification is of the form [-]XX:XX
191 minutes
= Math
.abs( arr
[0] ) * 60 + parseInt( arr
[1], 10 );
196 // Gracefully handle non-numbers.
197 if ( isNaN( minutes
) ) {
204 function updateTimezoneSelection () {
205 var minuteDiff
, localTime
,
206 type
= $tzSelect
.val();
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 if ( type
=== 'other' ) {
215 // Grab data from the textbox, parse it.
216 minuteDiff
= hoursToMinutes( $tzTextbox
.val() );
218 // Grab data from the $tzSelect value
219 minuteDiff
= parseInt( type
.split( '|' )[1], 10 ) || 0;
220 $tzTextbox
.val( minutesToHours( minuteDiff
) );
223 // Determine local time from server time and minutes difference, for display.
224 localTime
= servertime
+ minuteDiff
;
226 // Bring time within the [0,1440) range.
227 while ( localTime
< 0 ) {
230 while ( localTime
>= 1440 ) {
233 $localtimeHolder
.text( mediaWiki
.language
.convertNumber( minutesToHours( localTime
) ) );
236 if ( $tzSelect
.length
&& $tzTextbox
.length
) {
237 $tzSelect
.change( updateTimezoneSelection
);
238 $tzTextbox
.blur( updateTimezoneSelection
);
239 updateTimezoneSelection();
242 // Preserve the tab after saving the preferences
243 // Not using cookies, because their deletion results are inconsistent.
244 // Not using jStorage due to its enormous size (for this feature)
245 if ( window
.sessionStorage
) {
246 if ( sessionStorage
.getItem( 'mediawikiPreferencesTab' ) !== null ) {
247 switchPrefTab( sessionStorage
.getItem( 'mediawikiPreferencesTab' ), 'noHash' );
249 // Deleting the key, the tab states should be reset until we press Save
250 sessionStorage
.removeItem( 'mediawikiPreferencesTab' );
252 $( '#mw-prefs-form' ).submit( function () {
253 var storageData
= $( $preftoc
).find( 'li.selected a' ).attr( 'id' ).replace( 'preftab-', '' );
254 sessionStorage
.setItem( 'mediawikiPreferencesTab', storageData
);
258 // To disable all 'namespace' checkboxes in Search preferences
259 // when 'Search in all namespaces' checkbox is ticked.
260 $checkBoxes
= $( '#mw-htmlform-advancedsearchoptions input[id^=mw-input-wpsearchnamespaces]' );
261 if ( $( '#mw-input-wpsearcheverything' ).prop( 'checked' ) ) {
262 $checkBoxes
.prop( 'disabled', true );
264 $( '#mw-input-wpsearcheverything' ).change( function () {
265 $checkBoxes
.prop( 'disabled', $( this ).prop( 'checked' ) );