44 lines
984 B
JavaScript
Raw Normal View History

2021-07-16 11:41:58 -03:00
const Events = new Class({
2021-10-04 17:05:34 -07:00
$events: {},
2021-07-16 11:41:58 -03:00
2021-10-04 17:05:34 -07:00
_removeOn(string) {
return string.replace(/^on([A-Z])/, (full, first) => first.toLowerCase());
},
2021-07-16 11:41:58 -03:00
2021-10-04 17:05:34 -07:00
addEvent(type, fn, internal) {
type = this._removeOn(type);
2021-07-16 11:41:58 -03:00
2021-10-04 17:05:34 -07:00
this.$events[type] = (this.$events[type] || []).include(fn);
if (internal) fn.internal = true;
return this;
},
2021-07-16 11:41:58 -03:00
2021-10-04 17:05:34 -07:00
fireEvent(type, args, delay) {
type = this._removeOn(type);
const events = this.$events[type];
if (!events) return this;
args = Array.isArray(args) ? args : [args];
_.each(
events,
function (fn) {
if (delay) fn.delay(delay, this, args);
else fn.apply(this, args);
},
this,
);
return this;
},
2021-07-16 11:41:58 -03:00
2021-10-04 17:05:34 -07:00
removeEvent(type, fn) {
type = this._removeOn(type);
const events = this.$events[type];
if (events && !fn.internal) {
const index = events.indexOf(fn);
if (index != -1) events.splice(index, 1);
}
return this;
},
2021-07-16 11:41:58 -03:00
});
export default Events;