-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
464 lines (423 loc) · 13.3 KB
/
Copy pathmain.js
File metadata and controls
464 lines (423 loc) · 13.3 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
let selectedButton = null;
let settings = {
indentation: "expansion",
aliases: false
};
function selectButton(button) {
if (selectedButton) {
selectedButton.classList.remove("selected");
}
selectedButton = button;
selectedButton.classList.add("selected");
selectedButton.focus();
}
function getChildren(button) {
return button.parentElement.querySelector(".children");
}
function getButtonParent(button) {
if (button != document.getElementById("root")) {
return button.parentElement.parentElement.parentElement.querySelector(".node");
}
}
function getButtonFirstChild(button) {
if (!(children = getChildren(button))) return;
if (!(li = children.firstElementChild)) return;
return li.querySelector(".node");
}
function getButtonLastChild(button) {
if (!(children = getChildren(button))) return;
if (!(li = children.lastElementChild)) return;
return li.querySelector(".node");
}
function getButtonNextSibling(button) {
if (button == document.getElementById("root")) return;
let sibling = button.parentElement.nextElementSibling;
if (sibling) return sibling.querySelector(".node");
}
function getButtonPreviousSibling(button) {
if (button == document.getElementById("root")) return;
let sibling = button.parentElement.previousElementSibling;
if (sibling) return sibling.querySelector(".node");
}
function expandstr(str, n) {
if (str == "Root") {
return notation.expandLimit(n);
}
return notation.expand(notation.fromString(str), n);
}
function unexpandButton(button) {
const children = getChildren(button);
if (!children) return;
if (children.classList.contains("expanded")) {
if (children.firstElementChild.contains(selectedButton)) selectButton(button);
children.removeChild(children.firstElementChild);
if (!children.firstElementChild) {
children.classList.toggle("expanded");
button.parentElement.querySelector(".collapse").classList.add("hidden");
}
return true;
}
}
function expandButton(button) {
const children = getChildren(button);
if (!children) return;
if (!children.classList.contains("expanded")) {
children.classList.toggle("expanded");
button.parentElement.querySelector(".collapse").classList.remove("hidden");
}
let str = button.getAttribute("value") || "Root";
let index = 0;
if (notation.isSuccessor(expandstr(str, 0)) && !notation.isSuccessor(expandstr(str, 1))) {
index++; // avoid cases where a limit of limits expands into a successor at index 0
}
let compare = nextButtonDown(button);
if (compare) {
let compareTerm = notation.fromString(compare.getAttribute("value"));
let low = index;
let high = index + 1;
while (notation.lessOrEqual(expandstr(str, high), compareTerm)) {
low = high;
high *= 2;
if (high > 10000) throw new Error("index is too high");
}
while (low < high) { // binary search
const mid = Math.floor((low + high) / 2);
if (notation.lessOrEqual(expandstr(str, mid), compareTerm)) {
low = mid + 1;
} else {
high = mid;
}
}
index = low;
}
let term = expandstr(str, index);
return createButton(notation.toString(term), children, notation.isSuccessor(term));
}
function expandButtonRecursively(button) {
const ms = Date.now();
for (let i = 0; i < 1000; i++) {
button = expandButton(button);
if (!button) break;
if (Date.now() - ms > 200) break;
}
}
function indentli(li, index) {
if (settings.indentation == "expansion") {
li.style.marginLeft = `calc(25px * ${index})`;
} else if (settings.indentation == "noindent") {
li.style.marginLeft = ``;
} else if (settings.indentation == "FS") {
li.style.marginLeft = `25px`;
}
}
function createButton(value, parent, isLeaf) {
const childItem = document.createElement("li");
indentli(childItem, parent.children.length);
const button = document.createElement("button");
button.classList.add("node");
const collapse = document.createElement("button");
collapse.classList.add("toggle");
collapse.classList.add("collapse")
collapse.classList.add("hidden");
collapse.innerText = "<";
childItem.appendChild(collapse);
collapse.addEventListener("click", () => unexpandButton(button));
const expand = document.createElement("button");
expand.classList.add("toggle");
if (isLeaf) expand.classList.add("hidden");
expand.innerText = "+";
childItem.appendChild(expand);
expand.addEventListener("click", () => expandButtonRecursively(button));
childItem.appendChild(button);
if (!isLeaf) {
const childrenList = document.createElement("ul");
childrenList.classList.add("children");
childItem.appendChild(childrenList);
}
if (value !== null) {
button.setAttribute("value", value);
if (notation.convertToNotation) {
button.innerText = notation.convertToNotation(value);
} else {
button.innerText = value;
}
} else { // root node
button.innerText = notation.rootText || "Limit";
}
parent.prepend(childItem);
return button;
}
function nextButtonDown(button) {
if (firstChild = getButtonFirstChild(button)) {
return firstChild;
} else if (nextSibling = getButtonNextSibling(button)) {
return nextSibling;
} else if (parentButton = getButtonParent(button)) {
while (parentButton && !getButtonNextSibling(parentButton)) {
parentButton = getButtonParent(parentButton);
}
if (parentButton && (parentSibling = getButtonNextSibling(parentButton))) {
return parentSibling;
}
}
}
function moveSelectionDown() {
if (nextButton = nextButtonDown(selectedButton)) {
selectButton(nextButton);
}
}
function moveSelectionUp() {
if (selection = getButtonPreviousSibling(selectedButton)) {
while (getButtonLastChild(selection)) {
selection = getButtonLastChild(selection);
}
selectButton(selection);
} else if (parentButton = getButtonParent(selectedButton)) {
selectButton(parentButton);
}
}
function refreshIndentation() {
let stack = [document.getElementById("root")];
while (stack.length > 0) {
let button = stack.pop();
let current = getButtonLastChild(button);
let index = 0;
while (current) {
indentli(current.parentElement, index);
stack.push(current);
index++;
current = getButtonPreviousSibling(current);
}
}
}
function refreshTerms() {
let stack = [document.getElementById("root")];
while (stack.length > 0) {
let button = stack.pop();
let current = getButtonLastChild(button);
while (current) {
current.innerText = notation.convertToNotation(current.getAttribute("value"));
stack.push(current);
current = getButtonPreviousSibling(current);
}
}
}
function setIndentation(newIndentation) {
settings.indentation = newIndentation;
refreshIndentation();
}
function initialize() {
let titleElement = document.head.querySelector("title");
if (!titleElement) {
titleElement = document.createElement("title");
document.head.appendChild(titleElement);
}
let headerElement = document.body.querySelector("h2");
if (!headerElement) {
headerElement = document.createElement("h2");
document.body.insertBefore(headerElement, document.body.firstChild);
}
if (notation.title) {
titleElement.textContent = notation.title;
headerElement.textContent = notation.header || notation.title;
} else if (notation.missing) {
titleElement.textContent = "404 🧇";
headerElement.textContent = "404 notation not found, here's worm instead"
}
let container = document.getElementById("container");
if (!container) {
container = document.createElement("div");
container.setAttribute("id", "container");
document.body.appendChild(container);
}
let tree = document.getElementById("tree");
if (!tree) {
tree = document.createElement("div");
tree.setAttribute("id", "tree");
container.appendChild(tree);
}
let rootButton = document.getElementById("root");
if (!rootButton) {
rootButton = createButton(null, tree, false)
rootButton.setAttribute("id", "root");
}
selectButton(rootButton);
let settingsContainer = document.getElementById("settings");
if (!settingsContainer) {
settingsContainer = document.createElement("div");
settingsContainer.setAttribute("id", "settings");
container.appendChild(settingsContainer);
}
if (!notation.parameters) {
notation.parameters = [];
}
// Add default indentation parameter
notation.parameters.unshift({
legend: "Indentation:",
inputs: [
{type: "radio", id: "indentation", value: "expansion", label: "Align recursive expansion"},
{type: "radio", id: "indentation", value: "FS", label: "Align fundamental sequence"},
{type: "radio", id: "indentation", value: "noindent", label: "None"}
]
});
notation.indentation = "expansion";
// Read URL parameters for parameters with url: true
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
for (let param of notation.parameters) {
if (!param.url) continue;
const urlValue = urlParams.get(param.id);
if (urlValue != null) {
// Set value based on type
if (param.type === "checkbox") {
notation[param.id] = urlValue === "true" || urlValue === "1";
} else if (param.type === "radio") {
notation[param.id] = urlValue;
} else {
notation[param.id] = urlValue;
}
}
}
// Function to render a single parameter
function renderParam(param, container) {
let cont;
if (param.legend) {
cont = document.createElement("fieldset");
let legend = document.createElement("legend");
legend.textContent = param.legend;
cont.appendChild(legend);
} else {
cont = document.createElement("div");
}
container.appendChild(cont);
let inputs = param.inputs || [param];
for (let input of inputs) {
if (input.url) continue; // skip URL parameters
let div = document.createElement("div");
if (input.visibleIf) {
div.className = "param-visibility";
div._visibleIf = input.visibleIf;
}
let inp = document.createElement("input");
inp.type = input.type;
inp.id = input.id;
if (input.name) {
inp.name = input.name;
} else if (input.type === "radio") {
inp.name = input.id;
}
if (input.type === "checkbox") {
inp.checked = notation[input.id];
inp.addEventListener('change', () => {
notation[input.id] = inp.checked;
refreshTerms();
checkVisibility();
});
} else if (input.type === "radio") {
inp.value = input.value;
if (notation[input.id] === input.value) {
inp.checked = true;
}
inp.addEventListener('change', () => {
if (inp.checked) {
notation[input.id] = inp.value;
// Call setIndentation for indentation radio buttons
if (input.id === "indentation") {
setIndentation(inp.value);
} else {
refreshTerms();
}
checkVisibility();
}
});
}
div.appendChild(inp);
let label = document.createElement("label");
label.setAttribute("for", input.id);
label.textContent = ' ' + input.label;
div.appendChild(label);
cont.appendChild(div);
}
}
// Render parameters
for (let param of notation.parameters) {
if (param.url) continue;
renderParam(param, settingsContainer);
}
function checkVisibility() {
// Check visibility for parameters with visibleIf conditions
document.querySelectorAll('.param-visibility').forEach(div => {
if (div._visibleIf) {
div.style.display = div._visibleIf() ? "block" : "none";
}
});
}
checkVisibility();
let footer = document.getElementById("footer");
if (!footer) {
footer = document.createElement("div");
footer.setAttribute("id", "footer");
if (notation.footer) {
footer.innerHTML = notation.footer + " | ";
}
const indexLink = document.createElement("a");
indexLink.textContent = "Index";
let indexHref = "..";
const baseEl = document.querySelector("base");
if (baseEl && baseEl.href) {
indexHref = baseEl.href;
}
indexLink.setAttribute("href", indexHref);
footer.appendChild(indexLink);
document.body.appendChild(footer);
}
document.addEventListener("mousedown", (event) => {
const targetNode = event.target.closest(".node");
if (targetNode) {
event.preventDefault();
expandButton(targetNode);
selectButton(targetNode);
}
});
document.addEventListener("mouseup", (event) => {
const tree = document.getElementById("tree");
if (tree.contains(event.target) || event.target == document.body) {
if (window.getSelection().toString() === '') { // no selected text
selectedButton.focus();
}
}
});
document.addEventListener("keydown", (event) => {
const tree = document.getElementById("tree");
if (event.target != tree && event.target != document.body && !tree.contains(event.target)) return;
let handled = true;
if (event.key === "ArrowDown") {
moveSelectionDown();
} else if (event.key === "ArrowUp") {
moveSelectionUp();
} else if (event.key === "Backspace" || event.key === "ArrowLeft") { // unexpand
if (!unexpandButton(selectedButton)) {
moveSelectionUp(); // move up if there was nothing to unexpand
}
} else if (event.key === "Enter") { // expand recursively
expandButtonRecursively(selectedButton);
} else if (event.key === " ") { // expand once
expandButton(selectedButton);
} else if (event.key === "ArrowRight") { // expand and move selection down
expandButton(selectedButton);
moveSelectionDown();
} else if ((event.ctrlKey || event.metaKey) && event.key === 'c' && window.getSelection().toString() === '') {
navigator.clipboard.writeText(selectedButton.innerText);
} else {
handled = false;
}
if (handled) {
event.preventDefault();
}
});
}
if (document.readyState === 'complete') {
initialize();
} else {
window.addEventListener("load", initialize);
}