1
1
const $ = require ( 'jquery' )
2
2
3
+ const getPageCoordinates = function ( event ) {
4
+ if ( event . type . includes ( 'touch' ) ) {
5
+ return {
6
+ pageX : event . originalEvent . changedTouches [ 0 ] . pageX ,
7
+ pageY : event . originalEvent . changedTouches [ 0 ] . pageY
8
+ }
9
+ }
10
+ return { pageX : event . pageX , pageY : event . pageY }
11
+ }
12
+
3
13
module . exports = class Events {
4
14
constructor ( { parent, view, horizontal, vertical, actions} ) {
5
15
this . parent = parent
@@ -22,20 +32,22 @@ module.exports = class Events {
22
32
23
33
pan ( ) {
24
34
const $doc = $ ( document )
25
- this . view . on ( 'mousedown.srcissors' , ( e1 ) => {
35
+ this . view . on ( 'mousedown.srcissors touchstart.srcissors ' , ( e1 ) => {
26
36
const panData = {
27
37
startX : this . parent . preview . x ,
28
38
startY : this . parent . preview . y
29
39
}
30
40
31
41
e1 . preventDefault ( )
32
- $doc . on ( 'mousemove.srcissors-pan' , ( e2 ) => {
33
- panData . dx = e2 . pageX - e1 . pageX
34
- panData . dy = e2 . pageY - e1 . pageY
42
+ $doc . on ( 'mousemove.srcissors-pan touchmove.srcissors-pan' , ( e2 ) => {
43
+ const { pageX, pageY} = getPageCoordinates ( e2 )
44
+ const { pageX : prevPageX , pageY : prevPageY } = getPageCoordinates ( e1 )
45
+ panData . dx = pageX - prevPageX
46
+ panData . dy = pageY - prevPageY
35
47
this . parent . onPan ( panData )
36
- } ) . on ( 'mouseup.srcissors-pan' , ( ) => {
37
- $doc . off ( 'mouseup.srcissors-pan' )
38
- $doc . off ( 'mousemove.srcissors-pan' )
48
+ } ) . on ( 'mouseup.srcissors-pan touchend.srcissors-pan ' , ( ) => {
49
+ $doc . off ( 'mouseup.srcissors-pan touchend.srcissors-pan ' )
50
+ $doc . off ( 'mousemove.srcissors-pan touchmove.srcissors-pan ' )
39
51
40
52
// only trigger panEnd if pan has been called
41
53
if ( panData . dx != null ) this . parent . onPanEnd ( )
@@ -46,10 +58,10 @@ module.exports = class Events {
46
58
doubleClick ( ) {
47
59
let lastClick
48
60
49
- this . view . on ( 'mousedown.srcissors' , event => {
61
+ this . view . on ( 'mousedown.srcissors touchstart.srcissors ' , event => {
50
62
const now = new Date ( ) . getTime ( )
51
63
if ( lastClick && ( lastClick > ( now - this . doubleClickThreshold ) ) ) {
52
- this . parent . onDoubleClick ( { pageX : event . pageX , pageY : event . pageY } )
64
+ this . parent . onDoubleClick ( getPageCoordinates ( event ) )
53
65
}
54
66
lastClick = now
55
67
} )
@@ -73,7 +85,7 @@ module.exports = class Events {
73
85
positions . forEach ( position => {
74
86
const $handler = $template . clone ( )
75
87
$handler . addClass ( `resize-handler-${ position } ` )
76
- $handler . on ( 'mousedown.srcissors' , this . getResizeMouseDown ( position ) )
88
+ $handler . on ( 'mousedown.srcissors touchstart.srcissors ' , this . getResizeMouseDown ( position ) )
77
89
78
90
this . view . append ( $handler )
79
91
} )
@@ -83,30 +95,29 @@ module.exports = class Events {
83
95
const $doc = $ ( document )
84
96
85
97
return ( event ) => {
86
- let lastX = event . pageX
87
- let lastY = event . pageY
88
-
98
+ let { pageX : lastX , pageY : lastY } = getPageCoordinates ( event )
89
99
event . stopPropagation ( )
90
100
91
- $doc . on ( 'mousemove.srcissors-resize' , e2 => {
101
+ $doc . on ( 'mousemove.srcissors-resize touchmove.srcissors-resize ' , e2 => {
92
102
let dx , dy
103
+ const { pageX, pageY} = getPageCoordinates ( e2 )
93
104
switch ( position ) {
94
105
case 'top' : case 'bottom' :
95
- dy = e2 . pageY - lastY
106
+ dy = pageY - lastY
96
107
if ( position === 'top' ) { dy = - dy }
97
- lastY = e2 . pageY
108
+ lastY = pageY
98
109
break
99
110
case 'left' : case 'right' :
100
- dx = e2 . pageX - lastX
111
+ dx = pageX - lastX
101
112
if ( position === 'left' ) { dx = - dx }
102
- lastX = e2 . pageX
113
+ lastX = pageX
103
114
break
104
115
}
105
116
106
117
this . parent . onResize ( { position, dx, dy} )
107
- } ) . on ( 'mouseup.srcissors-resize' , ( ) => {
108
- $doc . off ( 'mouseup.srcissors-resize' )
109
- $doc . off ( 'mousemove.srcissors-resize' )
118
+ } ) . on ( 'mouseup.srcissors-resize touchend.srcissors-resize ' , ( ) => {
119
+ $doc . off ( 'mouseup.srcissors-resize touchmove.srcissors-resize ' )
120
+ $doc . off ( 'mousemove.srcissors-resize touchend.srcissors-resize ' )
110
121
111
122
// only trigger panEnd if pan has been called
112
123
this . parent . onResizeEnd ( { position} )
0 commit comments