wisemapping-frontend/packages/mindplot/lib/components/Events.js

44 lines
984 B
JavaScript
Raw Normal View History

2021-07-16 16:41:58 +02:00
const Events = new Class({
2021-10-05 02:05:34 +02:00
$events: {},
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
_removeOn(string) {
return string.replace(/^on([A-Z])/, (full, first) => first.toLowerCase());
},
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
addEvent(type, fn, internal) {
type = this._removeOn(type);
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
this.$events[type] = (this.$events[type] || []).include(fn);
if (internal) fn.internal = true;
return this;
},
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02: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 16:41:58 +02:00
2021-10-05 02:05:34 +02: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 16:41:58 +02:00
});
export default Events;