3 exports.__esModule = true;
5 var _events = require('./utils/events.js');
7 var Events = _interopRequireWildcard(_events);
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; } }
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`.
16 * @see [EventTarget Spec]{@link https://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventTarget}
19 var EventTarget = function EventTarget() {};
24 * @typedef {Object} EventTarget~Event
25 * @see [Properties]{@link https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent}
29 * All event listeners should follow the following format.
31 * @callback EventTarget~EventListener
34 * @param {EventTarget~Event} event
35 * the event that triggered this function
37 * @param {Object} [hash]
38 * hash of data sent during the event
42 * An object containing event names as keys and booleans as values.
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.
47 * @property EventTarget.prototype.allowedEvents_
51 * @file src/js/event-target.js
53 EventTarget.prototype.allowedEvents_ = {};
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.
59 * @param {string|string[]} type
60 * An event name or an array of event names.
62 * @param {EventTarget~EventListener} fn
63 * The function to call with `EventTarget`s
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;
70 this.addEventListener = function () {};
71 Events.on(this, type, fn);
72 this.addEventListener = ael;
76 * An alias of {@link EventTarget#on}. Allows `EventTarget` to mimic
77 * the standard DOM API.
80 * @see {@link EventTarget#on}
82 EventTarget.prototype.addEventListener = EventTarget.prototype.on;
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.
89 * @param {string|string[]} type
90 * An event name or an array of event names.
92 * @param {EventTarget~EventListener} fn
93 * The function to remove.
95 EventTarget.prototype.off = function (type, fn) {
96 Events.off(this, type, fn);
100 * An alias of {@link EventTarget#off}. Allows `EventTarget` to mimic
101 * the standard DOM API.
104 * @see {@link EventTarget#off}
106 EventTarget.prototype.removeEventListener = EventTarget.prototype.off;
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.
113 * @param {string|string[]} type
114 * An event name or an array of event names.
116 * @param {EventTarget~EventListener} fn
117 * The function to be called once for each event name.
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;
124 this.addEventListener = function () {};
125 Events.one(this, type, fn);
126 this.addEventListener = ael;
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.
134 * If the name of the `Event` that is being triggered is in `EventTarget.allowedEvents_`.
135 * Trigger will also call the `on` + `uppercaseEventName` function.
138 * 'click' is in `EventTarget.allowedEvents_`, so, trigger will attempt to call
139 * `onClick` if it exists.
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
145 EventTarget.prototype.trigger = function (event) {
146 var type = event.type || event;
148 if (typeof event === 'string') {
149 event = { type: type };
151 event = Events.fixEvent(event);
153 if (this.allowedEvents_[type] && this['on' + type]) {
154 this['on' + type](event);
157 Events.trigger(this, event);
161 * An alias of {@link EventTarget#trigger}. Allows `EventTarget` to mimic
162 * the standard DOM API.
165 * @see {@link EventTarget#trigger}
167 EventTarget.prototype.dispatchEvent = EventTarget.prototype.trigger;
169 exports['default'] = EventTarget;