wisemapping-frontend/packages/mindplot/lib/components/Events.js
2021-09-29 17:24:07 -03:00

46 lines
1.2 KiB
JavaScript

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;
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;
},
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;
},
});
export default Events;