-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathindex.js
380 lines (315 loc) · 11.6 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import '@babel/polyfill';
import 'element-qsa-scope';
import dragula from 'dragula';
import LiveRegion from 'live-region';
import createDebug from 'debug';
import Emitter from 'component-emitter';
import matches from 'dom-matches';
import defaults from './lib/defaults';
import queryAll from './lib/query-all';
const debug = createDebug('drag-on-drop:index');
const arrayHandler = (containers, userOptions = {}) => {
const { nested, dragulaOptions = {} } = userOptions;
const instances = [];
containers.forEach(container => {
instances.push(new DragonDrop(container, userOptions, nested));
});
if (nested) {
const onDrag = (el, source) => {
const instance = instances.find(inst => inst.container === source);
if (instance) {
instance.announcement('grabbed', el);
}
};
const onDrop = (el, source) => {
const instance = instances.find(inst => inst.container === source);
if (instance) {
instance.announcement('dropped', el).setItems();
}
};
const topMost = containers[0];
const lists = Array.from(containers);
lists.shift(); // remove the top-most conatainer
const topLevelDragula = dragula([topMost], {
...dragulaOptions,
moves: (_, __, handle) => !lists.find(l => l.contains(handle))
});
topLevelDragula.on('drag', onDrag);
topLevelDragula.on('drop', onDrop);
const nestedDragula = dragula(lists, {
...dragulaOptions,
accepts: (el, target, source) => {
// TODO: when `options.locked` is implemented...
// if (!options.locked) { return true }
return target === source;
}
});
nestedDragula.on('drag', onDrag);
nestedDragula.on('drop', onDrop);
instances.forEach((inst, i) => {
inst.dragula = i === 0 ? topLevelDragula : nestedDragula;
});
}
return instances;
};
export default class DragonDrop {
/**
* Dragon Drop
* @param {HTMLElement} container - The containing list
* @param {Object} userOptions - The user provided options
*
* @option {String} item - The selector for the drag items (qualified within
* container). Defaults to `'li'`.
* @option {String} handle - The selector for the handle. If set to
* false, the entire item will be used as the draggable region.
* Defaults to `'button'`.
* @option {String} activeClass - The class to be added to the item being dragged.
* Defaults to `'dragon-active'`.
* @option {String} inactiveClass - The class to be added to all of the other
* items when an item is being dragged. Defaults
* to `'dragon-inactive'`.
* @option {Object} annnouncement - The configuration object for live region announcements
* @option {Function} announcement.grabbed - The function called when an item is picked up.
* The currently grabbed element along with an
* array of all items are passed as arguments
* respectively. The function should return a
* string of text to be announced in the live region.
* @option {Function} announcement.dropped - The function called with an item is dropped. The
* newly dropped item along with an array of items
* are passed as arguments respectively. The function
* should return a string of text to be announced in
* the live region.
* @option {Function} announcement.reorder - The function called when the list has been reordered.
* The newly dropped item along with an array of items
* are passed as arguments respectively. The function
* should return a string of text to be announced in
* the live region.
* @option {Function} announcement.cancel - The function called when the reorder is cancelled
* (via ESC). No arguments passed in.
* @option {string} liveRegion.ariaLive - Optional ariaLive attribute to be passed to the live region. Valid values are "off", "polite", or "assertive". Default is "assertive".
* @option {string} liveRegion.ariaRelevant - Optional ariaRelevant attribute to be passed to the live region. Valid values are "additions", "removals", "text", and "all". Default is "additions".
* @option {boolean} liveRegion.ariaAtomic - Optional ariaAtomic attribute to be passed to the live region. Default is "true".
*/
constructor(container, userOptions = {}) {
if (Array.isArray(container)) {
return arrayHandler(container, userOptions);
}
// make the dragon an emitter
Emitter(this);
this.handledHandles = [];
this.initOptions(userOptions);
const { handle, nested } = this.options;
if (!nested) {
// if handle is truthy, pass this info along with
const dragulaOpts = handle && {
moves: (_, __, h) => matches(h, handle)
};
// init mouse drag via dragula
this.dragula = dragula([container], {
...userOptions.dragulaOptions,
...dragulaOpts
});
this.dragula.on('drag', (el) => {
this.emit('grabbed', container, el);
});
this.dragula.on('drop', (el) => {
this.emit('dropped', container, el);
});
}
const liveOpts = {
ariaLive: 'assertive',
ariaRelevant: 'additions',
ariaAtomic: 'true'
};
// init live region for custom announcements
this.liveRegion = new LiveRegion({
...liveOpts,
...userOptions.liveRegion
});
this.onKeydown = this.onKeydown.bind(this);
// initialize elements / events
this
.initElements(container)
.mouseEvents();
debug('dragon initialized: ', this);
return this;
}
/**
* Sets the dragon drop options (extending user opts with defaults)
* @param {Object} userOptions the user provided options
*/
initOptions(userOptions) {
userOptions.announcement = userOptions.announcement || {};
this.options = {
...defaults,
...userOptions,
announcement: {
...defaults.announcement,
...userOptions.announcement
}
};
return this;
}
initClick() {
const { activeClass, inactiveClass, nested } = this.options;
this.handles.forEach(handle => {
if (this.handledHandles.includes(handle)) {
return;
}
handle.addEventListener('click', e => {
if (nested) { e.stopPropagation(); }
const wasPressed = handle.getAttribute('data-drag-on') === 'true';
const type = wasPressed ? 'dropped' : 'grabbed';
// clean up
this.handles
.filter(h => h.getAttribute('aria-pressed') === 'true')
.forEach(h => {
h.setAttribute('aria-pressed', 'false');
h.setAttribute('data-drag-on', 'false');
h.classList.remove(activeClass);
});
handle.setAttribute('aria-pressed', `${!wasPressed}`);
handle.setAttribute('data-drag-on', `${!wasPressed}`);
const thisItem = this.items.find(itm => {
return itm === handle || itm.contains(handle);
});
this.announcement(type, thisItem);
this.emit(type, this.container, thisItem);
// configure classes (active and inactive)
this.items.forEach(it => {
const method = !wasPressed ? 'add' : 'remove';
const isTarget = it === handle || it.contains(handle);
it.classList[(isTarget && !wasPressed) ? 'add' : 'remove'](activeClass);
it.classList[(isTarget && !wasPressed) ? 'remove' : method](inactiveClass);
});
if (!wasPressed) {
// cache the initial order to allow for escape cancellation
this.cachedItems = queryAll(this.options.item, this.container);
}
});
this.handledHandles.push(handle);
});
return this;
}
/**
* Sets all element refs
* @param {HTMLElement} container the containing element
*/
initElements(container) {
this.container = container;
this.setItems().initClick();
// set all attrs/props/events on handle elements
this.handles.forEach(handle => {
handle.tabIndex = 0; // ensure it is focusable
if (handle.tagName !== 'BUTTON') {
handle.setAttribute('role', 'button');
}
// events
handle.removeEventListener('keydown', this.onKeydown);
handle.addEventListener('keydown', this.onKeydown);
});
return this;
}
setItems() {
const opts = this.options;
this.items = queryAll(opts.item, this.container);
this.handles = opts.handle
? queryAll([opts.item, opts.handle].join(' '), this.container)
: this.items;
return this;
}
onKeydown(e) {
const { nested } = this.options;
const { target, which } = e;
const isDrag = () => target.getAttribute('data-drag-on') === 'true';
switch (which) {
case 13:
case 32:
if (nested) { e.stopPropagation(); }
e.preventDefault();
target.click();
break;
case 37:
case 38:
case 39:
case 40:
if (isDrag()) {
e.preventDefault();
this.arrow(which, target);
}
break;
case 9:
if (isDrag()) {
target.click();
}
break;
case 27:
if (isDrag()) {
target.click();
this.cancel(e);
}
}
return this;
}
arrow(which, target) {
const handles = this.handles;
const items = this.items;
const isUp = which === 37 || which === 38;
const index = handles.indexOf(target);
const adjacentIndex = isUp ? index - 1 : index + 1;
const adjacentItem = handles[adjacentIndex];
const oldItem = items[index];
if (!adjacentItem || !oldItem) {
return;
}
const newItem = items[adjacentIndex];
const refNode = isUp ? newItem : newItem.nextElementSibling;
// move the item in the DOM
oldItem.parentNode.insertBefore(oldItem, refNode);
target.focus();
this
.setItems()
.emit('reorder', this.container, oldItem)
.announcement('reorder', oldItem);
return this;
}
announcement(type, item) {
debug(`${type} announcement`, item);
const config = this.options.announcement || {};
const funk = config[type];
if (funk && typeof funk === 'function') {
const msg = funk(item, this.items);
this.liveRegion.announce(msg, 5e3);
this.emit('announcement', msg);
}
return this;
}
mouseEvents() {
const { nested } = this.options;
if (!nested) {
this.dragula.on('drag', el => {
this.announcement('grabbed', el);
});
this.dragula.on('drop', el => {
this.announcement('dropped', el).setItems();
});
}
return this;
}
cancel(e) {
// cache active element so it can be focused after reorder
const focused = document.activeElement;
// restore the order of the list
this.cachedItems.forEach(item => this.container.appendChild(item));
this.items = this.cachedItems;
// ensure the handle stays focused
focused.focus();
this
.announcement('cancel')
.emit('cancel', e)
.setItems();
return this;
}
}
// make window.DragonDrop available rather than window.DragonDrop.default
module.exports = DragonDrop;