* Handle an event * @param {ChartEvent} e the event to handle * @param {boolean} [replay] - true if the event was replayed by `update` * @param {boolean} [inChartArea] - true if the event is inside chartArea * @return {boolean} true if the chart needs to re-render * @private
(e, replay, inChartArea)
| 1308 | * @private |
| 1309 | */ |
| 1310 | _handleEvent(e, replay, inChartArea) { |
| 1311 | const {_active: lastActive = [], options} = this; |
| 1312 | |
| 1313 | // If the event is replayed from `update`, we should evaluate with the final positions. |
| 1314 | // |
| 1315 | // The `replay`: |
| 1316 | // It's the last event (excluding click) that has occurred before `update`. |
| 1317 | // So mouse has not moved. It's also over the chart, because there is a `replay`. |
| 1318 | // |
| 1319 | // The why: |
| 1320 | // If animations are active, the elements haven't moved yet compared to state before update. |
| 1321 | // But if they will, we are activating the elements that would be active, if this check |
| 1322 | // was done after the animations have completed. => "final positions". |
| 1323 | // If there is no animations, the "final" and "current" positions are equal. |
| 1324 | // This is done so we do not have to evaluate the active elements each animation frame |
| 1325 | // - it would be expensive. |
| 1326 | const useFinalPosition = replay; |
| 1327 | let active = this._filterActive(this._getActiveElements(e, lastActive, inChartArea, useFinalPosition)); |
| 1328 | let previous = lastActive; |
| 1329 | const isClick = _isClickEvent(e, options.events); |
| 1330 | const lastEvent = determineLastEvent(e, this._lastEvent, inChartArea, isClick); |
| 1331 | |
| 1332 | if (inChartArea) { |
| 1333 | // Set _lastEvent to null while we are processing the event handlers. |
| 1334 | // This prevents recursion if the handler calls chart.update() |
| 1335 | this._lastEvent = null; |
| 1336 | |
| 1337 | // Invoke onHover hook |
| 1338 | callCallback(options.onHover, [e, active, this], this); |
| 1339 | |
| 1340 | if (isClick) { |
| 1341 | callCallback(options.onClick, [e, active, this], this); |
| 1342 | } |
| 1343 | |
| 1344 | // A handler may hide data. If it updates the chart, the update has already |
| 1345 | // dropped that data from `_active`, so compare with what the update left. |
| 1346 | active = this._filterActive(active); |
| 1347 | previous = this._active; |
| 1348 | } |
| 1349 | |
| 1350 | const changed = !_elementsEqual(active, previous); |
| 1351 | if (changed || replay) { |
| 1352 | this._active = active; |
| 1353 | this._updateHoverStyles(active, previous, replay); |
| 1354 | } |
| 1355 | // After the handlers: in the chart area the pointer's set is written over any other set a |
| 1356 | // handler wrote, so a set a handler made that is still active is the pointer's too. |
| 1357 | if (_pointerOwnsActive(inChartArea, replay, changed)) { |
| 1358 | this._activeSetByCode = false; |
| 1359 | } |
| 1360 | |
| 1361 | this._lastEvent = lastEvent; |
| 1362 | |
| 1363 | return changed; |
| 1364 | } |
| 1365 | |
| 1366 | /** |
| 1367 | * @param {ChartEvent} e - The event |
no test coverage detected