-
Notifications
You must be signed in to change notification settings - Fork 13.5k
/
index.ts
181 lines (156 loc) · 4.99 KB
/
index.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
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
import { doc } from '@utils/browser';
import type { Config } from '../../interface';
import { pointerCoord } from '../helpers';
export const startTapClick = (config: Config) => {
if (doc === undefined) {
return;
}
let lastActivated = 0;
let activatableEle: HTMLElement | undefined;
let activeRipple: Promise<() => void> | undefined;
let activeDefer: ReturnType<typeof setTimeout> | undefined;
const useRippleEffect = config.getBoolean('animated', true) && config.getBoolean('rippleEffect', true);
const clearDefers = new WeakMap<HTMLElement, any>();
const cancelActive = () => {
if (activeDefer) clearTimeout(activeDefer);
activeDefer = undefined;
if (activatableEle) {
removeActivated(false);
activatableEle = undefined;
}
};
const pointerDown = (ev: PointerEvent) => {
// Ignore right clicks
if (activatableEle || ev.button === 2) {
return;
}
setActivatedElement(getActivatableTarget(ev), ev);
};
const pointerUp = (ev: UIEvent) => {
setActivatedElement(undefined, ev);
};
const setActivatedElement = (el: HTMLElement | undefined, ev: UIEvent) => {
// do nothing
if (el && el === activatableEle) {
return;
}
if (activeDefer) clearTimeout(activeDefer);
activeDefer = undefined;
const { x, y } = pointerCoord(ev);
// deactivate selected
if (activatableEle) {
if (clearDefers.has(activatableEle)) {
throw new Error('internal error');
}
if (!activatableEle.classList.contains(ACTIVATED)) {
addActivated(activatableEle, x, y);
}
removeActivated(true);
}
// activate
if (el) {
const deferId = clearDefers.get(el);
if (deferId) {
clearTimeout(deferId);
clearDefers.delete(el);
}
el.classList.remove(ACTIVATED);
const callback = () => {
addActivated(el, x, y);
activeDefer = undefined;
};
if (isInstant(el)) {
callback();
} else {
activeDefer = setTimeout(callback, ADD_ACTIVATED_DEFERS);
}
}
activatableEle = el;
};
const addActivated = (el: HTMLElement, x: number, y: number) => {
lastActivated = Date.now();
el.classList.add(ACTIVATED);
if (!useRippleEffect) return;
const rippleEffect = getRippleEffect(el);
if (rippleEffect !== null) {
removeRipple();
activeRipple = rippleEffect.addRipple(x, y);
}
};
const removeRipple = () => {
if (activeRipple !== undefined) {
activeRipple.then((remove) => remove());
activeRipple = undefined;
}
};
const removeActivated = (smooth: boolean) => {
removeRipple();
const active = activatableEle;
if (!active) {
return;
}
const time = CLEAR_STATE_DEFERS - Date.now() + lastActivated;
if (smooth && time > 0 && !isInstant(active)) {
const deferId = setTimeout(() => {
active.classList.remove(ACTIVATED);
clearDefers.delete(active);
}, CLEAR_STATE_DEFERS);
clearDefers.set(active, deferId);
} else {
active.classList.remove(ACTIVATED);
}
};
doc.addEventListener('ionGestureCaptured', cancelActive);
doc.addEventListener('pointerdown', pointerDown, true);
doc.addEventListener('pointerup', pointerUp, true);
/**
* Tap click effects such as the ripple effect should
* not happen when scrolling. For example, if a user scrolls
* the page but also happens to do a touchstart on a button
* as part of the scroll, the ripple effect should not
* be dispatched. The ripple effect should only happen
* if the button is activated and the page is not scrolling.
*
* pointercancel is dispatched on a gesture when scrolling
* starts, so this lets us avoid having to listen for
* ion-content's scroll events.
*/
doc.addEventListener('pointercancel', cancelActive, true);
};
// TODO(FW-2832): type
const getActivatableTarget = (ev: UIEvent): any => {
if (ev.composedPath !== undefined) {
/**
* composedPath returns EventTarget[]. However,
* objects other than Element can be targets too.
* For example, AudioContext can be a target. In this
* case, we know that the event is a UIEvent so we
* can assume that the path will contain either Element
* or ShadowRoot.
*/
const path = ev.composedPath() as Element[] | ShadowRoot[];
for (let i = 0; i < path.length - 2; i++) {
const el = path[i];
if (!(el instanceof ShadowRoot) && el.classList.contains('ion-activatable')) {
return el;
}
}
} else {
return (ev.target as Element).closest('.ion-activatable');
}
};
const isInstant = (el: HTMLElement) => {
return el.classList.contains('ion-activatable-instant');
};
const getRippleEffect = (el: HTMLElement) => {
if (el.shadowRoot) {
const ripple = el.shadowRoot.querySelector('ion-ripple-effect');
if (ripple) {
return ripple;
}
}
return el.querySelector('ion-ripple-effect');
};
const ACTIVATED = 'ion-activated';
const ADD_ACTIVATED_DEFERS = 100;
const CLEAR_STATE_DEFERS = 150;