-
Notifications
You must be signed in to change notification settings - Fork 0
/
vertical.html
202 lines (169 loc) · 6.49 KB
/
vertical.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
body {
margin: 0;
--indifference-class-gap: 1.3rem;
}
.weak-order {
background-color: #ddd;
padding: 1rem;
padding-top: 0;
margin-top: 1rem;
display: flex;
flex-direction: column;
align-items: center;
}
.indifference-class {
display: flex;
flex-direction: row;
margin-top: calc(var(--indifference-class-gap) / 2);
margin-bottom: calc(-1 * var(--indifference-class-gap) / 2);
transition: margin 70ms ease-in-out; /* for animation */
}
.indifference-class:has(.item) {
margin-top: var(--indifference-class-gap);
margin-bottom: 0;
}
.item {
user-select: none;
width: 3rem;
height: 3rem;
line-height: 3rem;
text-align: center;
background-color: white;
box-sizing: border-box;
border: 1px solid black;
margin-left: -1px; /* to prevent double border */
cursor: move;
}
.item:first-child {
margin-left: 0;
}
.item.dragging {
background-color: antiquewhite;
}
</style>
</head>
<body>
<div class="weak-order">
<div class="indifference-class">
<div class="item" draggable="true">1</div>
<div class="item" draggable="true">2</div>
</div>
<div class="indifference-class">
<div class="item" draggable="true">3</div>
<div class="item" draggable="true">4</div>
</div>
<div class="indifference-class">
<div class="item" draggable="true">5</div>
<div class="item" draggable="true">6</div>
</div>
</div>
<script>
const items = document.querySelectorAll('.item');
const weakOrders = document.querySelectorAll('.weak-order');
function createEmptyIndifferenceClass() {
const emptyIndifferenceClass = document.createElement('div');
emptyIndifferenceClass.classList.add('indifference-class');
return emptyIndifferenceClass;
}
function removeEmptyIndifferenceClasses(weakOrder) {
const emptyIndifferenceClasses = [...weakOrder.querySelectorAll('.indifference-class')].filter(indifferenceClass => {
return indifferenceClass.children.length === 0;
});
emptyIndifferenceClasses.forEach(emptyIndifferenceClass => {
emptyIndifferenceClass.remove();
});
}
items.forEach(item => {
item.addEventListener('dragstart', (e) => {
item.classList.add('dragging');
item.dataset.justStartedDragging = true;
e.dataTransfer.setData('text/plain', item.textContent); // safari needs this
e.dataTransfer.effectAllowed = "move"; // cursor
})
item.addEventListener('dragend', () => {
const weakOrder = document.querySelector('.weak-order:has(.dragging)');
removeEmptyIndifferenceClasses(weakOrder);
item.classList.remove('dragging');
})
})
weakOrders.forEach(weakOrder => {
weakOrder.addEventListener('dragover', e => {
const item = document.querySelector('.dragging');
// check if item can be dropped here (originates from the same weak order)
if (!item.parentElement || !item.parentElement.parentElement || item.parentElement.parentElement !== weakOrder) {
return;
}
e.preventDefault()
const indifferenceClasses = [...weakOrder.querySelectorAll('.indifference-class')];
// target indifference class
const indifferenceClass = getIndifferenceClassAtY(weakOrder, e.clientY);
// is it currently empty, so that the item will create a "new" indifference class?
const indifferenceClassEmpty = indifferenceClass.children.length === 0;
// optimization: store information for later
// if the indifference class hasn't changed, we don't need to update empty indifference classes later
// exception: when we just started dragging, we need to draw initial new empty indifference classes
const stayedInSameIndifferenceClass = indifferenceClass == item.parentElement;
const justStartedDragging = item.dataset.justStartedDragging;
item.dataset.justStartedDragging = "";
// place item
const afterElement = getItemAtX(indifferenceClass, e.clientX);
const shouldAnimate = !stayedInSameIndifferenceClass && !indifferenceClassEmpty;
const animationOptions = {duration: 70, easing: 'ease-in-out'}
if (afterElement == null) {
if (shouldAnimate) {
item.animate([{ marginTop: 'var(--indifference-class-gap)' }, { marginTop: '0' }], animationOptions);
}
indifferenceClass.appendChild(item);
} else {
if (shouldAnimate) {
item.animate([{ marginBottom: 'var(--indifference-class-gap)' }, { marginBottom: '0' }], animationOptions);
}
indifferenceClass.insertBefore(item, afterElement);
}
// skip unnecessary updates
if (stayedInSameIndifferenceClass && !justStartedDragging) {
return;
}
// delete empty indifference classes
removeEmptyIndifferenceClasses(weakOrder);
// make empty indifference classes
if (indifferenceClass.children.length >= 2) {
// left
const leftEmptyIndifferenceClass = createEmptyIndifferenceClass();
weakOrder.insertBefore(leftEmptyIndifferenceClass, indifferenceClass);
// right
const rightEmptyIndifferenceClass = createEmptyIndifferenceClass();
weakOrder.insertBefore(rightEmptyIndifferenceClass, indifferenceClass.nextElementSibling);
}
})
})
function getIndifferenceClassAtY(weakOrder, y) {
const classes = [...weakOrder.querySelectorAll('.indifference-class')];
let nextClass = classes.find(elem => {
return y <= elem.getBoundingClientRect().top + elem.getBoundingClientRect().height;
});
if (!nextClass) {
nextClass = classes[classes.length - 1];
}
return nextClass;
}
function getItemAtX(indifferenceClass, x) {
const items = [...indifferenceClass.querySelectorAll('.item:not(.dragging)')];
let nextItem = items.find(item => {
return x <= item.getBoundingClientRect().left + item.getBoundingClientRect().width / 2;
});
return nextItem;
}
</script>
<!-- polyfill -->
<script src="DragDropTouch.js"></script>
</body>
</html>