@danielgindi/dom-utils 1.0.10 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -7
- package/dist/lib.cjs.js +318 -434
- package/dist/lib.cjs.js.map +1 -1
- package/dist/lib.cjs.min.js +2 -2
- package/dist/lib.cjs.min.js.map +1 -1
- package/dist/lib.es6.js +977 -1093
- package/dist/lib.es6.js.map +1 -1
- package/dist/lib.es6.min.js +2 -2
- package/dist/lib.es6.min.js.map +1 -1
- package/dist/lib.umd.js +318 -434
- package/dist/lib.umd.js.map +1 -1
- package/dist/lib.umd.min.js +2 -2
- package/dist/lib.umd.min.js.map +1 -1
- package/dist/types/Css.d.ts +130 -0
- package/dist/types/Dom.d.ts +33 -0
- package/dist/types/DomCompat.d.ts +55 -0
- package/dist/types/DomEventsSink.d.ts +20 -0
- package/dist/types/ScrollHelper.d.ts +39 -0
- package/dist/types/Touches.d.ts +14 -0
- package/dist/types/index.d.ts +7 -0
- package/eslint.config.mjs +47 -111
- package/lib/{Css.js → Css.ts} +448 -434
- package/lib/{Dom.js → Dom.ts} +132 -132
- package/lib/{DomCompat.js → DomCompat.ts} +276 -276
- package/lib/{DomEventsSink.js → DomEventsSink.ts} +70 -68
- package/lib/{ScrollHelper.js → ScrollHelper.ts} +158 -158
- package/lib/{Touches.js → Touches.ts} +84 -84
- package/package.json +28 -18
- package/tsconfig.json +17 -0
- package/tsconfig.scripts.json +10 -0
- package/tsconfig.tests.json +10 -0
- /package/lib/{index.js → index.ts} +0 -0
|
@@ -1,68 +1,70 @@
|
|
|
1
|
-
export default class DomEventsSink {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
* @param {
|
|
14
|
-
* @param {
|
|
15
|
-
* @
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
let
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
* @param {
|
|
32
|
-
* @param {
|
|
33
|
-
* @
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
let
|
|
39
|
-
|
|
40
|
-
let
|
|
41
|
-
|
|
42
|
-
let
|
|
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
|
-
|
|
1
|
+
export default class DomEventsSink {
|
|
2
|
+
private _events: { el: EventTarget; name: string; namespace?: string; handler: EventListenerOrEventListenerObject; useCapture: boolean }[];
|
|
3
|
+
|
|
4
|
+
constructor() {
|
|
5
|
+
/**
|
|
6
|
+
* @type {{el: EventTarget, name: string, handler: EventListenerOrEventListenerObject, useCapture: boolean}[]}
|
|
7
|
+
* @private
|
|
8
|
+
*/
|
|
9
|
+
this._events = [];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {EventTarget} el
|
|
14
|
+
* @param {string} eventName
|
|
15
|
+
* @param {EventListenerOrEventListenerObject} handler
|
|
16
|
+
* @param {boolean|AddEventListenerOptions} [optionsOrCapture=undefined]
|
|
17
|
+
* @returns {DomEventsSink}
|
|
18
|
+
*/
|
|
19
|
+
add(el: EventTarget, eventName: string, handler: EventListenerOrEventListenerObject, optionsOrCapture?: boolean | AddEventListenerOptions): DomEventsSink {
|
|
20
|
+
let parts = eventName.split('.');
|
|
21
|
+
let name = parts[0];
|
|
22
|
+
let namespace = parts[1];
|
|
23
|
+
|
|
24
|
+
el.addEventListener(name, handler, optionsOrCapture ? optionsOrCapture : false);
|
|
25
|
+
let useCapture = optionsOrCapture === true || typeof optionsOrCapture === 'object' && optionsOrCapture.capture === true;
|
|
26
|
+
this._events.push({ el: el, name: name, namespace: namespace, handler: handler, useCapture: useCapture });
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @param {EventTarget} [el=undefined]
|
|
32
|
+
* @param {string} [eventName=undefined]
|
|
33
|
+
* @param {EventListenerOrEventListenerObject} [handler=undefined]
|
|
34
|
+
* @param {boolean|EventListenerOptions} [optionsOrCapture=undefined]
|
|
35
|
+
* @returns {DomEventsSink}
|
|
36
|
+
*/
|
|
37
|
+
remove(el?: EventTarget, eventName?: string, handler?: EventListenerOrEventListenerObject, optionsOrCapture?: boolean | EventListenerOptions): DomEventsSink {
|
|
38
|
+
let parts = eventName ? eventName.split('.') : '';
|
|
39
|
+
let name = parts[0];
|
|
40
|
+
let namespace = parts[1];
|
|
41
|
+
|
|
42
|
+
let useCapture = optionsOrCapture === true || typeof optionsOrCapture === 'object' && optionsOrCapture.capture === true;
|
|
43
|
+
|
|
44
|
+
let keep = [];
|
|
45
|
+
let remove = [];
|
|
46
|
+
|
|
47
|
+
if (el || name || namespace || handler || optionsOrCapture !== undefined) {
|
|
48
|
+
for (let item of this._events) {
|
|
49
|
+
if ((el && item.el !== el) ||
|
|
50
|
+
(name && item.name !== name) ||
|
|
51
|
+
(namespace && item.namespace !== namespace) ||
|
|
52
|
+
(handler && item.handler !== handler) ||
|
|
53
|
+
(optionsOrCapture !== undefined && item.useCapture !== useCapture)) {
|
|
54
|
+
keep.push(item);
|
|
55
|
+
} else {
|
|
56
|
+
remove.push(item);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
} else {
|
|
60
|
+
remove = this._events;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
this._events = keep;
|
|
64
|
+
|
|
65
|
+
for (let item of remove) {
|
|
66
|
+
item.el.removeEventListener(item.name, item.handler, item.useCapture);
|
|
67
|
+
}
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -1,158 +1,158 @@
|
|
|
1
|
-
let rtlScrollType;
|
|
2
|
-
|
|
3
|
-
const detectRtlScrollType = () => {
|
|
4
|
-
const definer = document.createElement('div');
|
|
5
|
-
definer.dir = 'rtl';
|
|
6
|
-
Object.assign(definer.style, {
|
|
7
|
-
direction: 'rtl',
|
|
8
|
-
fontSize: '14px',
|
|
9
|
-
width: '1px',
|
|
10
|
-
height: '1px',
|
|
11
|
-
position: 'absolute',
|
|
12
|
-
top: '-1000px',
|
|
13
|
-
overflow: 'scroll',
|
|
14
|
-
});
|
|
15
|
-
definer.textContent = 'A';
|
|
16
|
-
document.body.appendChild(definer);
|
|
17
|
-
|
|
18
|
-
let type = 'reverse';
|
|
19
|
-
|
|
20
|
-
if (definer.scrollLeft > 0) {
|
|
21
|
-
type = 'default';
|
|
22
|
-
} else {
|
|
23
|
-
definer.scrollLeft = 1;
|
|
24
|
-
|
|
25
|
-
// bug: on some machines, chrome will have a positive delta of less than 1.
|
|
26
|
-
// a full scroll will still be in the negative direction.
|
|
27
|
-
// let's use Math.floor() to account for that bug.
|
|
28
|
-
if (Math.floor(definer.scrollLeft) === 0) {
|
|
29
|
-
type = 'negative';
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
definer.parentNode.removeChild(definer);
|
|
34
|
-
|
|
35
|
-
return type;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* @param {Element} el
|
|
40
|
-
* @param {number} left
|
|
41
|
-
* @param {boolean|undefined} [rtl] if unspecified, then it's automatically detected.
|
|
42
|
-
* @returns {number}
|
|
43
|
-
*/
|
|
44
|
-
function calculateNativeScrollLeftForLeft(el, left, rtl) {
|
|
45
|
-
if (rtl === undefined) {
|
|
46
|
-
rtl = getComputedStyle(el).direction === 'rtl';
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if (rtl === true && rtlScrollType === undefined) {
|
|
50
|
-
rtlScrollType = detectRtlScrollType();
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (rtl) {
|
|
54
|
-
switch (rtlScrollType) {
|
|
55
|
-
case 'negative':
|
|
56
|
-
return left - el.scrollWidth + el.clientWidth;
|
|
57
|
-
|
|
58
|
-
case 'reverse':
|
|
59
|
-
return el.scrollWidth - left - el.clientWidth;
|
|
60
|
-
|
|
61
|
-
default:
|
|
62
|
-
return left;
|
|
63
|
-
}
|
|
64
|
-
} else {
|
|
65
|
-
return left;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* @param {Element} el
|
|
71
|
-
* @param {boolean|undefined} [rtl] if unspecified, then it's automatically detected.
|
|
72
|
-
* @returns {number}
|
|
73
|
-
*/
|
|
74
|
-
function getScrollLeft(el, rtl) {
|
|
75
|
-
if (rtl === undefined) {
|
|
76
|
-
rtl = getComputedStyle(el).direction === 'rtl';
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (rtl === true && rtlScrollType === undefined) {
|
|
80
|
-
rtlScrollType = detectRtlScrollType();
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (rtl) {
|
|
84
|
-
switch (rtlScrollType) {
|
|
85
|
-
case 'negative':
|
|
86
|
-
return el.scrollLeft + el.scrollWidth - el.clientWidth;
|
|
87
|
-
|
|
88
|
-
case 'reverse':
|
|
89
|
-
return el.scrollWidth - el.scrollLeft - el.clientWidth;
|
|
90
|
-
|
|
91
|
-
default:
|
|
92
|
-
return el.scrollLeft;
|
|
93
|
-
}
|
|
94
|
-
} else {
|
|
95
|
-
return el.scrollLeft;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* @param {Element} el
|
|
101
|
-
* @param {number} left
|
|
102
|
-
* @param {boolean|undefined} [rtl] if unspecified, then it's automatically detected.
|
|
103
|
-
*/
|
|
104
|
-
function setScrollLeft(el, left, rtl) {
|
|
105
|
-
el.scrollLeft = calculateNativeScrollLeftForLeft(el, left, rtl);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* @param {Element} el
|
|
110
|
-
* @param {number} value
|
|
111
|
-
* @param {boolean|undefined} [rtl] if unspecified, then it's automatically detected.
|
|
112
|
-
* @returns {number}
|
|
113
|
-
*/
|
|
114
|
-
function calculateNativeScrollLeftForHorz(el, value, rtl) {
|
|
115
|
-
if (rtl === undefined) {
|
|
116
|
-
rtl = getComputedStyle(el).direction === 'rtl';
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
if (rtl) {
|
|
120
|
-
return calculateNativeScrollLeftForLeft(el, el.scrollWidth - el.clientWidth - value, rtl);
|
|
121
|
-
} else {
|
|
122
|
-
return calculateNativeScrollLeftForLeft(el, value, rtl);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* @param {Element} el
|
|
128
|
-
* @param {boolean|undefined} [rtl] if unspecified, then it's automatically detected.
|
|
129
|
-
* @returns {number}
|
|
130
|
-
*/
|
|
131
|
-
function getScrollHorz(el, rtl) {
|
|
132
|
-
if (rtl === undefined) {
|
|
133
|
-
rtl = getComputedStyle(el).direction === 'rtl';
|
|
134
|
-
}
|
|
135
|
-
if (rtl) {
|
|
136
|
-
return el.scrollWidth - el.clientWidth - getScrollLeft(el, rtl);
|
|
137
|
-
} else {
|
|
138
|
-
return getScrollLeft(el, rtl);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* @param {Element} el
|
|
144
|
-
* @param {number} horz
|
|
145
|
-
* @param {boolean|undefined} [rtl] if unspecified, then it's automatically detected.
|
|
146
|
-
*/
|
|
147
|
-
function setScrollHorz(el, horz, rtl) {
|
|
148
|
-
el.scrollLeft = calculateNativeScrollLeftForHorz(el, horz, rtl);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
export {
|
|
152
|
-
calculateNativeScrollLeftForLeft,
|
|
153
|
-
getScrollLeft,
|
|
154
|
-
setScrollLeft,
|
|
155
|
-
calculateNativeScrollLeftForHorz,
|
|
156
|
-
getScrollHorz,
|
|
157
|
-
setScrollHorz,
|
|
158
|
-
};
|
|
1
|
+
let rtlScrollType: string | undefined;
|
|
2
|
+
|
|
3
|
+
const detectRtlScrollType = () => {
|
|
4
|
+
const definer = document.createElement('div');
|
|
5
|
+
definer.dir = 'rtl';
|
|
6
|
+
Object.assign(definer.style, {
|
|
7
|
+
direction: 'rtl',
|
|
8
|
+
fontSize: '14px',
|
|
9
|
+
width: '1px',
|
|
10
|
+
height: '1px',
|
|
11
|
+
position: 'absolute',
|
|
12
|
+
top: '-1000px',
|
|
13
|
+
overflow: 'scroll',
|
|
14
|
+
});
|
|
15
|
+
definer.textContent = 'A';
|
|
16
|
+
document.body.appendChild(definer);
|
|
17
|
+
|
|
18
|
+
let type = 'reverse';
|
|
19
|
+
|
|
20
|
+
if (definer.scrollLeft > 0) {
|
|
21
|
+
type = 'default';
|
|
22
|
+
} else {
|
|
23
|
+
definer.scrollLeft = 1;
|
|
24
|
+
|
|
25
|
+
// bug: on some machines, chrome will have a positive delta of less than 1.
|
|
26
|
+
// a full scroll will still be in the negative direction.
|
|
27
|
+
// let's use Math.floor() to account for that bug.
|
|
28
|
+
if (Math.floor(definer.scrollLeft) === 0) {
|
|
29
|
+
type = 'negative';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
definer.parentNode.removeChild(definer);
|
|
34
|
+
|
|
35
|
+
return type;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @param {Element} el
|
|
40
|
+
* @param {number} left
|
|
41
|
+
* @param {boolean|undefined} [rtl] if unspecified, then it's automatically detected.
|
|
42
|
+
* @returns {number}
|
|
43
|
+
*/
|
|
44
|
+
function calculateNativeScrollLeftForLeft(el: Element, left: number, rtl?: boolean): number {
|
|
45
|
+
if (rtl === undefined) {
|
|
46
|
+
rtl = getComputedStyle(el).direction === 'rtl';
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (rtl === true && rtlScrollType === undefined) {
|
|
50
|
+
rtlScrollType = detectRtlScrollType();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (rtl) {
|
|
54
|
+
switch (rtlScrollType) {
|
|
55
|
+
case 'negative':
|
|
56
|
+
return left - el.scrollWidth + el.clientWidth;
|
|
57
|
+
|
|
58
|
+
case 'reverse':
|
|
59
|
+
return el.scrollWidth - left - el.clientWidth;
|
|
60
|
+
|
|
61
|
+
default:
|
|
62
|
+
return left;
|
|
63
|
+
}
|
|
64
|
+
} else {
|
|
65
|
+
return left;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @param {Element} el
|
|
71
|
+
* @param {boolean|undefined} [rtl] if unspecified, then it's automatically detected.
|
|
72
|
+
* @returns {number}
|
|
73
|
+
*/
|
|
74
|
+
function getScrollLeft(el: Element, rtl?: boolean): number {
|
|
75
|
+
if (rtl === undefined) {
|
|
76
|
+
rtl = getComputedStyle(el).direction === 'rtl';
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (rtl === true && rtlScrollType === undefined) {
|
|
80
|
+
rtlScrollType = detectRtlScrollType();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (rtl) {
|
|
84
|
+
switch (rtlScrollType) {
|
|
85
|
+
case 'negative':
|
|
86
|
+
return el.scrollLeft + el.scrollWidth - el.clientWidth;
|
|
87
|
+
|
|
88
|
+
case 'reverse':
|
|
89
|
+
return el.scrollWidth - el.scrollLeft - el.clientWidth;
|
|
90
|
+
|
|
91
|
+
default:
|
|
92
|
+
return el.scrollLeft;
|
|
93
|
+
}
|
|
94
|
+
} else {
|
|
95
|
+
return el.scrollLeft;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* @param {Element} el
|
|
101
|
+
* @param {number} left
|
|
102
|
+
* @param {boolean|undefined} [rtl] if unspecified, then it's automatically detected.
|
|
103
|
+
*/
|
|
104
|
+
function setScrollLeft(el: Element, left: number, rtl?: boolean): void {
|
|
105
|
+
el.scrollLeft = calculateNativeScrollLeftForLeft(el, left, rtl);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* @param {Element} el
|
|
110
|
+
* @param {number} value
|
|
111
|
+
* @param {boolean|undefined} [rtl] if unspecified, then it's automatically detected.
|
|
112
|
+
* @returns {number}
|
|
113
|
+
*/
|
|
114
|
+
function calculateNativeScrollLeftForHorz(el: Element, value: number, rtl?: boolean): number {
|
|
115
|
+
if (rtl === undefined) {
|
|
116
|
+
rtl = getComputedStyle(el).direction === 'rtl';
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (rtl) {
|
|
120
|
+
return calculateNativeScrollLeftForLeft(el, el.scrollWidth - el.clientWidth - value, rtl);
|
|
121
|
+
} else {
|
|
122
|
+
return calculateNativeScrollLeftForLeft(el, value, rtl);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* @param {Element} el
|
|
128
|
+
* @param {boolean|undefined} [rtl] if unspecified, then it's automatically detected.
|
|
129
|
+
* @returns {number}
|
|
130
|
+
*/
|
|
131
|
+
function getScrollHorz(el: Element, rtl?: boolean): number {
|
|
132
|
+
if (rtl === undefined) {
|
|
133
|
+
rtl = getComputedStyle(el).direction === 'rtl';
|
|
134
|
+
}
|
|
135
|
+
if (rtl) {
|
|
136
|
+
return el.scrollWidth - el.clientWidth - getScrollLeft(el, rtl);
|
|
137
|
+
} else {
|
|
138
|
+
return getScrollLeft(el, rtl);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* @param {Element} el
|
|
144
|
+
* @param {number} horz
|
|
145
|
+
* @param {boolean|undefined} [rtl] if unspecified, then it's automatically detected.
|
|
146
|
+
*/
|
|
147
|
+
function setScrollHorz(el: Element, horz: number, rtl?: boolean): void {
|
|
148
|
+
el.scrollLeft = calculateNativeScrollLeftForHorz(el, horz, rtl);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export {
|
|
152
|
+
calculateNativeScrollLeftForLeft,
|
|
153
|
+
getScrollLeft,
|
|
154
|
+
setScrollLeft,
|
|
155
|
+
calculateNativeScrollLeftForHorz,
|
|
156
|
+
getScrollHorz,
|
|
157
|
+
setScrollHorz,
|
|
158
|
+
};
|
|
@@ -1,84 +1,84 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @param {Element} el
|
|
3
|
-
* @param {Object} [options]
|
|
4
|
-
* @param {number|undefined} [options.distance=9]
|
|
5
|
-
* @param {function(event: TouchEvent)} [options.handler]
|
|
6
|
-
* @returns {{ unbind: Function }}
|
|
7
|
-
*/
|
|
8
|
-
const bindTouchTap = function (el, options) {
|
|
9
|
-
let currentTouchId = null;
|
|
10
|
-
let downPos = null;
|
|
11
|
-
|
|
12
|
-
const startHandler = evt => {
|
|
13
|
-
if (currentTouchId !== null) return;
|
|
14
|
-
|
|
15
|
-
let touch = evt.changedTouches[0];
|
|
16
|
-
currentTouchId = touch.identifier;
|
|
17
|
-
|
|
18
|
-
downPos = touch
|
|
19
|
-
? { x: touch.pageX, y: touch.pageY }
|
|
20
|
-
: { x: evt.pageX, y: evt.pageY };
|
|
21
|
-
|
|
22
|
-
el.addEventListener('touchend', endHandler);
|
|
23
|
-
el.addEventListener('touchcancel', cancelHandler);
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
const endHandler = evt => {
|
|
27
|
-
if (currentTouchId === null) return;
|
|
28
|
-
|
|
29
|
-
let touch = null;
|
|
30
|
-
|
|
31
|
-
if (evt.changedTouches) {
|
|
32
|
-
if (currentTouchId != null) {
|
|
33
|
-
for (let item of evt.changedTouches) {
|
|
34
|
-
if (item.identifier === currentTouchId) {
|
|
35
|
-
touch = item;
|
|
36
|
-
break;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (!touch) {
|
|
42
|
-
touch = evt.changedTouches[0];
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
let currentPos = touch
|
|
47
|
-
? { x: touch.pageX, y: touch.pageY }
|
|
48
|
-
: { x: evt.pageX, y: evt.pageY };
|
|
49
|
-
let startPos = downPos;
|
|
50
|
-
|
|
51
|
-
currentTouchId = null;
|
|
52
|
-
downPos = null;
|
|
53
|
-
|
|
54
|
-
if (options.distance !== null) {
|
|
55
|
-
let distanceThreshold = options.distance || 1;
|
|
56
|
-
let distanceTravelled = Math.hypot(Math.abs(currentPos.x - startPos.x), Math.abs(currentPos.y - startPos.y));
|
|
57
|
-
if (distanceTravelled >= distanceThreshold) return false;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
options.handler && options.handler(evt);
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
const cancelHandler = () => {
|
|
64
|
-
currentTouchId = null;
|
|
65
|
-
downPos = null;
|
|
66
|
-
|
|
67
|
-
el.removeEventListener('touchend', endHandler);
|
|
68
|
-
el.removeEventListener('touchcancel', cancelHandler);
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
el.addEventListener('touchstart', startHandler);
|
|
72
|
-
|
|
73
|
-
return {
|
|
74
|
-
unbind: () => {
|
|
75
|
-
el.removeEventListener('touchstart', startHandler);
|
|
76
|
-
el.removeEventListener('touchend', endHandler);
|
|
77
|
-
el.removeEventListener('touchcancel', cancelHandler);
|
|
78
|
-
},
|
|
79
|
-
};
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
export {
|
|
83
|
-
bindTouchTap,
|
|
84
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* @param {Element} el
|
|
3
|
+
* @param {Object} [options]
|
|
4
|
+
* @param {number|undefined} [options.distance=9]
|
|
5
|
+
* @param {function(event: TouchEvent)} [options.handler]
|
|
6
|
+
* @returns {{ unbind: Function }}
|
|
7
|
+
*/
|
|
8
|
+
const bindTouchTap = function (el: Element, options: { distance?: number | null; handler?: (event: TouchEvent) => void }): { unbind: () => void } {
|
|
9
|
+
let currentTouchId: number | null = null;
|
|
10
|
+
let downPos: { x: number; y: number } | null = null;
|
|
11
|
+
|
|
12
|
+
const startHandler = (evt: TouchEvent) => {
|
|
13
|
+
if (currentTouchId !== null) return;
|
|
14
|
+
|
|
15
|
+
let touch = evt.changedTouches[0];
|
|
16
|
+
currentTouchId = touch.identifier;
|
|
17
|
+
|
|
18
|
+
downPos = touch
|
|
19
|
+
? { x: touch.pageX, y: touch.pageY }
|
|
20
|
+
: { x: (evt as TouchEvent & { pageX: number }).pageX, y: (evt as TouchEvent & { pageY: number }).pageY };
|
|
21
|
+
|
|
22
|
+
el.addEventListener('touchend', endHandler);
|
|
23
|
+
el.addEventListener('touchcancel', cancelHandler);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const endHandler = (evt: TouchEvent) => {
|
|
27
|
+
if (currentTouchId === null) return;
|
|
28
|
+
|
|
29
|
+
let touch: Touch | null = null;
|
|
30
|
+
|
|
31
|
+
if (evt.changedTouches) {
|
|
32
|
+
if (currentTouchId != null) {
|
|
33
|
+
for (let item of evt.changedTouches) {
|
|
34
|
+
if (item.identifier === currentTouchId) {
|
|
35
|
+
touch = item;
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (!touch) {
|
|
42
|
+
touch = evt.changedTouches[0];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let currentPos = touch
|
|
47
|
+
? { x: touch.pageX, y: touch.pageY }
|
|
48
|
+
: { x: (evt as TouchEvent & { pageX: number }).pageX, y: (evt as TouchEvent & { pageY: number }).pageY };
|
|
49
|
+
let startPos = downPos;
|
|
50
|
+
|
|
51
|
+
currentTouchId = null;
|
|
52
|
+
downPos = null;
|
|
53
|
+
|
|
54
|
+
if (options.distance !== null) {
|
|
55
|
+
let distanceThreshold = options.distance || 1;
|
|
56
|
+
let distanceTravelled = Math.hypot(Math.abs(currentPos.x - startPos.x), Math.abs(currentPos.y - startPos.y));
|
|
57
|
+
if (distanceTravelled >= distanceThreshold) return false;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
options.handler && options.handler(evt);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const cancelHandler = () => {
|
|
64
|
+
currentTouchId = null;
|
|
65
|
+
downPos = null;
|
|
66
|
+
|
|
67
|
+
el.removeEventListener('touchend', endHandler);
|
|
68
|
+
el.removeEventListener('touchcancel', cancelHandler);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
el.addEventListener('touchstart', startHandler);
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
unbind: () => {
|
|
75
|
+
el.removeEventListener('touchstart', startHandler);
|
|
76
|
+
el.removeEventListener('touchend', endHandler);
|
|
77
|
+
el.removeEventListener('touchcancel', cancelHandler);
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export {
|
|
83
|
+
bindTouchTap,
|
|
84
|
+
};
|