9
9
10
10
'use strict' ;
11
11
12
+ var mouseOffset = require ( 'mouse-event-offset' ) ;
13
+
12
14
var Plotly = require ( '../../plotly' ) ;
13
15
var Lib = require ( '../../lib' ) ;
14
16
@@ -71,8 +73,9 @@ dragElement.init = function init(options) {
71
73
// so that others can look at and modify them
72
74
gd . _dragged = false ;
73
75
gd . _dragging = true ;
74
- startX = e . clientX ;
75
- startY = e . clientY ;
76
+ var offset = pointerOffset ( e )
77
+ startX = offset [ 0 ] ;
78
+ startY = offset [ 1 ] ;
76
79
initialTarget = e . target ;
77
80
78
81
newMouseDownTime = ( new Date ( ) ) . getTime ( ) ;
@@ -90,18 +93,25 @@ dragElement.init = function init(options) {
90
93
91
94
dragCover = coverSlip ( ) ;
92
95
96
+
93
97
dragCover . onmousemove = onMove ;
98
+ dragCover . ontouchmove = onMove ;
94
99
dragCover . onmouseup = onDone ;
95
100
dragCover . onmouseout = onDone ;
101
+ dragCover . ontouchend = onDone ;
102
+
103
+ // var moveEvent = new TouchEvent('touchstart', e)
104
+ // dragCover.dispatchEvent(moveEvent)
96
105
97
106
dragCover . style . cursor = window . getComputedStyle ( options . element ) . cursor ;
98
107
99
108
return Lib . pauseEvent ( e ) ;
100
109
}
101
110
102
111
function onMove ( e ) {
103
- var dx = e . clientX - startX ,
104
- dy = e . clientY - startY ,
112
+ var offset = pointerOffset ( e )
113
+ var dx = offset [ 0 ] - startX ,
114
+ dy = offset [ 1 ] - startY ,
105
115
minDrag = options . minDrag || constants . MINDRAG ;
106
116
107
117
if ( Math . abs ( dx ) < minDrag ) dx = 0 ;
@@ -118,8 +128,10 @@ dragElement.init = function init(options) {
118
128
119
129
function onDone ( e ) {
120
130
dragCover . onmousemove = null ;
131
+ dragCover . ontouchmove = null ;
121
132
dragCover . onmouseup = null ;
122
133
dragCover . onmouseout = null ;
134
+ dragCover . ontouchend = null ;
123
135
Lib . removeElement ( dragCover ) ;
124
136
125
137
if ( ! gd . _dragging ) {
@@ -143,12 +155,13 @@ dragElement.init = function init(options) {
143
155
e2 = new MouseEvent ( 'click' , e ) ;
144
156
}
145
157
catch ( err ) {
158
+ var offset = pointerOffset ( e )
146
159
e2 = document . createEvent ( 'MouseEvents' ) ;
147
160
e2 . initMouseEvent ( 'click' ,
148
161
e . bubbles , e . cancelable ,
149
162
e . view , e . detail ,
150
163
e . screenX , e . screenY ,
151
- e . clientX , e . clientY ,
164
+ offset [ 0 ] , offset [ 1 ] ,
152
165
e . ctrlKey , e . altKey , e . shiftKey , e . metaKey ,
153
166
e . button , e . relatedTarget ) ;
154
167
}
@@ -164,6 +177,7 @@ dragElement.init = function init(options) {
164
177
}
165
178
166
179
options . element . onmousedown = onStart ;
180
+ options . element . ontouchstart = onStart ;
167
181
options . element . style . pointerEvents = 'all' ;
168
182
} ;
169
183
@@ -191,3 +205,10 @@ function finishDrag(gd) {
191
205
gd . _dragging = false ;
192
206
if ( gd . _replotPending ) Plotly . plot ( gd ) ;
193
207
}
208
+
209
+ function pointerOffset ( e ) {
210
+ return mouseOffset (
211
+ e . changedTouches && e . changedTouches [ 0 ] || e ,
212
+ document . body
213
+ )
214
+ }
0 commit comments