-
Notifications
You must be signed in to change notification settings - Fork 5
/
common.js
74 lines (50 loc) · 1.4 KB
/
common.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
if(Map === undefined) {
var Map = function() {
this.data = {};
};
Map.prototype.set = function(str, obj) { this.data[str] = obj; };
Map.prototype.get = function(str) { return this.data[str]; };
Map.prototype.has = function(str) { return this.data[str] !== undefined; };
}
var EventHandler = function() {
this._eventListeners = new Map();
}
EventHandler.prototype.attachEventListener = function(event, fn) {
var listeners;
if(this._eventListeners.has(event)) {
listeners = this._eventListeners.get(event);
} else {
listeners = [];
}
listeners.push(fn);
this._eventListeners.set(event, listeners);
};
EventHandler.prototype.detachAllEventListeners = function() {
this._eventListeners = new Map();
};
EventHandler.prototype.detachEventListener = function(event, fn) {
if(fn === undefined) {
this._eventListeners.set(event, []);
return;
}
var listeners;
if(this._eventListeners.has(event)) {
listeners = this._eventListeners.get(event);
} else {
return;
}
var idx = listeners.indexOf(event);
if(idx > -1) {
listeners.slice(idx, 1);
}
this._eventListeners.set(event, listeners);
};
EventHandler.prototype._fireEvent = function(event, value) {
if(this._eventListeners.has(event)) {
var listeners = this._eventListeners.get(event);
for(var i = 0; i < listeners.length; i++) {
if(typeof listeners[i] == "function")
listeners[i](value);
}
}
};