-
Notifications
You must be signed in to change notification settings - Fork 283
/
EventHandler.js
404 lines (331 loc) · 19.2 KB
/
EventHandler.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require("react");
var _react2 = _interopRequireDefault(_react);
var _reactDom = require("react-dom");
var _reactDom2 = _interopRequireDefault(_reactDom);
var _propTypes = require("prop-types");
var _propTypes2 = _interopRequireDefault(_propTypes);
var _pondjs = require("pondjs");
var _util = require("../js/util");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /**
* Copyright (c) 2015-present, The Regents of the University of California,
* through Lawrence Berkeley National Laboratory (subject to receipt
* of any required approvals from the U.S. Dept. of Energy).
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
// eslint-disable-line
/**
* Internal component which provides the top level event catcher for the charts.
* This is a higher order component. It wraps a tree of SVG elements below it,
* passed in as this.props.children, and catches events that they do not handle.
*
* The EventHandler is responsible for pan and zoom events as well as other click
* and hover actions.
*/
var EventHandler = function (_React$Component) {
_inherits(EventHandler, _React$Component);
function EventHandler(props) {
_classCallCheck(this, EventHandler);
var _this = _possibleConstructorReturn(this, (EventHandler.__proto__ || Object.getPrototypeOf(EventHandler)).call(this, props));
_this.state = {
isPanning: false,
initialPanBegin: null,
initialPanEnd: null,
initialPanPosition: null
};
_this.handleScrollWheel = _this.handleScrollWheel.bind(_this);
_this.handleMouseDown = _this.handleMouseDown.bind(_this);
_this.handleMouseUp = _this.handleMouseUp.bind(_this);
_this.handleMouseOut = _this.handleMouseOut.bind(_this);
_this.handleMouseMove = _this.handleMouseMove.bind(_this);
_this.handleContextMenu = _this.handleContextMenu.bind(_this);
return _this;
}
_createClass(EventHandler, [{
key: "componentDidMount",
value: function componentDidMount() {
this.eventHandlerRef.addEventListener("wheel", this.handleScrollWheel, { passive: false });
}
// get the event mouse position relative to the event rect
}, {
key: "getOffsetMousePosition",
value: function getOffsetMousePosition(e) {
var offset = (0, _util.getElementOffset)(this.eventRect);
var x = e.pageX - offset.left;
var y = e.pageY - offset.top;
return [Math.round(x), Math.round(y)];
}
//
// Event handlers
//
}, {
key: "handleScrollWheel",
value: function handleScrollWheel(e) {
if (!this.props.enablePanZoom && !this.props.enableDragZoom) {
return;
}
e.preventDefault();
var SCALE_FACTOR = 0.001;
var scale = 1 + e.deltaY * SCALE_FACTOR;
if (scale > 3) {
scale = 3;
}
if (scale < 0.1) {
scale = 0.1;
}
var xy = this.getOffsetMousePosition(e);
var begin = this.props.scale.domain()[0].getTime();
var end = this.props.scale.domain()[1].getTime();
var center = this.props.scale.invert(xy[0]).getTime();
var beginScaled = center - parseInt((center - begin) * scale, 10);
var endScaled = center + parseInt((end - center) * scale, 10);
// Duration constraint
var duration = (end - begin) * scale;
if (this.props.minDuration) {
var minDuration = parseInt(this.props.minDuration, 10);
if (duration < this.props.minDuration) {
beginScaled = center - (center - begin) / (end - begin) * minDuration;
endScaled = center + (end - center) / (end - begin) * minDuration;
}
}
if (this.props.minTime && this.props.maxTime) {
var maxDuration = this.props.maxTime.getTime() - this.props.minTime.getTime();
if (duration > maxDuration) {
duration = maxDuration;
}
}
// Range constraint
if (this.props.minTime && beginScaled < this.props.minTime.getTime()) {
beginScaled = this.props.minTime.getTime();
endScaled = beginScaled + duration;
}
if (this.props.maxTime && endScaled > this.props.maxTime.getTime()) {
endScaled = this.props.maxTime.getTime();
beginScaled = endScaled - duration;
}
var newBegin = new Date(beginScaled);
var newEnd = new Date(endScaled);
var newTimeRange = new _pondjs.TimeRange(newBegin, newEnd);
if (this.props.onZoom) {
this.props.onZoom(newTimeRange);
}
}
}, {
key: "handleMouseDown",
value: function handleMouseDown(e) {
if (!this.props.enablePanZoom && !this.props.enableDragZoom) {
return;
}
if (e.button === 2) {
return;
}
e.preventDefault();
document.addEventListener("mouseover", this.handleMouseMove);
document.addEventListener("mouseup", this.handleMouseUp);
if (this.props.enableDragZoom) {
var offsetxy = this.getOffsetMousePosition(e);
this.setState({
isDragging: true,
initialDragZoom: offsetxy[0],
currentDragZoom: offsetxy[0]
});
}
if (this.props.enablePanZoom) {
var x = e.pageX;
var y = e.pageY;
var xy0 = [Math.round(x), Math.round(y)];
var begin = this.props.scale.domain()[0].getTime();
var end = this.props.scale.domain()[1].getTime();
this.setState({
isPanning: true,
initialPanBegin: begin,
initialPanEnd: end,
initialPanPosition: xy0
});
}
return false;
}
}, {
key: "handleMouseUp",
value: function handleMouseUp(e) {
if (!this.props.onMouseClick && !this.props.enablePanZoom && !this.props.enableDragZoom) {
return;
}
e.stopPropagation();
document.removeEventListener("mouseover", this.handleMouseMove);
document.removeEventListener("mouseup", this.handleMouseUp);
var offsetxy = this.getOffsetMousePosition(e);
var x = e.pageX;
var isPanning = this.state.initialPanPosition && Math.abs(x - this.state.initialPanPosition[0]) > 2;
var isDragging = this.state.initialDragZoom && Math.abs(offsetxy[0] - this.state.initialDragZoom) > 2;
if (this.props.onMouseClick && !isPanning && !isDragging) {
this.props.onMouseClick(offsetxy[0], offsetxy[1]);
}
if (this.props.enableDragZoom) {
if (isDragging) {
var start = this.props.scale.invert(this.state.initialDragZoom).getTime();
var end = this.props.scale.invert(this.state.currentDragZoom).getTime();
var newBegin = parseInt(start, 10);
var newEnd = parseInt(end, 10);
if (this.props.minTime && newBegin < this.props.minTime.getTime()) {
newBegin = this.props.minTime.getTime();
}
if (this.props.maxTime && newEnd > this.props.maxTime.getTime()) {
newEnd = this.props.maxTime.getTime();
}
var newTimeRange = new _pondjs.TimeRange([newBegin, newEnd].sort());
if (this.props.onZoom) {
this.props.onZoom(newTimeRange);
}
}
this.setState({
isDragging: false,
initialDragZoom: null,
initialPanEnd: null,
currentDragZoom: null
});
}
if (this.props.enablePanZoom) {
this.setState({
isPanning: false,
initialPanBegin: null,
initialPanEnd: null,
initialPanPosition: null
});
}
}
}, {
key: "handleMouseOut",
value: function handleMouseOut(e) {
e.preventDefault();
if (this.props.onMouseOut) {
this.props.onMouseOut();
}
}
}, {
key: "handleMouseMove",
value: function handleMouseMove(e) {
e.preventDefault();
var x = e.pageX;
var y = e.pageY;
var xy = [Math.round(x), Math.round(y)];
var offsetxy = this.getOffsetMousePosition(e);
if (this.state.isDragging) {
this.setState({
currentDragZoom: offsetxy[0]
});
}
if (this.state.isPanning) {
var xy0 = this.state.initialPanPosition;
var timeOffset = this.props.scale.invert(xy[0]).getTime() - this.props.scale.invert(xy0[0]).getTime();
var newBegin = parseInt(this.state.initialPanBegin - timeOffset, 10);
var newEnd = parseInt(this.state.initialPanEnd - timeOffset, 10);
var duration = parseInt(this.state.initialPanEnd - this.state.initialPanBegin, 10);
if (this.props.minTime && newBegin < this.props.minTime.getTime()) {
newBegin = this.props.minTime.getTime();
newEnd = newBegin + duration;
}
if (this.props.maxTime && newEnd > this.props.maxTime.getTime()) {
newEnd = this.props.maxTime.getTime();
newBegin = newEnd - duration;
}
var newTimeRange = new _pondjs.TimeRange(newBegin, newEnd);
if (this.props.onZoom) {
this.props.onZoom(newTimeRange);
}
} else if (this.props.onMouseMove) {
var mousePosition = this.getOffsetMousePosition(e);
if (this.props.onMouseMove) {
this.props.onMouseMove(mousePosition[0], mousePosition[1]);
}
}
}
}, {
key: "handleContextMenu",
value: function handleContextMenu(e) {
var x = e.pageX;
var y = e.pageY;
if (this.props.onContextMenu) {
this.props.onContextMenu(x, y);
}
}
//
// Render
//
}, {
key: "render",
value: function render() {
var _this2 = this;
var cursor = this.state.isPanning ? "-webkit-grabbing" : "default";
var handlers = {
onMouseDown: this.handleMouseDown,
onMouseMove: this.handleMouseMove,
onMouseOut: this.handleMouseOut,
onMouseUp: this.handleMouseUp,
onContextMenu: this.handleContextMenu
};
return _react2.default.createElement(
"g",
_extends({
pointerEvents: "all",
ref: function ref(c) {
_this2.eventHandlerRef = c;
}
}, handlers),
_react2.default.createElement("rect", {
key: "handler-hit-rect",
ref: function ref(c) {
_this2.eventRect = c;
},
style: { fill: "#000", opacity: 0.0, cursor: cursor },
x: 0,
y: 0,
width: this.props.width,
height: this.props.height
}),
this.props.children,
this.state.isDragging && _react2.default.createElement("rect", {
style: { opacity: 0.3, fill: "grey" },
x: Math.min(this.state.currentDragZoom, this.state.initialDragZoom),
y: 0,
width: Math.abs(this.state.currentDragZoom - this.state.initialDragZoom),
height: this.props.height,
pointerEvents: "none"
})
);
}
}]);
return EventHandler;
}(_react2.default.Component);
exports.default = EventHandler;
EventHandler.propTypes = {
children: _propTypes2.default.oneOfType([_propTypes2.default.arrayOf(_propTypes2.default.node), _propTypes2.default.node]),
enablePanZoom: _propTypes2.default.bool,
enableDragZoom: _propTypes2.default.bool,
scale: _propTypes2.default.func.isRequired,
width: _propTypes2.default.number.isRequired,
height: _propTypes2.default.number.isRequired,
maxTime: _propTypes2.default.instanceOf(Date),
minTime: _propTypes2.default.instanceOf(Date),
minDuration: _propTypes2.default.number,
onZoom: _propTypes2.default.func,
onMouseMove: _propTypes2.default.func,
onMouseOut: _propTypes2.default.func,
onMouseClick: _propTypes2.default.func,
onContextMenu: _propTypes2.default.func
};
EventHandler.defaultProps = {
enablePanZoom: false,
enableDragZoom: false
};