1
+ import { isPhone } from "../Device.js" ;
1
2
import EventProvider from "../EventProvider.js" ;
2
3
import scroll from "../animations/scroll.js" ;
3
4
4
5
const scrollEventName = "scroll" ;
5
- const touchEndEventName = "touchend" ;
6
+ const touchEndEventName = isPhone ( ) ? "touchend" : "mouseup ";
6
7
7
8
class ScrollEnablement extends EventProvider {
8
9
constructor ( containerComponent ) {
9
10
super ( ) ;
10
- containerComponent . addEventListener ( "touchstart" , this . ontouchstart . bind ( this ) , { passive : true } ) ;
11
- containerComponent . addEventListener ( "touchmove" , this . ontouchmove . bind ( this ) , { passive : true } ) ;
12
- containerComponent . addEventListener ( "touchend" , this . ontouchend . bind ( this ) , { passive : true } ) ;
11
+ this . containerComponent = containerComponent ;
12
+ this . mouseMove = this . ontouchmove . bind ( this ) ;
13
+ this . mouseUp = this . ontouchend . bind ( this ) ;
14
+ this . touchStart = this . ontouchstart . bind ( this ) ;
15
+
16
+ this . isPhone = isPhone ( ) ;
17
+
18
+ if ( this . isPhone ) {
19
+ containerComponent . addEventListener ( "touchstart" , this . touchStart , { passive : true } ) ;
20
+ containerComponent . addEventListener ( "touchmove" , this . mouseMove , { passive : true } ) ;
21
+ containerComponent . addEventListener ( "touchend" , this . mouseUp , { passive : true } ) ;
22
+ } else {
23
+ containerComponent . addEventListener ( "mousedown" , this . touchStart , { passive : true } ) ;
24
+ }
13
25
}
14
26
15
27
set scrollContainer ( container ) {
@@ -43,19 +55,25 @@ class ScrollEnablement extends EventProvider {
43
55
44
56
_isTouchInside ( touch ) {
45
57
const rect = this . _container . getBoundingClientRect ( ) ;
46
- const x = touch . clientX ;
47
- const y = touch . clientY ;
58
+ const x = this . isPhone ? touch . clientX : touch . x ;
59
+ const y = this . isPhone ? touch . clientY : touch . y ;
48
60
49
61
return x >= rect . left && x <= rect . right
50
62
&& y >= rect . top && y <= rect . bottom ;
51
63
}
52
64
53
65
ontouchstart ( event ) {
54
- const touch = event . touches [ 0 ] ;
55
- this . _prevDragX = touch . pageX ;
56
- this . _prevDragY = touch . pageY ;
66
+ const touch = this . isPhone ? event . touches [ 0 ] : null ;
67
+
68
+ if ( ! this . isPhone ) {
69
+ document . addEventListener ( "mouseup" , this . mouseUp , { passive : true } ) ;
70
+ document . addEventListener ( "mousemove" , this . mouseMove , { passive : true } ) ;
71
+ }
57
72
58
- this . _canScroll = this . _isTouchInside ( touch ) ;
73
+ this . _prevDragX = this . isPhone ? touch . pageX : event . x ;
74
+ this . _prevDragY = this . isPhone ? touch . pageY : event . y ;
75
+
76
+ this . _canScroll = this . _isTouchInside ( this . isPhone ? touch : event ) ;
59
77
}
60
78
61
79
ontouchmove ( event ) {
@@ -64,10 +82,10 @@ class ScrollEnablement extends EventProvider {
64
82
}
65
83
66
84
const container = this . _container ;
67
- const touch = event . touches [ 0 ] ;
85
+ const touch = this . isPhone ? event . touches [ 0 ] : null ;
68
86
69
- const dragX = touch . pageX ;
70
- const dragY = touch . pageY ;
87
+ const dragX = this . isPhone ? touch . pageX : event . x ;
88
+ const dragY = this . isPhone ? touch . pageY : event . y ;
71
89
72
90
container . scrollLeft += this . _prevDragX - dragX ;
73
91
container . scrollTop += this . _prevDragY - dragY ;
@@ -87,8 +105,8 @@ class ScrollEnablement extends EventProvider {
87
105
}
88
106
89
107
const container = this . _container ;
90
- const dragX = event . pageX ;
91
- const dragY = event . pageY ;
108
+ const dragX = this . isPhone ? event . pageX : event . x ;
109
+ const dragY = this . isPhone ? event . pageY : event . y ;
92
110
93
111
container . scrollLeft += this . _prevDragX - dragX ;
94
112
container . scrollTop += this . _prevDragY - dragY ;
@@ -100,6 +118,11 @@ class ScrollEnablement extends EventProvider {
100
118
101
119
this . _prevDragX = dragX ;
102
120
this . _prevDragY = dragY ;
121
+
122
+ if ( ! this . isPhone ) {
123
+ document . removeEventListener ( "mousemove" , this . mouseMove , { passive : true } ) ;
124
+ document . removeEventListener ( "mouseup" , this . mouseUp ) ;
125
+ }
103
126
}
104
127
}
105
128
0 commit comments