-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathindex.js
226 lines (186 loc) · 6.73 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var mouseOffset = require('mouse-event-offset');
var hasHover = require('has-hover');
var Lib = require('../../lib');
var constants = require('../../plots/cartesian/constants');
var interactConstants = require('../../constants/interactions');
var dragElement = module.exports = {};
dragElement.align = require('./align');
dragElement.getCursor = require('./cursor');
var unhover = require('./unhover');
dragElement.unhover = unhover.wrapped;
dragElement.unhoverRaw = unhover.raw;
/**
* Abstracts click & drag interactions
*
* During the interaction, a "coverSlip" element - a transparent
* div covering the whole page - is created, which has two key effects:
* - Lets you drag beyond the boundaries of the plot itself without
* dropping (but if you drag all the way out of the browser window the
* interaction will end)
* - Freezes the cursor: whatever mouse cursor the drag element had when the
* interaction started gets copied to the coverSlip for use until mouseup
*
* @param {object} options with keys:
* element (required) the DOM element to drag
* prepFn (optional) function(event, startX, startY)
* executed on mousedown
* startX and startY are the clientX and clientY pixel position
* of the mousedown event
* moveFn (optional) function(dx, dy, dragged)
* executed on move
* dx and dy are the net pixel offset of the drag,
* dragged is true/false, has the mouse moved enough to
* constitute a drag
* doneFn (optional) function(dragged, numClicks, e)
* executed on mouseup, or mouseout of window since
* we don't get events after that
* dragged is as in moveFn
* numClicks is how many clicks we've registered within
* a doubleclick time
* e is the original event
*/
dragElement.init = function init(options) {
var gd = options.gd,
numClicks = 1,
DBLCLICKDELAY = interactConstants.DBLCLICKDELAY,
startX,
startY,
newMouseDownTime,
cursor,
dragCover,
initialTarget;
if(!gd._mouseDownTime) gd._mouseDownTime = 0;
options.element.style.pointerEvents = 'all';
options.element.onmousedown = onStart;
options.element.ontouchstart = onStart;
function onStart(e) {
// make dragging and dragged into properties of gd
// so that others can look at and modify them
gd._dragged = false;
gd._dragging = true;
var offset = pointerOffset(e);
startX = offset[0];
startY = offset[1];
initialTarget = e.target;
newMouseDownTime = (new Date()).getTime();
if(newMouseDownTime - gd._mouseDownTime < DBLCLICKDELAY) {
// in a click train
numClicks += 1;
}
else {
// new click train
numClicks = 1;
gd._mouseDownTime = newMouseDownTime;
}
if(options.prepFn) options.prepFn(e, startX, startY);
if(hasHover) {
dragCover = coverSlip();
dragCover.style.cursor = window.getComputedStyle(options.element).cursor;
}
else {
// document acts as a dragcover for mobile, bc we can't create dragcover dynamically
dragCover = document;
cursor = window.getComputedStyle(document.documentElement).cursor;
document.documentElement.style.cursor = window.getComputedStyle(options.element).cursor;
}
dragCover.addEventListener('mousemove', onMove);
dragCover.addEventListener('mouseup', onDone);
dragCover.addEventListener('mouseout', onDone);
dragCover.addEventListener('touchmove', onMove);
dragCover.addEventListener('touchend', onDone);
return Lib.pauseEvent(e);
}
function onMove(e) {
var offset = pointerOffset(e),
dx = offset[0] - startX,
dy = offset[1] - startY,
minDrag = options.minDrag || constants.MINDRAG;
if(Math.abs(dx) < minDrag) dx = 0;
if(Math.abs(dy) < minDrag) dy = 0;
if(dx || dy) {
gd._dragged = true;
dragElement.unhover(gd);
}
if(options.moveFn) options.moveFn(dx, dy, gd._dragged);
return Lib.pauseEvent(e);
}
function onDone(e) {
dragCover.removeEventListener('mousemove', onMove);
dragCover.removeEventListener('mouseup', onDone);
dragCover.removeEventListener('mouseout', onDone);
dragCover.removeEventListener('touchmove', onMove);
dragCover.removeEventListener('touchend', onDone);
if(hasHover) {
Lib.removeElement(dragCover);
}
else if(cursor) {
dragCover.documentElement.style.cursor = cursor;
cursor = null;
}
if(!gd._dragging) {
gd._dragged = false;
return;
}
gd._dragging = false;
// don't count as a dblClick unless the mouseUp is also within
// the dblclick delay
if((new Date()).getTime() - gd._mouseDownTime > DBLCLICKDELAY) {
numClicks = Math.max(numClicks - 1, 1);
}
if(options.doneFn) options.doneFn(gd._dragged, numClicks, e);
if(!gd._dragged) {
var e2;
try {
e2 = new MouseEvent('click', e);
}
catch(err) {
var offset = pointerOffset(e);
e2 = document.createEvent('MouseEvents');
e2.initMouseEvent('click',
e.bubbles, e.cancelable,
e.view, e.detail,
e.screenX, e.screenY,
offset[0], offset[1],
e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
e.button, e.relatedTarget);
}
initialTarget.dispatchEvent(e2);
}
finishDrag(gd);
gd._dragged = false;
return Lib.pauseEvent(e);
}
};
function coverSlip() {
var cover = document.createElement('div');
cover.className = 'dragcover';
var cStyle = cover.style;
cStyle.position = 'fixed';
cStyle.left = 0;
cStyle.right = 0;
cStyle.top = 0;
cStyle.bottom = 0;
cStyle.zIndex = 999999999;
cStyle.background = 'none';
document.body.appendChild(cover);
return cover;
}
dragElement.coverSlip = coverSlip;
function finishDrag(gd) {
gd._dragging = false;
if(gd._replotPending) gd._plotAPI.plot();
}
function pointerOffset(e) {
return mouseOffset(
e.changedTouches ? e.changedTouches[0] : e,
document.body
);
}