| [a762898] | 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.mouseMoveMiddleware = exports.mouseMoveAction = exports.mouseClickMiddleware = exports.mouseClickAction = void 0;
|
|---|
| 7 | var _toolkit = require("@reduxjs/toolkit");
|
|---|
| 8 | var _tooltipSlice = require("./tooltipSlice");
|
|---|
| 9 | var _selectActivePropsFromChartPointer = require("./selectors/selectActivePropsFromChartPointer");
|
|---|
| 10 | var _selectTooltipEventType = require("./selectors/selectTooltipEventType");
|
|---|
| 11 | var _getChartPointer = require("../util/getChartPointer");
|
|---|
| 12 | var mouseClickAction = exports.mouseClickAction = (0, _toolkit.createAction)('mouseClick');
|
|---|
| 13 | var mouseClickMiddleware = exports.mouseClickMiddleware = (0, _toolkit.createListenerMiddleware)();
|
|---|
| 14 |
|
|---|
| 15 | // TODO: there's a bug here when you click the chart the activeIndex resets to zero
|
|---|
| 16 | mouseClickMiddleware.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 | });
|
|---|
| 30 | var mouseMoveAction = exports.mouseMoveAction = (0, _toolkit.createAction)('mouseMove');
|
|---|
| 31 | var 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 | */
|
|---|
| 41 | var rafId = null;
|
|---|
| 42 | mouseMoveMiddleware.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 | }); |
|---|