2 * JavaScript for Special:Preferences
4 jQuery( function ( $ ) {
5 var $preftoc
, $preferences
, $fieldsets
, $legends
,
7 $tzSelect
, $tzTextbox
, $localtimeHolder
, servertime
,
8 $checkBoxes
, allowCloseWindow
;
10 labelFunc = function () {
11 return this.id
.replace( /^mw-prefsection/g, 'preftab' );
14 $( '#prefsubmit' ).attr( 'id', 'prefcontrol' );
15 $preftoc
= $( '<ul>' )
20 $preferences
= $( '#preferences' )
21 .addClass( 'jsprefs' )
23 $fieldsets
= $preferences
.children( 'fieldset' )
27 'aria-hidden': 'true',
28 'aria-labelledby': labelFunc
30 .addClass( 'prefsection' );
33 .addClass( 'mainLegend' );
35 // Make sure the accessibility tip is selectable so that screen reader users take notice,
36 // but hide it per default to reduce interface clutter. Also make sure it becomes visible
37 // when selected. Similar to jquery.mw-jump
38 $( '<div>' ).addClass( 'mw-navigation-hint' )
39 .text( mediaWiki
.msg( 'prefs-tabs-navigation-hint' ) )
40 .attr( 'tabIndex', 0 )
41 .on( 'focus blur', function ( e
) {
42 if ( e
.type
=== 'blur' || e
.type
=== 'focusout' ) {
43 $( this ).css( 'height', '0' );
45 $( this ).css( 'height', 'auto' );
47 } ).insertBefore( $preftoc
);
50 * It uses document.getElementById for security reasons (HTML injections in $()).
53 * @param String name: the name of a tab without the prefix ("mw-prefsection-")
54 * @param String mode: [optional] A hash will be set according to the current
55 * open section. Set mode 'noHash' to surpress this.
57 function switchPrefTab( name
, mode
) {
59 // Handle hash manually to prevent jumping,
60 // therefore save and restore scrollTop to prevent jumping.
61 scrollTop
= $( window
).scrollTop();
62 if ( mode
!== 'noHash' ) {
63 location
.hash
= '#mw-prefsection-' + name
;
65 $( window
).scrollTop( scrollTop
);
67 $preftoc
.find( 'li' ).removeClass( 'selected' )
70 'aria-selected': 'false'
73 $tab
= $( document
.getElementById( 'preftab-' + name
) );
77 'aria-selected': 'true'
80 .parent().addClass( 'selected' );
82 $preferences
.children( 'fieldset' ).hide().attr( 'aria-hidden', 'true' );
83 $( document
.getElementById( 'mw-prefsection-' + name
) ).show().attr( 'aria-hidden', 'false' );
87 // Populate the prefToc
88 $legends
.each( function ( i
, legend
) {
89 var $legend
= $( legend
),
92 $legend
.parent().show();
94 ident
= $legend
.parent().attr( 'id' );
97 .attr( 'role', 'presentation' )
98 .addClass( i
=== 0 ? 'selected' : '' );
101 id
: ident
.replace( 'mw-prefsection', 'preftab' ),
104 tabIndex
: i
=== 0 ? 0 : -1,
105 'aria-selected': i
=== 0 ? 'true' : 'false',
106 'aria-controls': ident
108 .text( $legend
.text() );
110 $preftoc
.append( $li
);
113 // Enable keyboard users to use left and right keys to switch tabs
114 $preftoc
.on( 'keydown', function ( event
) {
119 if ( event
.keyCode
=== keyLeft
) {
120 $el
= $( '#preftoc li.selected' ).prev().find( 'a' );
121 } else if ( event
.keyCode
=== keyRight
) {
122 $el
= $( '#preftoc li.selected' ).next().find( 'a' );
126 if ( $el
.length
> 0 ) {
127 switchPrefTab( $el
.attr( 'href' ).replace( '#mw-prefsection-', '' ) );
131 // If we've reloaded the page or followed an open-in-new-window,
132 // make the selected tab visible.
133 hash
= location
.hash
;
134 if ( hash
.match( /^#mw-prefsection-[\w\-]+/ ) ) {
135 switchPrefTab( hash
.replace( '#mw-prefsection-', '' ) );
138 // In browsers that support the onhashchange event we will not bind click
139 // handlers and instead let the browser do the default behavior (clicking the
140 // <a href="#.."> will naturally set the hash, handled by onhashchange.
141 // But other things that change the hash will also be catched (e.g. using
142 // the Back and Forward browser navigation).
143 // Note the special check for IE "compatibility" mode.
144 if ( 'onhashchange' in window
&&
145 ( document
.documentMode
=== undefined || document
.documentMode
>= 8 )
147 $( window
).on( 'hashchange', function () {
148 var hash
= location
.hash
;
149 if ( hash
.match( /^#mw-prefsection-[\w\-]+/ ) ) {
150 switchPrefTab( hash
.replace( '#mw-prefsection-', '' ) );
151 } else if ( hash
=== '' ) {
152 switchPrefTab( 'personal', 'noHash' );
155 // In older browsers we'll bind a click handler as fallback.
156 // We must not have onhashchange *and* the click handlers, other wise
157 // the click handler calls switchPrefTab() which sets the hash value,
158 // which triggers onhashcange and calls switchPrefTab() again.
160 $preftoc
.on( 'click', 'li a', function ( e
) {
161 switchPrefTab( $( this ).attr( 'href' ).replace( '#mw-prefsection-', '' ) );
166 // Timezone functions.
167 // Guesses Timezone from browser and updates fields onchange.
169 $tzSelect
= $( '#mw-input-wptimecorrection' );
170 $tzTextbox
= $( '#mw-input-wptimecorrection-other' );
171 $localtimeHolder
= $( '#wpLocalTime' );
172 servertime
= parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
174 function minutesToHours( min
) {
175 var tzHour
= Math
.floor( Math
.abs( min
) / 60 ),
176 tzMin
= Math
.abs( min
) % 60,
177 tzString
= ( ( min
>= 0 ) ? '' : '-' ) + ( ( tzHour
< 10 ) ? '0' : '' ) + tzHour
+
178 ':' + ( ( tzMin
< 10 ) ? '0' : '' ) + tzMin
;
182 function hoursToMinutes( hour
) {
184 arr
= hour
.split( ':' );
186 arr
[0] = parseInt( arr
[0], 10 );
188 if ( arr
.length
=== 1 ) {
189 // Specification is of the form [-]XX
190 minutes
= arr
[0] * 60;
192 // Specification is of the form [-]XX:XX
193 minutes
= Math
.abs( arr
[0] ) * 60 + parseInt( arr
[1], 10 );
198 // Gracefully handle non-numbers.
199 if ( isNaN( minutes
) ) {
206 function updateTimezoneSelection() {
207 var minuteDiff
, localTime
,
208 type
= $tzSelect
.val();
210 if ( type
=== 'guess' ) {
211 // Get browser timezone & fill it in
212 minuteDiff
= -( new Date().getTimezoneOffset() );
213 $tzTextbox
.val( minutesToHours( minuteDiff
) );
214 $tzSelect
.val( 'other' );
215 $tzTextbox
.prop( 'disabled', false );
216 } else if ( type
=== 'other' ) {
217 // Grab data from the textbox, parse it.
218 minuteDiff
= hoursToMinutes( $tzTextbox
.val() );
220 // Grab data from the $tzSelect value
221 minuteDiff
= parseInt( type
.split( '|' )[1], 10 ) || 0;
222 $tzTextbox
.val( minutesToHours( minuteDiff
) );
225 // Determine local time from server time and minutes difference, for display.
226 localTime
= servertime
+ minuteDiff
;
228 // Bring time within the [0,1440) range.
229 localTime
= ( ( localTime
% 1440 ) + 1440 ) % 1440;
231 $localtimeHolder
.text( mediaWiki
.language
.convertNumber( minutesToHours( localTime
) ) );
234 if ( $tzSelect
.length
&& $tzTextbox
.length
) {
235 $tzSelect
.change( updateTimezoneSelection
);
236 $tzTextbox
.blur( updateTimezoneSelection
);
237 updateTimezoneSelection();
240 // Preserve the tab after saving the preferences
241 // Not using cookies, because their deletion results are inconsistent.
242 // Not using jStorage due to its enormous size (for this feature)
243 if ( window
.sessionStorage
) {
244 if ( sessionStorage
.getItem( 'mediawikiPreferencesTab' ) !== null ) {
245 switchPrefTab( sessionStorage
.getItem( 'mediawikiPreferencesTab' ), 'noHash' );
247 // Deleting the key, the tab states should be reset until we press Save
248 sessionStorage
.removeItem( 'mediawikiPreferencesTab' );
250 $( '#mw-prefs-form' ).submit( function () {
251 var storageData
= $( $preftoc
).find( 'li.selected a' ).attr( 'id' ).replace( 'preftab-', '' );
252 sessionStorage
.setItem( 'mediawikiPreferencesTab', storageData
);
256 // To disable all 'namespace' checkboxes in Search preferences
257 // when 'Search in all namespaces' checkbox is ticked.
258 $checkBoxes
= $( '#mw-htmlform-advancedsearchoptions input[id^=mw-input-wpsearchnamespaces]' );
259 if ( $( '#mw-input-wpsearcheverything' ).prop( 'checked' ) ) {
260 $checkBoxes
.prop( 'disabled', true );
262 $( '#mw-input-wpsearcheverything' ).change( function () {
263 $checkBoxes
.prop( 'disabled', $( this ).prop( 'checked' ) );
266 // Set up a message to notify users if they try to leave the page without
268 $( '#mw-prefs-form' ).data( 'origdata', $( '#mw-prefs-form' ).serialize() );
269 allowCloseWindow
= mediaWiki
.confirmCloseWindow( {
271 return $( '#mw-prefs-form' ).serialize() !== $( '#mw-prefs-form' ).data( 'origdata' );
274 message
: mediaWiki
.msg( 'prefswarning-warning', mediaWiki
.msg( 'saveprefs' ) ),
275 namespace: 'prefswarning'
277 $( '#mw-prefs-form' ).submit( $.proxy( allowCloseWindow
, 'release' ) );
278 $( '#mw-prefs-restoreprefs' ).click( $.proxy( allowCloseWindow
, 'release' ) );