-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
150 lines (110 loc) · 3.88 KB
/
index.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
const IMAGE_TYPES = ["image/jpeg", "image/png", "image/gif", "image/webp"];
const imageInput = document.getElementById("image-input");
const tierItems = document.getElementById("tier-items");
const tierItemsLabel = document.getElementById("tier-items-label");
const rows = document.querySelectorAll(".row");
let sourceContainer = null;
let draggedItem = null;
imageInput.addEventListener("change", handleFileInputChange);
tierItemsLabel.addEventListener("drop", handleLabelDrop);
tierItemsLabel.addEventListener("dragover", handleTierItemsDragOver);
tierItemsLabel.addEventListener("dragleave", handleLabelDragLeave);
tierItems.addEventListener("dragover", handleTierItemsDragOver);
tierItems.addEventListener("drop", handleTierItemsDrop);
tierItems.addEventListener("dragleave", handleTierItemsDragLeave);
for (const row of rows) {
row.addEventListener("dragover", handleTierItemsDragOver);
row.addEventListener("drop", handleTierItemsDrop);
row.addEventListener("dragleave", handleTierItemsDragLeave);
}
function handleLabelDrop(event) {
event.preventDefault();
const { dataTransfer } = event;
const { files } = dataTransfer;
const allFilesAreImage = Array.from(files).every((file) =>
IMAGE_TYPES.includes(file.type)
);
if (files.length === 0 || !allFilesAreImage) {
alert("Only images are allowed to be dropped here.");
return;
}
addItemsFromFileList(files, tierItems);
}
function handleLabelDragOver(event) {
event.preventDefault();
tierItemsLabel.classList.add("over");
}
function handleLabelDragLeave(event) {
event.preventDefault();
tierItemsLabel.classList.remove("over");
}
function handleFileInputChange(event) {
event.preventDefault();
const { files } = event.target;
const allFilesAreImage = Array.from(files).every((file) =>
IMAGE_TYPES.includes(file.type)
);
if (files.length === 0 || !allFilesAreImage) {
alert("Only images are allowed to be dropped here.");
return;
}
addItemsFromFileList(files, tierItems);
}
function handleTierItemsDragOver(event) {
event.preventDefault();
const { currentTarget } = event;
if (currentTarget === sourceContainer) return;
currentTarget.classList.add("drag-over");
const preview = document.querySelector(".drag-preview");
if (draggedItem && !preview) {
const previewClone = draggedItem.cloneNode(true);
previewClone.classList.add("drag-preview");
currentTarget.appendChild(previewClone);
}
}
function handleTierItemsDragLeave(event) {
event.preventDefault();
const { currentTarget } = event;
currentTarget.classList.remove("drag-over");
currentTarget.querySelector(".drag-preview")?.remove();
}
function handleTierItemsDrop(event) {
event.preventDefault();
const { dataTransfer, currentTarget } = event;
if (sourceContainer && draggedItem) {
sourceContainer.removeChild(draggedItem);
}
if (draggedItem) {
const src = dataTransfer.getData("text/plain");
const imgElement = addItem(src);
currentTarget.appendChild(imgElement);
}
currentTarget.classList.remove("drag-over");
currentTarget.querySelector(".drag-preview")?.remove();
}
function addItem(itemSrc) {
const imgElement = document.createElement("img");
imgElement.src = itemSrc;
imgElement.classList.add("tier-item");
imgElement.draggable = true;
imgElement.addEventListener("dragstart", (event) => {
sourceContainer = event.target.parentNode;
draggedItem = event.target;
event.dataTransfer.setData("text/plain", event.target.src);
});
imgElement.addEventListener("dragend", (event) => {
sourceContainer = null;
draggedItem = null;
});
tierItems.appendChild(imgElement);
return imgElement;
}
function addItemsFromFileList(fileList) {
for (const file of Array.from(fileList)) {
const reader = new FileReader();
reader.addEventListener("load", (eventReader) => {
addItem(eventReader.target.result);
});
reader.readAsDataURL(file);
}
}