KeyboardNavigationHandler.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* *
  2. *
  3. * (c) 2009-2021 Øystein Moseng
  4. *
  5. * Keyboard navigation handler base class definition
  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 U from '../Core/Utilities.js';
  14. var find = U.find;
  15. /**
  16. * Options for the keyboard navigation handler.
  17. *
  18. * @interface Highcharts.KeyboardNavigationHandlerOptionsObject
  19. */ /**
  20. * An array containing pairs of an array of keycodes, mapped to a handler
  21. * function. When the keycode is received, the handler is called with the
  22. * keycode as parameter.
  23. * @name Highcharts.KeyboardNavigationHandlerOptionsObject#keyCodeMap
  24. * @type {Array<Array<Array<number>, Function>>}
  25. */ /**
  26. * Function to run on initialization of module.
  27. * @name Highcharts.KeyboardNavigationHandlerOptionsObject#init
  28. * @type {Function}
  29. */ /**
  30. * Function to run before moving to next/prev module. Receives moving direction
  31. * as parameter: +1 for next, -1 for previous.
  32. * @name Highcharts.KeyboardNavigationHandlerOptionsObject#terminate
  33. * @type {Function|undefined}
  34. */ /**
  35. * Function to run to validate module. Should return false if module should not
  36. * run, true otherwise. Receives chart as parameter.
  37. * @name Highcharts.KeyboardNavigationHandlerOptionsObject#validate
  38. * @type {Function|undefined}
  39. */
  40. /* eslint-disable no-invalid-this, valid-jsdoc */
  41. /**
  42. * Define a keyboard navigation handler for use with a
  43. * Highcharts.AccessibilityComponent instance. This functions as an abstraction
  44. * layer for keyboard navigation, and defines a map of keyCodes to handler
  45. * functions.
  46. *
  47. * @requires module:modules/accessibility
  48. *
  49. * @sample highcharts/accessibility/custom-component
  50. * Custom accessibility component
  51. *
  52. * @class
  53. * @name Highcharts.KeyboardNavigationHandler
  54. *
  55. * @param {Highcharts.Chart} chart
  56. * The chart this module should act on.
  57. *
  58. * @param {Highcharts.KeyboardNavigationHandlerOptionsObject} options
  59. * Options for the keyboard navigation handler.
  60. */
  61. function KeyboardNavigationHandler(chart, options) {
  62. this.chart = chart;
  63. this.keyCodeMap = options.keyCodeMap || [];
  64. this.validate = options.validate;
  65. this.init = options.init;
  66. this.terminate = options.terminate;
  67. // Response enum
  68. this.response = {
  69. success: 1,
  70. prev: 2,
  71. next: 3,
  72. noHandler: 4,
  73. fail: 5 // Handler failed
  74. };
  75. }
  76. KeyboardNavigationHandler.prototype = {
  77. /**
  78. * Find handler function(s) for key code in the keyCodeMap and run it.
  79. *
  80. * @function KeyboardNavigationHandler#run
  81. * @param {global.KeyboardEvent} e
  82. * @return {number} Returns a response code indicating whether the run was
  83. * a success/fail/unhandled, or if we should move to next/prev module.
  84. */
  85. run: function (e) {
  86. var keyCode = e.which || e.keyCode;
  87. var response = this.response.noHandler;
  88. var handlerCodeSet = find(this.keyCodeMap, function (codeSet) {
  89. return codeSet[0].indexOf(keyCode) > -1;
  90. });
  91. if (handlerCodeSet) {
  92. response = handlerCodeSet[1].call(this, keyCode, e);
  93. }
  94. else if (keyCode === 9) {
  95. // Default tab handler, move to next/prev module
  96. response = this.response[e.shiftKey ? 'prev' : 'next'];
  97. }
  98. return response;
  99. }
  100. };
  101. export default KeyboardNavigationHandler;