| 1 |
// Append using posix-style a file name or directory to Base |
| 2 |
function append(Base, New) { |
| 3 |
if (!New) |
| 4 |
return Base; |
| 5 |
if (Base) |
| 6 |
Base += "/"; |
| 7 |
Base += New; |
| 8 |
return Base; |
| 9 |
} |
| 10 |
|
| 11 |
// Get relative path to access FilePath from CurrentDirectory |
| 12 |
function computeRelativePath(FilePath, CurrentDirectory) { |
| 13 |
var Path = FilePath; |
| 14 |
while (Path) { |
| 15 |
if (CurrentDirectory == Path) |
| 16 |
return FilePath.substring(Path.length + 1); |
| 17 |
Path = Path.substring(0, Path.lastIndexOf("/")); |
| 18 |
} |
| 19 |
|
| 20 |
var Dir = CurrentDirectory; |
| 21 |
var Result = ""; |
| 22 |
while (Dir) { |
| 23 |
if (Dir == FilePath) |
| 24 |
break; |
| 25 |
Dir = Dir.substring(0, Dir.lastIndexOf("/")); |
| 26 |
Result = append(Result, "..") |
| 27 |
} |
| 28 |
Result = append(Result, FilePath.substring(Dir.length)) |
| 29 |
return Result; |
| 30 |
} |
| 31 |
|
| 32 |
function genLink(Ref, CurrentDirectory) { |
| 33 |
var Path = computeRelativePath(Ref.Path, CurrentDirectory); |
| 34 |
if (Ref.RefType == "namespace") |
| 35 |
Path = append(Path, "index.html"); |
| 36 |
else |
| 37 |
Path = append(Path, Ref.Name + ".html") |
| 38 |
|
| 39 |
ANode = document.createElement("a"); |
| 40 |
ANode.setAttribute("href", Path); |
| 41 |
var TextNode = document.createTextNode(Ref.Name); |
| 42 |
ANode.appendChild(TextNode); |
| 43 |
return ANode; |
| 44 |
} |
| 45 |
|
| 46 |
function genHTMLOfIndex(Index, CurrentDirectory, IsOutermostList) { |
| 47 |
// Out will store the HTML elements that Index requires to be generated |
| 48 |
var Out = []; |
| 49 |
if (Index.Name) { |
| 50 |
var SpanNode = document.createElement("span"); |
| 51 |
var TextNode = document.createTextNode(Index.Name); |
| 52 |
SpanNode.appendChild(genLink(Index, CurrentDirectory)); |
| 53 |
Out.push(SpanNode); |
| 54 |
} |
| 55 |
if (Index.Children.length == 0) |
| 56 |
return Out; |
| 57 |
// Only the outermost list should use ol, the others should use ul |
| 58 |
var ListNodeName = IsOutermostList ? "ol" : "ul"; |
| 59 |
var ListNode = document.createElement(ListNodeName); |
| 60 |
for (Child of Index.Children) { |
| 61 |
var LiNode = document.createElement("li"); |
| 62 |
ChildNodes = genHTMLOfIndex(Child, CurrentDirectory, false); |
| 63 |
for (Node of ChildNodes) |
| 64 |
LiNode.appendChild(Node); |
| 65 |
ListNode.appendChild(LiNode); |
| 66 |
} |
| 67 |
Out.push(ListNode); |
| 68 |
return Out; |
| 69 |
} |
| 70 |
|
| 71 |
function createIndex(Index) { |
| 72 |
// Get the DOM element where the index will be created |
| 73 |
var IndexDiv = document.getElementById("sidebar-left"); |
| 74 |
// Get the relative path of this file |
| 75 |
CurrentDirectory = IndexDiv.getAttribute("path"); |
| 76 |
var IndexNodes = genHTMLOfIndex(Index, CurrentDirectory, true); |
| 77 |
for (Node of IndexNodes) |
| 78 |
IndexDiv.appendChild(Node); |
| 79 |
} |
| 80 |
|
| 81 |
// Runs after DOM loads |
| 82 |
document.addEventListener("DOMContentLoaded", function() { |
| 83 |
// JsonIndex is a variable from another file that contains the index |
| 84 |
// in JSON format |
| 85 |
var Index = JSON.parse(JsonIndex); |
| 86 |
createIndex(Index); |
| 87 |
}); |