d0e70de09f9ebdc7a27595500357fffb6ac339ef
[yaffs-website] / node_modules / video.js / es5 / event-target.js
1 'use strict';
2
3 exports.__esModule = true;
4
5 var _events = require('./utils/events.js');
6
7 var Events = _interopRequireWildcard(_events);
8
9 function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }
10
11 /**
12  * `EventTarget` is a class that can have the same API as the DOM `EventTarget`. It
13  * adds shorthand functions that wrap around lengthy functions. For example:
14  * the `on` function is a wrapper around `addEventListener`.
15  *
16  * @see [EventTarget Spec]{@link https://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventTarget}
17  * @class EventTarget
18  */
19 var EventTarget = function EventTarget() {};
20
21 /**
22  * A Custom DOM event.
23  *
24  * @typedef {Object} EventTarget~Event
25  * @see [Properties]{@link https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent}
26  */
27
28 /**
29  * All event listeners should follow the following format.
30  *
31  * @callback EventTarget~EventListener
32  * @this {EventTarget}
33  *
34  * @param {EventTarget~Event} event
35  *        the event that triggered this function
36  *
37  * @param {Object} [hash]
38  *        hash of data sent during the event
39  */
40
41 /**
42  * An object containing event names as keys and booleans as values.
43  *
44  * > NOTE: If an event name is set to a true value here {@link EventTarget#trigger}
45  *         will have extra functionality. See that function for more information.
46  *
47  * @property EventTarget.prototype.allowedEvents_
48  * @private
49  */
50 /**
51  * @file src/js/event-target.js
52  */
53 EventTarget.prototype.allowedEvents_ = {};
54
55 /**
56  * Adds an `event listener` to an instance of an `EventTarget`. An `event listener` is a
57  * function that will get called when an event with a certain name gets triggered.
58  *
59  * @param {string|string[]} type
60  *        An event name or an array of event names.
61  *
62  * @param {EventTarget~EventListener} fn
63  *        The function to call with `EventTarget`s
64  */
65 EventTarget.prototype.on = function (type, fn) {
66   // Remove the addEventListener alias before calling Events.on
67   // so we don't get into an infinite type loop
68   var ael = this.addEventListener;
69
70   this.addEventListener = function () {};
71   Events.on(this, type, fn);
72   this.addEventListener = ael;
73 };
74
75 /**
76  * An alias of {@link EventTarget#on}. Allows `EventTarget` to mimic
77  * the standard DOM API.
78  *
79  * @function
80  * @see {@link EventTarget#on}
81  */
82 EventTarget.prototype.addEventListener = EventTarget.prototype.on;
83
84 /**
85  * Removes an `event listener` for a specific event from an instance of `EventTarget`.
86  * This makes it so that the `event listener` will no longer get called when the
87  * named event happens.
88  *
89  * @param {string|string[]} type
90  *        An event name or an array of event names.
91  *
92  * @param {EventTarget~EventListener} fn
93  *        The function to remove.
94  */
95 EventTarget.prototype.off = function (type, fn) {
96   Events.off(this, type, fn);
97 };
98
99 /**
100  * An alias of {@link EventTarget#off}. Allows `EventTarget` to mimic
101  * the standard DOM API.
102  *
103  * @function
104  * @see {@link EventTarget#off}
105  */
106 EventTarget.prototype.removeEventListener = EventTarget.prototype.off;
107
108 /**
109  * This function will add an `event listener` that gets triggered only once. After the
110  * first trigger it will get removed. This is like adding an `event listener`
111  * with {@link EventTarget#on} that calls {@link EventTarget#off} on itself.
112  *
113  * @param {string|string[]} type
114  *        An event name or an array of event names.
115  *
116  * @param {EventTarget~EventListener} fn
117  *        The function to be called once for each event name.
118  */
119 EventTarget.prototype.one = function (type, fn) {
120   // Remove the addEventListener alialing Events.on
121   // so we don't get into an infinite type loop
122   var ael = this.addEventListener;
123
124   this.addEventListener = function () {};
125   Events.one(this, type, fn);
126   this.addEventListener = ael;
127 };
128
129 /**
130  * This function causes an event to happen. This will then cause any `event listeners`
131  * that are waiting for that event, to get called. If there are no `event listeners`
132  * for an event then nothing will happen.
133  *
134  * If the name of the `Event` that is being triggered is in `EventTarget.allowedEvents_`.
135  * Trigger will also call the `on` + `uppercaseEventName` function.
136  *
137  * Example:
138  * 'click' is in `EventTarget.allowedEvents_`, so, trigger will attempt to call
139  * `onClick` if it exists.
140  *
141  * @param {string|EventTarget~Event|Object} event
142  *        The name of the event, an `Event`, or an object with a key of type set to
143  *        an event name.
144  */
145 EventTarget.prototype.trigger = function (event) {
146   var type = event.type || event;
147
148   if (typeof event === 'string') {
149     event = { type: type };
150   }
151   event = Events.fixEvent(event);
152
153   if (this.allowedEvents_[type] && this['on' + type]) {
154     this['on' + type](event);
155   }
156
157   Events.trigger(this, event);
158 };
159
160 /**
161  * An alias of {@link EventTarget#trigger}. Allows `EventTarget` to mimic
162  * the standard DOM API.
163  *
164  * @function
165  * @see {@link EventTarget#trigger}
166  */
167 EventTarget.prototype.dispatchEvent = EventTarget.prototype.trigger;
168
169 exports['default'] = EventTarget;