You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
devdocs/assets/javascripts/lib/events.js

78 lines
2.0 KiB

// TODO: This file was created by bulk-decaffeinate.
// Sanity-check the conversion and remove this comment.
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS104: Avoid inline assignments
* DS207: Consider shorter variations of null checks
* DS208: Avoid top-level this
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/
class Events {
on(event, callback) {
if (event.indexOf(" ") >= 0) {
for (var name of Array.from(event.split(" "))) {
this.on(name, callback);
}
} else {
let base;
((base =
this._callbacks != null ? this._callbacks : (this._callbacks = {}))[
event
] != null
? base[event]
: (base[event] = [])
).push(callback);
}
return this;
}
off(event, callback) {
let callbacks, index;
if (event.indexOf(" ") >= 0) {
for (var name of Array.from(event.split(" "))) {
this.off(name, callback);
}
} else if (
(callbacks =
this._callbacks != null ? this._callbacks[event] : undefined) &&
(index = callbacks.indexOf(callback)) >= 0
) {
callbacks.splice(index, 1);
if (!callbacks.length) {
delete this._callbacks[event];
}
}
return this;
}
trigger(event, ...args) {
let callbacks;
this.eventInProgress = { name: event, args };
if (
(callbacks = this._callbacks != null ? this._callbacks[event] : undefined)
) {
for (var callback of Array.from(callbacks.slice(0))) {
if (typeof callback === "function") {
callback(...Array.from(args || []));
}
}
}
this.eventInProgress = null;
if (event !== "all") {
this.trigger("all", event, ...Array.from(args));
}
return this;
}
removeEvent(event) {
if (this._callbacks != null) {
for (var name of Array.from(event.split(" "))) {
delete this._callbacks[name];
}
}
return this;
}
}