Source: lib/util/fake_event.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.util.FakeEvent');
  18. /**
  19. * Create an Event work-alike object based on the provided dictionary.
  20. * The event should contain all of the same properties from the dict.
  21. *
  22. * @param {string} type
  23. * @param {Object=} opt_dict
  24. * @constructor
  25. * @extends {Event}
  26. */
  27. shaka.util.FakeEvent = function(type, opt_dict) {
  28. // Take properties from dict if present.
  29. let dict = opt_dict || {};
  30. for (let key in dict) {
  31. this[key] = dict[key];
  32. }
  33. // The properties below cannot be set by the dict. They are all provided for
  34. // compatibility with native events.
  35. /** @const {boolean} */
  36. this.bubbles = false;
  37. /** @type {boolean} */
  38. this.cancelable = false;
  39. /** @type {boolean} */
  40. this.defaultPrevented = false;
  41. /**
  42. * According to MDN, Chrome uses high-res timers instead of epoch time.
  43. * Follow suit so that timeStamps on FakeEvents use the same base as
  44. * on native Events.
  45. * @const {number}
  46. * @see https://developer.mozilla.org/en-US/docs/Web/API/Event/timeStamp
  47. */
  48. this.timeStamp = window.performance && window.performance.now ?
  49. window.performance.now() : Date.now();
  50. /** @const {string} */
  51. this.type = type;
  52. /** @const {boolean} */
  53. this.isTrusted = false;
  54. /** @type {EventTarget} */
  55. this.currentTarget = null;
  56. /** @type {EventTarget} */
  57. this.target = null;
  58. /**
  59. * Non-standard property read by FakeEventTarget to stop processing listeners.
  60. * @type {boolean}
  61. */
  62. this.stopped = false;
  63. };
  64. /**
  65. * Prevents the default action of the event. Has no effect if the event isn't
  66. * cancellable.
  67. * @override
  68. */
  69. shaka.util.FakeEvent.prototype.preventDefault = function() {
  70. if (this.cancelable) {
  71. this.defaultPrevented = true;
  72. }
  73. };
  74. /**
  75. * Stops processing event listeners for this event. Provided for compatibility
  76. * with native Events.
  77. * @override
  78. */
  79. shaka.util.FakeEvent.prototype.stopImmediatePropagation = function() {
  80. this.stopped = true;
  81. };
  82. /**
  83. * Does nothing, since FakeEvents do not bubble. Provided for compatibility
  84. * with native Events.
  85. * @override
  86. */
  87. shaka.util.FakeEvent.prototype.stopPropagation = function() {};