forked from Shopify/draggable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Droppable.js
299 lines (249 loc) · 8.05 KB
/
Droppable.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
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
import {closest} from 'shared/utils';
import Draggable from '../Draggable';
import {DroppableStartEvent, DroppableDroppedEvent, DroppableReturnedEvent, DroppableStopEvent} from './DroppableEvent';
const onDragStart = Symbol('onDragStart');
const onDragMove = Symbol('onDragMove');
const onDragStop = Symbol('onDragStop');
const dropInDropzone = Symbol('dropInDropZone');
const returnToOriginalDropzone = Symbol('returnToOriginalDropzone');
const closestDropzone = Symbol('closestDropzone');
const getDropzones = Symbol('getDropzones');
/**
* Returns an announcement message when the Draggable element is dropped into a dropzone element
* @param {DroppableDroppedEvent} droppableEvent
* @return {String}
*/
function onDroppableDroppedDefaultAnnouncement({dragEvent, dropzone}) {
const sourceText = dragEvent.source.textContent.trim() || dragEvent.source.id || 'draggable element';
const dropzoneText = dropzone.textContent.trim() || dropzone.id || 'droppable element';
return `Dropped ${sourceText} into ${dropzoneText}`;
}
/**
* Returns an announcement message when the Draggable element has returned to its original dropzone element
* @param {DroppableReturnedEvent} droppableEvent
* @return {String}
*/
function onDroppableReturnedDefaultAnnouncement({dragEvent, dropzone}) {
const sourceText = dragEvent.source.textContent.trim() || dragEvent.source.id || 'draggable element';
const dropzoneText = dropzone.textContent.trim() || dropzone.id || 'droppable element';
return `Returned ${sourceText} from ${dropzoneText}`;
}
/**
* @const {Object} defaultAnnouncements
* @const {Function} defaultAnnouncements['droppable:dropped']
* @const {Function} defaultAnnouncements['droppable:returned']
*/
const defaultAnnouncements = {
'droppable:dropped': onDroppableDroppedDefaultAnnouncement,
'droppable:returned': onDroppableReturnedDefaultAnnouncement,
};
const defaultClasses = {
'droppable:active': 'draggable-dropzone--active',
'droppable:occupied': 'draggable-dropzone--occupied',
};
const defaultOptions = {
dropzone: '.draggable-droppable',
};
/**
* Droppable is built on top of Draggable and allows dropping draggable elements
* into dropzone element
* @class Droppable
* @module Droppable
* @extends Draggable
*/
export default class Droppable extends Draggable {
/**
* Droppable constructor.
* @constructs Droppable
* @param {HTMLElement[]|NodeList|HTMLElement} containers - Droppable containers
* @param {Object} options - Options for Droppable
*/
constructor(containers = [], options = {}) {
super(containers, {
...defaultOptions,
...options,
classes: {
...defaultClasses,
...(options.classes || {}),
},
announcements: {
...defaultAnnouncements,
...(options.announcements || {}),
},
});
/**
* All dropzone elements on drag start
* @property dropzones
* @type {HTMLElement[]}
*/
this.dropzones = null;
/**
* Last dropzone element that the source was dropped into
* @property lastDropzone
* @type {HTMLElement}
*/
this.lastDropzone = null;
/**
* Initial dropzone element that the source was drag from
* @property initialDropzone
* @type {HTMLElement}
*/
this.initialDropzone = null;
this[onDragStart] = this[onDragStart].bind(this);
this[onDragMove] = this[onDragMove].bind(this);
this[onDragStop] = this[onDragStop].bind(this);
this.on('drag:start', this[onDragStart])
.on('drag:move', this[onDragMove])
.on('drag:stop', this[onDragStop]);
}
/**
* Destroys Droppable instance.
*/
destroy() {
super.destroy();
this.off('drag:start', this[onDragStart])
.off('drag:move', this[onDragMove])
.off('drag:stop', this[onDragStop]);
}
/**
* Drag start handler
* @private
* @param {DragStartEvent} event - Drag start event
*/
[onDragStart](event) {
if (event.canceled()) {
return;
}
this.dropzones = [...this[getDropzones]()];
const dropzone = closest(event.sensorEvent.target, this.options.dropzone);
if (!dropzone) {
event.cancel();
return;
}
const droppableStartEvent = new DroppableStartEvent({
dragEvent: event,
dropzone,
});
this.trigger(droppableStartEvent);
if (droppableStartEvent.canceled()) {
event.cancel();
return;
}
this.initialDropzone = dropzone;
for (const dropzoneElement of this.dropzones) {
if (dropzoneElement.classList.contains(this.getClassNameFor('droppable:occupied'))) {
continue;
}
dropzoneElement.classList.add(this.getClassNameFor('droppable:active'));
}
}
/**
* Drag move handler
* @private
* @param {DragMoveEvent} event - Drag move event
*/
[onDragMove](event) {
if (event.canceled()) {
return;
}
const dropzone = this[closestDropzone](event.sensorEvent.target);
const overEmptyDropzone = dropzone && !dropzone.classList.contains(this.getClassNameFor('droppable:occupied'));
if (overEmptyDropzone && this[dropInDropzone](event, dropzone)) {
this.lastDropzone = dropzone;
} else if ((!dropzone || dropzone === this.initialDropzone) && this.lastDropzone) {
this[returnToOriginalDropzone](event);
this.lastDropzone = null;
}
}
/**
* Drag stop handler
* @private
* @param {DragStopEvent} event - Drag stop event
*/
[onDragStop](event) {
const droppableStopEvent = new DroppableStopEvent({
dragEvent: event,
dropzone: this.lastDropzone || this.initialDropzone,
});
this.trigger(droppableStopEvent);
const occupiedClass = this.getClassNameFor('droppable:occupied');
for (const dropzone of this.dropzones) {
dropzone.classList.remove(this.getClassNameFor('droppable:active'));
}
if (this.lastDropzone && this.lastDropzone !== this.initialDropzone) {
this.initialDropzone.classList.remove(occupiedClass);
}
this.dropzones = null;
this.lastDropzone = null;
this.initialDropzone = null;
}
/**
* Drops a draggable element into a dropzone element
* @private
* @param {DragMoveEvent} event - Drag move event
* @param {HTMLElement} dropzone - Dropzone element to drop draggable into
*/
[dropInDropzone](event, dropzone) {
const droppableDroppedEvent = new DroppableDroppedEvent({
dragEvent: event,
dropzone,
});
this.trigger(droppableDroppedEvent);
if (droppableDroppedEvent.canceled()) {
return false;
}
const occupiedClass = this.getClassNameFor('droppable:occupied');
if (this.lastDropzone) {
this.lastDropzone.classList.remove(occupiedClass);
}
dropzone.appendChild(event.source);
dropzone.classList.add(occupiedClass);
return true;
}
/**
* Moves the previously dropped element back into its original dropzone
* @private
* @param {DragMoveEvent} event - Drag move event
*/
[returnToOriginalDropzone](event) {
const droppableReturnedEvent = new DroppableReturnedEvent({
dragEvent: event,
dropzone: this.lastDropzone,
});
this.trigger(droppableReturnedEvent);
if (droppableReturnedEvent.canceled()) {
return;
}
this.initialDropzone.appendChild(event.source);
this.lastDropzone.classList.remove(this.getClassNameFor('droppable:occupied'));
}
/**
* Returns closest dropzone element for even target
* @private
* @param {HTMLElement} target - Event target
* @return {HTMLElement|null}
*/
[closestDropzone](target) {
if (!this.dropzones) {
return null;
}
return closest(target, this.dropzones);
}
/**
* Returns all current dropzone elements for this draggable instance
* @private
* @return {NodeList|HTMLElement[]|Array}
*/
[getDropzones]() {
const dropzone = this.options.dropzone;
if (typeof dropzone === 'string') {
return document.querySelectorAll(dropzone);
} else if (dropzone instanceof NodeList || dropzone instanceof Array) {
return dropzone;
} else if (typeof dropzone === 'function') {
return dropzone();
} else {
return [];
}
}
}