-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
600 lines (539 loc) · 14.7 KB
/
app.js
File metadata and controls
600 lines (539 loc) · 14.7 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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
// Global constants
/**
* @const {HTMLElement}
*/
const DRAG_CONTAINER = document.getElementById("drag-container");
/**
* @const {HTMLElement}
*/
const TRASH_CONTAINER = document.getElementById("trash");
/**
* @const {number}
*/
const TRASH_OPACITY = 0.5;
/**
* @const {number}
*/
const TRASH_HOVER_OPACITY = 0.8;
/**
* @const {string}
*/
const DEPTH_BAR = "│";
/**
* @const {string}
*/
const NEST_BAR = "├─";
/**
* @const {string}
*/
const NEST_DEFAULT_PADDING = "0px";
/**
* @const {string}
*/
const NEST_LEFT_PADDING = "2px";
/**
* @const {string}
*/
const NEST_RIGHT_PADDING = "6px";
/**
* @const {string}
*/
const DEPTH_LEFT_PADDING = "2px";
/**
* @const {string}
*/
const DEPTH_RIGHT_PADDING = "16px";
/**
* Creates custom HTML element.
*
* @param {string} elementType - The HTML element type (e.g., 'div', 'span', 'p')
* @param {string} className - The element's class name
* @param {Object} [attributes={}] - An optional key-value object of element attributes (e.g., `{ id: 'my-id', 'data-value': '123' }`.
* @returns {HTMLElement} The newly created HTML element
*/
function createCustomElement(elementType, className, attributes = {}) {
/**
* @const {HTMLElement}
*/
const NEW_ELEMENT = document.createElement(elementType);
NEW_ELEMENT.classList.add(className);
Object.keys(attributes).forEach((key) => {
NEW_ELEMENT.setAttribute(key, attributes[key]);
});
return NEW_ELEMENT;
}
/**
* Updates nesting level for element and its children based on the nesting
* level of the drop target.
*
* @param {number} targetNestLevel - The nesting level of the target
* HTML element
* @param {HTMLElement} dragElement - The HTML element that is currently being
* dragged
*/
function updateNestLevel(targetNestLevel, dragElement) {
/**
* @const {number}
*/
const NEW_NEST_LEVEL = targetNestLevel !== -1 ? targetNestLevel + 1 : 0;
dragElement.dataset.nestingLevel = NEW_NEST_LEVEL;
if (dragElement.getAttribute("class") === "folder") {
/**
* @const {HTMLElement[]}
*/
const CHILDREN = Array.from(
dragElement.querySelector(".children").children,
);
CHILDREN.forEach((child) => {
updateNestLevel(NEW_NEST_LEVEL, child);
});
}
}
/**
* Removes the nest bars from HTML element and its children.
*
* @param {HTMLElement} element - The HTML that will have nest bars removed
*/
function removeNestDiv(element) {
element.querySelector(".depth").innerHTML = "";
element.querySelector(".nested").innerHTML = "";
if (element.getAttribute("class") === "folder") {
/**
* @const {HTMLElement[]}
*/
const CHILDREN = Array.from(element.querySelector(".children").children);
CHILDREN.forEach((child) => {
removeNestDiv(child);
});
}
}
/**
* Creates the depth bars for a given HTML element.
*
* @param {HTMLElement} element - HTML element which depth bars will be added
*/
function createDepth(element) {
/**
* @const {HTMLElement}
*/
const DEPTH = element.querySelector(".depth");
for (let i = 1; i < parseInt(element.dataset.nestingLevel); i++) {
/**
* @const {HTMLElement}
*/
const DEPTH_BARS = document.createElement("div");
DEPTH_BARS.innerHTML = DEPTH_BAR;
DEPTH_BARS.style.paddingLeft = DEPTH_LEFT_PADDING;
DEPTH_BARS.style.paddingRight = DEPTH_RIGHT_PADDING;
DEPTH.appendChild(DEPTH_BARS);
}
}
/**
* Creates the nesting bar for element and its children.
*
* @param {HTMLElement} element - element to create nesting bars
*/
function createNestBars(element) {
removeNestDiv(element);
createDepth(element);
/**
* @const {HTMLElement}
*/
const NESTED = element.querySelector(".nested");
NESTED.innerHTML = parseInt(element.dataset.nestingLevel) > 0 ? NEST_BAR : "";
NESTED.style.paddingRight =
NESTED.innerHTML === "" ? NEST_DEFAULT_PADDING : NEST_RIGHT_PADDING;
NESTED.style.paddingLeft =
NESTED.innerHTML === "" ? NEST_DEFAULT_PADDING : NEST_LEFT_PADDING;
if (element.getAttribute("class") === "folder") {
/**
* @const {HTMLElement[]}
*/
const CHILDREN = Array.from(element.querySelector(".children").children);
CHILDREN.forEach((child) => {
createNestBars(child);
});
}
}
/**
* Checks for duplicate same-level file/folder named HTML elements.
*
* @param {HTMLElement[]} children - Array of children HTML elements
* @param {string} elementName - Name of the individual child element
* @param {string} elementType - Type of the individual child element
* @return {boolean} If duplicate found
*/
function IsDuplicateName(children, elementName, elementType) {
/**
* @const {boolean}
*/
const IS_DUPLICATE = Array.from(children).some((child) => {
if (child && typeof child.querySelector === "function") {
/**
* @const {HTMLInputElement}
*/
const INPUT_ELEMENT = child.querySelector("input");
if (INPUT_ELEMENT) {
/**
* @const {string}
*/
const CHILD_NAME = INPUT_ELEMENT.getAttribute("name");
if (
elementName === CHILD_NAME &&
elementName !== `new_${elementType}`
) {
return true;
}
}
}
return false;
});
if (IS_DUPLICATE) {
alert(`A ${elementType} with that name already exists`);
}
return IS_DUPLICATE;
}
/**
* Creates file/folder HTML container.
*
* @param {string} type - Type of container ie file or folder
*/
function createContainer(type) {
/**
* @const {HTMLElement}
*/
const NEW_CONTAINER = createCustomElement("span", type, {
"data-nesting-level": 0,
draggable: true,
});
/**
* @const {HTMLElement}
*/
const CONTAINER_CONTENT = createCustomElement("div", "content");
/**
* @const {HTMLElement}
*/
const NESTING_BARS = createCustomElement("div", "nesting-bars");
/**
* @const {HTMLElement}
*/
const DEPTH = createCustomElement("div", "depth");
/**
* @const {HTMLElement}
*/
const NESTED = createCustomElement("div", "nested");
/**
* @const {HTMLElement}
*/
const DRAG_HANDLE = createCustomElement("i", "drag-handle");
/**
* @const {HTMLElement}
*/
const NAME_INPUT = createCustomElement("input", `${type}-name`);
DRAG_HANDLE.innerHTML = `<i class="bi bi-${type}-fill"></i>`;
NAME_INPUT.value = `new_${type}`;
NAME_INPUT.name = NAME_INPUT.value;
NAME_INPUT.classList.add(`${type}-name`);
NESTING_BARS.appendChild(DEPTH);
NESTING_BARS.appendChild(NESTED);
NAME_INPUT.addEventListener("focus", (e) => {
e.currentTarget.style.fontWeight = "bold";
});
NAME_INPUT.addEventListener("input", (e) => {
NEW_CONTAINER.draggable = false;
e.currentTarget.name = e.currentTarget.value.trim();
e.currentTarget.value = e.currentTarget.value.replace(/\s+/g, " ");
});
NAME_INPUT.addEventListener("keydown", (e) => {
if (e.key === "Enter" || e.key === "Escape") {
e.preventDefault();
e.currentTarget.blur();
}
});
NAME_INPUT.addEventListener("blur", (e) => {
NEW_CONTAINER.draggable = true;
e.currentTarget.value = e.currentTarget.value.trim();
e.currentTarget.style.fontWeight = "normal";
if (e.currentTarget.name === "") {
e.currentTarget.name = `new_${type}`;
e.currentTarget.value = e.currentTarget.name;
}
e.currentTarget.setSelectionRange(
e.currentTarget.value.length,
e.currentTarget.value.length,
);
/**
* const {HTMLElement[]}
*/
const SIBLINGS = Array.from(NEW_CONTAINER.parentElement.children).filter(
(child) => {
return (
child !== e.currentTarget.parentElement.parentElement &&
child.querySelector("input") &&
child.querySelector("input") !== e.currentTarget
);
},
);
/**
* @const {boolean}
*/
const DUPLICATE_SIBLING = SIBLINGS.some((sibling) => {
/**
* @const {string}
*/
const CURRENT_NAME = e.currentTarget.value;
/**
* @const {HTMLInputElement}
*/
const SIBLING_INPUT = sibling.querySelector("input");
return (
SIBLING_INPUT.value !== `new_${type}` &&
SIBLING_INPUT.value === CURRENT_NAME
);
});
// If sibling with same name exists returns to default name
if (DUPLICATE_SIBLING) {
alert(`A ${type} with the same name already exists`);
e.currentTarget.name = `new_${type}`;
e.currentTarget.value = e.currentTarget.name;
}
});
/**
* @const {HTMLElement}
*/
const NEST_ELEMENTS = createCustomElement("div", "children");
CONTAINER_CONTENT.appendChild(NESTING_BARS);
CONTAINER_CONTENT.appendChild(DRAG_HANDLE);
CONTAINER_CONTENT.appendChild(NAME_INPUT);
NEW_CONTAINER.appendChild(CONTAINER_CONTENT);
// Files cannot have elements nested in them
if (type === "folder") {
NEW_CONTAINER.appendChild(NEST_ELEMENTS);
}
DRAG_CONTAINER.appendChild(NEW_CONTAINER);
// New container event listeners
NEW_CONTAINER.addEventListener("dragstart", (e) => {
e.stopPropagation();
e.target.setAttribute("data-temp-id", "temporary_id");
e.dataTransfer.setData("text/plain", "temporary_id");
});
NEW_CONTAINER.addEventListener("dragover", (e) => {
e.preventDefault();
});
NEW_CONTAINER.addEventListener("drop", (e) => {
e.stopPropagation();
e.preventDefault();
/**
* @const {HTMLElement}
*/
const DROP_TARGET = e.currentTarget.querySelector(".children");
/**
* @const {HTMLElement}
*/
const DRAG_ELEMENT = document.querySelector(
'[data-temp-id="temporary_id"]',
);
/**
* @const {string}
*/
const DRAG_ELEMENT_NAME =
DRAG_ELEMENT.querySelector("input").getAttribute("name");
/**
* @const {number}
*/
const DROP_TARGET_NESTING_LEVEL = parseInt(
DROP_TARGET.parentElement.dataset.nestingLevel,
);
/**
* @const {HTMLCollection}
*/
const DROP_TARGET_CHILDREN = DROP_TARGET.children;
/**
* @const {string}
*/
const DRAG_ELEMENT_TYPE = DRAG_ELEMENT.getAttribute("class");
/**
* @const {boolean}
*/
const DUPLICATE = IsDuplicateName(
DROP_TARGET_CHILDREN,
DRAG_ELEMENT_NAME,
DRAG_ELEMENT_TYPE,
);
if (
!DRAG_ELEMENT.contains(DROP_TARGET) &&
DROP_TARGET.children !== DRAG_ELEMENT &&
!DUPLICATE
) {
updateNestLevel(DROP_TARGET_NESTING_LEVEL, DRAG_ELEMENT);
createNestBars(DRAG_ELEMENT);
DROP_TARGET.appendChild(DRAG_ELEMENT);
}
});
NEW_CONTAINER.addEventListener("dragend", (e) => {
e.stopPropagation();
/**
* @const {HTMLElement}
*/
const DRAG_ELEMENT = document.querySelector(
'[data-temp-id="temporary_id"]',
);
if (DRAG_ELEMENT) {
DRAG_ELEMENT.removeAttribute("data-temp-id");
}
});
}
/**
* Removes all files/folder from drag container (not including trash container).
*/
function handleClear() {
Array.from(DRAG_CONTAINER.children).forEach((child) => {
if (child !== TRASH_CONTAINER) {
child.remove();
}
});
}
/**
* Creates the file system based on a root folder.
*
* @param {JSZip} zip - the JSZip object
* @param {HTMLElement} parentElement - the HTML element that is the root folder
* @param {string} [basePath=""] - optional name for the zip folder itself
*/
function createFileSystem(zip, parentElement, basePath = "") {
/**
* @const {string}
*/
const FOLDER_NAME = parentElement
.querySelector(".folder-name")
.getAttribute("name");
/**
* @const {string}
*/
const FOLDER_PATH =
basePath === "" ? FOLDER_NAME : `${basePath}/${FOLDER_NAME}`;
zip.folder(FOLDER_PATH);
Array.from(parentElement.querySelector(".children").children).forEach(
(child) => {
if (child.getAttribute("class") === "file") {
/**
* @const {string}
*/
const FILE_NAME = child
.querySelector(".file-name")
.getAttribute("name");
zip.file(FOLDER_PATH + "/" + FILE_NAME, "");
} else {
/**
* @const {string}
*/
const CHILD_FOLDER_NAME = child
.querySelector(".folder-name")
.getAttribute("name");
zip.folder(FOLDER_PATH + "/" + CHILD_FOLDER_NAME);
createFileSystem(zip, child, FOLDER_PATH);
}
},
);
}
/**
* Creates and downloads file system to local storage as zip folder.
*/
function handleForge() {
/**
* @const {HTMLElement}
*/
const ROOT_ELEMENT = DRAG_CONTAINER.querySelector(".folder");
if (!ROOT_ELEMENT) {
alert("Root folder is missing");
return;
}
/**
* @const {HTMLCollection}
*/
const CHILDREN = ROOT_ELEMENT.querySelector(".children").children;
if (CHILDREN.length < 2) {
alert("Root folder must have at least two elements");
return;
}
/**
* @const {JSZip}
*/
const ZIP = new JSZip();
/**
* @const {string}
*/
const ROOT_NAME =
ROOT_ELEMENT.querySelector(".folder-name").getAttribute("name");
createFileSystem(ZIP, ROOT_ELEMENT);
ZIP.generateAsync({ type: "blob" }).then((blob) => {
saveAs(blob, `${ROOT_NAME}.zip`);
});
}
// Drag container event listeners
DRAG_CONTAINER.addEventListener("dragover", (e) => {
e.preventDefault();
});
DRAG_CONTAINER.addEventListener("drop", (e) => {
/**
* @const {HTMLElement}
*/
const DRAG_ELEMENT = document.querySelector('[data-temp-id="temporary_id"]');
e.stopPropagation();
e.preventDefault();
updateNestLevel(-1, DRAG_ELEMENT);
createNestBars(DRAG_ELEMENT);
e.currentTarget.appendChild(DRAG_ELEMENT);
});
DRAG_CONTAINER.addEventListener("dragend", (e) => {
/**
* @const {HTMLElement}
*/
const DRAG_ELEMENT = document.querySelector('[data-temp-id="temporary_id"]');
DRAG_ELEMENT.removeAttribute("data-temp-id");
e.stopPropagation();
});
// Trash container event listeners
TRASH_CONTAINER.addEventListener("dragover", (e) => {
e.currentTarget.style.opacity = TRASH_HOVER_OPACITY;
e.preventDefault();
e.stopPropagation();
});
TRASH_CONTAINER.addEventListener("dragleave", (e) => {
e.currentTarget.style.opacity = TRASH_OPACITY;
e.preventDefault();
e.stopPropagation();
});
TRASH_CONTAINER.addEventListener("drop", (e) => {
/**
* @const {HTMLElement}
*/
const DRAG_ELEMENT = document.querySelector('[data-temp-id="temporary_id"]');
e.currentTarget.style.opacity = TRASH_OPACITY;
e.preventDefault();
e.stopPropagation();
DRAG_ELEMENT.remove();
});
// Button click event listener
document.addEventListener("click", (e) => {
switch (e.target.id) {
case "addFolder":
createContainer("folder");
break;
case "addFile":
createContainer("file");
break;
case "clear":
handleClear();
break;
case "forge":
handleForge();
break;
default:
return;
}
});
// Page load event listener
document.addEventListener("DOMContentLoaded", (e) => {
e.currentTarget.getElementById("year").innerHTML = new Date().getFullYear();
});