-
Notifications
You must be signed in to change notification settings - Fork 258
/
Copy pathaction-handler.ts
293 lines (253 loc) · 8.53 KB
/
action-handler.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
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
282
283
284
285
286
287
288
289
290
291
292
293
import { noChange } from 'lit-html';
// import '@material/mwc-ripple';
// tslint:disable-next-line
import { Ripple } from '@material/mwc-ripple';
import { fireEvent } from './common/fire-event';
import { deepEqual } from './deep-equal';
import { AttributePart, Directive, DirectiveParameters, directive } from 'lit-html/directive';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const isTouch = 'ontouchstart' in window || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0;
interface ActionHandler extends HTMLElement {
holdTime: number;
bind(element: Element, options): void;
}
export interface ActionHandlerDetail {
action: 'hold' | 'tap' | 'double_tap';
}
export interface ActionHandlerOptions {
hasHold?: boolean;
hasDoubleClick?: boolean;
disabled?: boolean;
stopPropagation?: boolean;
repeat?: number;
repeatLimit?: number;
}
interface ActionHandlerElement extends HTMLElement {
actionHandler?: {
options: ActionHandlerOptions;
start?: (ev: Event) => void;
end?: (ev: Event) => void;
handleEnter?: (ev: KeyboardEvent) => void;
};
}
declare global {
interface HTMLElementTagNameMap {
'action-handler': ActionHandler;
}
interface HASSDomEvents {
action: ActionHandlerDetail;
}
}
class ActionHandler extends HTMLElement implements ActionHandler {
public holdTime = 500;
public ripple: Ripple;
protected timer?: number;
protected held = false;
private cancelled = false;
private dblClickTimeout?: number;
private repeatTimeout: NodeJS.Timeout | undefined;
private isRepeating = false;
private repeatCount = 0;
constructor() {
super();
this.ripple = document.createElement('mwc-ripple');
}
public connectedCallback(): void {
Object.assign(this.style, {
position: 'fixed',
width: isTouch ? '100px' : '50px',
height: isTouch ? '100px' : '50px',
transform: 'translate(-50%, -50%)',
pointerEvents: 'none',
zIndex: '999',
});
this.appendChild(this.ripple);
this.ripple.primary = true;
['touchcancel', 'mouseout', 'mouseup', 'touchmove', 'mousewheel', 'wheel', 'scroll'].forEach((ev) => {
document.addEventListener(
ev,
() => {
this.cancelled = true;
if (this.timer) {
this.stopAnimation();
clearTimeout(this.timer);
this.timer = undefined;
if (this.isRepeating && this.repeatTimeout) {
clearInterval(this.repeatTimeout);
this.isRepeating = false;
}
}
},
{ passive: true },
);
});
}
public bind(element: ActionHandlerElement, options: ActionHandlerOptions): void {
if (element.actionHandler && deepEqual(options, element.actionHandler.options)) {
return;
}
if (element.actionHandler) {
element.removeEventListener('touchstart', element.actionHandler.start!);
element.removeEventListener('touchend', element.actionHandler.end!);
element.removeEventListener('touchcancel', element.actionHandler.end!);
element.removeEventListener('mousedown', element.actionHandler.start!);
element.removeEventListener('click', element.actionHandler.end!);
element.removeEventListener('keyup', element.actionHandler.handleEnter!);
} else {
element.addEventListener('contextmenu', (ev: Event) => {
const e = ev || window.event;
if (e.preventDefault) {
e.preventDefault();
}
if (e.stopPropagation) {
e.stopPropagation();
}
e.cancelBubble = true;
e.returnValue = false;
return false;
});
}
element.actionHandler = { options };
if (options.disabled) {
return;
}
element.actionHandler.start = (ev: Event) => {
if (options.stopPropagation) {
ev.stopPropagation();
}
this.cancelled = false;
let x;
let y;
if ((ev as TouchEvent).touches) {
x = (ev as TouchEvent).touches[0].clientX;
y = (ev as TouchEvent).touches[0].clientY;
} else {
x = (ev as MouseEvent).clientX;
y = (ev as MouseEvent).clientY;
}
if (options.hasHold) {
this.held = false;
this.timer = window.setTimeout(() => {
this.startAnimation(x, y);
this.held = true;
if (options.repeat && !this.isRepeating) {
this.repeatCount = 0;
this.isRepeating = true;
this.repeatTimeout = setInterval(() => {
fireEvent(element, 'action', { action: 'hold' });
this.repeatCount++;
if (this.repeatTimeout && options.repeatLimit && this.repeatCount >= options.repeatLimit) {
clearInterval(this.repeatTimeout);
this.isRepeating = false;
}
}, options.repeat);
}
}, this.holdTime);
}
};
element.actionHandler.end = (ev: Event) => {
if (options.stopPropagation) {
ev.stopPropagation();
}
// Don't respond when moved or scrolled while touch
if (['touchend', 'touchcancel'].includes(ev.type) && this.cancelled) {
if (this.isRepeating && this.repeatTimeout) {
clearInterval(this.repeatTimeout);
this.isRepeating = false;
}
return;
}
const target = ev.target as HTMLElement;
// Prevent mouse event if touch event
if (ev.cancelable) {
ev.preventDefault();
}
if (options.hasHold) {
clearTimeout(this.timer);
if (this.isRepeating && this.repeatTimeout) {
clearInterval(this.repeatTimeout);
}
this.isRepeating = false;
this.stopAnimation();
this.timer = undefined;
}
if (options.hasHold && this.held) {
if (!options.repeat) {
fireEvent(target, 'action', { action: 'hold' });
}
} else if (options.hasDoubleClick) {
if ((ev.type === 'click' && (ev as MouseEvent).detail < 2) || !this.dblClickTimeout) {
this.dblClickTimeout = window.setTimeout(() => {
this.dblClickTimeout = undefined;
fireEvent(target, 'action', { action: 'tap' });
}, 250);
} else {
clearTimeout(this.dblClickTimeout);
this.dblClickTimeout = undefined;
fireEvent(target, 'action', { action: 'double_tap' });
}
} else {
fireEvent(target, 'action', { action: 'tap' });
}
};
element.actionHandler.handleEnter = (ev: KeyboardEvent) => {
if (ev.keyCode !== 13) {
return;
}
(ev.currentTarget as ActionHandlerElement).actionHandler!.end!(ev);
};
element.addEventListener('touchstart', element.actionHandler.start, {
passive: true,
});
element.addEventListener('touchend', element.actionHandler.end);
element.addEventListener('touchcancel', element.actionHandler.end);
element.addEventListener('mousedown', element.actionHandler.start, {
passive: true,
});
element.addEventListener('click', element.actionHandler.end);
element.addEventListener('keyup', element.actionHandler.handleEnter);
}
private startAnimation(x: number, y: number): void {
Object.assign(this.style, {
left: `${x}px`,
top: `${y}px`,
display: null,
});
this.ripple.disabled = false;
this.ripple.startPress();
this.ripple.unbounded = true;
}
private stopAnimation(): void {
this.ripple.endPress();
this.ripple.disabled = true;
this.style.display = 'none';
}
}
customElements.define('button-card-action-handler', ActionHandler);
const getActionHandler = (): ActionHandler => {
const body = document.body;
if (body.querySelector('button-card-action-handler')) {
return body.querySelector('button-card-action-handler') as ActionHandler;
}
const actionhandler = document.createElement('button-card-action-handler');
body.appendChild(actionhandler);
return actionhandler as ActionHandler;
};
export const actionHandlerBind = (element: ActionHandlerElement, options?: ActionHandlerOptions) => {
const actionhandler: ActionHandler = getActionHandler();
if (!actionhandler) {
return;
}
actionhandler.bind(element, options);
};
export const actionHandler = directive(
class extends Directive {
update(part: AttributePart, [options]: DirectiveParameters<this>) {
actionHandlerBind(part.element as ActionHandlerElement, options);
return noChange;
}
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
render(_options?: ActionHandlerOptions) {}
},
);