@tradingaction/core 2.0.12 → 2.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/LICENSE +24 -24
  2. package/README.md +5 -5
  3. package/lib/CanvasContainer.d.ts +19 -19
  4. package/lib/CanvasContainer.js +27 -27
  5. package/lib/Chart.d.ts +32 -32
  6. package/lib/Chart.js +56 -56
  7. package/lib/Chart.js.map +1 -1
  8. package/lib/ChartCanvas.d.ts +235 -235
  9. package/lib/ChartCanvas.js +770 -770
  10. package/lib/ChartCanvas.js.map +1 -1
  11. package/lib/EventCapture.d.ts +131 -131
  12. package/lib/EventCapture.js +488 -488
  13. package/lib/GenericChartComponent.d.ts +21 -21
  14. package/lib/GenericChartComponent.js +74 -74
  15. package/lib/GenericComponent.d.ts +81 -81
  16. package/lib/GenericComponent.js +354 -354
  17. package/lib/GenericComponent.js.map +1 -1
  18. package/lib/MoreProps.d.ts +16 -16
  19. package/lib/MoreProps.js +1 -1
  20. package/lib/index.d.ts +7 -7
  21. package/lib/index.js +7 -7
  22. package/lib/useEvent.d.ts +1 -1
  23. package/lib/useEvent.js +12 -12
  24. package/lib/utils/ChartDataUtil.d.ts +49 -49
  25. package/lib/utils/ChartDataUtil.js +204 -204
  26. package/lib/utils/PureComponent.d.ts +4 -4
  27. package/lib/utils/PureComponent.js +9 -9
  28. package/lib/utils/accumulatingWindow.d.ts +15 -15
  29. package/lib/utils/accumulatingWindow.js +97 -97
  30. package/lib/utils/barWidth.d.ts +15 -15
  31. package/lib/utils/barWidth.js +26 -26
  32. package/lib/utils/closestItem.d.ts +5 -5
  33. package/lib/utils/closestItem.js +44 -44
  34. package/lib/utils/evaluator.d.ts +7 -7
  35. package/lib/utils/evaluator.js +93 -93
  36. package/lib/utils/identity.d.ts +1 -1
  37. package/lib/utils/identity.js +1 -1
  38. package/lib/utils/index.d.ts +46 -46
  39. package/lib/utils/index.js +125 -125
  40. package/lib/utils/noop.d.ts +1 -1
  41. package/lib/utils/noop.js +2 -2
  42. package/lib/utils/shallowEqual.d.ts +1 -1
  43. package/lib/utils/shallowEqual.js +21 -21
  44. package/lib/utils/slidingWindow.d.ts +19 -19
  45. package/lib/utils/slidingWindow.js +108 -108
  46. package/lib/utils/strokeDasharray.d.ts +3 -3
  47. package/lib/utils/strokeDasharray.js +36 -36
  48. package/lib/utils/zipper.d.ts +7 -7
  49. package/lib/utils/zipper.js +35 -35
  50. package/lib/zoom/index.d.ts +1 -1
  51. package/lib/zoom/index.js +1 -1
  52. package/lib/zoom/zoomBehavior.d.ts +10 -10
  53. package/lib/zoom/zoomBehavior.js +17 -17
  54. package/package.json +2 -2
  55. package/src/Chart.tsx +2 -2
  56. package/src/ChartCanvas.tsx +1 -1
  57. package/src/GenericComponent.tsx +1 -0
@@ -1,489 +1,489 @@
1
- var __rest = (this && this.__rest) || function (s, e) {
2
- var t = {};
3
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
- t[p] = s[p];
5
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
- t[p[i]] = s[p[i]];
9
- }
10
- return t;
11
- };
12
- import { pointer, pointers, select } from "d3-selection";
13
- import * as React from "react";
14
- import { d3Window, getTouchProps, MOUSEENTER, MOUSELEAVE, MOUSEMOVE, mousePosition, MOUSEUP, TOUCHEND, TOUCHMOVE, touchPosition, } from "./utils";
15
- import { getCurrentCharts } from "./utils/ChartDataUtil";
16
- export class EventCapture extends React.Component {
17
- constructor(props) {
18
- super(props);
19
- this.dx = 0;
20
- this.dy = 0;
21
- this.mouseInside = false;
22
- this.mouseInteraction = true;
23
- this.ref = React.createRef();
24
- this.handleEnter = (e) => {
25
- const { onMouseEnter } = this.props;
26
- if (onMouseEnter === undefined) {
27
- return;
28
- }
29
- this.mouseInside = true;
30
- if (!this.state.panInProgress && !this.state.dragInProgress) {
31
- const win = d3Window(this.ref.current);
32
- select(win).on(MOUSEMOVE, this.handleMouseMove);
33
- }
34
- onMouseEnter(e);
35
- };
36
- this.handleLeave = (e) => {
37
- const { onMouseLeave } = this.props;
38
- if (onMouseLeave === undefined) {
39
- return;
40
- }
41
- this.mouseInside = false;
42
- if (!this.state.panInProgress && !this.state.dragInProgress) {
43
- const win = d3Window(this.ref.current);
44
- select(win).on(MOUSEMOVE, null);
45
- }
46
- onMouseLeave(e);
47
- };
48
- this.handleWheel = (e) => {
49
- const { pan, onPan, zoom, onZoom } = this.props;
50
- if (!pan && !zoom) {
51
- return;
52
- }
53
- const { panInProgress } = this.state;
54
- const yZoom = Math.abs(e.deltaY) > Math.abs(e.deltaX) && Math.abs(e.deltaY) > 0;
55
- const mouseXY = mousePosition(e);
56
- e.preventDefault();
57
- if (zoom && this.focus && yZoom && !panInProgress) {
58
- const zoomDir = e.deltaY > 0 ? 1 : -1;
59
- if (onZoom !== undefined) {
60
- onZoom(zoomDir, mouseXY, e);
61
- }
62
- }
63
- else if (this.focus) {
64
- if (this.shouldPan() && this.state.panStart !== undefined) {
65
- // pan already in progress
66
- const { panStartXScale, chartsToPan } = this.state.panStart;
67
- this.lastNewPos = mouseXY;
68
- this.panHappened = true;
69
- if (this.dx === undefined) {
70
- this.dx = 0;
71
- }
72
- if (this.dy === undefined) {
73
- this.dy = 0;
74
- }
75
- this.dx -= e.deltaX;
76
- this.dy += e.deltaY;
77
- const dxdy = { dx: this.dx, dy: this.dy };
78
- if (onPan !== undefined) {
79
- onPan(mouseXY, panStartXScale, dxdy, chartsToPan, e);
80
- }
81
- }
82
- else {
83
- const { xScale, chartConfig } = this.props;
84
- const currentCharts = getCurrentCharts(chartConfig, mouseXY);
85
- this.dx = 0;
86
- this.dy = 0;
87
- this.setState({
88
- panInProgress: true,
89
- panStart: {
90
- panStartXScale: xScale,
91
- panOrigin: mouseXY,
92
- chartsToPan: currentCharts,
93
- },
94
- });
95
- }
96
- this.queuePanEnd(e);
97
- }
98
- };
99
- this.handleMouseMove = (e) => {
100
- const { onMouseMove, mouseMove } = this.props;
101
- if (onMouseMove === undefined) {
102
- return;
103
- }
104
- if (this.mouseInteraction && mouseMove && !this.state.panInProgress) {
105
- const newPos = pointer(e, this.ref.current);
106
- onMouseMove(newPos, "mouse", e);
107
- }
108
- };
109
- this.handleClick = (e) => {
110
- const mouseXY = mousePosition(e);
111
- const { onClick, onDoubleClick } = this.props;
112
- if (!this.panHappened && !this.dragHappened) {
113
- if (this.clicked && onDoubleClick !== undefined) {
114
- onDoubleClick(mouseXY, e);
115
- this.clicked = false;
116
- }
117
- else if (onClick !== undefined) {
118
- onClick(mouseXY, e);
119
- this.clicked = true;
120
- setTimeout(() => {
121
- if (this.clicked) {
122
- this.clicked = false;
123
- }
124
- }, 400);
125
- }
126
- }
127
- };
128
- this.handleRightClick = (e) => {
129
- e.stopPropagation();
130
- e.preventDefault();
131
- const { onContextMenu, onPanEnd } = this.props;
132
- const mouseXY = mousePosition(e, this.ref.current.getBoundingClientRect());
133
- if (this.state.panStart !== undefined) {
134
- const { panStartXScale, panOrigin: [dx, dy], chartsToPan, } = this.state.panStart;
135
- if (this.panHappened && onPanEnd !== undefined) {
136
- onPanEnd(mouseXY, panStartXScale, { dx, dy }, chartsToPan, e);
137
- }
138
- const win = d3Window(this.ref.current);
139
- select(win).on(MOUSEMOVE, null).on(MOUSEUP, null);
140
- this.setState({
141
- panInProgress: false,
142
- panStart: undefined,
143
- });
144
- }
145
- if (onContextMenu !== undefined) {
146
- onContextMenu(mouseXY, e);
147
- }
148
- };
149
- this.handleDrag = (e) => {
150
- const { onDrag } = this.props;
151
- if (onDrag === undefined) {
152
- return;
153
- }
154
- this.dragHappened = true;
155
- const { dragStartPosition } = this.state;
156
- if (dragStartPosition === undefined) {
157
- return;
158
- }
159
- const mouseXY = pointer(e, this.ref.current);
160
- onDrag({
161
- startPos: dragStartPosition,
162
- mouseXY,
163
- }, e);
164
- };
165
- this.handleDragEnd = (e) => {
166
- const mouseXY = pointer(e, this.ref.current);
167
- const win = d3Window(this.ref.current);
168
- select(win)
169
- // @ts-ignore
170
- .on(MOUSEMOVE, this.mouseInside ? this.handleMouseMove : null)
171
- .on(MOUSEUP, null);
172
- if (this.dragHappened) {
173
- const { onDragComplete } = this.props;
174
- if (onDragComplete !== undefined) {
175
- onDragComplete({ mouseXY }, e);
176
- }
177
- }
178
- this.setState({
179
- dragInProgress: false,
180
- });
181
- this.mouseInteraction = true;
182
- };
183
- this.canPan = () => {
184
- const { getAllPanConditions } = this.props;
185
- const { pan: initialPanEnabled } = this.props;
186
- const { panEnabled, draggable: somethingSelected } = getAllPanConditions().reduce((returnObj, a) => {
187
- return {
188
- draggable: returnObj.draggable || a.draggable,
189
- panEnabled: returnObj.panEnabled && a.panEnabled,
190
- };
191
- }, {
192
- draggable: false,
193
- panEnabled: initialPanEnabled,
194
- });
195
- return {
196
- panEnabled,
197
- somethingSelected,
198
- };
199
- };
200
- this.handleMouseDown = (e) => {
201
- if (e.button !== 0) {
202
- return;
203
- }
204
- const { xScale, chartConfig, onMouseDown } = this.props;
205
- this.panHappened = false;
206
- this.dragHappened = false;
207
- this.focus = true;
208
- if (!this.state.panInProgress && this.mouseInteraction) {
209
- const mouseXY = mousePosition(e);
210
- const currentCharts = getCurrentCharts(chartConfig, mouseXY);
211
- const { panEnabled, somethingSelected } = this.canPan();
212
- const pan = panEnabled && !somethingSelected;
213
- if (pan) {
214
- this.setState({
215
- panInProgress: pan,
216
- panStart: {
217
- panStartXScale: xScale,
218
- panOrigin: mouseXY,
219
- chartsToPan: currentCharts,
220
- },
221
- });
222
- const win = d3Window(this.ref.current);
223
- select(win).on(MOUSEMOVE, this.handlePan).on(MOUSEUP, this.handlePanEnd);
224
- }
225
- else if (somethingSelected) {
226
- this.setState({
227
- panInProgress: false,
228
- dragInProgress: true,
229
- panStart: undefined,
230
- dragStartPosition: mouseXY,
231
- });
232
- const { onDragStart } = this.props;
233
- if (onDragStart !== undefined) {
234
- onDragStart({ startPos: mouseXY }, e);
235
- }
236
- const win = d3Window(this.ref.current);
237
- select(win).on(MOUSEMOVE, this.handleDrag).on(MOUSEUP, this.handleDragEnd);
238
- }
239
- if (onMouseDown !== undefined) {
240
- onMouseDown(mouseXY, currentCharts, e);
241
- }
242
- }
243
- e.preventDefault();
244
- };
245
- this.shouldPan = () => {
246
- const { pan: panEnabled, onPan } = this.props;
247
- return panEnabled && onPan && this.state.panStart !== undefined;
248
- };
249
- this.handlePan = (e) => {
250
- if (this.shouldPan() && this.state.panStart !== undefined) {
251
- this.panHappened = true;
252
- const { panStartXScale, panOrigin, chartsToPan } = this.state.panStart;
253
- let dx;
254
- let dy;
255
- let mouseXY;
256
- if (this.mouseInteraction) {
257
- mouseXY = pointer(e, this.ref.current);
258
- this.lastNewPos = mouseXY;
259
- dx = mouseXY[0] - panOrigin[0];
260
- dy = mouseXY[1] - panOrigin[1];
261
- }
262
- else {
263
- mouseXY = pointers(e, this.ref.current)[0];
264
- this.lastNewPos = mouseXY;
265
- dx = panOrigin[0] - mouseXY[0];
266
- dy = panOrigin[1] - mouseXY[1];
267
- }
268
- this.dx = dx;
269
- this.dy = dy;
270
- const { onPan } = this.props;
271
- if (onPan !== undefined) {
272
- onPan(mouseXY, panStartXScale, { dx, dy }, chartsToPan, e);
273
- }
274
- }
275
- };
276
- this.handlePanEnd = (e) => {
277
- const { pan: panEnabled, onPanEnd } = this.props;
278
- if (this.state.panStart !== undefined) {
279
- const { panStartXScale, chartsToPan } = this.state.panStart;
280
- const win = d3Window(this.ref.current);
281
- select(win)
282
- // @ts-ignore
283
- .on(MOUSEMOVE, this.mouseInside ? this.handleMouseMove : null)
284
- .on(MOUSEUP, null)
285
- .on(TOUCHMOVE, null)
286
- .on(TOUCHEND, null);
287
- if (this.panHappened && panEnabled && onPanEnd) {
288
- const { dx = 0, dy = 0 } = this;
289
- delete this.dx;
290
- delete this.dy;
291
- if (this.lastNewPos !== undefined) {
292
- onPanEnd(this.lastNewPos, panStartXScale, { dx, dy }, chartsToPan, e);
293
- }
294
- }
295
- this.setState({
296
- panInProgress: false,
297
- panStart: undefined,
298
- });
299
- }
300
- };
301
- this.handleTouchMove = (e) => {
302
- const { onMouseMove } = this.props;
303
- if (onMouseMove === undefined) {
304
- return;
305
- }
306
- const touch = getTouchProps(e.touches[0]);
307
- const touchXY = touchPosition(touch, e);
308
- onMouseMove(touchXY, "touch", e);
309
- };
310
- this.handleTouchStart = (e) => {
311
- this.mouseInteraction = false;
312
- const { pan: panEnabled, chartConfig, onMouseMove, xScale, onPanEnd } = this.props;
313
- if (e.touches.length === 1) {
314
- this.panHappened = false;
315
- const touchXY = touchPosition(getTouchProps(e.touches[0]), e);
316
- if (onMouseMove !== undefined) {
317
- onMouseMove(touchXY, "touch", e);
318
- }
319
- if (panEnabled) {
320
- const currentCharts = getCurrentCharts(chartConfig, touchXY);
321
- this.setState({
322
- panInProgress: true,
323
- panStart: {
324
- panStartXScale: xScale,
325
- panOrigin: touchXY,
326
- chartsToPan: currentCharts,
327
- },
328
- });
329
- const win = d3Window(this.ref.current);
330
- select(win).on(TOUCHMOVE, this.handlePan, false).on(TOUCHEND, this.handlePanEnd, false);
331
- }
332
- }
333
- else if (e.touches.length === 2) {
334
- // pinch zoom begin
335
- // do nothing pinch zoom is handled in handleTouchMove
336
- const { panInProgress, panStart } = this.state;
337
- if (panInProgress && panEnabled && onPanEnd && panStart !== undefined) {
338
- const { panStartXScale, panOrigin: [dx, dy], chartsToPan, } = panStart;
339
- const win = d3Window(this.ref.current);
340
- select(win)
341
- // @ts-ignore
342
- .on(MOUSEMOVE, this.mouseInside ? this.handleMouseMove : null)
343
- .on(MOUSEUP, null)
344
- .on(TOUCHMOVE, this.handlePinchZoom, false)
345
- .on(TOUCHEND, this.handlePinchZoomEnd, false);
346
- const touch1Pos = touchPosition(getTouchProps(e.touches[0]), e);
347
- const touch2Pos = touchPosition(getTouchProps(e.touches[1]), e);
348
- if (this.panHappened && panEnabled && onPanEnd && this.lastNewPos !== undefined) {
349
- onPanEnd(this.lastNewPos, panStartXScale, { dx, dy }, chartsToPan, e);
350
- }
351
- this.setState({
352
- panInProgress: false,
353
- pinchZoomStart: {
354
- xScale,
355
- touch1Pos,
356
- touch2Pos,
357
- range: xScale.range(),
358
- chartsToPan,
359
- },
360
- });
361
- }
362
- }
363
- };
364
- this.handlePinchZoom = (e) => {
365
- const { pinchZoomStart } = this.state;
366
- if (pinchZoomStart === undefined) {
367
- return;
368
- }
369
- const { xScale, zoom: zoomEnabled, onPinchZoom } = this.props;
370
- if (!zoomEnabled || onPinchZoom === undefined) {
371
- return;
372
- }
373
- const [touch1Pos, touch2Pos] = pointers(this.ref.current);
374
- const { chartsToPan } = pinchZoomStart, initialPinch = __rest(pinchZoomStart, ["chartsToPan"]);
375
- onPinchZoom(initialPinch, {
376
- touch1Pos,
377
- touch2Pos,
378
- xScale,
379
- }, e);
380
- };
381
- this.handlePinchZoomEnd = (e) => {
382
- const win = d3Window(this.ref.current);
383
- select(win).on(TOUCHMOVE, null).on(TOUCHEND, null);
384
- const { pinchZoomStart } = this.state;
385
- if (pinchZoomStart === undefined) {
386
- return;
387
- }
388
- const { chartsToPan } = pinchZoomStart, initialPinch = __rest(pinchZoomStart, ["chartsToPan"]);
389
- const { zoom: zoomEnabled, onPinchZoomEnd } = this.props;
390
- if (zoomEnabled && onPinchZoomEnd) {
391
- onPinchZoomEnd(initialPinch, e);
392
- }
393
- this.setState({
394
- pinchZoomStart: undefined,
395
- });
396
- };
397
- this.setCursorClass = (cursorOverrideClass) => {
398
- if (cursorOverrideClass !== this.state.cursorOverrideClass) {
399
- this.setState({
400
- cursorOverrideClass: cursorOverrideClass === null ? undefined : cursorOverrideClass,
401
- });
402
- }
403
- };
404
- this.focus = props.focus;
405
- this.state = {
406
- panInProgress: false,
407
- };
408
- }
409
- componentDidMount() {
410
- const { disableInteraction } = this.props;
411
- const { current } = this.ref;
412
- if (current === null) {
413
- return;
414
- }
415
- if (!disableInteraction) {
416
- // @ts-ignore
417
- select(current).on(MOUSEENTER, this.handleEnter).on(MOUSELEAVE, this.handleLeave);
418
- // @ts-ignore
419
- current.addEventListener("wheel", this.handleWheel, { passive: false });
420
- }
421
- }
422
- componentDidUpdate() {
423
- this.componentDidMount();
424
- }
425
- componentWillUnmount() {
426
- const { disableInteraction } = this.props;
427
- const { current } = this.ref;
428
- if (current === null) {
429
- return;
430
- }
431
- if (!disableInteraction) {
432
- select(current).on(MOUSEENTER, null).on(MOUSELEAVE, null);
433
- const win = d3Window(current);
434
- select(win).on(MOUSEMOVE, null);
435
- // @ts-ignore
436
- current.removeEventListener("wheel", this.handleWheel, { passive: false });
437
- }
438
- }
439
- queuePanEnd(e) {
440
- if (this.panEndTimeout !== undefined) {
441
- window.clearTimeout(this.panEndTimeout);
442
- }
443
- this.panEndTimeout = window.setTimeout(() => {
444
- this.handlePanEnd(e);
445
- }, 100);
446
- }
447
- cancelDrag() {
448
- const win = d3Window(this.ref.current);
449
- select(win)
450
- // @ts-ignore
451
- .on(MOUSEMOVE, this.mouseInside ? this.handleMouseMove : null)
452
- .on(MOUSEUP, null);
453
- this.setState({
454
- dragInProgress: false,
455
- });
456
- this.mouseInteraction = true;
457
- }
458
- render() {
459
- const { height, width, disableInteraction, useCrossHairStyleCursor } = this.props;
460
- const className = disableInteraction
461
- ? undefined
462
- : this.state.cursorOverrideClass !== undefined
463
- ? this.state.cursorOverrideClass
464
- : !useCrossHairStyleCursor
465
- ? undefined
466
- : this.state.panInProgress
467
- ? "react-financial-charts-grabbing-cursor"
468
- : "react-financial-charts-crosshair-cursor";
469
- const interactionProps = disableInteraction
470
- ? undefined
471
- : {
472
- onMouseDown: this.handleMouseDown,
473
- onClick: this.handleClick,
474
- onContextMenu: this.handleRightClick,
475
- onTouchStart: this.handleTouchStart,
476
- onTouchMove: this.handleTouchMove,
477
- };
478
- return (React.createElement("rect", Object.assign({ ref: this.ref, className: className, width: width, height: height, style: { opacity: 0 } }, interactionProps)));
479
- }
480
- }
481
- EventCapture.defaultProps = {
482
- mouseMove: false,
483
- zoom: false,
484
- pan: false,
485
- panSpeedMultiplier: 1,
486
- focus: false,
487
- disableInteraction: false,
488
- };
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
12
+ import { pointer, pointers, select } from "d3-selection";
13
+ import * as React from "react";
14
+ import { d3Window, getTouchProps, MOUSEENTER, MOUSELEAVE, MOUSEMOVE, mousePosition, MOUSEUP, TOUCHEND, TOUCHMOVE, touchPosition, } from "./utils";
15
+ import { getCurrentCharts } from "./utils/ChartDataUtil";
16
+ export class EventCapture extends React.Component {
17
+ constructor(props) {
18
+ super(props);
19
+ this.dx = 0;
20
+ this.dy = 0;
21
+ this.mouseInside = false;
22
+ this.mouseInteraction = true;
23
+ this.ref = React.createRef();
24
+ this.handleEnter = (e) => {
25
+ const { onMouseEnter } = this.props;
26
+ if (onMouseEnter === undefined) {
27
+ return;
28
+ }
29
+ this.mouseInside = true;
30
+ if (!this.state.panInProgress && !this.state.dragInProgress) {
31
+ const win = d3Window(this.ref.current);
32
+ select(win).on(MOUSEMOVE, this.handleMouseMove);
33
+ }
34
+ onMouseEnter(e);
35
+ };
36
+ this.handleLeave = (e) => {
37
+ const { onMouseLeave } = this.props;
38
+ if (onMouseLeave === undefined) {
39
+ return;
40
+ }
41
+ this.mouseInside = false;
42
+ if (!this.state.panInProgress && !this.state.dragInProgress) {
43
+ const win = d3Window(this.ref.current);
44
+ select(win).on(MOUSEMOVE, null);
45
+ }
46
+ onMouseLeave(e);
47
+ };
48
+ this.handleWheel = (e) => {
49
+ const { pan, onPan, zoom, onZoom } = this.props;
50
+ if (!pan && !zoom) {
51
+ return;
52
+ }
53
+ const { panInProgress } = this.state;
54
+ const yZoom = Math.abs(e.deltaY) > Math.abs(e.deltaX) && Math.abs(e.deltaY) > 0;
55
+ const mouseXY = mousePosition(e);
56
+ e.preventDefault();
57
+ if (zoom && this.focus && yZoom && !panInProgress) {
58
+ const zoomDir = e.deltaY > 0 ? 1 : -1;
59
+ if (onZoom !== undefined) {
60
+ onZoom(zoomDir, mouseXY, e);
61
+ }
62
+ }
63
+ else if (this.focus) {
64
+ if (this.shouldPan() && this.state.panStart !== undefined) {
65
+ // pan already in progress
66
+ const { panStartXScale, chartsToPan } = this.state.panStart;
67
+ this.lastNewPos = mouseXY;
68
+ this.panHappened = true;
69
+ if (this.dx === undefined) {
70
+ this.dx = 0;
71
+ }
72
+ if (this.dy === undefined) {
73
+ this.dy = 0;
74
+ }
75
+ this.dx -= e.deltaX;
76
+ this.dy += e.deltaY;
77
+ const dxdy = { dx: this.dx, dy: this.dy };
78
+ if (onPan !== undefined) {
79
+ onPan(mouseXY, panStartXScale, dxdy, chartsToPan, e);
80
+ }
81
+ }
82
+ else {
83
+ const { xScale, chartConfig } = this.props;
84
+ const currentCharts = getCurrentCharts(chartConfig, mouseXY);
85
+ this.dx = 0;
86
+ this.dy = 0;
87
+ this.setState({
88
+ panInProgress: true,
89
+ panStart: {
90
+ panStartXScale: xScale,
91
+ panOrigin: mouseXY,
92
+ chartsToPan: currentCharts,
93
+ },
94
+ });
95
+ }
96
+ this.queuePanEnd(e);
97
+ }
98
+ };
99
+ this.handleMouseMove = (e) => {
100
+ const { onMouseMove, mouseMove } = this.props;
101
+ if (onMouseMove === undefined) {
102
+ return;
103
+ }
104
+ if (this.mouseInteraction && mouseMove && !this.state.panInProgress) {
105
+ const newPos = pointer(e, this.ref.current);
106
+ onMouseMove(newPos, "mouse", e);
107
+ }
108
+ };
109
+ this.handleClick = (e) => {
110
+ const mouseXY = mousePosition(e);
111
+ const { onClick, onDoubleClick } = this.props;
112
+ if (!this.panHappened && !this.dragHappened) {
113
+ if (this.clicked && onDoubleClick !== undefined) {
114
+ onDoubleClick(mouseXY, e);
115
+ this.clicked = false;
116
+ }
117
+ else if (onClick !== undefined) {
118
+ onClick(mouseXY, e);
119
+ this.clicked = true;
120
+ setTimeout(() => {
121
+ if (this.clicked) {
122
+ this.clicked = false;
123
+ }
124
+ }, 400);
125
+ }
126
+ }
127
+ };
128
+ this.handleRightClick = (e) => {
129
+ e.stopPropagation();
130
+ e.preventDefault();
131
+ const { onContextMenu, onPanEnd } = this.props;
132
+ const mouseXY = mousePosition(e, this.ref.current.getBoundingClientRect());
133
+ if (this.state.panStart !== undefined) {
134
+ const { panStartXScale, panOrigin: [dx, dy], chartsToPan, } = this.state.panStart;
135
+ if (this.panHappened && onPanEnd !== undefined) {
136
+ onPanEnd(mouseXY, panStartXScale, { dx, dy }, chartsToPan, e);
137
+ }
138
+ const win = d3Window(this.ref.current);
139
+ select(win).on(MOUSEMOVE, null).on(MOUSEUP, null);
140
+ this.setState({
141
+ panInProgress: false,
142
+ panStart: undefined,
143
+ });
144
+ }
145
+ if (onContextMenu !== undefined) {
146
+ onContextMenu(mouseXY, e);
147
+ }
148
+ };
149
+ this.handleDrag = (e) => {
150
+ const { onDrag } = this.props;
151
+ if (onDrag === undefined) {
152
+ return;
153
+ }
154
+ this.dragHappened = true;
155
+ const { dragStartPosition } = this.state;
156
+ if (dragStartPosition === undefined) {
157
+ return;
158
+ }
159
+ const mouseXY = pointer(e, this.ref.current);
160
+ onDrag({
161
+ startPos: dragStartPosition,
162
+ mouseXY,
163
+ }, e);
164
+ };
165
+ this.handleDragEnd = (e) => {
166
+ const mouseXY = pointer(e, this.ref.current);
167
+ const win = d3Window(this.ref.current);
168
+ select(win)
169
+ // @ts-ignore
170
+ .on(MOUSEMOVE, this.mouseInside ? this.handleMouseMove : null)
171
+ .on(MOUSEUP, null);
172
+ if (this.dragHappened) {
173
+ const { onDragComplete } = this.props;
174
+ if (onDragComplete !== undefined) {
175
+ onDragComplete({ mouseXY }, e);
176
+ }
177
+ }
178
+ this.setState({
179
+ dragInProgress: false,
180
+ });
181
+ this.mouseInteraction = true;
182
+ };
183
+ this.canPan = () => {
184
+ const { getAllPanConditions } = this.props;
185
+ const { pan: initialPanEnabled } = this.props;
186
+ const { panEnabled, draggable: somethingSelected } = getAllPanConditions().reduce((returnObj, a) => {
187
+ return {
188
+ draggable: returnObj.draggable || a.draggable,
189
+ panEnabled: returnObj.panEnabled && a.panEnabled,
190
+ };
191
+ }, {
192
+ draggable: false,
193
+ panEnabled: initialPanEnabled,
194
+ });
195
+ return {
196
+ panEnabled,
197
+ somethingSelected,
198
+ };
199
+ };
200
+ this.handleMouseDown = (e) => {
201
+ if (e.button !== 0) {
202
+ return;
203
+ }
204
+ const { xScale, chartConfig, onMouseDown } = this.props;
205
+ this.panHappened = false;
206
+ this.dragHappened = false;
207
+ this.focus = true;
208
+ if (!this.state.panInProgress && this.mouseInteraction) {
209
+ const mouseXY = mousePosition(e);
210
+ const currentCharts = getCurrentCharts(chartConfig, mouseXY);
211
+ const { panEnabled, somethingSelected } = this.canPan();
212
+ const pan = panEnabled && !somethingSelected;
213
+ if (pan) {
214
+ this.setState({
215
+ panInProgress: pan,
216
+ panStart: {
217
+ panStartXScale: xScale,
218
+ panOrigin: mouseXY,
219
+ chartsToPan: currentCharts,
220
+ },
221
+ });
222
+ const win = d3Window(this.ref.current);
223
+ select(win).on(MOUSEMOVE, this.handlePan).on(MOUSEUP, this.handlePanEnd);
224
+ }
225
+ else if (somethingSelected) {
226
+ this.setState({
227
+ panInProgress: false,
228
+ dragInProgress: true,
229
+ panStart: undefined,
230
+ dragStartPosition: mouseXY,
231
+ });
232
+ const { onDragStart } = this.props;
233
+ if (onDragStart !== undefined) {
234
+ onDragStart({ startPos: mouseXY }, e);
235
+ }
236
+ const win = d3Window(this.ref.current);
237
+ select(win).on(MOUSEMOVE, this.handleDrag).on(MOUSEUP, this.handleDragEnd);
238
+ }
239
+ if (onMouseDown !== undefined) {
240
+ onMouseDown(mouseXY, currentCharts, e);
241
+ }
242
+ }
243
+ e.preventDefault();
244
+ };
245
+ this.shouldPan = () => {
246
+ const { pan: panEnabled, onPan } = this.props;
247
+ return panEnabled && onPan && this.state.panStart !== undefined;
248
+ };
249
+ this.handlePan = (e) => {
250
+ if (this.shouldPan() && this.state.panStart !== undefined) {
251
+ this.panHappened = true;
252
+ const { panStartXScale, panOrigin, chartsToPan } = this.state.panStart;
253
+ let dx;
254
+ let dy;
255
+ let mouseXY;
256
+ if (this.mouseInteraction) {
257
+ mouseXY = pointer(e, this.ref.current);
258
+ this.lastNewPos = mouseXY;
259
+ dx = mouseXY[0] - panOrigin[0];
260
+ dy = mouseXY[1] - panOrigin[1];
261
+ }
262
+ else {
263
+ mouseXY = pointers(e, this.ref.current)[0];
264
+ this.lastNewPos = mouseXY;
265
+ dx = panOrigin[0] - mouseXY[0];
266
+ dy = panOrigin[1] - mouseXY[1];
267
+ }
268
+ this.dx = dx;
269
+ this.dy = dy;
270
+ const { onPan } = this.props;
271
+ if (onPan !== undefined) {
272
+ onPan(mouseXY, panStartXScale, { dx, dy }, chartsToPan, e);
273
+ }
274
+ }
275
+ };
276
+ this.handlePanEnd = (e) => {
277
+ const { pan: panEnabled, onPanEnd } = this.props;
278
+ if (this.state.panStart !== undefined) {
279
+ const { panStartXScale, chartsToPan } = this.state.panStart;
280
+ const win = d3Window(this.ref.current);
281
+ select(win)
282
+ // @ts-ignore
283
+ .on(MOUSEMOVE, this.mouseInside ? this.handleMouseMove : null)
284
+ .on(MOUSEUP, null)
285
+ .on(TOUCHMOVE, null)
286
+ .on(TOUCHEND, null);
287
+ if (this.panHappened && panEnabled && onPanEnd) {
288
+ const { dx = 0, dy = 0 } = this;
289
+ delete this.dx;
290
+ delete this.dy;
291
+ if (this.lastNewPos !== undefined) {
292
+ onPanEnd(this.lastNewPos, panStartXScale, { dx, dy }, chartsToPan, e);
293
+ }
294
+ }
295
+ this.setState({
296
+ panInProgress: false,
297
+ panStart: undefined,
298
+ });
299
+ }
300
+ };
301
+ this.handleTouchMove = (e) => {
302
+ const { onMouseMove } = this.props;
303
+ if (onMouseMove === undefined) {
304
+ return;
305
+ }
306
+ const touch = getTouchProps(e.touches[0]);
307
+ const touchXY = touchPosition(touch, e);
308
+ onMouseMove(touchXY, "touch", e);
309
+ };
310
+ this.handleTouchStart = (e) => {
311
+ this.mouseInteraction = false;
312
+ const { pan: panEnabled, chartConfig, onMouseMove, xScale, onPanEnd } = this.props;
313
+ if (e.touches.length === 1) {
314
+ this.panHappened = false;
315
+ const touchXY = touchPosition(getTouchProps(e.touches[0]), e);
316
+ if (onMouseMove !== undefined) {
317
+ onMouseMove(touchXY, "touch", e);
318
+ }
319
+ if (panEnabled) {
320
+ const currentCharts = getCurrentCharts(chartConfig, touchXY);
321
+ this.setState({
322
+ panInProgress: true,
323
+ panStart: {
324
+ panStartXScale: xScale,
325
+ panOrigin: touchXY,
326
+ chartsToPan: currentCharts,
327
+ },
328
+ });
329
+ const win = d3Window(this.ref.current);
330
+ select(win).on(TOUCHMOVE, this.handlePan, false).on(TOUCHEND, this.handlePanEnd, false);
331
+ }
332
+ }
333
+ else if (e.touches.length === 2) {
334
+ // pinch zoom begin
335
+ // do nothing pinch zoom is handled in handleTouchMove
336
+ const { panInProgress, panStart } = this.state;
337
+ if (panInProgress && panEnabled && onPanEnd && panStart !== undefined) {
338
+ const { panStartXScale, panOrigin: [dx, dy], chartsToPan, } = panStart;
339
+ const win = d3Window(this.ref.current);
340
+ select(win)
341
+ // @ts-ignore
342
+ .on(MOUSEMOVE, this.mouseInside ? this.handleMouseMove : null)
343
+ .on(MOUSEUP, null)
344
+ .on(TOUCHMOVE, this.handlePinchZoom, false)
345
+ .on(TOUCHEND, this.handlePinchZoomEnd, false);
346
+ const touch1Pos = touchPosition(getTouchProps(e.touches[0]), e);
347
+ const touch2Pos = touchPosition(getTouchProps(e.touches[1]), e);
348
+ if (this.panHappened && panEnabled && onPanEnd && this.lastNewPos !== undefined) {
349
+ onPanEnd(this.lastNewPos, panStartXScale, { dx, dy }, chartsToPan, e);
350
+ }
351
+ this.setState({
352
+ panInProgress: false,
353
+ pinchZoomStart: {
354
+ xScale,
355
+ touch1Pos,
356
+ touch2Pos,
357
+ range: xScale.range(),
358
+ chartsToPan,
359
+ },
360
+ });
361
+ }
362
+ }
363
+ };
364
+ this.handlePinchZoom = (e) => {
365
+ const { pinchZoomStart } = this.state;
366
+ if (pinchZoomStart === undefined) {
367
+ return;
368
+ }
369
+ const { xScale, zoom: zoomEnabled, onPinchZoom } = this.props;
370
+ if (!zoomEnabled || onPinchZoom === undefined) {
371
+ return;
372
+ }
373
+ const [touch1Pos, touch2Pos] = pointers(this.ref.current);
374
+ const { chartsToPan } = pinchZoomStart, initialPinch = __rest(pinchZoomStart, ["chartsToPan"]);
375
+ onPinchZoom(initialPinch, {
376
+ touch1Pos,
377
+ touch2Pos,
378
+ xScale,
379
+ }, e);
380
+ };
381
+ this.handlePinchZoomEnd = (e) => {
382
+ const win = d3Window(this.ref.current);
383
+ select(win).on(TOUCHMOVE, null).on(TOUCHEND, null);
384
+ const { pinchZoomStart } = this.state;
385
+ if (pinchZoomStart === undefined) {
386
+ return;
387
+ }
388
+ const { chartsToPan } = pinchZoomStart, initialPinch = __rest(pinchZoomStart, ["chartsToPan"]);
389
+ const { zoom: zoomEnabled, onPinchZoomEnd } = this.props;
390
+ if (zoomEnabled && onPinchZoomEnd) {
391
+ onPinchZoomEnd(initialPinch, e);
392
+ }
393
+ this.setState({
394
+ pinchZoomStart: undefined,
395
+ });
396
+ };
397
+ this.setCursorClass = (cursorOverrideClass) => {
398
+ if (cursorOverrideClass !== this.state.cursorOverrideClass) {
399
+ this.setState({
400
+ cursorOverrideClass: cursorOverrideClass === null ? undefined : cursorOverrideClass,
401
+ });
402
+ }
403
+ };
404
+ this.focus = props.focus;
405
+ this.state = {
406
+ panInProgress: false,
407
+ };
408
+ }
409
+ componentDidMount() {
410
+ const { disableInteraction } = this.props;
411
+ const { current } = this.ref;
412
+ if (current === null) {
413
+ return;
414
+ }
415
+ if (!disableInteraction) {
416
+ // @ts-ignore
417
+ select(current).on(MOUSEENTER, this.handleEnter).on(MOUSELEAVE, this.handleLeave);
418
+ // @ts-ignore
419
+ current.addEventListener("wheel", this.handleWheel, { passive: false });
420
+ }
421
+ }
422
+ componentDidUpdate() {
423
+ this.componentDidMount();
424
+ }
425
+ componentWillUnmount() {
426
+ const { disableInteraction } = this.props;
427
+ const { current } = this.ref;
428
+ if (current === null) {
429
+ return;
430
+ }
431
+ if (!disableInteraction) {
432
+ select(current).on(MOUSEENTER, null).on(MOUSELEAVE, null);
433
+ const win = d3Window(current);
434
+ select(win).on(MOUSEMOVE, null);
435
+ // @ts-ignore
436
+ current.removeEventListener("wheel", this.handleWheel, { passive: false });
437
+ }
438
+ }
439
+ queuePanEnd(e) {
440
+ if (this.panEndTimeout !== undefined) {
441
+ window.clearTimeout(this.panEndTimeout);
442
+ }
443
+ this.panEndTimeout = window.setTimeout(() => {
444
+ this.handlePanEnd(e);
445
+ }, 100);
446
+ }
447
+ cancelDrag() {
448
+ const win = d3Window(this.ref.current);
449
+ select(win)
450
+ // @ts-ignore
451
+ .on(MOUSEMOVE, this.mouseInside ? this.handleMouseMove : null)
452
+ .on(MOUSEUP, null);
453
+ this.setState({
454
+ dragInProgress: false,
455
+ });
456
+ this.mouseInteraction = true;
457
+ }
458
+ render() {
459
+ const { height, width, disableInteraction, useCrossHairStyleCursor } = this.props;
460
+ const className = disableInteraction
461
+ ? undefined
462
+ : this.state.cursorOverrideClass !== undefined
463
+ ? this.state.cursorOverrideClass
464
+ : !useCrossHairStyleCursor
465
+ ? undefined
466
+ : this.state.panInProgress
467
+ ? "react-financial-charts-grabbing-cursor"
468
+ : "react-financial-charts-crosshair-cursor";
469
+ const interactionProps = disableInteraction
470
+ ? undefined
471
+ : {
472
+ onMouseDown: this.handleMouseDown,
473
+ onClick: this.handleClick,
474
+ onContextMenu: this.handleRightClick,
475
+ onTouchStart: this.handleTouchStart,
476
+ onTouchMove: this.handleTouchMove,
477
+ };
478
+ return (React.createElement("rect", Object.assign({ ref: this.ref, className: className, width: width, height: height, style: { opacity: 0 } }, interactionProps)));
479
+ }
480
+ }
481
+ EventCapture.defaultProps = {
482
+ mouseMove: false,
483
+ zoom: false,
484
+ pan: false,
485
+ panSpeedMultiplier: 1,
486
+ focus: false,
487
+ disableInteraction: false,
488
+ };
489
489
  //# sourceMappingURL=EventCapture.js.map