forked from GameGodS3/DropPoint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.new.js
More file actions
133 lines (122 loc) · 3.4 KB
/
renderer.new.js
File metadata and controls
133 lines (122 loc) · 3.4 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
// File List which will contain list of dicts in the format
// {filePath:"file/Path", fileType:"filetype"}
let filelist = [];
// Custom event to trigger on push.
// Will be used for creating UI elements.
const filePushEvent = new Event("file-push");
Object.defineProperty(filelist, "push", {
value: function () {
for (var i = 0, n = this.length, l = arguments.length; i < l; i++, n++) {
// Adding pushed element one by one into the array
this[n] = arguments[i];
}
// Trigger the event
document.dispatchEvent(filePushEvent);
return n;
},
});
// Testing by growing the file list
// let growfilelist = () => {
// for (let i = 0; i < 8; i++) {
// filelist.push({
// fileName: "TestFile",
// fileType: filetypes[Math.floor(Math.random() * filetypes.length)],
// });
// }
// console.log(filelist);
// };
// FileQueue to implement file icons animation
class FileQueueUI {
constructor() {
this.queue = [];
}
enqueue(ele) {
if (this.queue.length > 2) {
this.dequeue();
}
this.queue.push(ele);
}
dequeue() {
if (this.isEmpty()) return "Underflow";
return this.queue.shift();
}
front() {
if (this.isEmpty()) return "No elements in Queue";
return this.queue[0];
}
isEmpty() {
return this.queue.length === 0;
}
printQueue() {
var str = "";
for (var i = 0; i < this.queue.length; i++) {
str += this.queue[i] + " ";
}
return str;
}
length() {
return this.queue.length;
}
}
// UI image elements
const imageHolder = document.querySelectorAll(".file-icon img");
let fq = new FileQueueUI();
document.addEventListener("file-push", () => {
fq.enqueue(filelist[filelist.length - 1].fileType);
for (let i = 0; i < fq.length(); i++) {
imageHolder[i].src = "./media/" + fq.queue[fq.length() - 1 - i] + ".png";
}
});
// Holder area where files will be dragged and dropped
const holder = document.getElementById("droppoint");
// Adding "dragged" class to the holder when the file is dragged over it
// "Dragged" class is used to do the border animation
holder.ondragover = (e) => {
e.preventDefault;
e.stopPropagation;
holder.setAttribute("class", "dragged");
return false;
};
holder.ondragenter = (e) => {
e.preventDefault;
e.stopPropagation;
holder.setAttribute("class", "dragged");
return false;
};
// Removing the "dragged" class from the holder
// when the file is dragged out of it
holder.ondragleave = (e) => {
e.preventDefault;
e.stopPropagation;
holder.removeAttribute("class");
return false;
};
holder.ondragend = (e) => {
e.preventDefault;
e.stopPropagation;
holder.removeAttribute("class");
return false;
};
const uploadArea = document.querySelector(".upload");
const dragOutArea = document.querySelector("#drag");
holder.ondrop = (e) => {
e.preventDefault();
// Remove animation borders on file drop inside DropPoint
holder.removeAttribute("class");
// Get the files from the event
for (let f of e.dataTransfer.files) {
// Check if the file is already in the filelist
if (filelist.find((ele) => ele.fileName == f.name)) {
alert("File already in the list");
continue;
}
// Add the file to the filelist
filelist.push({
filepath: f.path.toString(),
fileType:
f.type.split("/")[0] !== "application" ? f.type.split("/")[0] : "file",
});
}
uploadArea.style.display = "none";
dragOutArea.style.display = "flex";
};