Branch merge of preferences work branch. Includes fixes for several bugs. WARNING...
[lhc/web/wiklou.git] / skins / common / prefs.js
1
2 // generate toc from prefs form, fold sections
3 // XXX: needs testing on IE/Mac and safari
4 // more comments to follow
5 function tabbedprefs() {
6 var prefform = document.getElementById('preferences');
7 if (!prefform || !document.createElement) {
8 return;
9 }
10 if (prefform.nodeName.toLowerCase() == 'a') {
11 return; // Occasional IE problem
12 }
13 prefform.className = prefform.className + 'jsprefs';
14 var sections = [];
15 var children = prefform.childNodes;
16 var seci = 0;
17 for (var i = 0; i < children.length; i++) {
18 if (children[i].nodeName.toLowerCase() == 'fieldset') {
19 children[i].id = 'prefsection-' + seci;
20 children[i].className = 'prefsection';
21 if (is_opera || is_khtml) {
22 children[i].className = 'prefsection operaprefsection';
23 }
24 var legends = children[i].getElementsByTagName('legend');
25 sections[seci] = {};
26 if (legends[0]) legends[0].className = 'mainLegend';
27 if (legends[0] && legends[0].firstChild.nodeValue) {
28 sections[seci].text = legends[0].firstChild.nodeValue;
29 } else {
30 sections[seci].text = '# ' + seci;
31 }
32 sections[seci].secid = children[i].id;
33 seci++;
34 if (sections.length != 1) {
35 children[i].style.display = 'none';
36 } else {
37 var selectedid = children[i].id;
38 }
39 }
40 }
41 var toc = document.createElement('ul');
42 toc.id = 'preftoc';
43 toc.selectedid = selectedid;
44 for (i = 0; i < sections.length; i++) {
45 var li = document.createElement('li');
46 if (i === 0) {
47 li.className = 'selected';
48 }
49 var a = document.createElement('a');
50 a.href = '#' + sections[i].secid;
51 a.onmousedown = a.onclick = uncoversection;
52 a.appendChild(document.createTextNode(sections[i].text));
53 a.secid = sections[i].secid;
54 li.appendChild(a);
55 toc.appendChild(li);
56 }
57 prefform.parentNode.insertBefore(toc, prefform.parentNode.childNodes[0]);
58 document.getElementById('prefsubmit').id = 'prefcontrol';
59 }
60
61 function uncoversection() {
62 var oldsecid = this.parentNode.parentNode.selectedid;
63 var newsec = document.getElementById(this.secid);
64 if (oldsecid != this.secid) {
65 var ul = document.getElementById('preftoc');
66 document.getElementById(oldsecid).style.display = 'none';
67 newsec.style.display = 'block';
68 ul.selectedid = this.secid;
69 var lis = ul.getElementsByTagName('li');
70 for (var i = 0; i< lis.length; i++) {
71 lis[i].className = '';
72 }
73 this.parentNode.className = 'selected';
74 }
75 return false;
76 }
77
78 // Timezone stuff
79 // tz in format [+-]HHMM
80 function checkTimezone(tz, msg) {
81 var localclock = new Date();
82 // returns negative offset from GMT in minutes
83 var tzRaw = localclock.getTimezoneOffset();
84 var tzHour = Math.floor( Math.abs(tzRaw) / 60);
85 var tzMin = Math.abs(tzRaw) % 60;
86 var tzString = ((tzRaw >= 0) ? "-" : "+") + ((tzHour < 10) ? "0" : "") + tzHour + ((tzMin < 10) ? "0" : "") + tzMin;
87 if (tz != tzString) {
88 var junk = msg.split('$1');
89 document.write(junk[0] + "UTC" + tzString + junk[1]);
90 }
91 }
92
93 function timezoneSetup() {
94 var tzSelect = document.getElementById( 'mw-input-timecorrection' );
95 var tzTextbox = document.getElementById( 'mw-input-timecorrection-other' );
96
97 if (tzSelect && tzTextbox) {
98 addHandler( tzSelect, 'change', function(e) { updateTimezoneSelection(false); } );
99 addHandler( tzTextbox, 'blur', function(e) { updateTimezoneSelection(true); } );
100 }
101
102 updateTimezoneSelection(false);
103 }
104
105 // in [-]HH:MM format...
106 // won't yet work with non-even tzs
107 function fetchTimezone() {
108 // FIXME: work around Safari bug
109 var localclock = new Date();
110 // returns negative offset from GMT in minutes
111 var tzRaw = localclock.getTimezoneOffset();
112 var tzHour = Math.floor( Math.abs(tzRaw) / 60);
113 var tzMin = Math.abs(tzRaw) % 60;
114 var tzString = ((tzRaw >= 0) ? "-" : "") + ((tzHour < 10) ? "0" : "") + tzHour +
115 ":" + ((tzMin < 10) ? "0" : "") + tzMin;
116 return tzString;
117 }
118
119 function guessTimezone() {
120 var textbox = document.getElementById("mw-input-timecorrection-other");
121 var selector = document.getElementById( 'mw-input-timecorrection' );
122
123 selector.value = 'other';
124 textbox.value = fetchTimezone();
125 textbox.disabled = false; // The changed handler doesn't trip, obviously.
126 updateTimezoneSelection(true);
127 }
128
129 function updateTimezoneSelection(force_offset) {
130 var selector = document.getElementById("mw-input-timecorrection");
131
132 if (selector.value == 'guess') {
133 return guessTimezone();
134 }
135
136 var textbox = document.getElementById( 'mw-input-timecorrection-other' );
137 var localtimeHolder = document.getElementById("wpLocalTime");
138 var servertime = document.getElementsByName("wpServerTime")[0].value;
139 var minDiff = 0;
140
141 // Compatibility code.
142 if (!selector.value) selector.value = selector.options[selector.selectedIndex].value;
143
144 // Handle force_offset
145 if (force_offset) selector.value = 'other';
146
147 // Get min_diff
148 if (selector.value == 'other') {
149 // Grab data from the textbox, parse it.
150 var diffArr = textbox.value.split(':');
151 if (diffArr.length == 1) {
152 // Specification is of the form [-]XX
153 minDiff = parseInt(diffArr[0], 10) * 60;
154 } else {
155 // Specification is of the form [-]XX:XX
156 minDiff = Math.abs(parseInt(diffArr[0], 10))*60 + parseInt(diffArr[1], 10);
157 if (parseInt(diffArr[0], 10) < 0) minDiff = -minDiff;
158 }
159 } else {
160 // Grab data from the selector value
161 var diffArr = selector.value.split('|');
162 minDiff = parseInt(diffArr[1], 10);
163 }
164
165 // Gracefully handle non-numbers.
166 if (isNaN(minDiff)) minDiff = 0;
167
168 // Determine local time from server time and minutes difference, for display.
169 var localTime = parseInt(servertime, 10) + minDiff;
170
171 // Bring time within the [0,1440) range.
172 while (localTime < 0) localTime += 1440;
173 while (localTime >= 1440) localTime -= 1440;
174
175 // Split to hour and minute
176 var hour = String(Math.floor(localTime/60));
177 if (hour.length<2) hour = '0'+hour;
178 var min = String(localTime%60);
179 if (min.length<2) min = '0'+min;
180 changeText(localtimeHolder, hour+':'+min);
181
182 // If the user selected from the drop-down, fill the offset field.
183 if (selector.value != 'other') {
184 hour = String(Math.abs(Math.floor(minDiff/60)));
185 if (hour.length<2) hour = '0'+hour;
186 if (minDiff < 0) hour = '-'+hour;
187 min = String(minDiff%60);
188 if (min.length<2) min = '0'+min;
189 textbox.value = hour+':'+min;
190 }
191 }
192
193 addOnloadHook(timezoneSetup);
194 addOnloadHook(tabbedprefs);