SeriesKeyboardNavigation.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /* *
  2. *
  3. * (c) 2009-2020 Øystein Moseng
  4. *
  5. * Handle keyboard navigation for series.
  6. *
  7. * License: www.highcharts.com/license
  8. *
  9. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  10. *
  11. * */
  12. 'use strict';
  13. import Series from '../../../Core/Series/Series.js';
  14. var seriesTypes = Series.seriesTypes;
  15. import CartesianSeries from '../../../Core/Series/CartesianSeries.js';
  16. import Chart from '../../../Core/Chart/Chart.js';
  17. import Point from '../../../Core/Series/Point.js';
  18. import U from '../../../Core/Utilities.js';
  19. var defined = U.defined, extend = U.extend;
  20. import KeyboardNavigationHandler from '../../KeyboardNavigationHandler.js';
  21. import EventProvider from '../../Utils/EventProvider.js';
  22. import ChartUtilities from '../../Utils/ChartUtilities.js';
  23. var getPointFromXY = ChartUtilities.getPointFromXY, getSeriesFromName = ChartUtilities.getSeriesFromName, scrollToPoint = ChartUtilities.scrollToPoint;
  24. import '../../../Series/ColumnSeries.js';
  25. import '../../../Series/PieSeries.js';
  26. /* eslint-disable no-invalid-this, valid-jsdoc */
  27. /*
  28. * Set for which series types it makes sense to move to the closest point with
  29. * up/down arrows, and which series types should just move to next series.
  30. */
  31. CartesianSeries.prototype.keyboardMoveVertical = true;
  32. ['column', 'pie'].forEach(function (type) {
  33. if (seriesTypes[type]) {
  34. seriesTypes[type].prototype.keyboardMoveVertical = false;
  35. }
  36. });
  37. /**
  38. * Get the index of a point in a series. This is needed when using e.g. data
  39. * grouping.
  40. *
  41. * @private
  42. * @function getPointIndex
  43. *
  44. * @param {Highcharts.AccessibilityPoint} point
  45. * The point to find index of.
  46. *
  47. * @return {number|undefined}
  48. * The index in the series.points array of the point.
  49. */
  50. function getPointIndex(point) {
  51. var index = point.index, points = point.series.points, i = points.length;
  52. if (points[index] !== point) {
  53. while (i--) {
  54. if (points[i] === point) {
  55. return i;
  56. }
  57. }
  58. }
  59. else {
  60. return index;
  61. }
  62. }
  63. /**
  64. * Determine if series navigation should be skipped
  65. *
  66. * @private
  67. * @function isSkipSeries
  68. *
  69. * @param {Highcharts.Series} series
  70. *
  71. * @return {boolean|number|undefined}
  72. */
  73. function isSkipSeries(series) {
  74. var a11yOptions = series.chart.options.accessibility, seriesNavOptions = a11yOptions.keyboardNavigation.seriesNavigation, seriesA11yOptions = series.options.accessibility || {}, seriesKbdNavOptions = seriesA11yOptions.keyboardNavigation;
  75. return seriesKbdNavOptions && seriesKbdNavOptions.enabled === false ||
  76. seriesA11yOptions.enabled === false ||
  77. series.options.enableMouseTracking === false || // #8440
  78. !series.visible ||
  79. // Skip all points in a series where pointNavigationEnabledThreshold is
  80. // reached
  81. (seriesNavOptions.pointNavigationEnabledThreshold &&
  82. seriesNavOptions.pointNavigationEnabledThreshold <=
  83. series.points.length);
  84. }
  85. /**
  86. * Determine if navigation for a point should be skipped
  87. *
  88. * @private
  89. * @function isSkipPoint
  90. *
  91. * @param {Highcharts.Point} point
  92. *
  93. * @return {boolean|number|undefined}
  94. */
  95. function isSkipPoint(point) {
  96. var a11yOptions = point.series.chart.options.accessibility;
  97. return point.isNull &&
  98. a11yOptions.keyboardNavigation.seriesNavigation.skipNullPoints ||
  99. point.visible === false ||
  100. isSkipSeries(point.series);
  101. }
  102. /**
  103. * Get the point in a series that is closest (in pixel distance) to a reference
  104. * point. Optionally supply weight factors for x and y directions.
  105. *
  106. * @private
  107. * @function getClosestPoint
  108. *
  109. * @param {Highcharts.Point} point
  110. * @param {Highcharts.Series} series
  111. * @param {number} [xWeight]
  112. * @param {number} [yWeight]
  113. *
  114. * @return {Highcharts.Point|undefined}
  115. */
  116. function getClosestPoint(point, series, xWeight, yWeight) {
  117. var minDistance = Infinity, dPoint, minIx, distance, i = series.points.length, hasUndefinedPosition = function (point) {
  118. return !(defined(point.plotX) && defined(point.plotY));
  119. };
  120. if (hasUndefinedPosition(point)) {
  121. return;
  122. }
  123. while (i--) {
  124. dPoint = series.points[i];
  125. if (hasUndefinedPosition(dPoint)) {
  126. continue;
  127. }
  128. distance = (point.plotX - dPoint.plotX) *
  129. (point.plotX - dPoint.plotX) *
  130. (xWeight || 1) +
  131. (point.plotY - dPoint.plotY) *
  132. (point.plotY - dPoint.plotY) *
  133. (yWeight || 1);
  134. if (distance < minDistance) {
  135. minDistance = distance;
  136. minIx = i;
  137. }
  138. }
  139. return defined(minIx) ? series.points[minIx] : void 0;
  140. }
  141. /**
  142. * Highlights a point (show tooltip and display hover state).
  143. *
  144. * @private
  145. * @function Highcharts.Point#highlight
  146. *
  147. * @return {Highcharts.Point}
  148. * This highlighted point.
  149. */
  150. Point.prototype.highlight = function () {
  151. var chart = this.series.chart;
  152. if (!this.isNull) {
  153. this.onMouseOver(); // Show the hover marker and tooltip
  154. }
  155. else {
  156. if (chart.tooltip) {
  157. chart.tooltip.hide(0);
  158. }
  159. // Don't call blur on the element, as it messes up the chart div's focus
  160. }
  161. scrollToPoint(this);
  162. // We focus only after calling onMouseOver because the state change can
  163. // change z-index and mess up the element.
  164. if (this.graphic) {
  165. chart.setFocusToElement(this.graphic);
  166. }
  167. chart.highlightedPoint = this;
  168. return this;
  169. };
  170. /**
  171. * Function to highlight next/previous point in chart.
  172. *
  173. * @private
  174. * @function Highcharts.Chart#highlightAdjacentPoint
  175. *
  176. * @param {boolean} next
  177. * Flag for the direction.
  178. *
  179. * @return {Highcharts.Point|boolean}
  180. * Returns highlighted point on success, false on failure (no adjacent
  181. * point to highlight in chosen direction).
  182. */
  183. Chart.prototype.highlightAdjacentPoint = function (next) {
  184. var chart = this, series = chart.series, curPoint = chart.highlightedPoint, curPointIndex = curPoint && getPointIndex(curPoint) || 0, curPoints = (curPoint && curPoint.series.points), lastSeries = chart.series && chart.series[chart.series.length - 1], lastPoint = lastSeries && lastSeries.points &&
  185. lastSeries.points[lastSeries.points.length - 1], newSeries, newPoint;
  186. // If no points, return false
  187. if (!series[0] || !series[0].points) {
  188. return false;
  189. }
  190. if (!curPoint) {
  191. // No point is highlighted yet. Try first/last point depending on move
  192. // direction
  193. newPoint = next ? series[0].points[0] : lastPoint;
  194. }
  195. else {
  196. // We have a highlighted point.
  197. // Grab next/prev point & series
  198. newSeries = series[curPoint.series.index + (next ? 1 : -1)];
  199. newPoint = curPoints[curPointIndex + (next ? 1 : -1)];
  200. if (!newPoint && newSeries) {
  201. // Done with this series, try next one
  202. newPoint = newSeries.points[next ? 0 : newSeries.points.length - 1];
  203. }
  204. // If there is no adjacent point, we return false
  205. if (!newPoint) {
  206. return false;
  207. }
  208. }
  209. // Recursively skip points
  210. if (isSkipPoint(newPoint)) {
  211. // If we skip this whole series, move to the end of the series before we
  212. // recurse, just to optimize
  213. newSeries = newPoint.series;
  214. if (isSkipSeries(newSeries)) {
  215. chart.highlightedPoint = next ?
  216. newSeries.points[newSeries.points.length - 1] :
  217. newSeries.points[0];
  218. }
  219. else {
  220. // Otherwise, just move one point
  221. chart.highlightedPoint = newPoint;
  222. }
  223. // Retry
  224. return chart.highlightAdjacentPoint(next);
  225. }
  226. // There is an adjacent point, highlight it
  227. return newPoint.highlight();
  228. };
  229. /**
  230. * Highlight first valid point in a series. Returns the point if successfully
  231. * highlighted, otherwise false. If there is a highlighted point in the series,
  232. * use that as starting point.
  233. *
  234. * @private
  235. * @function Highcharts.Series#highlightFirstValidPoint
  236. *
  237. * @return {boolean|Highcharts.Point}
  238. */
  239. CartesianSeries.prototype.highlightFirstValidPoint = function () {
  240. var curPoint = this.chart.highlightedPoint, start = (curPoint && curPoint.series) === this ?
  241. getPointIndex(curPoint) :
  242. 0, points = this.points, len = points.length;
  243. if (points && len) {
  244. for (var i = start; i < len; ++i) {
  245. if (!isSkipPoint(points[i])) {
  246. return points[i].highlight();
  247. }
  248. }
  249. for (var j = start; j >= 0; --j) {
  250. if (!isSkipPoint(points[j])) {
  251. return points[j].highlight();
  252. }
  253. }
  254. }
  255. return false;
  256. };
  257. /**
  258. * Highlight next/previous series in chart. Returns false if no adjacent series
  259. * in the direction, otherwise returns new highlighted point.
  260. *
  261. * @private
  262. * @function Highcharts.Chart#highlightAdjacentSeries
  263. *
  264. * @param {boolean} down
  265. *
  266. * @return {Highcharts.Point|boolean}
  267. */
  268. Chart.prototype.highlightAdjacentSeries = function (down) {
  269. var chart = this, newSeries, newPoint, adjacentNewPoint, curPoint = chart.highlightedPoint, lastSeries = chart.series && chart.series[chart.series.length - 1], lastPoint = lastSeries && lastSeries.points &&
  270. lastSeries.points[lastSeries.points.length - 1];
  271. // If no point is highlighted, highlight the first/last point
  272. if (!chart.highlightedPoint) {
  273. newSeries = down ? (chart.series && chart.series[0]) : lastSeries;
  274. newPoint = down ?
  275. (newSeries && newSeries.points && newSeries.points[0]) : lastPoint;
  276. return newPoint ? newPoint.highlight() : false;
  277. }
  278. newSeries = chart.series[curPoint.series.index + (down ? -1 : 1)];
  279. if (!newSeries) {
  280. return false;
  281. }
  282. // We have a new series in this direction, find the right point
  283. // Weigh xDistance as counting much higher than Y distance
  284. newPoint = getClosestPoint(curPoint, newSeries, 4);
  285. if (!newPoint) {
  286. return false;
  287. }
  288. // New series and point exists, but we might want to skip it
  289. if (isSkipSeries(newSeries)) {
  290. // Skip the series
  291. newPoint.highlight();
  292. adjacentNewPoint = chart.highlightAdjacentSeries(down); // Try recurse
  293. if (!adjacentNewPoint) {
  294. // Recurse failed
  295. curPoint.highlight();
  296. return false;
  297. }
  298. // Recurse succeeded
  299. return adjacentNewPoint;
  300. }
  301. // Highlight the new point or any first valid point back or forwards from it
  302. newPoint.highlight();
  303. return newPoint.series.highlightFirstValidPoint();
  304. };
  305. /**
  306. * Highlight the closest point vertically.
  307. *
  308. * @private
  309. * @function Highcharts.Chart#highlightAdjacentPointVertical
  310. *
  311. * @param {boolean} down
  312. *
  313. * @return {Highcharts.Point|boolean}
  314. */
  315. Chart.prototype.highlightAdjacentPointVertical = function (down) {
  316. var curPoint = this.highlightedPoint, minDistance = Infinity, bestPoint;
  317. if (!defined(curPoint.plotX) || !defined(curPoint.plotY)) {
  318. return false;
  319. }
  320. this.series.forEach(function (series) {
  321. if (isSkipSeries(series)) {
  322. return;
  323. }
  324. series.points.forEach(function (point) {
  325. if (!defined(point.plotY) || !defined(point.plotX) ||
  326. point === curPoint) {
  327. return;
  328. }
  329. var yDistance = point.plotY - curPoint.plotY, width = Math.abs(point.plotX - curPoint.plotX), distance = Math.abs(yDistance) * Math.abs(yDistance) +
  330. width * width * 4; // Weigh horizontal distance highly
  331. // Reverse distance number if axis is reversed
  332. if (series.yAxis && series.yAxis.reversed) {
  333. yDistance *= -1;
  334. }
  335. if (yDistance <= 0 && down || yDistance >= 0 && !down || // Chk dir
  336. distance < 5 || // Points in same spot => infinite loop
  337. isSkipPoint(point)) {
  338. return;
  339. }
  340. if (distance < minDistance) {
  341. minDistance = distance;
  342. bestPoint = point;
  343. }
  344. });
  345. });
  346. return bestPoint ? bestPoint.highlight() : false;
  347. };
  348. /**
  349. * @private
  350. * @param {Highcharts.Chart} chart
  351. * @return {Highcharts.Point|boolean}
  352. */
  353. function highlightFirstValidPointInChart(chart) {
  354. var res = false;
  355. delete chart.highlightedPoint;
  356. res = chart.series.reduce(function (acc, cur) {
  357. return acc || cur.highlightFirstValidPoint();
  358. }, false);
  359. return res;
  360. }
  361. /**
  362. * @private
  363. * @param {Highcharts.Chart} chart
  364. * @return {Highcharts.Point|boolean}
  365. */
  366. function highlightLastValidPointInChart(chart) {
  367. var numSeries = chart.series.length, i = numSeries, res = false;
  368. while (i--) {
  369. chart.highlightedPoint = chart.series[i].points[chart.series[i].points.length - 1];
  370. // Highlight first valid point in the series will also
  371. // look backwards. It always starts from currently
  372. // highlighted point.
  373. res = chart.series[i].highlightFirstValidPoint();
  374. if (res) {
  375. break;
  376. }
  377. }
  378. return res;
  379. }
  380. /**
  381. * @private
  382. * @param {Highcharts.Chart} chart
  383. */
  384. function updateChartFocusAfterDrilling(chart) {
  385. highlightFirstValidPointInChart(chart);
  386. if (chart.focusElement) {
  387. chart.focusElement.removeFocusBorder();
  388. }
  389. }
  390. /**
  391. * @private
  392. * @class
  393. * @name Highcharts.SeriesKeyboardNavigation
  394. */
  395. function SeriesKeyboardNavigation(chart, keyCodes) {
  396. this.keyCodes = keyCodes;
  397. this.chart = chart;
  398. }
  399. extend(SeriesKeyboardNavigation.prototype, /** @lends Highcharts.SeriesKeyboardNavigation */ {
  400. /**
  401. * Init the keyboard navigation
  402. */
  403. init: function () {
  404. var keyboardNavigation = this, chart = this.chart, e = this.eventProvider = new EventProvider();
  405. e.addEvent(CartesianSeries, 'destroy', function () {
  406. return keyboardNavigation.onSeriesDestroy(this);
  407. });
  408. e.addEvent(chart, 'afterDrilldown', function () {
  409. updateChartFocusAfterDrilling(this);
  410. });
  411. e.addEvent(chart, 'drilldown', function (e) {
  412. var point = e.point, series = point.series;
  413. keyboardNavigation.lastDrilledDownPoint = {
  414. x: point.x,
  415. y: point.y,
  416. seriesName: series ? series.name : ''
  417. };
  418. });
  419. e.addEvent(chart, 'drillupall', function () {
  420. setTimeout(function () {
  421. keyboardNavigation.onDrillupAll();
  422. }, 10);
  423. });
  424. },
  425. onDrillupAll: function () {
  426. // After drillup we want to find the point that was drilled down to and
  427. // highlight it.
  428. var last = this.lastDrilledDownPoint, chart = this.chart, series = last && getSeriesFromName(chart, last.seriesName), point;
  429. if (last && series && defined(last.x) && defined(last.y)) {
  430. point = getPointFromXY(series, last.x, last.y);
  431. }
  432. // Container focus can be lost on drillup due to deleted elements.
  433. if (chart.container) {
  434. chart.container.focus();
  435. }
  436. if (point && point.highlight) {
  437. point.highlight();
  438. }
  439. if (chart.focusElement) {
  440. chart.focusElement.removeFocusBorder();
  441. }
  442. },
  443. /**
  444. * @return {Highcharts.KeyboardNavigationHandler}
  445. */
  446. getKeyboardNavigationHandler: function () {
  447. var keyboardNavigation = this, keys = this.keyCodes, chart = this.chart, inverted = chart.inverted;
  448. return new KeyboardNavigationHandler(chart, {
  449. keyCodeMap: [
  450. [inverted ? [keys.up, keys.down] : [keys.left, keys.right], function (keyCode) {
  451. return keyboardNavigation.onKbdSideways(this, keyCode);
  452. }],
  453. [inverted ? [keys.left, keys.right] : [keys.up, keys.down], function (keyCode) {
  454. return keyboardNavigation.onKbdVertical(this, keyCode);
  455. }],
  456. [[keys.enter, keys.space], function () {
  457. if (chart.highlightedPoint) {
  458. chart.highlightedPoint.firePointEvent('click');
  459. }
  460. return this.response.success;
  461. }]
  462. ],
  463. init: function (dir) {
  464. return keyboardNavigation.onHandlerInit(this, dir);
  465. },
  466. terminate: function () {
  467. return keyboardNavigation.onHandlerTerminate();
  468. }
  469. });
  470. },
  471. /**
  472. * @private
  473. * @param {Highcharts.KeyboardNavigationHandler} handler
  474. * @param {number} keyCode
  475. * @return {number}
  476. * response
  477. */
  478. onKbdSideways: function (handler, keyCode) {
  479. var keys = this.keyCodes, isNext = keyCode === keys.right || keyCode === keys.down;
  480. return this.attemptHighlightAdjacentPoint(handler, isNext);
  481. },
  482. /**
  483. * @private
  484. * @param {Highcharts.KeyboardNavigationHandler} handler
  485. * @param {number} keyCode
  486. * @return {number}
  487. * response
  488. */
  489. onKbdVertical: function (handler, keyCode) {
  490. var chart = this.chart, keys = this.keyCodes, isNext = keyCode === keys.down || keyCode === keys.right, navOptions = chart.options.accessibility.keyboardNavigation
  491. .seriesNavigation;
  492. // Handle serialized mode, act like left/right
  493. if (navOptions.mode && navOptions.mode === 'serialize') {
  494. return this.attemptHighlightAdjacentPoint(handler, isNext);
  495. }
  496. // Normal mode, move between series
  497. var highlightMethod = (chart.highlightedPoint &&
  498. chart.highlightedPoint.series.keyboardMoveVertical) ?
  499. 'highlightAdjacentPointVertical' :
  500. 'highlightAdjacentSeries';
  501. chart[highlightMethod](isNext);
  502. return handler.response.success;
  503. },
  504. /**
  505. * @private
  506. * @param {Highcharts.KeyboardNavigationHandler} handler
  507. * @param {number} initDirection
  508. * @return {number}
  509. * response
  510. */
  511. onHandlerInit: function (handler, initDirection) {
  512. var chart = this.chart;
  513. if (initDirection > 0) {
  514. highlightFirstValidPointInChart(chart);
  515. }
  516. else {
  517. highlightLastValidPointInChart(chart);
  518. }
  519. return handler.response.success;
  520. },
  521. /**
  522. * @private
  523. */
  524. onHandlerTerminate: function () {
  525. var _a, _b;
  526. var chart = this.chart;
  527. var curPoint = chart.highlightedPoint;
  528. (_a = chart.tooltip) === null || _a === void 0 ? void 0 : _a.hide(0);
  529. (_b = curPoint === null || curPoint === void 0 ? void 0 : curPoint.onMouseOut) === null || _b === void 0 ? void 0 : _b.call(curPoint);
  530. delete chart.highlightedPoint;
  531. },
  532. /**
  533. * Function that attempts to highlight next/prev point. Handles wrap around.
  534. * @private
  535. * @param {Highcharts.KeyboardNavigationHandler} handler
  536. * @param {boolean} directionIsNext
  537. * @return {number}
  538. * response
  539. */
  540. attemptHighlightAdjacentPoint: function (handler, directionIsNext) {
  541. var chart = this.chart, wrapAround = chart.options.accessibility.keyboardNavigation
  542. .wrapAround, highlightSuccessful = chart.highlightAdjacentPoint(directionIsNext);
  543. if (!highlightSuccessful) {
  544. if (wrapAround) {
  545. return handler.init(directionIsNext ? 1 : -1);
  546. }
  547. return handler.response[directionIsNext ? 'next' : 'prev'];
  548. }
  549. return handler.response.success;
  550. },
  551. /**
  552. * @private
  553. */
  554. onSeriesDestroy: function (series) {
  555. var chart = this.chart, currentHighlightedPointDestroyed = chart.highlightedPoint &&
  556. chart.highlightedPoint.series === series;
  557. if (currentHighlightedPointDestroyed) {
  558. delete chart.highlightedPoint;
  559. if (chart.focusElement) {
  560. chart.focusElement.removeFocusBorder();
  561. }
  562. }
  563. },
  564. /**
  565. * @private
  566. */
  567. destroy: function () {
  568. this.eventProvider.removeAddedEvents();
  569. }
  570. });
  571. export default SeriesKeyboardNavigation;