2 * JavaScript for Special:Preferences
4 jQuery( document
).ready( function ( $ ) {
5 var $preftoc
, $preferences
, $fieldsets
, $legends
,
7 $tzSelect
, $tzTextbox
, $localtimeHolder
, servertime
;
9 $( '#prefsubmit' ).attr( 'id', 'prefcontrol' );
11 $preftoc
= $('<ul id="preftoc"></ul>');
12 $preferences
= $( '#preferences' )
13 .addClass( 'jsprefs' )
15 $fieldsets
= $preferences
.children( 'fieldset' )
17 .addClass( 'prefsection' );
20 .addClass( 'mainLegend' );
23 * It uses document.getElementById for security reasons (HTML injections in $()).
25 * @param String name: the name of a tab without the prefix ("mw-prefsection-")
26 * @param String mode: [optional] A hash will be set according to the current
27 * open section. Set mode 'noHash' to surpress this.
29 function switchPrefTab( name
, mode
) {
31 // Handle hash manually to prevent jumping,
32 // therefore save and restore scrollTop to prevent jumping.
33 scrollTop
= $( window
).scrollTop();
34 if ( mode
!== 'noHash' ) {
35 window
.location
.hash
= '#mw-prefsection-' + name
;
37 $( window
).scrollTop( scrollTop
);
39 $preftoc
.find( 'li' ).removeClass( 'selected' );
40 $tab
= $( document
.getElementById( 'preftab-' + name
) );
42 $tab
.parent().addClass( 'selected' );
43 $preferences
.children( 'fieldset' ).hide();
44 $( document
.getElementById( 'mw-prefsection-' + name
) ).show();
48 // Populate the prefToc
49 $legends
.each( function ( i
, legend
) {
50 var $legend
= $(legend
),
53 $legend
.parent().show();
55 ident
= $legend
.parent().attr( 'id' );
58 .addClass( i
=== 0 ? 'selected' : '' );
61 id
: ident
.replace( 'mw-prefsection', 'preftab' ),
64 .text( $legend
.text() );
66 $preftoc
.append( $li
);
69 // If we've reloaded the page or followed an open-in-new-window,
70 // make the selected tab visible.
71 hash
= window
.location
.hash
;
72 if ( hash
.match( /^#mw-prefsection-[\w\-]+/ ) ) {
73 switchPrefTab( hash
.replace( '#mw-prefsection-' , '' ) );
76 // In browsers that support the onhashchange event we will not bind click
77 // handlers and instead let the browser do the default behavior (clicking the
78 // <a href="#.."> will naturally set the hash, handled by onhashchange.
79 // But other things that change the hash will also be catched (e.g. using
80 // the Back and Forward browser navigation).
81 // Note the special check for IE "compatibility" mode.
82 if ( 'onhashchange' in window
&&
83 ( document
.documentMode
=== undefined || document
.documentMode
>= 8 )
85 $(window
).on( 'hashchange' , function () {
86 var hash
= window
.location
.hash
;
87 if ( hash
.match( /^#mw-prefsection-[\w\-]+/ ) ) {
88 switchPrefTab( hash
.replace( '#mw-prefsection-', '' ) );
89 } else if ( hash
=== '' ) {
90 switchPrefTab( 'personal', 'noHash' );
93 // In older browsers we'll bind a click handler as fallback.
94 // We must not have onhashchange *and* the click handlers, other wise
95 // the click handler calls switchPrefTab() which sets the hash value,
96 // which triggers onhashcange and calls switchPrefTab() again.
98 $preftoc
.on( 'click', 'li a', function ( e
) {
99 switchPrefTab( $( this ).attr( 'href' ).replace( '#mw-prefsection-', '' ) );
105 * Timezone functions.
106 * Guesses Timezone from browser and updates fields onchange
109 $tzSelect
= $( '#mw-input-wptimecorrection' );
110 $tzTextbox
= $( '#mw-input-wptimecorrection-other' );
111 $localtimeHolder
= $( '#wpLocalTime' );
112 servertime
= parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
114 function minutesToHours( min
) {
115 var tzHour
= Math
.floor( Math
.abs( min
) / 60 ),
116 tzMin
= Math
.abs( min
) % 60,
117 tzString
= ( ( min
>= 0 ) ? '' : '-' ) + ( ( tzHour
< 10 ) ? '0' : '' ) + tzHour
+
118 ':' + ( ( tzMin
< 10 ) ? '0' : '' ) + tzMin
;
122 function hoursToMinutes( hour
) {
124 arr
= hour
.split( ':' );
126 arr
[0] = parseInt( arr
[0], 10 );
128 if ( arr
.length
=== 1 ) {
129 // Specification is of the form [-]XX
130 minutes
= arr
[0] * 60;
132 // Specification is of the form [-]XX:XX
133 minutes
= Math
.abs( arr
[0] ) * 60 + parseInt( arr
[1], 10 );
138 // Gracefully handle non-numbers.
139 if ( isNaN( minutes
) ) {
146 function updateTimezoneSelection () {
147 var minuteDiff
, localTime
,
148 type
= $tzSelect
.val();
150 if ( type
=== 'guess' ) {
151 // Get browser timezone & fill it in
152 minuteDiff
= -( new Date().getTimezoneOffset() );
153 $tzTextbox
.val( minutesToHours( minuteDiff
) );
154 $tzSelect
.val( 'other' );
155 $tzTextbox
.prop( 'disabled', false );
156 } else if ( type
=== 'other' ) {
157 // Grab data from the textbox, parse it.
158 minuteDiff
= hoursToMinutes( $tzTextbox
.val() );
160 // Grab data from the $tzSelect value
161 minuteDiff
= parseInt( type
.split( '|' )[1], 10 ) || 0;
162 $tzTextbox
.val( minutesToHours( minuteDiff
) );
165 // Determine local time from server time and minutes difference, for display.
166 localTime
= servertime
+ minuteDiff
;
168 // Bring time within the [0,1440) range.
169 while ( localTime
< 0 ) {
172 while ( localTime
>= 1440 ) {
175 $localtimeHolder
.text( minutesToHours( localTime
) );
178 if ( $tzSelect
.length
&& $tzTextbox
.length
) {
179 $tzSelect
.change( updateTimezoneSelection
);
180 $tzTextbox
.blur( updateTimezoneSelection
);
181 updateTimezoneSelection();
184 // Preserve the tab after saving the preferences
185 // Not using cookies, because their deletion results are inconsistent.
186 // Not using jStorage due to its enormous size (for this feature)
187 if ( window
.sessionStorage
) {
188 if ( sessionStorage
.getItem( 'mediawikiPreferencesTab' ) !== null ) {
189 switchPrefTab( sessionStorage
.getItem( 'mediawikiPreferencesTab' ), 'noHash' );
191 // Deleting the key, the tab states should be reset until we press Save
192 sessionStorage
.removeItem( 'mediawikiPreferencesTab' );
194 $( '#mw-prefs-form' ).submit( function () {
195 var storageData
= $( $preftoc
).find( 'li.selected a' ).attr( 'id' ).replace( 'preftab-', '' );
196 sessionStorage
.setItem( 'mediawikiPreferencesTab', storageData
);