source: node_modules/recharts/lib/state/mouseEventsMiddleware.js

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 3.5 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.mouseMoveMiddleware = exports.mouseMoveAction = exports.mouseClickMiddleware = exports.mouseClickAction = void 0;
7var _toolkit = require("@reduxjs/toolkit");
8var _tooltipSlice = require("./tooltipSlice");
9var _selectActivePropsFromChartPointer = require("./selectors/selectActivePropsFromChartPointer");
10var _selectTooltipEventType = require("./selectors/selectTooltipEventType");
11var _getChartPointer = require("../util/getChartPointer");
12var mouseClickAction = exports.mouseClickAction = (0, _toolkit.createAction)('mouseClick');
13var mouseClickMiddleware = exports.mouseClickMiddleware = (0, _toolkit.createListenerMiddleware)();
14
15// TODO: there's a bug here when you click the chart the activeIndex resets to zero
16mouseClickMiddleware.startListening({
17 actionCreator: mouseClickAction,
18 effect: (action, listenerApi) => {
19 var mousePointer = action.payload;
20 var activeProps = (0, _selectActivePropsFromChartPointer.selectActivePropsFromChartPointer)(listenerApi.getState(), (0, _getChartPointer.getChartPointer)(mousePointer));
21 if ((activeProps === null || activeProps === void 0 ? void 0 : activeProps.activeIndex) != null) {
22 listenerApi.dispatch((0, _tooltipSlice.setMouseClickAxisIndex)({
23 activeIndex: activeProps.activeIndex,
24 activeDataKey: undefined,
25 activeCoordinate: activeProps.activeCoordinate
26 }));
27 }
28 }
29});
30var mouseMoveAction = exports.mouseMoveAction = (0, _toolkit.createAction)('mouseMove');
31var mouseMoveMiddleware = exports.mouseMoveMiddleware = (0, _toolkit.createListenerMiddleware)();
32
33/*
34 * This single rafId is safe because:
35 * 1. Each chart has its own Redux store instance with its own middleware
36 * 2. mouseMoveAction only fires from one DOM element (the chart wrapper)
37 * 3. Rapid mousemove events from the same element SHOULD debounce - we only care about the latest position
38 * This is different from externalEventsMiddleware which handles multiple event types
39 * (click, mouseenter, mouseleave, etc.) that should NOT cancel each other.
40 */
41var rafId = null;
42mouseMoveMiddleware.startListening({
43 actionCreator: mouseMoveAction,
44 effect: (action, listenerApi) => {
45 var mousePointer = action.payload;
46
47 // Cancel any pending animation frame
48 if (rafId !== null) {
49 cancelAnimationFrame(rafId);
50 }
51 var chartPointer = (0, _getChartPointer.getChartPointer)(mousePointer);
52
53 // Schedule the dispatch for the next animation frame
54 rafId = requestAnimationFrame(() => {
55 var state = listenerApi.getState();
56 var tooltipEventType = (0, _selectTooltipEventType.selectTooltipEventType)(state, state.tooltip.settings.shared);
57 // this functionality only applies to charts that have axes
58 if (tooltipEventType === 'axis') {
59 var activeProps = (0, _selectActivePropsFromChartPointer.selectActivePropsFromChartPointer)(state, chartPointer);
60 if ((activeProps === null || activeProps === void 0 ? void 0 : activeProps.activeIndex) != null) {
61 listenerApi.dispatch((0, _tooltipSlice.setMouseOverAxisIndex)({
62 activeIndex: activeProps.activeIndex,
63 activeDataKey: undefined,
64 activeCoordinate: activeProps.activeCoordinate
65 }));
66 } else {
67 // this is needed to clear tooltip state when the mouse moves out of the inRange (svg - offset) function, but not yet out of the svg
68 listenerApi.dispatch((0, _tooltipSlice.mouseLeaveChart)());
69 }
70 }
71 rafId = null;
72 });
73 }
74});
Note: See TracBrowser for help on using the repository browser.