This repository was archived by the owner on Aug 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
158 lines (142 loc) · 5.59 KB
/
main.js
File metadata and controls
158 lines (142 loc) · 5.59 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
function urlParam(key, value) {
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
}
function renderFunction(json) {
// Renders the function's name and documentation, as well as buttons
// to delete and save said function.
const msg = document.getElementById("function_msg");
msg.innerHTML = "<h2>" + json["msg"] + "</h2>";
let output = "";
for (const fn of json["functions"]) {
output += `<div data-uuid="${fn.uuid}"\>`;
// function header
output += `<table><thead><tr><td><h3>${fn.name}</h3></td><td>`;
output += `<td> <input type="checkbox" style="width: 16px; height: 16px" data-uuid="${fn.uuid}"> </td>`;
output += `<td> <input type="image" src="trash.svg" style="width:16px;height:16px;" onclick=onClickDelete("${fn.uuid}")></td>`;
output += "</td></tr></thead></table>";
// function paragraph
output += `<p>${fn.docs}</p>`;
output += "</div>";
}
document.getElementById("functions").innerHTML = output;
msg.scrollIntoView();
}
function mainFormToURL() {
// Grabs the items from the main form, and correctly formats them for
// use server-side.
const form = document
.getElementById("search_form");
const oldFormData = new FormData(form);
const formData = [];
for (const pair of oldFormData.entries()) {
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) {
renderFunction(json);
}
if (this.status === 404) {
const msg = document.getElementById("function_msg");
msg.innerHTML = "<h2 style='color:red'>" + json["msg"] + "</h2>";
document.getElementById("functions").innerHTML = "";
msg.scrollIntoView();
}
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
function removeUuidFromScreen(uuid) {
// Deletes all instances of a particular UUID
const elements = document.querySelectorAll(`div[data-uuid="${uuid}"]`);
Array.prototype.forEach.call(elements, function(node) {
node.remove();
});
}
function onClickDelete(uuid) {
// Remove the provided UUID from the screen and from the server. If the
// uuid belongs to a specialized function, its parent will be deleted,
// and all children on the page will be removed.
removeUuidFromScreen(uuid);
const xmlhttp = new XMLHttpRequest();
const url = "/func?uuid=" + uuid;
xmlhttp.onreadystatechange = function() {
if (this.readyState === 4) {
const json = JSON.parse(this.responseText);
if (this.status === 200) {
json["uuids"].forEach(removeUuidFromScreen);
} else if (this.status === 405) {
onClickDelete(json["parent_uuid"]);
}
}
};
xmlhttp.open("DELETE", url, true);
xmlhttp.send();
}
function clearResults() {
// Clears all areas where results are displayed.
document.getElementById("function_msg").innerHTML = "";
document.getElementById("functions").innerHTML = "";
}
function saveResults() {
// Saves the user's results to a secondary div, without adding duplicate copies of any particular
// result.
const functions = document.getElementById("functions");
const selectedFunctions = functions.querySelectorAll('input[type=checkbox]');
const savedFunctions = document.getElementById("saved_functions");
if (!savedFunctions.innerHTML) {
savedFunctions.innerHTML = "<h2>Saved Functions</h2>";
}
Array.from(selectedFunctions)
.filter((el) => el.checked)
.map((el) => `div[data-uuid="${el.getAttribute("data-uuid")}"]`)
.filter((selector) => savedFunctions.querySelectorAll(selector).length === 0)
.forEach((selector) => {
const child = document.querySelector(selector)
.cloneNode(true);
child.querySelectorAll('input[type=checkbox]').forEach(
(el) => el.remove()
);
savedFunctions.appendChild(child);
});
}
function toJsonAndPush() {
// Extracts the items from the Function Add form, formats them as JSON,
// and sends a post request to the server.
const form = document.getElementById("add_function_form");
const formData = new FormData(form);
const dict = { generics: [] };
for (const pair of formData.entries()) {
if (pair[0] === "inputs" || pair[0] === "outputs") {
dict[pair[0]] = pair[1].split(",").map((s) => s.trim()).filter((s) => s.length);
} else {
dict[pair[0]] = pair[1];
}
}
const xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "/func",true);
xmlhttp.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
xmlhttp.send(JSON.stringify(dict));
form.reset();
}