forked from gridstack/gridstack.js
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdd-droppable.ts
More file actions
179 lines (154 loc) · 6.38 KB
/
dd-droppable.ts
File metadata and controls
179 lines (154 loc) · 6.38 KB
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
/**
* dd-droppable.ts 12.4.3
* Copyright (c) 2021-2025 Alain Dumesny - see GridStack root license
*/
import { DDDraggable } from './dd-draggable';
import { DDManager } from './dd-manager';
import { DDBaseImplement, HTMLElementExtendOpt } from './dd-base-impl';
import { Utils } from './utils';
import { DDElementHost } from './dd-element';
import { DDTouch, isTouch, pointerenter, pointerleave } from './dd-touch';
import { DDUIData } from './types';
export interface DDDroppableOpt {
accept?: string | ((el: HTMLElement) => boolean);
drop?: (event: DragEvent, ui: DDUIData) => void;
over?: (event: DragEvent, ui: DDUIData) => void;
out?: (event: DragEvent, ui: DDUIData) => void;
}
// let count = 0; // TEST
export class DDDroppable extends DDBaseImplement implements HTMLElementExtendOpt<DDDroppableOpt> {
public accept: (el: HTMLElement) => boolean;
constructor(public el: HTMLElement, public option: DDDroppableOpt = {}) {
super();
// create var event binding so we can easily remove and still look like TS methods (unlike anonymous functions)
this._mouseEnter = this._mouseEnter.bind(this);
this._mouseLeave = this._mouseLeave.bind(this);
this.enable();
this._setupAccept();
}
public on(event: 'drop' | 'dropover' | 'dropout', callback: (event: DragEvent) => void): void {
super.on(event, callback);
}
public off(event: 'drop' | 'dropover' | 'dropout'): void {
super.off(event);
}
public enable(): void {
if (this.disabled === false) return;
super.enable();
this.el.classList.add('ui-droppable');
this.el.classList.remove('ui-droppable-disabled');
this.el.addEventListener('mouseenter', this._mouseEnter);
this.el.addEventListener('mouseleave', this._mouseLeave);
if (isTouch) {
this.el.addEventListener('pointerenter', pointerenter);
this.el.addEventListener('pointerleave', pointerleave);
}
}
public disable(forDestroy = false): void {
if (this.disabled === true) return;
super.disable();
this.el.classList.remove('ui-droppable');
if (!forDestroy) this.el.classList.add('ui-droppable-disabled');
this.el.removeEventListener('mouseenter', this._mouseEnter);
this.el.removeEventListener('mouseleave', this._mouseLeave);
if (isTouch) {
this.el.removeEventListener('pointerenter', pointerenter);
this.el.removeEventListener('pointerleave', pointerleave);
}
}
public destroy(): void {
this.disable(true);
this.el.classList.remove('ui-droppable');
this.el.classList.remove('ui-droppable-disabled');
super.destroy();
}
public updateOption(opts: DDDroppableOpt): DDDroppable {
Object.keys(opts).forEach(key => this.option[key] = opts[key]);
this._setupAccept();
return this;
}
/** @internal called when the cursor enters our area - prepare for a possible drop and track leaving */
protected _mouseEnter(e: MouseEvent): void {
// console.log(`${count++} Enter ${this.el.id}`); // TEST
if (!DDManager.dragElement) return;
// During touch drag operations, ignore real browser-generated mouseenter events (isTrusted:true) vs our simulated ones (isTrusted:false).
// The browser can fire spurious mouseenter events when we dispatch simulated mousemove events.
if (DDTouch.touchHandled && e.isTrusted) return
if (!this._canDrop(DDManager.dragElement.el)) return;
e.preventDefault();
e.stopPropagation();
DDManager.dragElement._stopScrolling();
// make sure when we enter this, that the last one gets a leave FIRST to correctly cleanup as we don't always do
if (DDManager.dropElement && DDManager.dropElement !== this) {
DDManager.dropElement._mouseLeave(e as DragEvent, true); // calledByEnter = true
}
DDManager.dropElement = this;
const ev = Utils.initEvent<DragEvent>(e, { target: this.el, type: 'dropover' });
if (this.option.over) {
this.option.over(ev, this._ui(DDManager.dragElement))
}
this.triggerEvent('dropover', ev);
this.el.classList.add('ui-droppable-over');
// console.log('tracking'); // TEST
}
/** @internal called when the item is leaving our area, stop tracking if we had moving item */
protected _mouseLeave(e: MouseEvent, calledByEnter = false): void {
// console.log(`${count++} Leave ${this.el.id}`); // TEST
if (!DDManager.dragElement || DDManager.dropElement !== this) return;
e.preventDefault();
e.stopPropagation();
// stop the old grid's auto-scroll only when entering a new grid; if leaving to empty space keep scrolling until mouseup
if (calledByEnter) DDManager.dragElement._stopScrolling();
const ev = Utils.initEvent<DragEvent>(e, { target: this.el, type: 'dropout' });
if (this.option.out) {
this.option.out(ev, this._ui(DDManager.dragElement))
}
this.triggerEvent('dropout', ev);
if (DDManager.dropElement === this) {
delete DDManager.dropElement;
// console.log('not tracking'); // TEST
// if we're still over a parent droppable, send it an enter as we don't get one from leaving nested children
if (!calledByEnter) {
let parentDrop: DDDroppable;
let parent: DDElementHost = this.el.parentElement;
while (!parentDrop && parent) {
parentDrop = parent.ddElement?.ddDroppable;
parent = parent.parentElement;
}
if (parentDrop) {
parentDrop._mouseEnter(e);
}
}
}
}
/** item is being dropped on us - called by the drag mouseup handler - this calls the client drop event */
public drop(e: MouseEvent): void {
e.preventDefault();
const ev = Utils.initEvent<DragEvent>(e, { target: this.el, type: 'drop' });
if (this.option.drop) {
this.option.drop(ev, this._ui(DDManager.dragElement))
}
this.triggerEvent('drop', ev);
}
/** @internal true if element matches the string/method accept option */
protected _canDrop(el: HTMLElement): boolean {
return el && (!this.accept || this.accept(el));
}
/** @internal */
protected _setupAccept(): DDDroppable {
if (!this.option.accept) return this;
if (typeof this.option.accept === 'string') {
this.accept = (el: HTMLElement) => el.classList.contains(this.option.accept as string) || el.matches(this.option.accept as string);
} else {
this.accept = this.option.accept;
}
return this;
}
/** @internal */
protected _ui(drag: DDDraggable): DDUIData {
return {
draggable: drag.el,
...drag.ui()
};
}
}