forked from Tencent/vConsole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.js
69 lines (60 loc) · 1.23 KB
/
plugin.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
/**
* vConsole Plugin Class
*
* @author WechatFE
*/
class VConsolePlugin {
constructor(id, name = 'newPlugin') {
this.id = id;
this.name = name;
this.eventList = {};
}
get id() {
return this._id;
}
set id(value) {
if (!value) {
throw 'Plugin ID cannot be empty';
}
this._id = value.toLowerCase();
}
get name() {
return this._name;
}
set name(value) {
if (!value) {
throw 'Plugin name cannot be empty';
}
this._name = value;
}
/**
* register an event
* @public
* @param string
* @param function
*/
on(eventName, callback) {
this.eventList[eventName] = callback;
return this;
}
/**
* trigger an event
* @public
* @param string
* @param mixed
*/
trigger(eventName, data) {
if (typeof this.eventList[eventName] === 'function') {
// registered by `.on()` method
this.eventList[eventName].call(this, data);
} else {
// registered by `.onXxx()` method
let method = 'on' + eventName.charAt(0).toUpperCase() + eventName.slice(1);
if (typeof this[method] === 'function') {
this[method].call(this, data);
}
}
return this;
}
} // END class
export default VConsolePlugin;