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
26 * @param String name: the name of a tab without the prefix ("mw-prefsection-")
27 * @param String mode: [optional] A hash will be set according to the current
28 * open section. Set mode 'noHash' to surpress this.
30 function switchPrefTab( name
, mode
) {
32 // Handle hash manually to prevent jumping,
33 // therefore save and restore scrollTop to prevent jumping.
34 scrollTop
= $( window
).scrollTop();
35 if ( mode
!== 'noHash' ) {
36 window
.location
.hash
= '#mw-prefsection-' + name
;
38 $( window
).scrollTop( scrollTop
);
40 $preftoc
.find( 'li' ).removeClass( 'selected' );
41 $tab
= $( document
.getElementById( 'preftab-' + name
) );
43 $tab
.parent().addClass( 'selected' );
44 $preferences
.children( 'fieldset' ).hide();
45 $( document
.getElementById( 'mw-prefsection-' + name
) ).show();
49 // Populate the prefToc
50 $legends
.each( function ( i
, legend
) {
51 var $legend
= $(legend
),
54 $legend
.parent().show();
56 ident
= $legend
.parent().attr( 'id' );
59 .addClass( i
=== 0 ? 'selected' : '' );
62 id
: ident
.replace( 'mw-prefsection', 'preftab' ),
65 .text( $legend
.text() );
67 $preftoc
.append( $li
);
70 // If we've reloaded the page or followed an open-in-new-window,
71 // make the selected tab visible.
72 hash
= window
.location
.hash
;
73 if ( hash
.match( /^#mw-prefsection-[\w\-]+/ ) ) {
74 switchPrefTab( hash
.replace( '#mw-prefsection-' , '' ) );
77 // In browsers that support the onhashchange event we will not bind click
78 // handlers and instead let the browser do the default behavior (clicking the
79 // <a href="#.."> will naturally set the hash, handled by onhashchange.
80 // But other things that change the hash will also be catched (e.g. using
81 // the Back and Forward browser navigation).
82 if ( 'onhashchange' in window
) {
83 $(window
).on( 'hashchange' , function () {
84 var hash
= window
.location
.hash
;
85 if ( hash
.match( /^#mw-prefsection-[\w\-]+/ ) ) {
86 switchPrefTab( hash
.replace( '#mw-prefsection-', '' ) );
87 } else if ( hash
=== '' ) {
88 switchPrefTab( 'personal', 'noHash' );
91 // In older browsers we'll bind a click handler as fallback.
92 // We must not have onhashchange *and* the click handlers, other wise
93 // the click handler calls switchPrefTab() which sets the hash value,
94 // which triggers onhashcange and calls switchPrefTab() again.
96 $preftoc
.on( 'click', 'li a', function ( e
) {
97 switchPrefTab( $( this ).attr( 'href' ).replace( '#mw-prefsection-', '' ) );
103 * Timezone functions.
104 * Guesses Timezone from browser and updates fields onchange
107 $tzSelect
= $( '#mw-input-wptimecorrection' );
108 $tzTextbox
= $( '#mw-input-wptimecorrection-other' );
109 $localtimeHolder
= $( '#wpLocalTime' );
110 servertime
= parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
112 function minutesToHours( min
) {
113 var tzHour
= Math
.floor( Math
.abs( min
) / 60 ),
114 tzMin
= Math
.abs( min
) % 60,
115 tzString
= ( ( min
>= 0 ) ? '' : '-' ) + ( ( tzHour
< 10 ) ? '0' : '' ) + tzHour
+
116 ':' + ( ( tzMin
< 10 ) ? '0' : '' ) + tzMin
;
120 function hoursToMinutes( hour
) {
122 arr
= hour
.split( ':' );
124 arr
[0] = parseInt( arr
[0], 10 );
126 if ( arr
.length
=== 1 ) {
127 // Specification is of the form [-]XX
128 minutes
= arr
[0] * 60;
130 // Specification is of the form [-]XX:XX
131 minutes
= Math
.abs( arr
[0] ) * 60 + parseInt( arr
[1], 10 );
136 // Gracefully handle non-numbers.
137 if ( isNaN( minutes
) ) {
144 function updateTimezoneSelection () {
145 var minuteDiff
, localTime
,
146 type
= $tzSelect
.val();
148 if ( type
=== 'guess' ) {
149 // Get browser timezone & fill it in
150 minuteDiff
= -( new Date().getTimezoneOffset() );
151 $tzTextbox
.val( minutesToHours( minuteDiff
) );
152 $tzSelect
.val( 'other' );
153 $tzTextbox
.prop( 'disabled', false );
154 } else if ( type
=== 'other' ) {
155 // Grab data from the textbox, parse it.
156 minuteDiff
= hoursToMinutes( $tzTextbox
.val() );
158 // Grab data from the $tzSelect value
159 minuteDiff
= parseInt( type
.split( '|' )[1], 10 ) || 0;
160 $tzTextbox
.val( minutesToHours( minuteDiff
) );
163 // Determine local time from server time and minutes difference, for display.
164 localTime
= servertime
+ minuteDiff
;
166 // Bring time within the [0,1440) range.
167 while ( localTime
< 0 ) {
170 while ( localTime
>= 1440 ) {
173 $localtimeHolder
.text( minutesToHours( localTime
) );
176 if ( $tzSelect
.length
&& $tzTextbox
.length
) {
177 $tzSelect
.change( updateTimezoneSelection
);
178 $tzTextbox
.blur( updateTimezoneSelection
);
179 updateTimezoneSelection();