UNPKG

1.52 kBJavaScriptView Raw
1const G = require('@antv/g/lib');
2const CommonUtil = G.CommonUtil;
3
4function bindInteraction(Lib, View) {
5 // binding on Library
6 Lib._Interactions = {};
7 Lib.registerInteraction = function(type, constructor) {
8 Lib._Interactions[type] = constructor;
9 };
10 Lib.getInteraction = function(type) {
11 return G2._Interactions[type];
12 };
13
14 // binding on View
15 View.prototype.getInteractions = function() {
16 const me = this;
17 if (!me._interactions) {
18 me._interactions = {};
19 }
20 return me._interactions;
21 };
22 View.prototype._setInteraction = function(type, interaction) {
23 const me = this;
24 const interactions = me.getInteractions();
25 if (interactions[type] && interactions[type] !== interaction) { // only one interaction for a key
26 interactions[type].destroy();
27 }
28 interactions[type] = interaction;
29 };
30 View.prototype.clearInteraction = function(type) {
31 const me = this;
32 const interactions = me.getInteractions();
33 if (type) {
34 interactions[type] && interactions[type].destroy();
35 delete interactions[type];
36 } else {
37 CommonUtil.each(interactions, (interaction, key) => {
38 interaction.destroy();
39 delete interactions[key];
40 });
41 }
42 };
43 View.prototype.interact = View.prototype.interaction = function(type, cfg) {
44 const me = this;
45 const Ctor = G2.getInteraction(type);
46 const interaction = new Ctor(cfg, me);
47 me._setInteraction(type, interaction);
48 return me;
49 };
50}
51
52module.exports = bindInteraction;