report.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. (function (window, document) {
  2. "use strict";
  3. var tabs = {};
  4. function changeElementClass(element, classValue) {
  5. if (element.getAttribute("className")) {
  6. element.setAttribute("className", classValue);
  7. } else {
  8. element.setAttribute("class", classValue);
  9. }
  10. }
  11. function getClassAttribute(element) {
  12. if (element.getAttribute("className")) {
  13. return element.getAttribute("className");
  14. } else {
  15. return element.getAttribute("class");
  16. }
  17. }
  18. function addClass(element, classValue) {
  19. changeElementClass(element, getClassAttribute(element) + " " + classValue);
  20. }
  21. function removeClass(element, classValue) {
  22. changeElementClass(element, getClassAttribute(element).replace(classValue, ""));
  23. }
  24. function initTabs() {
  25. var container = document.getElementById("tabs");
  26. tabs.tabs = findTabs(container);
  27. tabs.titles = findTitles(tabs.tabs);
  28. tabs.headers = findHeaders(container);
  29. tabs.select = select;
  30. tabs.deselectAll = deselectAll;
  31. tabs.select(0);
  32. return true;
  33. }
  34. function getCheckBox() {
  35. return document.getElementById("line-wrapping-toggle");
  36. }
  37. function getLabelForCheckBox() {
  38. return document.getElementById("label-for-line-wrapping-toggle");
  39. }
  40. function findCodeBlocks() {
  41. var spans = document.getElementById("tabs").getElementsByTagName("span");
  42. var codeBlocks = [];
  43. for (var i = 0; i < spans.length; ++i) {
  44. if (spans[i].className.indexOf("code") >= 0) {
  45. codeBlocks.push(spans[i]);
  46. }
  47. }
  48. return codeBlocks;
  49. }
  50. function forAllCodeBlocks(operation) {
  51. var codeBlocks = findCodeBlocks();
  52. for (var i = 0; i < codeBlocks.length; ++i) {
  53. operation(codeBlocks[i], "wrapped");
  54. }
  55. }
  56. function toggleLineWrapping() {
  57. var checkBox = getCheckBox();
  58. if (checkBox.checked) {
  59. forAllCodeBlocks(addClass);
  60. } else {
  61. forAllCodeBlocks(removeClass);
  62. }
  63. }
  64. function initControls() {
  65. if (findCodeBlocks().length > 0) {
  66. var checkBox = getCheckBox();
  67. var label = getLabelForCheckBox();
  68. checkBox.onclick = toggleLineWrapping;
  69. checkBox.checked = false;
  70. removeClass(label, "hidden");
  71. }
  72. }
  73. function switchTab() {
  74. var id = this.id.substr(1);
  75. for (var i = 0; i < tabs.tabs.length; i++) {
  76. if (tabs.tabs[i].id === id) {
  77. tabs.select(i);
  78. break;
  79. }
  80. }
  81. return false;
  82. }
  83. function select(i) {
  84. this.deselectAll();
  85. changeElementClass(this.tabs[i], "tab selected");
  86. changeElementClass(this.headers[i], "selected");
  87. while (this.headers[i].firstChild) {
  88. this.headers[i].removeChild(this.headers[i].firstChild);
  89. }
  90. var h2 = document.createElement("H2");
  91. h2.appendChild(document.createTextNode(this.titles[i]));
  92. this.headers[i].appendChild(h2);
  93. }
  94. function deselectAll() {
  95. for (var i = 0; i < this.tabs.length; i++) {
  96. changeElementClass(this.tabs[i], "tab deselected");
  97. changeElementClass(this.headers[i], "deselected");
  98. while (this.headers[i].firstChild) {
  99. this.headers[i].removeChild(this.headers[i].firstChild);
  100. }
  101. var a = document.createElement("A");
  102. a.setAttribute("id", "ltab" + i);
  103. a.setAttribute("href", "#tab" + i);
  104. a.onclick = switchTab;
  105. a.appendChild(document.createTextNode(this.titles[i]));
  106. this.headers[i].appendChild(a);
  107. }
  108. }
  109. function findTabs(container) {
  110. return findChildElements(container, "DIV", "tab");
  111. }
  112. function findHeaders(container) {
  113. var owner = findChildElements(container, "UL", "tabLinks");
  114. return findChildElements(owner[0], "LI", null);
  115. }
  116. function findTitles(tabs) {
  117. var titles = [];
  118. for (var i = 0; i < tabs.length; i++) {
  119. var tab = tabs[i];
  120. var header = findChildElements(tab, "H2", null)[0];
  121. header.parentNode.removeChild(header);
  122. if (header.innerText) {
  123. titles.push(header.innerText);
  124. } else {
  125. titles.push(header.textContent);
  126. }
  127. }
  128. return titles;
  129. }
  130. function findChildElements(container, name, targetClass) {
  131. var elements = [];
  132. var children = container.childNodes;
  133. for (var i = 0; i < children.length; i++) {
  134. var child = children.item(i);
  135. if (child.nodeType === 1 && child.nodeName === name) {
  136. if (targetClass && child.className.indexOf(targetClass) < 0) {
  137. continue;
  138. }
  139. elements.push(child);
  140. }
  141. }
  142. return elements;
  143. }
  144. // Entry point.
  145. window.onload = function() {
  146. initTabs();
  147. initControls();
  148. };
  149. } (window, window.document));