g6/plugins/base.js

50 lines
1.0 KiB
JavaScript
Raw Normal View History

2019-03-20 15:06:36 +08:00
const deepMix = require('@antv/util/lib/deep-mix');
const each = require('@antv/util/lib/each');
const wrapBehavior = require('@antv/util/lib/event/wrap-behavior');
class PluginBase {
constructor(cfgs) {
2019-05-20 17:26:10 +08:00
this._cfgs = deepMix(this.getDefaultCfgs(), cfgs);
2019-03-20 15:06:36 +08:00
}
2019-05-20 17:26:10 +08:00
getDefaultCfgs() {
2019-03-20 15:06:36 +08:00
return {};
}
2019-03-29 10:30:13 +08:00
initPlugin(graph) {
2019-03-20 15:06:36 +08:00
const self = this;
self.set('graph', graph);
const events = self.getEvents();
const bindEvents = {};
2019-03-20 15:06:36 +08:00
each(events, (v, k) => {
const event = wrapBehavior(self, v);
bindEvents[k] = event;
graph.on(k, event);
});
this._events = bindEvents;
2019-03-28 18:07:27 +08:00
this.init();
2019-03-20 15:06:36 +08:00
}
2019-03-28 18:07:27 +08:00
init() {}
2019-03-20 15:06:36 +08:00
getEvents() {
return {};
}
get(key) {
return this._cfgs[key];
}
set(key, val) {
this._cfgs[key] = val;
}
2019-03-29 10:30:13 +08:00
destroy() {}
destroyPlugin() {
this.destroy();
2019-03-20 15:06:36 +08:00
const graph = this.get('graph');
const events = this._events;
each(events, (v, k) => {
2019-03-20 15:06:36 +08:00
graph.off(k, v);
});
this._events = null;
this._cfgs = null;
this.destroyed = true;
}
}
module.exports = PluginBase;