Skip to content

Commit ae8e9aa

Browse files
author
Elusive
committed
Fixed touch handling in mouseDragFactory method
1 parent d78b276 commit ae8e9aa

File tree

10 files changed

+136
-91
lines changed

10 files changed

+136
-91
lines changed

dist/frost-dom-bundle.js

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* FrostDOM Bundle v2.1.0
2+
* FrostDOM Bundle v2.1.2
33
* https://github.com/elusivecodes/FrostCore
44
* https://github.com/elusivecodes/FrostDOM
55
*/
@@ -1105,7 +1105,7 @@
11051105
});
11061106

11071107
/**
1108-
* FrostDOM v2.1.1
1108+
* FrostDOM v2.1.2
11091109
* https://github.com/elusivecodes/FrostDOM
11101110
*/
11111111
(function(global, factory) {
@@ -3599,11 +3599,7 @@
35993599
* @returns {DOM~eventCallback} The mouse drag event callback.
36003600
*/
36013601
mouseDragFactory(down, move, up, options = {}) {
3602-
const { debounce, passive } = {
3603-
debounce: true,
3604-
passive: true,
3605-
...options
3606-
};
3602+
const { debounce = true, passive = true, touches = 1 } = options;
36073603

36083604
if (move && debounce) {
36093605
move = this.constructor.debounce(move);
@@ -3617,6 +3613,10 @@
36173613
return e => {
36183614
const isTouch = e.type === 'touchstart';
36193615

3616+
if (isTouch && e.touches.length !== touches) {
3617+
return;
3618+
}
3619+
36203620
if (down && down(e) === false) {
36213621
return;
36223622
}
@@ -3625,37 +3625,49 @@
36253625
e.preventDefault();
36263626
}
36273627

3628+
if (!move && !up) {
3629+
return;
3630+
}
3631+
36283632
const moveEvent = isTouch ?
36293633
'touchmove' :
36303634
'mousemove';
36313635

3632-
if (move) {
3633-
this.addEvent(window, moveEvent, move, { passive });
3634-
}
3636+
const realMove = e => {
3637+
if (isTouch && e.touches.length !== touches) {
3638+
return;
3639+
}
36353640

3636-
if (move || up) {
3637-
const upEvent = isTouch ?
3638-
'touchend' :
3639-
'mouseup';
3641+
if (!move) {
3642+
return;
3643+
}
36403644

3641-
const realUp = e => {
3642-
if (up && up(e) === false) {
3643-
return;
3644-
}
3645+
move(e);
3646+
};
36453647

3646-
if (isTouch) {
3647-
e.preventDefault();
3648-
}
3648+
const upEvent = isTouch ?
3649+
'touchend' :
3650+
'mouseup';
36493651

3650-
this.removeEvent(window, upEvent, realUp);
3652+
const realUp = e => {
3653+
if (isTouch && e.touches.length !== touches) {
3654+
return;
3655+
}
36513656

3652-
if (move) {
3653-
this.removeEvent(window, moveEvent, move);
3654-
}
3655-
};
3657+
if (up && up(e) === false) {
3658+
return;
3659+
}
36563660

3657-
this.addEvent(window, upEvent, realUp, { passive });
3658-
}
3661+
if (isTouch) {
3662+
e.preventDefault();
3663+
}
3664+
3665+
this.removeEvent(window, moveEvent, realMove);
3666+
this.removeEvent(window, upEvent, realUp);
3667+
};
3668+
3669+
this.addEvent(window, moveEvent, realMove, { passive });
3670+
this.addEvent(window, upEvent, realUp, { passive });
36593671
};
36603672
}
36613673

dist/frost-dom-bundle.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/frost-dom.js

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* FrostDOM v2.1.1
2+
* FrostDOM v2.1.2
33
* https://github.com/elusivecodes/FrostDOM
44
*/
55
(function(global, factory) {
@@ -2493,11 +2493,7 @@
24932493
* @returns {DOM~eventCallback} The mouse drag event callback.
24942494
*/
24952495
mouseDragFactory(down, move, up, options = {}) {
2496-
const { debounce, passive } = {
2497-
debounce: true,
2498-
passive: true,
2499-
...options
2500-
};
2496+
const { debounce = true, passive = true, touches = 1 } = options;
25012497

25022498
if (move && debounce) {
25032499
move = this.constructor.debounce(move);
@@ -2511,6 +2507,10 @@
25112507
return e => {
25122508
const isTouch = e.type === 'touchstart';
25132509

2510+
if (isTouch && e.touches.length !== touches) {
2511+
return;
2512+
}
2513+
25142514
if (down && down(e) === false) {
25152515
return;
25162516
}
@@ -2519,37 +2519,49 @@
25192519
e.preventDefault();
25202520
}
25212521

2522+
if (!move && !up) {
2523+
return;
2524+
}
2525+
25222526
const moveEvent = isTouch ?
25232527
'touchmove' :
25242528
'mousemove';
25252529

2526-
if (move) {
2527-
this.addEvent(window, moveEvent, move, { passive });
2528-
}
2530+
const realMove = e => {
2531+
if (isTouch && e.touches.length !== touches) {
2532+
return;
2533+
}
25292534

2530-
if (move || up) {
2531-
const upEvent = isTouch ?
2532-
'touchend' :
2533-
'mouseup';
2535+
if (!move) {
2536+
return;
2537+
}
25342538

2535-
const realUp = e => {
2536-
if (up && up(e) === false) {
2537-
return;
2538-
}
2539+
move(e);
2540+
};
25392541

2540-
if (isTouch) {
2541-
e.preventDefault();
2542-
}
2542+
const upEvent = isTouch ?
2543+
'touchend' :
2544+
'mouseup';
25432545

2544-
this.removeEvent(window, upEvent, realUp);
2546+
const realUp = e => {
2547+
if (isTouch && e.touches.length !== touches) {
2548+
return;
2549+
}
25452550

2546-
if (move) {
2547-
this.removeEvent(window, moveEvent, move);
2548-
}
2549-
};
2551+
if (up && up(e) === false) {
2552+
return;
2553+
}
25502554

2551-
this.addEvent(window, upEvent, realUp, { passive });
2552-
}
2555+
if (isTouch) {
2556+
e.preventDefault();
2557+
}
2558+
2559+
this.removeEvent(window, moveEvent, realMove);
2560+
this.removeEvent(window, upEvent, realUp);
2561+
};
2562+
2563+
this.addEvent(window, moveEvent, realMove, { passive });
2564+
this.addEvent(window, upEvent, realUp, { passive });
25532565
};
25542566
}
25552567

dist/frost-dom.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "frostdom",
3-
"version": "2.1.1",
3+
"version": "2.1.2",
44
"description": "FrostDOM is a free, open-source DOM manipulation library for JavaScript.",
55
"keywords": [
66
"dom",

src/DOM/dom/events/event-factory.js

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ Object.assign(DOM.prototype, {
1515
* @returns {DOM~eventCallback} The mouse drag event callback.
1616
*/
1717
mouseDragFactory(down, move, up, options = {}) {
18-
const { debounce, passive } = {
19-
debounce: true,
20-
passive: true,
21-
...options
22-
};
18+
const { debounce = true, passive = true, touches = 1 } = options;
2319

2420
if (move && debounce) {
2521
move = this.constructor.debounce(move);
@@ -33,6 +29,10 @@ Object.assign(DOM.prototype, {
3329
return e => {
3430
const isTouch = e.type === 'touchstart';
3531

32+
if (isTouch && e.touches.length !== touches) {
33+
return;
34+
}
35+
3636
if (down && down(e) === false) {
3737
return;
3838
}
@@ -41,37 +41,49 @@ Object.assign(DOM.prototype, {
4141
e.preventDefault();
4242
}
4343

44+
if (!move && !up) {
45+
return;
46+
}
47+
4448
const moveEvent = isTouch ?
4549
'touchmove' :
4650
'mousemove';
4751

48-
if (move) {
49-
this.addEvent(window, moveEvent, move, { passive });
50-
}
52+
const realMove = e => {
53+
if (isTouch && e.touches.length !== touches) {
54+
return;
55+
}
5156

52-
if (move || up) {
53-
const upEvent = isTouch ?
54-
'touchend' :
55-
'mouseup';
57+
if (!move) {
58+
return;
59+
}
5660

57-
const realUp = e => {
58-
if (up && up(e) === false) {
59-
return;
60-
}
61+
move(e);
62+
};
6163

62-
if (isTouch) {
63-
e.preventDefault();
64-
}
64+
const upEvent = isTouch ?
65+
'touchend' :
66+
'mouseup';
6567

66-
this.removeEvent(window, upEvent, realUp);
68+
const realUp = e => {
69+
if (isTouch && e.touches.length !== touches) {
70+
return;
71+
}
6772

68-
if (move) {
69-
this.removeEvent(window, moveEvent, move);
70-
}
71-
};
73+
if (up && up(e) === false) {
74+
return;
75+
}
7276

73-
this.addEvent(window, upEvent, realUp, { passive });
74-
}
77+
if (isTouch) {
78+
e.preventDefault();
79+
}
80+
81+
this.removeEvent(window, moveEvent, realMove);
82+
this.removeEvent(window, upEvent, realUp);
83+
};
84+
85+
this.addEvent(window, moveEvent, realMove, { passive });
86+
this.addEvent(window, upEvent, realUp, { passive });
7587
};
7688
}
7789

src/bundle_wrapper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* FrostDOM Bundle v2.1.0
2+
* FrostDOM Bundle v2.1.2
33
* https://github.com/elusivecodes/FrostCore
44
* https://github.com/elusivecodes/FrostDOM
55
*/

src/wrapper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* FrostDOM v2.1.1
2+
* FrostDOM v2.1.2
33
* https://github.com/elusivecodes/FrostDOM
44
*/
55
(function(global, factory) {

test/DOM/events/factory/mouseDragFactory.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,21 @@ describe('#mouseDragFactory', function() {
224224
it('works with touch events', async function() {
225225
assert.strictEqual(
226226
await exec(_ => {
227+
const touch = new Touch({
228+
identifier: 1,
229+
target: document.body
230+
});
231+
227232
let result = 0;
228-
const downEvent = new Event('touchstart');
229-
const moveEvent = new Event('touchmove', {
233+
const downEvent = new TouchEvent('touchstart', {
234+
touches: [touch]
235+
});
236+
const moveEvent = new TouchEvent('touchmove', {
237+
touches: [touch],
230238
bubbles: true
231239
});
232-
const upEvent = new Event('touchend', {
240+
const upEvent = new TouchEvent('touchend', {
241+
touches: [touch],
233242
bubbles: true
234243
});
235244
dom.addEvent(

0 commit comments

Comments
 (0)