-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
emitter.js
86 lines (77 loc) · 2.24 KB
/
emitter.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
75
76
77
78
79
80
81
82
83
84
85
86
// The Emitter class provides the ability to call `.on()` on Dropzone to listen
// to events.
// It is strongly based on component's emitter class, and I removed the
// functionality because of the dependency hell with different frameworks.
export default class Emitter {
// Add an event listener for given event
on(event, fn) {
this._callbacks = this._callbacks || {};
// Create namespace for this event
if (!this._callbacks[event]) {
this._callbacks[event] = [];
}
this._callbacks[event].push(fn);
return this;
}
emit(event, ...args) {
this._callbacks = this._callbacks || {};
let callbacks = this._callbacks[event];
if (callbacks) {
for (let callback of callbacks) {
callback.apply(this, args);
}
}
// trigger a corresponding DOM event
if (this.element) {
this.element.dispatchEvent(
this.makeEvent("dropzone:" + event, { args: args })
);
}
return this;
}
makeEvent(eventName, detail) {
let params = { bubbles: true, cancelable: true, detail: detail };
if (typeof window.CustomEvent === "function") {
return new CustomEvent(eventName, params);
} else {
// IE 11 support
// https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent
var evt = document.createEvent("CustomEvent");
evt.initCustomEvent(
eventName,
params.bubbles,
params.cancelable,
params.detail
);
return evt;
}
}
// Remove event listener for given event. If fn is not provided, all event
// listeners for that event will be removed. If neither is provided, all
// event listeners will be removed.
off(event, fn) {
if (!this._callbacks || arguments.length === 0) {
this._callbacks = {};
return this;
}
// specific event
let callbacks = this._callbacks[event];
if (!callbacks) {
return this;
}
// remove all handlers
if (arguments.length === 1) {
delete this._callbacks[event];
return this;
}
// remove specific handler
for (let i = 0; i < callbacks.length; i++) {
let callback = callbacks[i];
if (callback === fn) {
callbacks.splice(i, 1);
break;
}
}
return this;
}
}