Merging resourceloader branch into trunk. Full documentation is at http://www.mediawi...
[lhc/web/wiklou.git] / resources / mediawiki / legacy / mediawiki.legacy.prefs.js
1 /*
2 * Legacy emulation for the now depricated skins/common/prefs.js
3 *
4 * Generate toc from prefs form, fold sections
5 *
6 * FIXME: Needs testing on IE/Mac and Safari
7 */
8
9 ( function( $, mw ) {
10
11 /* Extension */
12
13 $.extend( true, mw.legacy, {
14
15 /* Functions */
16
17 'tabbedprefs': function() {
18 var prefform = document.getElementById( 'preferences' );
19 if ( !prefform || !document.createElement ) {
20 return;
21 }
22 if ( prefform.nodeName.toLowerCase() == 'a' ) {
23 return; // Occasional IE problem
24 }
25 prefform.className = prefform.className + 'jsprefs';
26 var sections = [];
27 var children = prefform.childNodes;
28 var seci = 0;
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';
33 if ( is_opera ) {
34 children[i].className = 'prefsection operaprefsection';
35 }
36 var legends = children[i].getElementsByTagName('legend');
37 sections[seci] = {};
38 if ( legends[0] ) {
39 legends[0].className = 'mainLegend';
40 }
41 if ( legends[0] && legends[0].firstChild.nodeValue ) {
42 sections[seci].text = legends[0].firstChild.nodeValue;
43 } else {
44 sections[seci].text = '# ' + seci;
45 }
46 sections[seci].secid = children[i].id;
47 seci++;
48 if ( sections.length != 1 ) {
49 children[i].style.display = 'none';
50 } else {
51 var selectedid = children[i].id;
52 }
53 }
54 }
55 var toc = document.createElement( 'ul' );
56 toc.id = 'preftoc';
57 toc.selectedid = selectedid;
58 for ( i = 0; i < sections.length; i++ ) {
59 var li = document.createElement( 'li' );
60 if ( i === 0 ) {
61 li.className = 'selected';
62 }
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;
68 li.appendChild( a );
69 toc.appendChild( li );
70 }
71 prefform.parentNode.insertBefore( toc, prefform.parentNode.childNodes[0] );
72 document.getElementById( 'prefsubmit' ).id = 'prefcontrol';
73 },
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 = '';
85 }
86 this.parentNode.className = 'selected';
87 }
88 return false;
89 },
90 /**
91 * Timezone stuff tz in format [+-]HHMM
92 */
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] );
103 }
104 },
105 'timezoneSetup': function() {
106 var tzSelect = document.getElementById( 'mw-input-timecorrection' );
107 var tzTextbox = document.getElementById( 'mw-input-timecorrection-other' );
108
109 if ( tzSelect && tzTextbox ) {
110 addHandler( tzSelect, 'change', function( e ) { updateTimezoneSelection( false ); } );
111 addHandler( tzTextbox, 'blur', function( e ) { updateTimezoneSelection( true ); } );
112 }
113
114 updateTimezoneSelection( false );
115 },
116 /**
117 * Timezone stuff tz in format [-]HH:MM - won't yet work with non-even tzs
118 */
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;
128 return tzString;
129 },
130 'guessTimezone': function() {
131 var textbox = document.getElementById( 'mw-input-timecorrection-other' );
132 var selector = document.getElementById( 'mw-input-timecorrection' );
133
134 selector.value = 'other';
135 textbox.value = fetchTimezone();
136 textbox.disabled = false; // The changed handler doesn't trip, obviously.
137 updateTimezoneSelection( true );
138 },
139 'updateTimezoneSelection': function( force_offset ) {
140 var selector = document.getElementById( 'mw-input-timecorrection' );
141
142 if ( selector.value == 'guess' ) {
143 return guessTimezone();
144 }
145
146 var textbox = document.getElementById( 'mw-input-timecorrection-other' );
147 var localtimeHolder = document.getElementById( 'wpLocalTime' );
148 var servertime = document.getElementsByName( 'wpServerTime' )[0].value;
149 var minDiff = 0;
150
151 // Compatibility code.
152 if ( !selector.value ) {
153 selector.value = selector.options[selector.selectedIndex].value;
154 }
155
156 // Handle force_offset
157 if ( force_offset ) {
158 selector.value = 'other';
159 }
160
161 // Get min_diff
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;
168 } else {
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 ) {
172 minDiff = -minDiff;
173 }
174 }
175 } else {
176 // Grab data from the selector value
177 var diffArr = selector.value.split('|');
178 minDiff = parseInt( diffArr[1], 10 );
179 }
180
181 // Gracefully handle non-numbers.
182 if ( isNaN( minDiff ) ) {
183 minDiff = 0;
184 }
185
186 // Determine local time from server time and minutes difference, for display.
187 var localTime = parseInt( servertime, 10 ) + minDiff;
188
189 // Bring time within the [0,1440) range.
190 while ( localTime < 0 ) {
191 localTime += 1440;
192 }
193 while ( localTime >= 1440 ) {
194 localTime -= 1440;
195 }
196
197 // Split to hour and minute
198 var hour = String( Math.floor( localTime / 60 ) );
199 if ( hour.length < 2 ) {
200 hour = '0' + hour;
201 }
202 var min = String(localTime%60);
203 if ( min.length < 2 ) {
204 min = '0' + min;
205 }
206 changeText( localtimeHolder, hour + ':' + min );
207
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 ) {
212 hour = '0' + hour;
213 }
214 if ( minDiff < 0 ) {
215 hour = '-' + hour;
216 }
217 min = String(minDiff%60);
218 if ( min.length < 2 ) {
219 min = '0' + min;
220 }
221 textbox.value = hour + ':' + min;
222 }
223 }
224 } );
225
226 /* Initialization */
227
228 $( document ).ready( function() {
229 mw.legacy.timezoneSetup();
230 mw.legacy.tabbedprefs();
231 } );
232
233 } )( jQuery, mediaWiki );