-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent-target.js
55 lines (45 loc) · 1.68 KB
/
event-target.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
module.exports = EventTarget;
var Class = require('./class');
/**
* A browser-based event emitter that takes advantage of the built-in C++ eventing the browser provides, giving a
* consistent eventing mechanism everywhere in your front-end app.
*/
function EventTarget() {
Object.defineProperty(this, '__event_node', { value: document.createDocumentFragment() });
}
Class.extend(EventTarget, {
// Add event listener
addEventListener: function addEventListener(type, listener) {
this.__event_node.addEventListener(type, listener);
},
on: function on(type, listener) {
this.addEventListener(type, listener);
},
// Removes event listener
removeEventListener: function removeEventListener(type, listener) {
this.__event_node.removeEventListener(type, listener);
if (listener && listener.__event_one) {
this.__event_node.removeEventListener(type, listener.__event_one);
}
},
off: function off(type, listener) {
this.removeEventListener(type, listener);
},
// Add event listener to only get called once, returns wrapped method for removing if needed
one: function one(type, listener) {
if (typeof listener !== 'function') return;
if (!listener.__event_one) {
var self = this;
Object.defineProperty(listener, '__event_one', { value: function(event) {
self.removeEventListener(type, listener.__event_one);
listener.call(self, event);
}});
}
this.addEventListener(type, listener.__event_one);
},
// Dispatch event and trigger listeners
dispatchEvent: function dispatchEvent(event) {
Object.defineProperty(event, 'target', { value: this });
return this.__event_node.dispatchEvent(event);
}
});