-
-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Expand file tree
/
Copy pathitem.js
More file actions
281 lines (240 loc) · 9.03 KB
/
Copy pathitem.js
File metadata and controls
281 lines (240 loc) · 9.03 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
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
import Component from '@glimmer/component';
import {action} from '@ember/object';
import {inject as service} from '@ember/service';
// A touch long-press is the mobile equivalent of a right-click for opening the
// context menu, since touch devices have no contextmenu gesture of their own.
const LONG_PRESS_DURATION_MS = 500;
const LONG_PRESS_MOVE_THRESHOLD_PX = 10;
// The compatibility "ghost" click fires at the release point; only swallow clicks
// within this radius of it so a real tap on a menu action is never discarded.
const GHOST_CLICK_RADIUS_PX = 20;
function clearTextSelection() {
if (window.getSelection) {
if (window.getSelection().empty) {
window.getSelection().empty();
} else if (window.getSelection().removeAllRanges) {
window.getSelection().removeAllRanges();
}
}
}
export default class ItemComponent extends Component {
@service dropdown;
longPressTimer = null;
longPressFired = false;
touchStartX = 0;
touchStartY = 0;
ghostClickHandler = null;
ghostClickTimer = null;
nativeContextMenuHandler = null;
get selectionList() {
return this.args.model;
}
get id() {
return this.args.id;
}
get isSelected() {
return this.selectionList.isSelected(this.id);
}
willDestroy() {
super.willDestroy(...arguments);
this.cancelLongPress();
this.cancelGhostClickSuppression();
this.releaseNativeContextMenu();
}
cancelLongPress() {
if (this.longPressTimer) {
clearTimeout(this.longPressTimer);
this.longPressTimer = null;
}
}
// A long-press opens the menu while the finger is still down; on release the browser
// fires a synthetic "ghost" click that the dropdown service's body-click handler reads
// as an outside click, closing the menu instantly. Swallow that one click at the
// document level (capture phase, before it reaches the overlay or body) — but only when
// it lands at the release point, so a real tap on a menu action elsewhere is never
// discarded, even if the browser happens not to emit the ghost click.
suppressGhostClick() {
this.cancelGhostClickSuppression();
this.ghostClickHandler = (event) => {
const isGhostClick = Math.abs(event.clientX - this.touchStartX) <= GHOST_CLICK_RADIUS_PX
&& Math.abs(event.clientY - this.touchStartY) <= GHOST_CLICK_RADIUS_PX;
if (isGhostClick) {
event.preventDefault();
event.stopPropagation();
}
this.cancelGhostClickSuppression();
};
document.addEventListener('click', this.ghostClickHandler, true);
this.ghostClickTimer = setTimeout(() => this.cancelGhostClickSuppression(), 500);
}
cancelGhostClickSuppression() {
if (this.ghostClickHandler) {
document.removeEventListener('click', this.ghostClickHandler, true);
this.ghostClickHandler = null;
}
if (this.ghostClickTimer) {
clearTimeout(this.ghostClickTimer);
this.ghostClickTimer = null;
}
}
// While the finger is held, the browser's own long-press fires a native contextmenu
// event; if it lands on the menu's overlay, the overlay's onContextMenuOutside handler
// closes the menu mid-press. Eat contextmenu at the document level for the duration of
// the touch so our timer is the only thing that opens or holds the menu.
suppressNativeContextMenu() {
if (this.nativeContextMenuHandler) {
return;
}
this.nativeContextMenuHandler = (event) => {
event.preventDefault();
event.stopPropagation();
};
document.addEventListener('contextmenu', this.nativeContextMenuHandler, true);
}
releaseNativeContextMenu() {
if (this.nativeContextMenuHandler) {
document.removeEventListener('contextmenu', this.nativeContextMenuHandler, true);
this.nativeContextMenuHandler = null;
}
}
openContextMenu(x, y) {
if (this.isSelected) {
this.dropdown.toggleDropdown('context-menu', this, {left: x, top: y, selectionList: this.selectionList});
} else {
this.selectionList.clearSelection();
this.selectionList.toggleItem(this.id);
this.selectionList.clearOnNextUnfreeze();
this.dropdown.toggleDropdown('context-menu', this, {left: x, top: y, selectionList: this.selectionList});
}
}
/**
* We use the mouse down event because it allows us to cancel any text selection using preventDefault
*/
@action
onMouseDown(event) {
if (!this.selectionList.enabled) {
return;
}
// If event target has data-ignore-select or one of its partens, then ignore the event
if (event.target.closest('[data-ignore-select]')) {
return;
}
const shiftKey = event.shiftKey;
const ctrlKey = event.ctrlKey || event.metaKey;
if (ctrlKey) {
this.selectionList.toggleItem(this.id);
event.preventDefault();
event.stopPropagation();
clearTextSelection();
} else if (shiftKey) {
try {
this.selectionList.shiftItem(this.id);
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
}
event.preventDefault();
event.stopPropagation();
clearTextSelection();
}
}
@action
onClick(event) {
// A long-press opens the context menu while the finger is still down; swallow
// the click that fires on release so the row doesn't also navigate
if (this.longPressFired) {
this.longPressFired = false;
event.preventDefault();
event.stopPropagation();
return;
}
if (!this.selectionList.enabled) {
return;
}
// If event target has data-ignore-select or one of its partens, then ignore the event
if (event.target.closest('[data-ignore-select]')) {
return;
}
const shiftKey = event.shiftKey;
const ctrlKey = event.ctrlKey || event.metaKey;
if (!ctrlKey && !shiftKey) {
return;
}
event.preventDefault();
event.stopPropagation();
clearTextSelection();
}
@action
onContextMenu(event) {
if (!this.selectionList.enabled) {
return;
}
// If event target has data-ignore-select or one of its partens, then ignore the event
if (event.target.closest('[data-ignore-select]')) {
return;
}
// Some touch platforms (e.g. Android) emit a native contextmenu on long-press in
// addition to our timer. If the long-press already opened the menu, just swallow
// this event so we don't toggle it straight back closed.
if (this.longPressFired) {
event.preventDefault();
event.stopPropagation();
return;
}
this.cancelLongPress();
this.openContextMenu(event.clientX, event.clientY);
event.preventDefault();
event.stopPropagation();
}
@action
onTouchStart(event) {
if (!this.selectionList.enabled) {
return;
}
if (event.target.closest('[data-ignore-select]')) {
return;
}
this.cancelLongPress();
this.longPressFired = false;
if (event.touches.length !== 1) {
return;
}
const touch = event.touches[0];
this.touchStartX = touch.clientX;
this.touchStartY = touch.clientY;
this.suppressNativeContextMenu();
this.longPressTimer = setTimeout(() => {
this.longPressTimer = null;
this.longPressFired = true;
this.openContextMenu(this.touchStartX, this.touchStartY);
}, LONG_PRESS_DURATION_MS);
}
@action
onTouchMove(event) {
if (!this.longPressTimer) {
return;
}
const touch = event.touches[0];
if (!touch) {
return;
}
// A press that drifts past the threshold is a scroll, not a long-press
const dx = Math.abs(touch.clientX - this.touchStartX);
const dy = Math.abs(touch.clientY - this.touchStartY);
if (dx > LONG_PRESS_MOVE_THRESHOLD_PX || dy > LONG_PRESS_MOVE_THRESHOLD_PX) {
this.cancelLongPress();
}
}
@action
onTouchEnd() {
this.cancelLongPress();
this.releaseNativeContextMenu();
// If the long-press opened the menu, the finger lift fires a synthetic ghost click
// right after this; arm the one-shot suppressor now so that click can't close the menu.
// Arming here (at release) rather than when the menu opened means it works no matter
// how long the press was held.
if (this.longPressFired) {
this.suppressGhostClick();
}
}
}