-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.js
138 lines (121 loc) · 4.29 KB
/
path.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Code for dealing with item highlighting
let oldHash = window.location.hash.slice(1);
document.addEventListener('DOMContentLoaded', init, false);
function init() {
window.addEventListener("hashchange", highlightLinked, false);
}
function urlParam(key, value) {
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
}
function renderFunctions(json) {
// Renders the function, including name and documentation, into the page
let output = "";
for (const fn of json["functions"]) {
let style = "padding: 24px 24px 24px 24px;";
if (oldHash === fn.uuid) {
style += "background-color: #FFFF9F;"
}
output += `<div id="${fn.uuid}" data-uuid="${fn["uuid"]}" style="${style}">`
output += `<h3>${fn.name}</h3>`
output += `<p>${fn.docs}</p>`;
output += "</div>";
}
document.getElementById("functions").innerHTML = output;
}
function renderPath(json) {
// Renders the paths in JSON format to the page.
const uuidMap = new Map();
json["functions"].forEach(function (x) {
uuidMap.set(x["uuid"], x);
});
const msg = document.getElementById("path_msg");
msg.innerHTML = "<h2>" + json["msg"] + "</h2>";
let output = "";
for (const path of json["paths"]) {
output += "<p>" + path.map((uuid) => (
`<a href="#${uuid}" class="link-dark">${uuidMap.get(uuid).name}</a>`
)).join(" -> ") + "<\p><br>";
}
document.getElementById("paths").innerHTML = output;
renderFunctions(json);
msg.scrollIntoView();
}
function mainFormToURL() {
// Grabs the items from the main form, and correctly formats them for
// use server-side.
const form = document
.getElementById("path_form");
const oldFormData = new FormData(form);
const formData = [];
for (const pair of oldFormData.entries()) {
if (pair[0].startsWith("discard")) {
continue;
}
if (pair[0] === "inputs" || pair[0] === "outputs") {
for (const item of pair[1].split(",")) {
if (!item.trim()) {
continue;
}
formData.push(urlParam(pair[0], item.trim()));
}
} else {
if (!pair[1]) {
continue;
}
formData.push(urlParam(pair[0], pair[1]));
}
}
return form.action + "?" + formData.join("&");
}
function searchAndDisplay() {
// When form is submitted, handles process of fetching all information
// and displaying it.
const url = mainFormToURL();
const xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState === 4) {
const json = JSON.parse(this.responseText);
if (this.status === 200) {
renderPath(json);
}
if (this.status === 404) {
document.getElementById("path_msg").innerHTML = "<h2 style='color:red'>" + json["msg"] + "</h2>";
document.getElementById("paths").innerHTML = "";
}
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
function clearResults() {
// Clears all areas where results are displayed
document.getElementById("path_msg").innerHTML = "";
document.getElementById("paths").innerHTML = "";
document.getElementById("functions").innerHTML = "";
}
function updatePathLen(updated) {
// Connects slider to text so that changing one changes the other
const pathLenSlider = document.getElementById("path_length");
const pathLenText = document.getElementById("path_length_t");
if (updated === "slider") {
pathLenText.value = pathLenSlider.value;
} else {
pathLenSlider.value = pathLenText.value;
}
}
function highlightLinked() {
// When user clicks on a link, they will be sent to a function and it
// will be highlighted.
const highlighted = document.getElementById(window.location.hash.slice(1));
// Remove old highlights
if (oldHash) {
const oldHighlighted = document.getElementById(oldHash);
if (oldHighlighted) {
oldHighlighted.style.backgroundColor = "#FFFFFF"
}
}
if (highlighted) {
highlighted.style.backgroundColor = "#FFFF9F";
}
oldHash = window.location.hash.slice(1);
}