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