1 // generate toc from prefs form, fold sections
2 // XXX: needs testing on IE/Mac and safari
3 // more comments to follow
4 window
.tabbedprefs = function() {
5 var prefform
= document
.getElementById( 'preferences' );
6 if ( !prefform
|| !document
.createElement
) {
9 if ( prefform
.nodeName
.toLowerCase() == 'a' ) {
10 return; // Occasional IE problem
12 prefform
.className
= prefform
.className
+ 'jsprefs';
14 var children
= prefform
.childNodes
;
16 for ( var i
= 0; i
< children
.length
; i
++ ) {
17 if ( children
[i
].nodeName
.toLowerCase() == 'fieldset' ) {
18 children
[i
].id
= 'prefsection-' + seci
;
19 children
[i
].className
= 'prefsection';
21 children
[i
].className
= 'prefsection operaprefsection';
23 var legends
= children
[i
].getElementsByTagName('legend');
26 legends
[0].className
= 'mainLegend';
28 if ( legends
[0] && legends
[0].firstChild
.nodeValue
) {
29 sections
[seci
].text
= legends
[0].firstChild
.nodeValue
;
31 sections
[seci
].text
= '# ' + seci
;
33 sections
[seci
].secid
= children
[i
].id
;
35 if ( sections
.length
!= 1 ) {
36 children
[i
].style
.display
= 'none';
38 var selectedid
= children
[i
].id
;
42 var toc
= document
.createElement( 'ul' );
44 toc
.selectedid
= selectedid
;
45 for ( i
= 0; i
< sections
.length
; i
++ ) {
46 var li
= document
.createElement( 'li' );
48 li
.className
= 'selected';
50 var a
= document
.createElement( 'a' );
51 a
.href
= '#' + sections
[i
].secid
;
52 a
.onmousedown
= a
.onclick
= uncoversection
;
53 a
.appendChild( document
.createTextNode( sections
[i
].text
) );
54 a
.secid
= sections
[i
].secid
;
56 toc
.appendChild( li
);
58 prefform
.parentNode
.insertBefore( toc
, prefform
.parentNode
.childNodes
[0] );
59 document
.getElementById( 'prefsubmit' ).id
= 'prefcontrol';
62 window
.uncoversection = function() {
63 var oldsecid
= this.parentNode
.parentNode
.selectedid
;
64 var newsec
= document
.getElementById( this.secid
);
65 if ( oldsecid
!= this.secid
) {
66 var ul
= document
.getElementById( 'preftoc' );
67 document
.getElementById( oldsecid
).style
.display
= 'none';
68 newsec
.style
.display
= 'block';
69 ul
.selectedid
= this.secid
;
70 var lis
= ul
.getElementsByTagName( 'li' );
71 for ( var i
= 0; i
< lis
.length
; i
++ ) {
72 lis
[i
].className
= '';
74 this.parentNode
.className
= 'selected';
80 // tz in format [+-]HHMM
81 window
.checkTimezone = function( tz
, msg
) {
82 var localclock
= new Date();
83 // returns negative offset from GMT in minutes
84 var tzRaw
= localclock
.getTimezoneOffset();
85 var tzHour
= Math
.floor( Math
.abs( tzRaw
) / 60 );
86 var tzMin
= Math
.abs( tzRaw
) % 60;
87 var tzString
= ( ( tzRaw
>= 0 ) ? '-' : '+' ) + ( ( tzHour
< 10 ) ? '0' : '' ) + tzHour
+ ( ( tzMin
< 10 ) ? '0' : '' ) + tzMin
;
88 if ( tz
!= tzString
) {
89 var junk
= msg
.split('$1');
90 document
.write( junk
[0] + 'UTC' + tzString
+ junk
[1] );
94 window
.timezoneSetup = function() {
95 var tzSelect
= document
.getElementById( 'mw-input-timecorrection' );
96 var tzTextbox
= document
.getElementById( 'mw-input-timecorrection-other' );
98 if ( tzSelect
&& tzTextbox
) {
99 addHandler( tzSelect
, 'change', function( e
) { updateTimezoneSelection( false ); } );
100 addHandler( tzTextbox
, 'blur', function( e
) { updateTimezoneSelection( true ); } );
103 updateTimezoneSelection( false );
106 // in [-]HH:MM format...
107 // won't yet work with non-even tzs
108 window
.fetchTimezone = function() {
109 // FIXME: work around Safari bug
110 var localclock
= new Date();
111 // returns negative offset from GMT in minutes
112 var tzRaw
= localclock
.getTimezoneOffset();
113 var tzHour
= Math
.floor( Math
.abs( tzRaw
) / 60 );
114 var tzMin
= Math
.abs( tzRaw
) % 60;
115 var tzString
= ( ( tzRaw
>= 0 ) ? '-' : '' ) + ( ( tzHour
< 10 ) ? '0' : '' ) + tzHour
+
116 ':' + ( ( tzMin
< 10 ) ? '0' : '' ) + tzMin
;
120 window
.guessTimezone = function() {
121 var textbox
= document
.getElementById( 'mw-input-timecorrection-other' );
122 var selector
= document
.getElementById( 'mw-input-timecorrection' );
124 selector
.value
= 'other';
125 textbox
.value
= fetchTimezone();
126 textbox
.disabled
= false; // The changed handler doesn't trip, obviously.
127 updateTimezoneSelection( true );
130 window
.updateTimezoneSelection = function( force_offset
) {
131 var selector
= document
.getElementById( 'mw-input-timecorrection' );
133 if ( selector
.value
== 'guess' ) {
134 return guessTimezone();
137 var textbox
= document
.getElementById( 'mw-input-timecorrection-other' );
138 var localtimeHolder
= document
.getElementById( 'wpLocalTime' );
139 var servertime
= document
.getElementsByName( 'wpServerTime' )[0].value
;
142 // Compatibility code.
143 if ( !selector
.value
) {
144 selector
.value
= selector
.options
[selector
.selectedIndex
].value
;
147 // Handle force_offset
148 if ( force_offset
) {
149 selector
.value
= 'other';
153 if ( selector
.value
== 'other' ) {
154 // Grab data from the textbox, parse it.
155 var diffArr
= textbox
.value
.split(':');
156 if ( diffArr
.length
== 1 ) {
157 // Specification is of the form [-]XX
158 minDiff
= parseInt( diffArr
[0], 10 ) * 60;
160 // Specification is of the form [-]XX:XX
161 minDiff
= Math
.abs( parseInt( diffArr
[0], 10 ) ) * 60 + parseInt( diffArr
[1], 10 );
162 if ( parseInt( diffArr
[0], 10 ) < 0 ) {
167 // Grab data from the selector value
168 var diffArr
= selector
.value
.split('|');
169 minDiff
= parseInt( diffArr
[1], 10 );
172 // Gracefully handle non-numbers.
173 if ( isNaN( minDiff
) ) {
177 // Determine local time from server time and minutes difference, for display.
178 var localTime
= parseInt( servertime
, 10 ) + minDiff
;
180 // Bring time within the [0,1440) range.
181 while ( localTime
< 0 ) {
184 while ( localTime
>= 1440 ) {
188 // Split to hour and minute
189 var hour
= String( Math
.floor( localTime
/ 60 ) );
190 if ( hour
.length
< 2 ) {
193 var min
= String(localTime
%60);
194 if ( min
.length
< 2 ) {
197 changeText( localtimeHolder
, hour
+ ':' + min
);
199 // If the user selected from the drop-down, fill the offset field.
200 if ( selector
.value
!= 'other' ) {
201 hour
= String( Math
.abs( Math
.floor( minDiff
/ 60 ) ) );
202 if ( hour
.length
< 2 ) {
208 min
= String(minDiff
%60);
209 if ( min
.length
< 2 ) {
212 textbox
.value
= hour
+ ':' + min
;
216 addOnloadHook( timezoneSetup
);
217 addOnloadHook( tabbedprefs
);