Source: document.js

  1. /**
  2. * @file Build table of contents and index.
  3. * @version April 20, 2017
  4. *
  5. * @author Olivier Pirson --- http://www.opimedia.be/
  6. * @license GPLv3 --- Copyright (C) 2017 Olivier Pirson
  7. */
  8. (function () {
  9. "use strict";
  10. /**
  11. * Build index
  12. * in a HTML list in #Index
  13. * with all .index with a id.
  14. */
  15. function buildIndex() {
  16. const elements = document.getElementsByClassName("index");
  17. const indices = [];
  18. for (let element of elements) {
  19. const id = element.id;
  20. if (id) {
  21. indices.push([element.innerHTML, id]);
  22. }
  23. }
  24. indices.sort(function(a, b) {
  25. a = a[0].toUpperCase();
  26. b = b[0].toUpperCase();
  27. return (a < b
  28. ? -1
  29. : (a > b
  30. ? 1
  31. : 0));
  32. });
  33. const lists = document.getElementById("Index").children[1].children;
  34. for (let i = 0; i < indices.length; ++i) {
  35. const index = indices[i];
  36. const li = document.createElement("li");
  37. li.innerHTML = '<a href="#' + index[1] + '">' + index[0] + '</a>';
  38. lists[(i > indices.length/3
  39. ? (i > 2*indices.length/3
  40. ? 2
  41. : 1)
  42. : 0)].appendChild(li);
  43. }
  44. }
  45. /**
  46. * Build the table of contents
  47. * in a HTML list listHtmlElement
  48. * with all id and > header > h* of sectionHtmlElements
  49. * and recursively for subsections.
  50. *
  51. * @param {HTMLElement} listHtmlElement
  52. * @param {HTMLCollection} sectionHtmlElements
  53. */
  54. function buildTableOfContents(listHtmlElement, sectionHtmlElements) {
  55. assert(listHtmlElement instanceof HTMLElement, listHtmlElement);
  56. assert(sectionHtmlElements instanceof HTMLCollection, sectionHtmlElements);
  57. for (let section of sectionHtmlElements) {
  58. const id = section.id;
  59. if (id !== "Contents") {
  60. const hContent = section.children[0].children[0].innerHTML;
  61. const matches = /^((.|\n)+)<a.+?<\/a>$/im.exec(hContent);
  62. const title = matches[1];
  63. const li = document.createElement("li");
  64. li.innerHTML = '<a href="#' + id + '">' + title + '</a>';
  65. const subSections = section.getElementsByTagName("section");
  66. if (subSections) {
  67. const subList = document.createElement("ul");
  68. buildTableOfContents(subList, subSections);
  69. li.appendChild(subList);
  70. }
  71. listHtmlElement.appendChild(li);
  72. }
  73. }
  74. }
  75. window.addEventListener("load", function () {
  76. buildIndex();
  77. buildTableOfContents(document.getElementById("Contents").children[1].children[0],
  78. document.getElementsByTagName("main")[0].children);
  79. // Show/hide subsections in the table contents panel
  80. document.getElementById("button-Contents-expand-collapse")
  81. .addEventListener("click",
  82. function () {
  83. const contents = document.getElementById("Contents");
  84. cssInvertClass(contents, "collapse");
  85. },
  86. false);
  87. // Show/hide the table contents panel
  88. document.getElementById("button-Contents-on-off")
  89. .addEventListener("click",
  90. function () {
  91. const contents = document.getElementById("Contents");
  92. cssInvertClass(contents, "off");
  93. },
  94. false);
  95. });
  96. }());