domeventdata.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/view/observer/domeventdata
  7. */
  8. import { extend } from 'lodash-es';
  9. /**
  10. * Information about a DOM event in context of the {@link module:engine/view/document~Document}.
  11. * It wraps the native event, which usually should not be used as the wrapper contains
  12. * additional data (like key code for keyboard events).
  13. */
  14. export default class DomEventData {
  15. /**
  16. * @param {module:engine/view/view~View} view The instance of the view controller.
  17. * @param {Event} domEvent The DOM event.
  18. * @param {Object} [additionalData] Additional properties that the instance should contain.
  19. */
  20. constructor( view, domEvent, additionalData ) {
  21. /**
  22. * Instance of the view controller.
  23. *
  24. * @readonly
  25. * @member {module:engine/view/view~View} module:engine/view/observer/observer~Observer.DomEvent#view
  26. */
  27. this.view = view;
  28. /**
  29. * The instance of the document.
  30. *
  31. * @readonly
  32. * @member {module:engine/view/document~Document} module:engine/view/observer/observer~Observer.DomEvent#document
  33. */
  34. this.document = view.document;
  35. /**
  36. * The DOM event.
  37. *
  38. * @readonly
  39. * @member {Event} module:engine/view/observer/observer~Observer.DomEvent#domEvent
  40. */
  41. this.domEvent = domEvent;
  42. /**
  43. * The DOM target.
  44. *
  45. * @readonly
  46. * @member {HTMLElement} module:engine/view/observer/observer~Observer.DomEvent#target
  47. */
  48. this.domTarget = domEvent.target;
  49. extend( this, additionalData );
  50. }
  51. /**
  52. * The tree view element representing the target.
  53. *
  54. * @readonly
  55. * @type module:engine/view/element~Element
  56. */
  57. get target() {
  58. return this.view.domConverter.mapDomToView( this.domTarget );
  59. }
  60. /**
  61. * Prevents the native's event default action.
  62. */
  63. preventDefault() {
  64. this.domEvent.preventDefault();
  65. }
  66. /**
  67. * Stops native event propagation.
  68. */
  69. stopPropagation() {
  70. this.domEvent.stopPropagation();
  71. }
  72. }