-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
166 lines (136 loc) · 4.63 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<style>
.container {
display: flex;
padding: 30px;
height: 500px;
}
.drop-zone {
display: flex;
flex-basis: 50%;
flex-wrap: wrap;
align-content: start;
padding: 15px;
border: 3px dashed #b3b3b3;
border-radius: 10px;
}
.item, .new-item {
margin: 0 10px 10px 0;
display: flex;
justify-content: center;
align-items: center;
color: hsla(0, 0%, 40%, 1);
}
.item, .new-item {
border: 2px dashed #783db3;
border-radius: 10px;
width: 50px;
height: 50px;
cursor: move;
user-select: none;
}
.new-item {
cursor: pointer;
border: 2px dashed #b3b3b3;
font-size: 16px;
}
</style>
</head>
<body class="container text-center">
<div class="container">
<div class="drop-zone">
<div class="new-item">+</div>
</div>
<div class="drop-zone">
<div class="new-item">+</div>
</div>
<div class="drop-zone">
<div class="new-item">+</div>
</div>
<div class="drop-zone">
<div class="new-item">+</div>
</div>
</div>
<script>
let counter = 0;
let currentDrag;
document.addEventListener('click', e => {
if (e.target.classList.contains('new-item')) {
const newItem = createItem();
const zone = getCurrentZone(e.target);
zone.insertBefore(newItem, zone.lastElementChild)
}
});
document.addEventListener('dragstart', (e) => {
const zone = getCurrentZone(e.target);
if (zone) {
currentDrag = { startZone: zone, node: e.target };
e.dataTransfer.setData('text/html', 'dragstart');
}
});
document.addEventListener('dragover', (e) => {
const zone = getCurrentZone(e.target);
if (zone) {
e.preventDefault();
}
});
document.addEventListener('drop', (e) => {
e.preventDefault();
const zone = getCurrentZone(e.target);
if (currentDrag) {
if (zone && currentDrag.startZone !== zone) {
if (e.target.classList.contains('item')) {
zone.insertBefore(currentDrag.node, e.target.nextElementSibling);
} else {
zone.insertBefore(currentDrag.node, zone.lastElementChild);
}
}
currentDrag = null;
} else {
const dt = e.dataTransfer;
if (dt.files && dt.files.length) {
const fd = new FormData();
for (const file of dt.files) {
fd.append('files', file);
const reader = new FileReader();
reader.readAsDataURL(file);
reader.addEventListener('load', () => {
const item = createItem(reader.result);
zone.insertBefore(item, zone.lastElementChild);
});
}
fetch('/foo', { method: 'POST', body: fd });
}
}
});
function createItem(background) {
const newDiv = document.createElement('div');
counter++;
if (background) {
newDiv.style.background = `url(${background})`;
newDiv.style.backgroundSize = `cover`;
} else {
newDiv.textContent = counter;
}
newDiv.classList.add('item');
newDiv.draggable = true;
return newDiv;
}
function getCurrentZone(from) {
// return from.parentNode;
// return from.closest('.drop-zone');
do {
if (from.classList.contains('drop-zone')) {
return from;
}
} while (from = from.parentElement);
return null;
}
</script>
</body>
</html>