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