Fixed bug that cause ResourceLoader to generate mediaWiki.loader.implement calls...
[mediawiki.git] / skins / common / prefs.js
blobb0ed97c25b20af5f38b2910fdb2cc8870470a124
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 ) {
7                 return;
8         }
9         if ( prefform.nodeName.toLowerCase() == 'a' ) {
10                 return; // Occasional IE problem
11         }
12         prefform.className = prefform.className + 'jsprefs';
13         var sections = [];
14         var children = prefform.childNodes;
15         var seci = 0;
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';
20                         if ( is_opera ) {
21                                 children[i].className = 'prefsection operaprefsection';
22                         }
23                         var legends = children[i].getElementsByTagName('legend');
24                         sections[seci] = {};
25                         if ( legends[0] ) {
26                                 legends[0].className = 'mainLegend';
27                         }
28                         if ( legends[0] && legends[0].firstChild.nodeValue ) {
29                                 sections[seci].text = legends[0].firstChild.nodeValue;
30                         } else {
31                                 sections[seci].text = '# ' + seci;
32                         }
33                         sections[seci].secid = children[i].id;
34                         seci++;
35                         if ( sections.length != 1 ) {
36                                 children[i].style.display = 'none';
37                         } else {
38                                 var selectedid = children[i].id;
39                         }
40                 }
41         }
42         var toc = document.createElement( 'ul' );
43         toc.id = 'preftoc';
44         toc.selectedid = selectedid;
45         for ( i = 0; i < sections.length; i++ ) {
46                 var li = document.createElement( 'li' );
47                 if ( i === 0 ) {
48                         li.className = 'selected';
49                 }
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;
55                 li.appendChild( a );
56                 toc.appendChild( li );
57         }
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 = '';
73                 }
74                 this.parentNode.className = 'selected';
75         }
76         return false;
79 // Timezone stuff
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] );
91         }
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 ); } );
101         }
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;
117         return tzString;
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();
135         }
137         var textbox = document.getElementById( 'mw-input-timecorrection-other' );
138         var localtimeHolder = document.getElementById( 'wpLocalTime' );
139         var servertime = document.getElementsByName( 'wpServerTime' )[0].value;
140         var minDiff = 0;
142         // Compatibility code.
143         if ( !selector.value ) {
144                 selector.value = selector.options[selector.selectedIndex].value;
145         }
147         // Handle force_offset
148         if ( force_offset ) {
149                 selector.value = 'other';
150         }
152         // Get min_diff
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;
159                 } else {
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 ) {
163                                 minDiff = -minDiff;
164                         }
165                 }
166         } else {
167                 // Grab data from the selector value
168                 var diffArr = selector.value.split('|');
169                 minDiff = parseInt( diffArr[1], 10 );
170         }
172         // Gracefully handle non-numbers.
173         if ( isNaN( minDiff ) ) {
174                 minDiff = 0;
175         }
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 ) {
182                 localTime += 1440;
183         }
184         while ( localTime >= 1440 ) {
185                 localTime -= 1440;
186         }
188         // Split to hour and minute
189         var hour = String( Math.floor( localTime / 60 ) );
190         if ( hour.length < 2 ) {
191                 hour = '0' + hour;
192         }
193         var min = String(localTime%60);
194         if ( min.length < 2 ) {
195                 min = '0' + min;
196         }
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 ) {
203                         hour = '0' + hour;
204                 }
205                 if ( minDiff < 0 ) {
206                         hour = '-' + hour;
207                 }
208                 min = String(minDiff%60);
209                 if ( min.length < 2 ) {
210                         min = '0' + min;
211                 }
212                 textbox.value = hour + ':' + min;
213         }
216 addOnloadHook( timezoneSetup );
217 addOnloadHook( tabbedprefs );