forked from Shopify/draggable
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCollidable.ts
127 lines (108 loc) · 3.76 KB
/
Collidable.ts
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
import { DragMoveEvent, DragStopEvent } from '../../Draggable';
import AbstractPlugin from '../../shared/AbstractPlugin';
import { closest } from '../../shared/utils';
import { CollideInEvent, CollideOutEvent } from './CollideEvent';
const onDragMove = Symbol('onDragMove');
const onDragStop = Symbol('onDragStop');
const onRequestAnimationFrame = Symbol('onRequestAnimationFrame');
/**
* Collidable plugin which detects colliding elements while dragging
* @class Collidable
* @module Collidable
* @extends AbstractPlugin
*/
export default class Collidable extends AbstractPlugin {
/*** Keeps track of currently colliding elements */
currentlyCollidingElement: HTMLElement | null = null;
/*** Keeps track of currently colliding elements */
lastCollidingElement: HTMLElement | null = null;
/*** Animation frame for finding colliding elements */
currentAnimationFrame: number | null = null;
/**
* Attaches plugins event listeners
*/
attach() {
this.draggable
.on('drag:move', this[onDragMove])
.on('drag:stop', this[onDragStop]);
}
/**
* Detaches plugins event listeners
*/
detach() {
this.draggable
.off('drag:move', this[onDragMove])
.off('drag:stop', this[onDragStop]);
}
/*** Returns current collidables based on `collidables` option */
getCollidables(): Element[] {
const collidables = this.draggable.options.collidables;
if (typeof collidables === 'string') {
return Array.prototype.slice.call(document.querySelectorAll(collidables));
} else if (
collidables instanceof NodeList ||
collidables instanceof Array
) {
return Array.prototype.slice.call(collidables);
} else if (collidables instanceof HTMLElement) {
return [collidables];
} else if (typeof collidables === 'function') {
return collidables();
} else {
return [];
}
}
/*** Drag move handler */
private [onDragMove] = (event: DragMoveEvent) => {
const target = event.sensorEvent.target;
this.currentAnimationFrame = requestAnimationFrame(
this[onRequestAnimationFrame](target)
);
if (this.currentlyCollidingElement) event.preventDefault();
const collidableInEvent = new CollideInEvent({
dragEvent: event,
collidingElement: this.currentlyCollidingElement,
});
const collidableOutEvent = new CollideOutEvent({
dragEvent: event,
collidingElement: this.lastCollidingElement,
});
const enteringCollidable = Boolean(
this.currentlyCollidingElement &&
this.lastCollidingElement !== this.currentlyCollidingElement
);
const leavingCollidable = Boolean(
!this.currentlyCollidingElement && this.lastCollidingElement
);
if (enteringCollidable) {
if (this.lastCollidingElement) {
this.draggable.trigger(collidableOutEvent);
}
this.draggable.trigger(collidableInEvent);
} else if (leavingCollidable) {
this.draggable.trigger(collidableOutEvent);
}
this.lastCollidingElement = this.currentlyCollidingElement;
};
/*** Drag stop handler */
private [onDragStop] = (event: DragStopEvent) => {
const lastCollidingElement =
this.currentlyCollidingElement || this.lastCollidingElement;
const collidableOutEvent = new CollideOutEvent({
dragEvent: event,
collidingElement: lastCollidingElement,
});
if (lastCollidingElement) this.draggable.trigger(collidableOutEvent);
this.lastCollidingElement = null;
this.currentlyCollidingElement = null;
};
/*** Animation frame function */
private [onRequestAnimationFrame] = (target: HTMLElement) => {
return () => {
const collidables = this.getCollidables();
this.currentlyCollidingElement = closest(target, (element) =>
collidables.includes(element)
);
};
};
}