-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Swappable.js
184 lines (156 loc) · 4.31 KB
/
Swappable.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
import Draggable from '../Draggable';
import {
SwappableStartEvent,
SwappableSwapEvent,
SwappableSwappedEvent,
SwappableStopEvent,
} from './SwappableEvent';
const onDragStart = Symbol('onDragStart');
const onDragOver = Symbol('onDragOver');
const onDragStop = Symbol('onDragStop');
/**
* Returns an announcement message when the Draggable element is swapped with another draggable element
* @param {SwappableSwappedEvent} swappableEvent
* @return {String}
*/
function onSwappableSwappedDefaultAnnouncement({dragEvent, swappedElement}) {
const sourceText =
dragEvent.source.textContent.trim() ||
dragEvent.source.id ||
'swappable element';
const overText =
swappedElement.textContent.trim() ||
swappedElement.id ||
'swappable element';
return `Swapped ${sourceText} with ${overText}`;
}
/**
* @const {Object} defaultAnnouncements
* @const {Function} defaultAnnouncements['swappabled:swapped']
*/
const defaultAnnouncements = {
'swappabled:swapped': onSwappableSwappedDefaultAnnouncement,
};
/**
* Swappable is built on top of Draggable and allows swapping of draggable elements.
* Order is irrelevant to Swappable.
* @class Swappable
* @module Swappable
* @extends Draggable
*/
export default class Swappable extends Draggable {
/**
* Swappable constructor.
* @constructs Swappable
* @param {HTMLElement[]|NodeList|HTMLElement} containers - Swappable containers
* @param {Object} options - Options for Swappable
*/
constructor(containers = [], options = {}) {
super(containers, {
...options,
announcements: {
...defaultAnnouncements,
...(options.announcements || {}),
},
});
/**
* Last draggable element that was dragged over
* @property lastOver
* @type {HTMLElement}
*/
this.lastOver = null;
this[onDragStart] = this[onDragStart].bind(this);
this[onDragOver] = this[onDragOver].bind(this);
this[onDragStop] = this[onDragStop].bind(this);
this.on('drag:start', this[onDragStart])
.on('drag:over', this[onDragOver])
.on('drag:stop', this[onDragStop]);
}
/**
* Destroys Swappable instance.
*/
destroy() {
super.destroy();
this.off('drag:start', this._onDragStart)
.off('drag:over', this._onDragOver)
.off('drag:stop', this._onDragStop);
}
/**
* Drag start handler
* @private
* @param {DragStartEvent} event - Drag start event
*/
[onDragStart](event) {
const swappableStartEvent = new SwappableStartEvent({
dragEvent: event,
});
this.trigger(swappableStartEvent);
if (swappableStartEvent.canceled()) {
event.cancel();
}
}
/**
* Drag over handler
* @private
* @param {DragOverEvent} event - Drag over event
*/
[onDragOver](event) {
if (
event.over === event.originalSource ||
event.over === event.source ||
event.canceled()
) {
return;
}
const swappableSwapEvent = new SwappableSwapEvent({
dragEvent: event,
over: event.over,
overContainer: event.overContainer,
});
this.trigger(swappableSwapEvent);
if (swappableSwapEvent.canceled()) {
return;
}
// swap originally swapped element back
if (this.lastOver && this.lastOver !== event.over) {
swap(this.lastOver, event.source);
}
if (this.lastOver === event.over) {
this.lastOver = null;
} else {
this.lastOver = event.over;
}
swap(event.source, event.over);
const swappableSwappedEvent = new SwappableSwappedEvent({
dragEvent: event,
swappedElement: event.over,
});
this.trigger(swappableSwappedEvent);
}
/**
* Drag stop handler
* @private
* @param {DragStopEvent} event - Drag stop event
*/
[onDragStop](event) {
const swappableStopEvent = new SwappableStopEvent({
dragEvent: event,
});
this.trigger(swappableStopEvent);
this.lastOver = null;
}
}
function withTempElement(callback) {
const tmpElement = document.createElement('div');
callback(tmpElement);
tmpElement.remove();
}
function swap(source, over) {
const overParent = over.parentNode;
const sourceParent = source.parentNode;
withTempElement((tmpElement) => {
sourceParent.insertBefore(tmpElement, source);
overParent.insertBefore(source, over);
sourceParent.insertBefore(over, tmpElement);
});
}