forked from plotly/plotly.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
200 lines (160 loc) · 5.79 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
/**
* 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 Plotly = require('../../plotly');
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
* @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
* setCursor (optional) function(event)
* executed on mousemove before mousedown
* the purpose of this callback is to update the mouse cursor before
* the click & drag interaction has been initiated
*/
dragElement.init = function init(options) {
var gd = Lib.getPlotDiv(options.element) || {},
numClicks = 1,
DBLCLICKDELAY = interactConstants.DBLCLICKDELAY,
startX,
startY,
newMouseDownTime,
dragCover,
initialTarget,
initialOnMouseMove;
if(!gd._mouseDownTime) gd._mouseDownTime = 0;
function onStart(e) {
// disable call to options.setCursor(evt)
options.element.onmousemove = initialOnMouseMove;
// make dragging and dragged into properties of gd
// so that others can look at and modify them
gd._dragged = false;
gd._dragging = true;
startX = e.clientX;
startY = e.clientY;
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);
dragCover = coverSlip();
dragCover.onmousemove = onMove;
dragCover.onmouseup = onDone;
dragCover.onmouseout = onDone;
dragCover.style.cursor = window.getComputedStyle(options.element).cursor;
return Lib.pauseEvent(e);
}
function onMove(e) {
var dx = e.clientX - startX,
dy = e.clientY - 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) {
// re-enable call to options.setCursor(evt)
initialOnMouseMove = options.element.onmousemove;
if(options.setCursor) options.element.onmousemove = options.setCursor;
dragCover.onmousemove = null;
dragCover.onmouseup = null;
dragCover.onmouseout = null;
Lib.removeElement(dragCover);
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) {
e2 = document.createEvent('MouseEvents');
e2.initMouseEvent('click',
e.bubbles, e.cancelable,
e.view, e.detail,
e.screenX, e.screenY,
e.clientX, e.clientY,
e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
e.button, e.relatedTarget);
}
initialTarget.dispatchEvent(e2);
}
finishDrag(gd);
gd._dragged = false;
return Lib.pauseEvent(e);
}
// enable call to options.setCursor(evt)
initialOnMouseMove = options.element.onmousemove;
if(options.setCursor) options.element.onmousemove = options.setCursor;
options.element.onmousedown = onStart;
options.element.style.pointerEvents = 'all';
};
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) Plotly.plot(gd);
}