xref: /llvm-project/clang-tools-extra/clang-doc/assets/index.js (revision 91450f1b57b34034376662dae5452af8c992c103)
1function genLink(Ref) {
2  // we treat the file paths different depending on if we're
3  // serving via a http server or viewing from a local
4  var Path = window.location.protocol.startsWith("file") ?
5      `${window.location.protocol}//${window.location.host}/${Ref.Path}` :
6      `${window.location.protocol}//${RootPath}/${Ref.Path}`;
7  if (Ref.RefType === "namespace") {
8    Path = `${Path}/index.html`
9  } else if (Ref.Path === "") {
10      Path = `${Path}${Ref.Name}.html`;
11  } else {
12    Path = `${Path}/${Ref.Name}.html`;
13  }
14  ANode = document.createElement("a");
15  ANode.setAttribute("href", Path);
16  var TextNode = document.createTextNode(Ref.Name);
17  ANode.appendChild(TextNode);
18  return ANode;
19}
20
21function genHTMLOfIndex(Index, CurrentDirectory, IsOutermostList) {
22  // Out will store the HTML elements that Index requires to be generated
23  var Out = [];
24  if (Index.Name) {
25    var SpanNode = document.createElement("span");
26    var TextNode = document.createTextNode(Index.Name);
27    SpanNode.appendChild(genLink(Index, CurrentDirectory));
28    Out.push(SpanNode);
29  }
30  if (Index.Children.length == 0)
31    return Out;
32  // Only the outermost list should use ol, the others should use ul
33  var ListNodeName = IsOutermostList ? "ol" : "ul";
34  var ListNode = document.createElement(ListNodeName);
35  for (Child of Index.Children) {
36    var LiNode = document.createElement("li");
37    ChildNodes = genHTMLOfIndex(Child, CurrentDirectory, false);
38    for (Node of ChildNodes)
39      LiNode.appendChild(Node);
40    ListNode.appendChild(LiNode);
41  }
42  Out.push(ListNode);
43  return Out;
44}
45
46function createIndex(Index) {
47  // Get the DOM element where the index will be created
48  var IndexDiv = document.getElementById("sidebar-left");
49  // Get the relative path of this file
50  CurrentDirectory = IndexDiv.getAttribute("path");
51  var IndexNodes = genHTMLOfIndex(Index, CurrentDirectory, true);
52  for (Node of IndexNodes)
53    IndexDiv.appendChild(Node);
54}
55
56// Runs after DOM loads
57document.addEventListener("DOMContentLoaded", function() {
58  // LoadIndex is an asynchronous function that will be generated clang-doc.
59  // It ensures that the function call will not block as soon the page loads,
60  // since the index object are often huge and can contain thousands of lines.
61  LoadIndex().then((Index) => { createIndex(Index); });
62});
63