xref: /llvm-project/clang-tools-extra/clang-doc/assets/index.js (revision 91450f1b57b34034376662dae5452af8c992c103)
1*91450f1bSPeterChou1function genLink(Ref) {
2*91450f1bSPeterChou1  // we treat the file paths different depending on if we're
3*91450f1bSPeterChou1  // serving via a http server or viewing from a local
4*91450f1bSPeterChou1  var Path = window.location.protocol.startsWith("file") ?
5*91450f1bSPeterChou1      `${window.location.protocol}//${window.location.host}/${Ref.Path}` :
6*91450f1bSPeterChou1      `${window.location.protocol}//${RootPath}/${Ref.Path}`;
7*91450f1bSPeterChou1  if (Ref.RefType === "namespace") {
8*91450f1bSPeterChou1    Path = `${Path}/index.html`
9*91450f1bSPeterChou1  } else if (Ref.Path === "") {
10*91450f1bSPeterChou1      Path = `${Path}${Ref.Name}.html`;
11*91450f1bSPeterChou1  } else {
12*91450f1bSPeterChou1    Path = `${Path}/${Ref.Name}.html`;
137dfe0bc3SDiego Astiazaran  }
147dfe0bc3SDiego Astiazaran  ANode = document.createElement("a");
157dfe0bc3SDiego Astiazaran  ANode.setAttribute("href", Path);
167dfe0bc3SDiego Astiazaran  var TextNode = document.createTextNode(Ref.Name);
177dfe0bc3SDiego Astiazaran  ANode.appendChild(TextNode);
187dfe0bc3SDiego Astiazaran  return ANode;
197dfe0bc3SDiego Astiazaran}
207dfe0bc3SDiego Astiazaran
213550da79SDiego Astiazaranfunction genHTMLOfIndex(Index, CurrentDirectory, IsOutermostList) {
227dfe0bc3SDiego Astiazaran  // Out will store the HTML elements that Index requires to be generated
237dfe0bc3SDiego Astiazaran  var Out = [];
247dfe0bc3SDiego Astiazaran  if (Index.Name) {
257dfe0bc3SDiego Astiazaran    var SpanNode = document.createElement("span");
267dfe0bc3SDiego Astiazaran    var TextNode = document.createTextNode(Index.Name);
277dfe0bc3SDiego Astiazaran    SpanNode.appendChild(genLink(Index, CurrentDirectory));
287dfe0bc3SDiego Astiazaran    Out.push(SpanNode);
297dfe0bc3SDiego Astiazaran  }
307dfe0bc3SDiego Astiazaran  if (Index.Children.length == 0)
317dfe0bc3SDiego Astiazaran    return Out;
323550da79SDiego Astiazaran  // Only the outermost list should use ol, the others should use ul
333550da79SDiego Astiazaran  var ListNodeName = IsOutermostList ? "ol" : "ul";
343550da79SDiego Astiazaran  var ListNode = document.createElement(ListNodeName);
357dfe0bc3SDiego Astiazaran  for (Child of Index.Children) {
367dfe0bc3SDiego Astiazaran    var LiNode = document.createElement("li");
373550da79SDiego Astiazaran    ChildNodes = genHTMLOfIndex(Child, CurrentDirectory, false);
387dfe0bc3SDiego Astiazaran    for (Node of ChildNodes)
397dfe0bc3SDiego Astiazaran      LiNode.appendChild(Node);
403550da79SDiego Astiazaran    ListNode.appendChild(LiNode);
417dfe0bc3SDiego Astiazaran  }
423550da79SDiego Astiazaran  Out.push(ListNode);
437dfe0bc3SDiego Astiazaran  return Out;
447dfe0bc3SDiego Astiazaran}
457dfe0bc3SDiego Astiazaran
467dfe0bc3SDiego Astiazaranfunction createIndex(Index) {
477dfe0bc3SDiego Astiazaran  // Get the DOM element where the index will be created
483550da79SDiego Astiazaran  var IndexDiv = document.getElementById("sidebar-left");
497dfe0bc3SDiego Astiazaran  // Get the relative path of this file
507dfe0bc3SDiego Astiazaran  CurrentDirectory = IndexDiv.getAttribute("path");
513550da79SDiego Astiazaran  var IndexNodes = genHTMLOfIndex(Index, CurrentDirectory, true);
527dfe0bc3SDiego Astiazaran  for (Node of IndexNodes)
537dfe0bc3SDiego Astiazaran    IndexDiv.appendChild(Node);
547dfe0bc3SDiego Astiazaran}
557dfe0bc3SDiego Astiazaran
567dfe0bc3SDiego Astiazaran// Runs after DOM loads
577dfe0bc3SDiego Astiazarandocument.addEventListener("DOMContentLoaded", function() {
58a9b1e80aSPeterChou1  // LoadIndex is an asynchronous function that will be generated clang-doc.
59a9b1e80aSPeterChou1  // It ensures that the function call will not block as soon the page loads,
60a9b1e80aSPeterChou1  // since the index object are often huge and can contain thousands of lines.
61a9b1e80aSPeterChou1  LoadIndex().then((Index) => { createIndex(Index); });
627dfe0bc3SDiego Astiazaran});
63