report.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. var tabs = new Object();
  2. function initTabs() {
  3. var container = document.getElementById('tabs');
  4. tabs.tabs = findTabs(container);
  5. tabs.titles = findTitles(tabs.tabs);
  6. tabs.headers = findHeaders(container);
  7. tabs.select = select;
  8. tabs.deselectAll = deselectAll;
  9. tabs.select(0);
  10. return true;
  11. }
  12. window.onload = initTabs;
  13. function switchTab() {
  14. var id = this.id.substr(1);
  15. for (var i = 0; i < tabs.tabs.length; i++) {
  16. if (tabs.tabs[i].id == id) {
  17. tabs.select(i);
  18. break;
  19. }
  20. }
  21. return false;
  22. }
  23. function select(i) {
  24. this.deselectAll();
  25. changeElementClass(this.tabs[i], 'tab selected');
  26. changeElementClass(this.headers[i], 'selected');
  27. while (this.headers[i].firstChild) {
  28. this.headers[i].removeChild(this.headers[i].firstChild);
  29. }
  30. var h2 = document.createElement('H2');
  31. h2.appendChild(document.createTextNode(this.titles[i]));
  32. this.headers[i].appendChild(h2);
  33. }
  34. function deselectAll() {
  35. for (var i = 0; i < this.tabs.length; i++) {
  36. changeElementClass(this.tabs[i], 'tab deselected');
  37. changeElementClass(this.headers[i], 'deselected');
  38. while (this.headers[i].firstChild) {
  39. this.headers[i].removeChild(this.headers[i].firstChild);
  40. }
  41. var a = document.createElement('A');
  42. a.setAttribute('id', 'ltab' + i);
  43. a.setAttribute('href', '#tab' + i);
  44. a.onclick = switchTab;
  45. a.appendChild(document.createTextNode(this.titles[i]));
  46. this.headers[i].appendChild(a);
  47. }
  48. }
  49. function changeElementClass(element, classValue) {
  50. if (element.getAttribute('className')) {
  51. /* IE */
  52. element.setAttribute('className', classValue)
  53. } else {
  54. element.setAttribute('class', classValue)
  55. }
  56. }
  57. function findTabs(container) {
  58. return findChildElements(container, 'DIV', 'tab');
  59. }
  60. function findHeaders(container) {
  61. var owner = findChildElements(container, 'UL', 'tabLinks');
  62. return findChildElements(owner[0], 'LI', null);
  63. }
  64. function findTitles(tabs) {
  65. var titles = new Array();
  66. for (var i = 0; i < tabs.length; i++) {
  67. var tab = tabs[i];
  68. var header = findChildElements(tab, 'H2', null)[0];
  69. header.parentNode.removeChild(header);
  70. if (header.innerText) {
  71. titles.push(header.innerText)
  72. } else {
  73. titles.push(header.textContent)
  74. }
  75. }
  76. return titles;
  77. }
  78. function findChildElements(container, name, targetClass) {
  79. var elements = new Array();
  80. var children = container.childNodes;
  81. for (var i = 0; i < children.length; i++) {
  82. var child = children.item(i);
  83. if (child.nodeType == 1 && child.nodeName == name) {
  84. if (targetClass && child.className.indexOf(targetClass) < 0) {
  85. continue;
  86. }
  87. elements.push(child);
  88. }
  89. }
  90. return elements;
  91. }