-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
353 lines (294 loc) · 10 KB
/
script.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
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
let dirHandle;
let directoryData;
let d3HierarchyData;
let currentIndex = 0;
const addButton = document.getElementById("add-button");
const refreshButton = document.getElementById("refresh-button");
const topTextContent = document.getElementById("top-text-content");
const treeTextContent = document.getElementById("tree-text-content");
const fileTreeContainer = document.getElementById("file-tree-container");
const texts = [
"Tip: Alt + click to Expand / Collapse slowly",
"Tip: Use your mouse wheel or pinch to zoom in/out",
"Source: https://github.com/dhextras/File-Viz",
];
const errorText =
"Error Accessing Directory Try Reloading or contact Devoloper { Dhextras }.";
const initialText =
"Pls Choose a folder to view the Tree structure.\nSupported Browsers: Chrome 86, Opera 72, Edge 86";
const reloadErrorText = "Try selecting a Directory before reloading.";
document.addEventListener("DOMContentLoaded", () => {
displayText(texts);
addButton.addEventListener("click", accessDirectory);
refreshButton.addEventListener("click", reloadDirectory);
});
/**
* Chart function to create a collapsible tree chart using D3.js.
* This function is based on the example from the official D3.js ObservableHQ notebook:
* https://observablehq.com/@d3/collapsible-tree
*
* @param {Object} data - The hierarchical data to visualize as a tree.
* @returns {Element} - The SVG element containing the tree chart.
*/
function createFileTree(data) {
// Specify the charts’ dimensions. The height is variable, depending on the laydirectoryData.
const width = 928;
const marginTop = 10;
const marginRight = 10;
const marginBottom = 10;
const marginLeft = 100;
// Rows are separated by dx pixels, columns by dy pixels. These names can be counter-intuitive
// (dx is a height, and dy a width). This because the tree must be viewed with the root at the
// “bottom”, in the data domain. The width of a column is based on the tree’s height.
const root = d3.hierarchy(data);
const dx = 15;
const dy = 200;
// Define the tree laydirectoryData and the shape for links.
const tree = d3.tree().nodeSize([dx, dy]);
const diagonal = d3
.linkHorizontal()
.x((d) => d.y)
.y((d) => d.x);
// Create the SVG container, a layer for the links and a layer for the nodes.
const svg = d3
.create("svg")
.attr("id", "file-tree-svg")
.attr("width", width)
.attr("height", dx)
.attr("viewBox", [-marginLeft, -marginTop, width, dx])
.attr(
"style",
"width: 100%; height: 100%; font: 12px sans-serif; user-select: none;"
);
// Transition Zooming
const zoom = d3
.zoom()
.scaleExtent([0.1, 10])
.on("zoom", (event) => {
const { transform } = event;
gNode.attr("transform", transform);
gLink.attr("transform", transform);
});
svg.call(zoom);
const gLink = svg
.append("g")
.attr("fill", "none")
.attr("stroke", "#008eff")
.attr("stroke-opacity", 0.4)
.attr("stroke-width", 1.8);
const gNode = svg
.append("g")
.attr("cursor", "pointer")
.attr("pointer-events", "all");
function update(event, source) {
const duration = event?.altKey ? 2500 : 250; // hold the alt key to slow down the transition
const nodes = root.descendants().reverse();
const links = root.links();
// Compute the new tree laydirectoryData.
tree(root);
let left = root;
let right = root;
root.eachBefore((node) => {
if (node.x < left.x) left = node;
if (node.x > right.x) right = node;
});
const height = right.x - left.x + marginTop + marginBottom;
const transition = svg
.transition()
.duration(duration)
.attr("height", height)
.attr("viewBox", [-marginLeft, left.x - marginTop, width, height])
.tween(
"resize",
window.ResizeObserver ? null : () => () => svg.dispatch("toggle")
);
// Update the nodes…
const node = gNode.selectAll("g").data(nodes, (d) => d.id);
// Enter any new nodes at the parent's previous position.
const nodeEnter = node
.enter()
.append("g")
.attr("transform", (d) => `translate(${source.y0},${source.x0})`)
.attr("fill-opacity", 0)
.attr("stroke-opacity", 0)
.on("click", (event, d) => {
d.children = d.children ? null : d._children;
update(event, d);
});
nodeEnter
.append("circle")
.attr("r", 5)
.attr("fill", (d) => (d._children ? "#0078d7" : "#bfbfbf"))
.attr("stroke-width", 10);
nodeEnter
.append("text")
.attr("dy", (d) => (d._children ? "0.31em" : "0.35em"))
.attr("x", (d) => (d._children ? -8 : 8))
.attr("text-anchor", (d) => (d._children ? "end" : "start"))
.attr("font-weight", (d) => (d._children ? "bold" : "none"))
.attr("font-size", (d) => (d._children ? "13px" : "12px"))
.text((d) => d.data.name)
.clone(true)
.lower()
.attr("stroke-linejoin", "round")
.attr("stroke-width", 3)
.attr("stroke", "white");
// Transition nodes to their new position.
const nodeUpdate = node
.merge(nodeEnter)
.transition(transition)
.attr("transform", (d) => `translate(${d.y},${d.x})`)
.attr("fill-opacity", 1)
.attr("stroke-opacity", 1);
// Transition exiting nodes to the parent's new position.
const nodeExit = node
.exit()
.transition(transition)
.remove()
.attr("transform", (d) => `translate(${source.y},${source.x})`)
.attr("fill-opacity", 0)
.attr("stroke-opacity", 0);
// Update the links…
const link = gLink.selectAll("path").data(links, (d) => d.target.id);
// Enter any new links at the parent's previous position.
const linkEnter = link
.enter()
.append("path")
.attr("d", (d) => {
const o = { x: source.x0, y: source.y0 };
return diagonal({ source: o, target: o });
});
// Transition links to their new position.
link.merge(linkEnter).transition(transition).attr("d", diagonal);
// Transition exiting nodes to the parent's new position.
link
.exit()
.transition(transition)
.remove()
.attr("d", (d) => {
const o = { x: source.x, y: source.y };
return diagonal({ source: o, target: o });
});
// Stash the old positions for transition.
root.eachBefore((d) => {
d.x0 = d.x;
d.y0 = d.y;
});
}
// Do the first update to the initial configuration of the tree — where a number of nodes
// are open (arbitrarily selected as the root, plus nodes with 7 letters).
root.x0 = dy / 2;
root.y0 = 0;
root.descendants().forEach((d, i) => {
d.id = i;
d._children = d.children;
if (d.depth && d.data.name.length !== 7) d.children = null;
});
update(null, root);
return svg.node();
}
// Handler Functions for Adding and reloading Directories
async function accessDirectory() {
try {
directoryData = {};
dirHandle = await window.showDirectoryPicker();
updateLoadingScreen();
await handleDirectoryEntry(dirHandle, directoryData);
d3HierarchyData = convertDataToD3Hierarchy(directoryData, dirHandle.name);
renderFileTree(d3HierarchyData);
} catch (error) {
console.error("Error accessing files:", error);
treeTextContent.textContent = errorText;
treeTextContent.style.color = "red";
setTimeout(() => {
treeTextContent.style.color = "rgb(1, 146, 61)";
treeTextContent.innerText = initialText;
}, 3000);
dirHandle = null;
}
}
async function reloadDirectory() {
if (dirHandle) {
updateLoadingScreen();
await handleDirectoryEntry(dirHandle, directoryData);
d3HierarchyData = convertDataToD3Hierarchy(directoryData, dirHandle.name);
renderFileTree(d3HierarchyData);
} else {
console.log("Try selecting a Directory before reloading.");
treeTextContent.textContent = reloadErrorText;
setTimeout(() => {
treeTextContent.innerText = initialText;
}, 3000);
}
}
async function handleDirectoryEntry(dirHandle, directoryData) {
for await (const entry of dirHandle.values()) {
if (entry.kind === "file") {
const file = await entry.getFile();
directoryData[file.name] = file;
}
if (entry.kind === "directory") {
const newDirectoryData = (directoryData[entry.name] = {});
await handleDirectoryEntry(entry, newDirectoryData);
}
}
}
function convertDataToD3Hierarchy(data, name) {
return {
name,
children: Object.keys(data).map((key) => {
if (data[key] instanceof File) {
return { name: key, type: "file" };
} else {
return convertDataToD3Hierarchy(data[key], key);
}
}),
};
}
// Few Boilerplate codes
function renderFileTree(data) {
const chart = createFileTree(data);
fileTreeContainer.textContent = "";
fileTreeContainer.appendChild(chart);
}
function displayText() {
const text = texts[currentIndex];
let index = 0;
const typingInterval = 65;
const typingEffect = setInterval(() => {
topTextContent.textContent += text[index];
index++;
if (index === text.length) {
clearInterval(typingEffect);
setTimeout(() => {
topTextContent.classList.remove("bounce");
disappearText();
}, 2000);
}
}, typingInterval);
}
function disappearText() {
const text = topTextContent.textContent;
let index = 0;
const disappearingInterval = 3;
const disappearingEffect = setInterval(() => {
topTextContent.textContent = text.slice(index + 1, -(index + 1));
index++;
if (index >= text.length - 1) {
clearInterval(disappearingEffect);
topTextContent.textContent = "";
topTextContent.classList.add("bounce");
currentIndex = (currentIndex + 1) % texts.length;
displayText();
}
}, disappearingInterval);
}
function updateLoadingScreen() {
const existingSvg = document.getElementById("file-tree-svg");
if (existingSvg) {
fileTreeContainer.removeChild(existingSvg);
}
fileTreeContainer.textContent = "";
fileTreeContainer.innerHTML =
'<div class="tree-extra-stuff" style="color: black;"><i class="fa-solid fa-spinner fa-spin fa-2xl"></i></div>';
}