forked from codante-io/rinha-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
175 lines (153 loc) · 4.92 KB
/
main.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
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
const main = document.querySelector("main");
const inputLabel = main.querySelector("label");
const input = main.querySelector("input");
const reader = new FileReader();
const queue = [];
const enqueuedByPass = 150;
let enqueuedThisPass = 0;
const lastElementObserver = new IntersectionObserver(handleScrollForward, { rootMargin: "0px 0px 20% 0px" });
let lastElement;
inputLabel.addEventListener("keypress", event => {
if (event.key === "Enter") input.click();
});
input.addEventListener("change", event => {
handleFileChange(event.currentTarget.files[0]);
});
function handleFileChange(file) {
reader.addEventListener("load", event => {
try {
const json = JSON.parse(event.currentTarget.result);
main.replaceChildren();
renderTitle(file.name);
enqueue(json, null, null, true);
for (const item of queue) render(item);
lastElement = document.getElementById(queue[queue.length - 1].id);
lastElementObserver.observe(lastElement);
enqueuedThisPass = 0;
} catch (error) {
console.log(error);
renderError();
}
});
reader.readAsText(file);
}
// ENQUEUING
function enqueue(data, parent = null, indexOnParent = null, root = false) {
const item = buildItem(data, parent, indexOnParent, root);
queue.push(item);
if (parent?.children != null) parent.children.push(item);
enqueuedThisPass += 1;
if (!isObject(data)) return item;
let childIndex = 0;
for (const [property, value] of Object.entries(data)) {
li = enqueue({}, item, childIndex);
enqueue(property, li, 0);
enqueue(value, li, 1);
childIndex += 1;
item.indexOfLastEnqueuedChild = childIndex;
if (enqueuedThisPass >= enqueuedByPass) break;
}
return item;
}
function buildItem(data, parent = null, indexOnParent = null, root) {
return {
id: crypto.randomUUID(),
data,
parent,
indexOnParent,
children: [],
indexOfLastEnqueuedChild: 0,
root,
};
}
// RENDERING
function renderTitle(filename) {
const title = document.createElement("h1");
title.textContent = filename;
title.setAttribute("id", "file-title");
main.appendChild(title);
}
function renderError() {
const renderedErrorMessage = document.getElementById("error-message");
if (renderedErrorMessage != null) return;
const errorMessage = document.createElement("p");
errorMessage.textContent = "Invalid file. Please load a valid JSON file.";
errorMessage.setAttribute("id", "error-message");
main.appendChild(errorMessage);
}
function render(item) {
const tag = getTag(item.data);
const element = document.createElement(tag);
element.setAttribute("id", item.id);
if (tag === "span") {
const cssClass = getCssClass(item);
element.setAttribute("class", cssClass);
element.textContent = getTextContent(item);
}
if (item.root === true) element.setAttribute("class", "root");
const parentElement = item.parent != null ? document.getElementById(item.parent.id) : main;
parentElement.appendChild(element);
}
function getTag(data) {
if (!isObject(data)) return "span";
if (Array.isArray(data)) return "ol";
if (!isEmpty(data)) return "ul";
return "li";
}
function getCssClass(item) {
if (item.indexOnParent === 1) return "value-span";
const valueData = item.parent.children[1].data;
if (Array.isArray(valueData)) return "name-span ol-title";
if (isNaN(item.data)) return "name-span";
return "number-span";
}
function getTextContent(item) {
const data = item.data;
if (item.indexOnParent === 0) return `${data}:`;
if (typeof data === "string") return ` "${data}"`;
return ` ${item.data}`;
}
//SCROLLING
function handleScrollForward(entries, _observer) {
for (const entry of entries) {
if (entry.isIntersecting) {
const previousQueueLength = queue.length;
const lastEnqueued = queue[queue.length - 1];
enqueueForward(lastEnqueued);
for (const item of queue.slice(previousQueueLength)) render(item);
enqueuedThisPass = 0;
lastElementObserver.unobserve(lastElement);
lastElement = document.getElementById(queue[queue.length - 1].id);
lastElementObserver.observe(lastElement);
}
}
}
function enqueueForward(item) {
const data = item.data;
const parent = item.parent;
if (parent != null && !isObject(data)) {
enqueueForward(item.parent);
return;
}
const childrenToEnqueue = Object.entries(data).slice(item.indexOfLastEnqueuedChild);
if (parent != null && childrenToEnqueue.length === 0) {
enqueueForward(item.parent);
return;
}
let childIndex = item.indexOfLastEnqueuedChild;
for (const [property, value] of childrenToEnqueue) {
li = enqueue({}, item, childIndex);
enqueue(property, li, 0);
enqueue(value, li, 1);
childIndex += 1;
item.indexOfLastEnqueuedChild = childIndex;
if (enqueuedThisPass >= enqueuedByPass) break;
}
}
//HELPERS
function isObject(data) {
return data != null && typeof data === "object";
}
function isEmpty(object) {
return Object.keys(object).length === 0;
}