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 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. var dict = opt_dict || {};
  30. for (var key in dict) {
  31. this[key] = dict[key];
  32. }
  33. // These Properties below cannot be set by dict. They are all provided for
  34. // compatibility with native events.
  35. /** @const {boolean} */
  36. this.bubbles = false;
  37. /** @const {boolean} */
  38. this.cancelable = false;
  39. /** @const {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() : Date.now();
  49. /** @const {string} */
  50. this.type = type;
  51. /** @const {boolean} */
  52. this.isTrusted = false;
  53. /** @type {EventTarget} */
  54. this.currentTarget = null;
  55. /** @type {EventTarget} */
  56. this.target = null;
  57. /**
  58. * Non-standard property read by FakeEventTarget to stop processing listeners.
  59. * @type {boolean}
  60. */
  61. this.stopped = false;
  62. };
  63. /**
  64. * Does nothing, since FakeEvents have no default. Provided for compatibility
  65. * with native Events.
  66. */
  67. shaka.util.FakeEvent.prototype.preventDefault = function() {};
  68. /**
  69. * Stops processing event listeners for this event. Provided for compatibility
  70. * with native Events.
  71. */
  72. shaka.util.FakeEvent.prototype.stopImmediatePropagation = function() {
  73. this.stopped = true;
  74. };
  75. /**
  76. * Does nothing, since FakeEvents do not bubble. Provided for compatibility
  77. * with native Events.
  78. */
  79. shaka.util.FakeEvent.prototype.stopPropagation = function() {};