-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongpressbutton.js
More file actions
121 lines (109 loc) · 3.07 KB
/
longpressbutton.js
File metadata and controls
121 lines (109 loc) · 3.07 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
const CSSident = 'lpb-';
/**
* Create new LongPressButton and return the Button itself
*
* @param {HTMLElement} button
* @returns HtmlElement
*/
class LongPressButton {
constructor(button, duration) {
this._button = button;
this._button.classList.add(CSSident + 'button');
this._loader = document.createElement('div');
this._loader.className = CSSident + 'loader';
this._fn = button.onclick;
this._button.onclick = () => {};
this._touch = false;
this._timeout = duration;
this._loader.style.animationDuration = `${this._timeout}ms`;
this._timer = undefined;
this._button.addEventListener(
'touchstart',
(event) => {
event.preventDefault();
this._touch = true;
this._loader.classList.add(CSSident + 'loading');
this._timer = window.setTimeout((event) => this._exec(event), this._timeout, event);
},
true
);
window.addEventListener(
'touchend',
(event) => {
event.preventDefault();
if (this._touch) {
this._loader.classList.remove(CSSident + 'loading');
clearTimeout(this._timer);
this._touch = false;
}
},
true
);
this._button.addEventListener(
'mousedown',
(event) => {
event.preventDefault();
this._touch = true;
this._loader.classList.add(CSSident + 'loading');
this._timer = window.setTimeout((event) => this._exec(event), this._timeout, event);
},
true
);
window.addEventListener(
'mouseup',
(event) => {
event.preventDefault();
if (this._touch) {
this._loader.classList.remove(CSSident + 'loading');
clearTimeout(this._timer);
this._touch = false;
}
},
true
);
this._button.append(this._loader);
return this._button;
}
_onAnimationEnd() {
this._loader.classList.remove(CSSident + 'loaded');
this._loader.removeEventListener('animationend', this._onAnimationEnd);
}
_exec(event) {
this._touch = false;
this._loader.classList.remove(CSSident + 'loading');
this._loader.classList.add(CSSident + 'loaded');
this._loader.addEventListener('animationend', this._onAnimationEnd.bind(this));
this._button.dispatchEvent(new Event('longpress', event));
this._fn ? this._fn(event) : null;
}
}
/**
* @param {HTMLElement} searchArea
*/
const init = (searchArea = undefined) => {
const buttons = searchArea
? searchArea.querySelectorAll('[data-longpress]')
: document.querySelectorAll('[data-longpress]');
buttons.forEach((button) => {
new LongPressButton(button, button.dataset.longpress);
});
return buttons;
};
export const lpButton = {
/**
* Create new LongPressButton and return the Button itself
*
* @param {HTMLElement} button
* @param {number} duration
* @returns HtmlElement
*/
newButton: (button, duration) => {
return new LongPressButton(button, duration ?? 1000);
},
/**
* Initialize document and create LongPressButtons.
*
* @param {HTMLElement} searchArea
*/
init,
};