source: trip-planner-front/node_modules/sockjs-client/lib/event/eventtarget.js@ 6a80231

Last change on this file since 6a80231 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.8 KB
RevLine 
[6a3a178]1'use strict';
2
3/* Simplified implementation of DOM2 EventTarget.
4 * http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventTarget
5 */
6
7function EventTarget() {
8 this._listeners = {};
9}
10
11EventTarget.prototype.addEventListener = function(eventType, listener) {
12 if (!(eventType in this._listeners)) {
13 this._listeners[eventType] = [];
14 }
15 var arr = this._listeners[eventType];
16 // #4
17 if (arr.indexOf(listener) === -1) {
18 // Make a copy so as not to interfere with a current dispatchEvent.
19 arr = arr.concat([listener]);
20 }
21 this._listeners[eventType] = arr;
22};
23
24EventTarget.prototype.removeEventListener = function(eventType, listener) {
25 var arr = this._listeners[eventType];
26 if (!arr) {
27 return;
28 }
29 var idx = arr.indexOf(listener);
30 if (idx !== -1) {
31 if (arr.length > 1) {
32 // Make a copy so as not to interfere with a current dispatchEvent.
33 this._listeners[eventType] = arr.slice(0, idx).concat(arr.slice(idx + 1));
34 } else {
35 delete this._listeners[eventType];
36 }
37 return;
38 }
39};
40
41EventTarget.prototype.dispatchEvent = function() {
42 var event = arguments[0];
43 var t = event.type;
44 // equivalent of Array.prototype.slice.call(arguments, 0);
45 var args = arguments.length === 1 ? [event] : Array.apply(null, arguments);
46 // TODO: This doesn't match the real behavior; per spec, onfoo get
47 // their place in line from the /first/ time they're set from
48 // non-null. Although WebKit bumps it to the end every time it's
49 // set.
50 if (this['on' + t]) {
51 this['on' + t].apply(this, args);
52 }
53 if (t in this._listeners) {
54 // Grab a reference to the listeners list. removeEventListener may alter the list.
55 var listeners = this._listeners[t];
56 for (var i = 0; i < listeners.length; i++) {
57 listeners[i].apply(this, args);
58 }
59 }
60};
61
62module.exports = EventTarget;
Note: See TracBrowser for help on using the repository browser.