2 * Legacy emulation for the now depricated skins/common/prefs.js
4 * Generate toc from prefs form, fold sections
6 * FIXME: Needs testing on IE/Mac and Safari
13 $.extend( true, mw
.legacy
, {
17 'tabbedprefs': function() {
18 var prefform
= document
.getElementById( 'preferences' );
19 if ( !prefform
|| !document
.createElement
) {
22 if ( prefform
.nodeName
.toLowerCase() == 'a' ) {
23 return; // Occasional IE problem
25 prefform
.className
= prefform
.className
+ 'jsprefs';
27 var children
= prefform
.childNodes
;
29 for ( var i
= 0; i
< children
.length
; i
++ ) {
30 if ( children
[i
].nodeName
.toLowerCase() == 'fieldset' ) {
31 children
[i
].id
= 'prefsection-' + seci
;
32 children
[i
].className
= 'prefsection';
34 children
[i
].className
= 'prefsection operaprefsection';
36 var legends
= children
[i
].getElementsByTagName('legend');
39 legends
[0].className
= 'mainLegend';
41 if ( legends
[0] && legends
[0].firstChild
.nodeValue
) {
42 sections
[seci
].text
= legends
[0].firstChild
.nodeValue
;
44 sections
[seci
].text
= '# ' + seci
;
46 sections
[seci
].secid
= children
[i
].id
;
48 if ( sections
.length
!= 1 ) {
49 children
[i
].style
.display
= 'none';
51 var selectedid
= children
[i
].id
;
55 var toc
= document
.createElement( 'ul' );
57 toc
.selectedid
= selectedid
;
58 for ( i
= 0; i
< sections
.length
; i
++ ) {
59 var li
= document
.createElement( 'li' );
61 li
.className
= 'selected';
63 var a
= document
.createElement( 'a' );
64 a
.href
= '#' + sections
[i
].secid
;
65 a
.onmousedown
= a
.onclick
= uncoversection
;
66 a
.appendChild( document
.createTextNode( sections
[i
].text
) );
67 a
.secid
= sections
[i
].secid
;
69 toc
.appendChild( li
);
71 prefform
.parentNode
.insertBefore( toc
, prefform
.parentNode
.childNodes
[0] );
72 document
.getElementById( 'prefsubmit' ).id
= 'prefcontrol';
74 'uncoversection': function() {
75 var oldsecid
= this.parentNode
.parentNode
.selectedid
;
76 var newsec
= document
.getElementById( this.secid
);
77 if ( oldsecid
!= this.secid
) {
78 var ul
= document
.getElementById( 'preftoc' );
79 document
.getElementById( oldsecid
).style
.display
= 'none';
80 newsec
.style
.display
= 'block';
81 ul
.selectedid
= this.secid
;
82 var lis
= ul
.getElementsByTagName( 'li' );
83 for ( var i
= 0; i
< lis
.length
; i
++ ) {
84 lis
[i
].className
= '';
86 this.parentNode
.className
= 'selected';
91 * Timezone stuff tz in format [+-]HHMM
93 'checkTimezone': function( tz
, msg
) {
94 var localclock
= new Date();
95 // returns negative offset from GMT in minutes
96 var tzRaw
= localclock
.getTimezoneOffset();
97 var tzHour
= Math
.floor( Math
.abs( tzRaw
) / 60 );
98 var tzMin
= Math
.abs( tzRaw
) % 60;
99 var tzString
= ( ( tzRaw
>= 0 ) ? '-' : '+' ) + ( ( tzHour
< 10 ) ? '0' : '' ) + tzHour
+ ( ( tzMin
< 10 ) ? '0' : '' ) + tzMin
;
100 if ( tz
!= tzString
) {
101 var junk
= msg
.split('$1');
102 document
.write( junk
[0] + 'UTC' + tzString
+ junk
[1] );
105 'timezoneSetup': function() {
106 var tzSelect
= document
.getElementById( 'mw-input-timecorrection' );
107 var tzTextbox
= document
.getElementById( 'mw-input-timecorrection-other' );
109 if ( tzSelect
&& tzTextbox
) {
110 addHandler( tzSelect
, 'change', function( e
) { updateTimezoneSelection( false ); } );
111 addHandler( tzTextbox
, 'blur', function( e
) { updateTimezoneSelection( true ); } );
114 updateTimezoneSelection( false );
117 * Timezone stuff tz in format [-]HH:MM - won't yet work with non-even tzs
119 'fetchTimezone': function() {
120 // FIXME: work around Safari bug
121 var localclock
= new Date();
122 // returns negative offset from GMT in minutes
123 var tzRaw
= localclock
.getTimezoneOffset();
124 var tzHour
= Math
.floor( Math
.abs( tzRaw
) / 60 );
125 var tzMin
= Math
.abs( tzRaw
) % 60;
126 var tzString
= ( ( tzRaw
>= 0 ) ? '-' : '' ) + ( ( tzHour
< 10 ) ? '0' : '' ) + tzHour
+
127 ':' + ( ( tzMin
< 10 ) ? '0' : '' ) + tzMin
;
130 'guessTimezone': function() {
131 var textbox
= document
.getElementById( 'mw-input-timecorrection-other' );
132 var selector
= document
.getElementById( 'mw-input-timecorrection' );
134 selector
.value
= 'other';
135 textbox
.value
= fetchTimezone();
136 textbox
.disabled
= false; // The changed handler doesn't trip, obviously.
137 updateTimezoneSelection( true );
139 'updateTimezoneSelection': function( force_offset
) {
140 var selector
= document
.getElementById( 'mw-input-timecorrection' );
142 if ( selector
.value
== 'guess' ) {
143 return guessTimezone();
146 var textbox
= document
.getElementById( 'mw-input-timecorrection-other' );
147 var localtimeHolder
= document
.getElementById( 'wpLocalTime' );
148 var servertime
= document
.getElementsByName( 'wpServerTime' )[0].value
;
151 // Compatibility code.
152 if ( !selector
.value
) {
153 selector
.value
= selector
.options
[selector
.selectedIndex
].value
;
156 // Handle force_offset
157 if ( force_offset
) {
158 selector
.value
= 'other';
162 if ( selector
.value
== 'other' ) {
163 // Grab data from the textbox, parse it.
164 var diffArr
= textbox
.value
.split(':');
165 if ( diffArr
.length
== 1 ) {
166 // Specification is of the form [-]XX
167 minDiff
= parseInt( diffArr
[0], 10 ) * 60;
169 // Specification is of the form [-]XX:XX
170 minDiff
= Math
.abs( parseInt( diffArr
[0], 10 ) ) * 60 + parseInt( diffArr
[1], 10 );
171 if ( parseInt( diffArr
[0], 10 ) < 0 ) {
176 // Grab data from the selector value
177 var diffArr
= selector
.value
.split('|');
178 minDiff
= parseInt( diffArr
[1], 10 );
181 // Gracefully handle non-numbers.
182 if ( isNaN( minDiff
) ) {
186 // Determine local time from server time and minutes difference, for display.
187 var localTime
= parseInt( servertime
, 10 ) + minDiff
;
189 // Bring time within the [0,1440) range.
190 while ( localTime
< 0 ) {
193 while ( localTime
>= 1440 ) {
197 // Split to hour and minute
198 var hour
= String( Math
.floor( localTime
/ 60 ) );
199 if ( hour
.length
< 2 ) {
202 var min
= String(localTime
%60);
203 if ( min
.length
< 2 ) {
206 changeText( localtimeHolder
, hour
+ ':' + min
);
208 // If the user selected from the drop-down, fill the offset field.
209 if ( selector
.value
!= 'other' ) {
210 hour
= String( Math
.abs( Math
.floor( minDiff
/ 60 ) ) );
211 if ( hour
.length
< 2 ) {
217 min
= String(minDiff
%60);
218 if ( min
.length
< 2 ) {
221 textbox
.value
= hour
+ ':' + min
;
228 $( document
).ready( function() {
229 mw
.legacy
.timezoneSetup();
230 mw
.legacy
.tabbedprefs();
233 } )( jQuery
, mediaWiki
);