prefs.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // generate toc from prefs form, fold sections
  2. // XXX: needs testing on IE/Mac and safari
  3. // more comments to follow
  4. function tabbedprefs() {
  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 || is_khtml) {
  21. children[i].className = 'prefsection operaprefsection';
  22. }
  23. var legends = children[i].getElementsByTagName('legend');
  24. sections[seci] = {};
  25. if (legends[0]) legends[0].className = 'mainLegend';
  26. if (legends[0] && legends[0].firstChild.nodeValue) {
  27. sections[seci].text = legends[0].firstChild.nodeValue;
  28. } else {
  29. sections[seci].text = '# ' + seci;
  30. }
  31. sections[seci].secid = children[i].id;
  32. seci++;
  33. if (sections.length != 1) {
  34. children[i].style.display = 'none';
  35. } else {
  36. var selectedid = children[i].id;
  37. }
  38. }
  39. }
  40. var toc = document.createElement('ul');
  41. toc.id = 'preftoc';
  42. toc.selectedid = selectedid;
  43. for (i = 0; i < sections.length; i++) {
  44. var li = document.createElement('li');
  45. if (i === 0) {
  46. li.className = 'selected';
  47. }
  48. var a = document.createElement('a');
  49. a.href = '#' + sections[i].secid;
  50. a.onmousedown = a.onclick = uncoversection;
  51. a.appendChild(document.createTextNode(sections[i].text));
  52. a.secid = sections[i].secid;
  53. li.appendChild(a);
  54. toc.appendChild(li);
  55. }
  56. prefform.parentNode.insertBefore(toc, prefform.parentNode.childNodes[0]);
  57. document.getElementById('prefsubmit').id = 'prefcontrol';
  58. }
  59. function uncoversection() {
  60. var oldsecid = this.parentNode.parentNode.selectedid;
  61. var newsec = document.getElementById(this.secid);
  62. if (oldsecid != this.secid) {
  63. var ul = document.getElementById('preftoc');
  64. document.getElementById(oldsecid).style.display = 'none';
  65. newsec.style.display = 'block';
  66. ul.selectedid = this.secid;
  67. var lis = ul.getElementsByTagName('li');
  68. for (var i = 0; i< lis.length; i++) {
  69. lis[i].className = '';
  70. }
  71. this.parentNode.className = 'selected';
  72. }
  73. return false;
  74. }
  75. // Timezone stuff
  76. // tz in format [+-]HHMM
  77. function checkTimezone(tz, msg) {
  78. var localclock = new Date();
  79. // returns negative offset from GMT in minutes
  80. var tzRaw = localclock.getTimezoneOffset();
  81. var tzHour = Math.floor( Math.abs(tzRaw) / 60);
  82. var tzMin = Math.abs(tzRaw) % 60;
  83. var tzString = ((tzRaw >= 0) ? "-" : "+") + ((tzHour < 10) ? "0" : "") + tzHour + ((tzMin < 10) ? "0" : "") + tzMin;
  84. if (tz != tzString) {
  85. var junk = msg.split('$1');
  86. document.write(junk[0] + "UTC" + tzString + junk[1]);
  87. }
  88. }
  89. function unhidetzbutton() {
  90. var tzb = document.getElementById('guesstimezonebutton');
  91. if (tzb) {
  92. tzb.style.display = 'inline';
  93. }
  94. updateTimezoneSelection(false);
  95. }
  96. // in [-]HH:MM format...
  97. // won't yet work with non-even tzs
  98. function fetchTimezone() {
  99. // FIXME: work around Safari bug
  100. var localclock = new Date();
  101. // returns negative offset from GMT in minutes
  102. var tzRaw = localclock.getTimezoneOffset();
  103. var tzHour = Math.floor( Math.abs(tzRaw) / 60);
  104. var tzMin = Math.abs(tzRaw) % 60;
  105. var tzString = ((tzRaw >= 0) ? "-" : "") + ((tzHour < 10) ? "0" : "") + tzHour +
  106. ":" + ((tzMin < 10) ? "0" : "") + tzMin;
  107. return tzString;
  108. }
  109. function guessTimezone(box) {
  110. document.getElementsByName("wpHourDiff")[0].value = fetchTimezone();
  111. updateTimezoneSelection(true);
  112. }
  113. function updateTimezoneSelection(force_offset) {
  114. var wpTimeZone = document.getElementsByName("wpTimeZone")[0];
  115. var wpHourDiff = document.getElementsByName("wpHourDiff")[0];
  116. var wpLocalTime = document.getElementById("wpLocalTime");
  117. var wpServerTime = document.getElementsByName("wpServerTime")[0];
  118. var minDiff = 0;
  119. if (force_offset) wpTimeZone.selectedIndex = 1;
  120. if (wpTimeZone.selectedIndex == 1) {
  121. wpHourDiff.disabled = false;
  122. var diffArr = wpHourDiff.value.split(':');
  123. if (diffArr.length == 1) {
  124. minDiff = parseInt(diffArr[0], 10) * 60;
  125. } else {
  126. minDiff = Math.abs(parseInt(diffArr[0], 10))*60 + parseInt(diffArr[1], 10);
  127. if (parseInt(diffArr[0], 10) < 0) minDiff = -minDiff;
  128. }
  129. } else {
  130. wpHourDiff.disabled = true;
  131. var diffArr = wpTimeZone.options[wpTimeZone.selectedIndex].value.split('|');
  132. minDiff = parseInt(diffArr[1], 10);
  133. }
  134. if (isNaN(minDiff)) minDiff = 0;
  135. var localTime = parseInt(wpServerTime.value, 10) + minDiff;
  136. while (localTime < 0) localTime += 1440;
  137. while (localTime >= 1440) localTime -= 1440;
  138. var hour = String(Math.floor(localTime/60));
  139. if (hour.length<2) hour = '0'+hour;
  140. var min = String(localTime%60);
  141. if (min.length<2) min = '0'+min;
  142. changeText(wpLocalTime, hour+':'+min);
  143. if (wpTimeZone.selectedIndex != 1) {
  144. hour = String(Math.abs(Math.floor(minDiff/60)));
  145. if (hour.length<2) hour = '0'+hour;
  146. if (minDiff < 0) hour = '-'+hour;
  147. min = String(minDiff%60);
  148. if (min.length<2) min = '0'+min;
  149. wpHourDiff.value = hour+':'+min;
  150. }
  151. }
  152. hookEvent("load", unhidetzbutton);
  153. hookEvent("load", tabbedprefs);