forked from Stuffpicker/XTriggerEvent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXTriggerEvent.js
More file actions
186 lines (171 loc) · 6.77 KB
/
XTriggerEvent.js
File metadata and controls
186 lines (171 loc) · 6.77 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
export default class XTriggerEvent{
/**
* private global keeper
*/
#O={removedCEvent:{}}
/**
* Add listener to custom event.
* Should be called before registration of the custom event.
* @param {String} ev_name
* @param {Function} cb
* @returns XTriggerEvent
*/
on(ev_name,cb){
cb = cb || function(){}
typeof cb =='function'?
'':
(cb = function(){},
console.error("\nThis.triggerOn(arg1,arg2): second arg2 should be a callback function!"))
if( !!this.#O[ev_name])
{ console.log("__",this.#O[ev_name]["cb"])
if(!!this.#O[ev_name]["cb"]?.includes(cb))
{console.warn("\nIn this.triggerOn(arg1,arg2): the second arg2 callback already exists!") }
else{this.#O[ev_name]["cb"].push(cb); console.log("__",this.#O[ev_name]["cb"])}
}else{
if(Object.keys(this.#O.removedCEvent).includes(ev_name)){
}else{
this.#O[ev_name]={cb:[cb]};
}
}
return this;
}
triggerOn = this.on
addEventListener = this.on
/**
* Register and dispatch custom event.
* You can use this.registerDispatchEvent(ev_name,argOpt). They are equals.
* @param {String} ev_name
* @param {Any} argOpt
* @returns this
*/
registerEvent(ev_name,argOpt){
argOpt = argOpt || ''
if(!!this.#O[ev_name]){
//test and registerEvent my custom event
this.#O[ev_name]["argOpt"]=argOpt
this.#O[ev_name]["cb"]?this.#O[ev_name]["cb"]:this.#O[ev_name]["cb"]=[function(){}]
//test and dispatch it
if(!!this.#O[ev_name]['cb']){
if(this.#O[ev_name]['cb'].length>0){
let cbs=this.#O[ev_name]['cb'] ;
cbs.forEach(cb => {
cb(this.#O[ev_name]["argOpt"]);
});
}else{console.warn(new Error("\nNo callback available for this customed event '"+ev_name+"'."))}
}else{console.warn(new Error("\nTest for dispatching fails for '"+ ev_name + "'. Check it!"))}
}else{
if(Object.keys(this.#O.removedCEvent).includes(ev_name)){
console.warn("\nThis custom event '"+ ev_name + "' not available. May be already removed. Check it!")
}else{
this.#O[ev_name]["argOpt"]=argOpt
this.#O[ev_name]["cb"] ? this.#O[ev_name]["cb"] : this.#O[ev_name]["cb"]=[function(){}]
this.#O[ev_name]["cb"].length>0 ? (this.#O[ev_name]['cb'].forEach(cb => {
cb(this.#O[ev_name]["argOpt"]);
})) : ""
}
}
console.table(this)
return this;
}
/**
* is the clone of this.registerEvent. They are the same!
*/
registerDispatchEvent=this.registerEvent
/**
* use it with very strong care!
* this.dispatchEvent(ev_name) should be called by chaining or separately!
* @param {String} ev_name
* @param {Any} argOpt
* @returns this
*/
registerEventSkipDispatch(ev_name,argOpt){
argOpt = argOpt || ''
console.log(ev_name)
if(!!this.#O[ev_name]){
this.#O[ev_name]["argOpt"]=argOpt
this.#O[ev_name]["cb"]=function(){}
}else{
this.#O[ev_name]["argOpt"]=argOpt
this.#O[ev_name]["cb"]=function(){}
}
return this;
}
/**
* Dispatch this event_name. To be used with strng care!
* @param {String} ev_name
* @returns this
*/
dispatchEvent(ev_name){
if(!!this.#O[ev_name]){
//test and dispatch it
if(!!this.#O[ev_name]['cb']){
if(this.#O[ev_name]['cb'].length>0){
let cbs=this.#O[ev_name]['cb'] ;
cbs.forEach(cb => {
cb(this.#O[ev_name]["argOpt"]??"")
});
}
}else{console.warn("\nDispatch fails for this cust. event '" +ev_name +"'." )}
}else{console.warn("\nThis cust. event " +ev_name +" no exists!")}
return this;
}
/**
* Remove a callback linked from this custom event. It detaches a callback from a custom event.
* Note the difference with unRegisterEvent(event_name).
* unRegisterEvent(event_name) remove custome event Object including linked callback.
* @param {String} ev_name
* @param {Function} cb
* @returns this
*/
unDispatchEvent(ev_name,cb){
cb=cb||null
ev_name = ev_name || null
if(!!this.#O[ev_name]){
if(typeof cb =='function'){
this.#O[ev_name]['cb'] && this.#O[ev_name]['cb'].length>0?
this.#O[ev_name]['cb']=filter(cb_ => {cb_ !=cb }):
""
}else{console.error("\nThis.unDispatchEvent(arg1,arg2): second arg2 should be a callback function!")}
}else{console.error("\nThis"+ev_name+" no exist!")}
return this;
}
/**
* Delete a registered custom event or Array of custom events.
* @param {String|String[]} ev_name or [ev_name]
* @returns this
*/
unRegisterEvent(ev_name){
if(typeof ev_name ==="string"){ev_name=[ev_name]}
if(Array.isArray(ev_name)){
ev_name.forEach(ev=>{
let unr=Object.assign({},this.#O[ev]);
delete this.#O[ev];
console.info("\nEvent '"+ev+"' removed with success!");
this.#O.removedCEvent[ev]=unr;
})
//!!this.#O[ev_name]? console.info("\nThis Event ",ev_name," do not exists!");
}else{throw new Error("\n '",ev_name, "' only a String or Array as argument!")}
console.log("--table: "); console.table(this.#O.removedCEvent[ev_name]);
console.table(this.#O); console.log(this.#O);
return this;
}
/**
* Restore a removed custom event or Array of custom events.
* @param {String|String[]} ev_name or [ev_name]
* @returns this
*/
restoreEvent(ev_name){
console.table(this.#O.removedCEvent[ev_name])
if(typeof ev_name ==="string"){ev_name=[ev_name]}
if(Array.isArray(ev_name)){
ev_name.forEach(ev=>{
let unr = Object.assign({}, this.#O.removedCEvent[ev]);
delete this.#O.removedCEvent[ev];
this.#O[ev]=unr;
console.info("\nEvent '"+ev+"' restored with success!");
})
//!!this.#O[ev_name]? console.info("\nThis Event ",ev_name," do not exists!");
}else{throw new Error("\n '",ev_name,"' only a String or Array as argument!")}
return this;
}
}