ui.keyboard_processor.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * DevExtreme (ui/widget/ui.keyboard_processor.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var _renderer = require("../../core/renderer");
  11. var _renderer2 = _interopRequireDefault(_renderer);
  12. var _events_engine = require("../../events/core/events_engine");
  13. var _events_engine2 = _interopRequireDefault(_events_engine);
  14. var _class = require("../../core/class");
  15. var _class2 = _interopRequireDefault(_class);
  16. var _array = require("../../core/utils/array");
  17. var _iterator = require("../../core/utils/iterator");
  18. var _utils = require("../../events/utils");
  19. function _interopRequireDefault(obj) {
  20. return obj && obj.__esModule ? obj : {
  21. "default": obj
  22. }
  23. }
  24. var COMPOSITION_START_EVENT = "compositionstart";
  25. var COMPOSITION_END_EVENT = "compositionend";
  26. var KEYDOWN_EVENT = "keydown";
  27. var NAMESPACE = "KeyboardProcessor";
  28. var KeyboardProcessor = _class2.default.inherit({
  29. _keydown: (0, _utils.addNamespace)(KEYDOWN_EVENT, NAMESPACE),
  30. _compositionStart: (0, _utils.addNamespace)(COMPOSITION_START_EVENT, NAMESPACE),
  31. _compositionEnd: (0, _utils.addNamespace)(COMPOSITION_END_EVENT, NAMESPACE),
  32. ctor: function(options) {
  33. var _this = this;
  34. options = options || {};
  35. if (options.element) {
  36. this._element = (0, _renderer2.default)(options.element)
  37. }
  38. if (options.focusTarget) {
  39. this._focusTarget = options.focusTarget
  40. }
  41. this._handler = options.handler;
  42. this._context = options.context;
  43. this._childProcessors = [];
  44. if (this._element) {
  45. this._processFunction = function(e) {
  46. var isNotFocusTarget = _this._focusTarget && _this._focusTarget !== e.target && (0, _array.inArray)(e.target, _this._focusTarget) < 0;
  47. var shouldSkipProcessing = _this._isComposingJustFinished && 229 === e.which || _this._isComposing || isNotFocusTarget;
  48. _this._isComposingJustFinished = false;
  49. if (!shouldSkipProcessing) {
  50. _this.process(e)
  51. }
  52. };
  53. this._toggleProcessingWithContext = this.toggleProcessing.bind(this);
  54. _events_engine2.default.on(this._element, this._keydown, this._processFunction);
  55. _events_engine2.default.on(this._element, this._compositionStart, this._toggleProcessingWithContext);
  56. _events_engine2.default.on(this._element, this._compositionEnd, this._toggleProcessingWithContext)
  57. }
  58. },
  59. dispose: function() {
  60. if (this._element) {
  61. _events_engine2.default.off(this._element, this._keydown, this._processFunction);
  62. _events_engine2.default.off(this._element, this._compositionStart, this._toggleProcessingWithContext);
  63. _events_engine2.default.off(this._element, this._compositionEnd, this._toggleProcessingWithContext)
  64. }
  65. this._element = void 0;
  66. this._handler = void 0;
  67. this._context = void 0;
  68. this._childProcessors = void 0
  69. },
  70. clearChildren: function() {
  71. this._childProcessors = []
  72. },
  73. push: function(child) {
  74. if (!this._childProcessors) {
  75. this.clearChildren()
  76. }
  77. this._childProcessors.push(child);
  78. return child
  79. },
  80. attachChildProcessor: function() {
  81. var childProcessor = new KeyboardProcessor;
  82. this._childProcessors.push(childProcessor);
  83. return childProcessor
  84. },
  85. reinitialize: function(childHandler, childContext) {
  86. this._context = childContext;
  87. this._handler = childHandler;
  88. return this
  89. },
  90. process: function(e) {
  91. var args = {
  92. keyName: (0, _utils.normalizeKeyName)(e),
  93. key: e.key,
  94. code: e.code,
  95. ctrl: e.ctrlKey,
  96. location: e.location,
  97. metaKey: e.metaKey,
  98. shift: e.shiftKey,
  99. alt: e.altKey,
  100. which: e.which,
  101. originalEvent: e
  102. };
  103. var handlerResult = this._handler && this._handler.call(this._context, args);
  104. if (handlerResult && this._childProcessors) {
  105. (0, _iterator.each)(this._childProcessors, function(index, childProcessor) {
  106. childProcessor.process(e)
  107. })
  108. }
  109. },
  110. toggleProcessing: function(_ref) {
  111. var type = _ref.type;
  112. this._isComposing = type === COMPOSITION_START_EVENT;
  113. this._isComposingJustFinished = !this._isComposing
  114. }
  115. });
  116. module.exports = KeyboardProcessor;