1 | import { each, get } from '@antv/util';
|
2 | import { getComponents, isInBox } from './action/util';
|
3 |
|
4 |
|
5 |
|
6 | var Context = (function () {
|
7 | function Context(view) {
|
8 |
|
9 | this.actions = [];
|
10 |
|
11 | this.event = null;
|
12 | this.cacheMap = {};
|
13 | this.view = view;
|
14 | }
|
15 | |
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | Context.prototype.cache = function () {
|
22 | var params = [];
|
23 | for (var _i = 0; _i < arguments.length; _i++) {
|
24 | params[_i] = arguments[_i];
|
25 | }
|
26 | if (params.length === 1) {
|
27 | return this.cacheMap[params[0]];
|
28 | }
|
29 | else if (params.length === 2) {
|
30 | this.cacheMap[params[0]] = params[1];
|
31 | }
|
32 | };
|
33 | |
34 |
|
35 |
|
36 |
|
37 | Context.prototype.getAction = function (name) {
|
38 | return this.actions.find(function (action) { return action.name === name; });
|
39 | };
|
40 | |
41 |
|
42 |
|
43 |
|
44 | Context.prototype.addAction = function (action) {
|
45 | this.actions.push(action);
|
46 | };
|
47 | |
48 |
|
49 |
|
50 |
|
51 | Context.prototype.removeAction = function (action) {
|
52 | var actions = this.actions;
|
53 | var index = this.actions.indexOf(action);
|
54 | if (index >= 0) {
|
55 | actions.splice(index, 1);
|
56 | }
|
57 | };
|
58 | |
59 |
|
60 |
|
61 | Context.prototype.getCurrentPoint = function () {
|
62 | var event = this.event;
|
63 | if (event) {
|
64 | if (event.target instanceof HTMLElement) {
|
65 | var canvas = this.view.getCanvas();
|
66 | var point = canvas.getPointByClient(event.clientX, event.clientY);
|
67 | return point;
|
68 | }
|
69 | else {
|
70 | return {
|
71 | x: event.x,
|
72 | y: event.y,
|
73 | };
|
74 | }
|
75 | }
|
76 | return null;
|
77 | };
|
78 | |
79 |
|
80 |
|
81 |
|
82 | Context.prototype.getCurrentShape = function () {
|
83 | return get(this.event, ['gEvent', 'shape']);
|
84 | };
|
85 | /**
|
86 | * 当前的触发是否在 View 内
|
87 | */
|
88 | Context.prototype.isInPlot = function () {
|
89 | var point = this.getCurrentPoint();
|
90 | if (point) {
|
91 | return this.view.isPointInPlot(point);
|
92 | }
|
93 | return false;
|
94 | };
|
95 | |
96 |
|
97 |
|
98 |
|
99 | Context.prototype.isInShape = function (name) {
|
100 | var shape = this.getCurrentShape();
|
101 | if (shape) {
|
102 | return shape.get('name') === name;
|
103 | }
|
104 | return false;
|
105 | };
|
106 | |
107 |
|
108 |
|
109 |
|
110 | Context.prototype.isInComponent = function (name) {
|
111 | var components = getComponents(this.view);
|
112 | var point = this.getCurrentPoint();
|
113 | if (point) {
|
114 | return !!components.find(function (component) {
|
115 | var bbox = component.getBBox();
|
116 | if (name) {
|
117 | return component.get('name') === name && isInBox(bbox, point);
|
118 | }
|
119 | else {
|
120 | return isInBox(bbox, point);
|
121 | }
|
122 | });
|
123 | }
|
124 | return false;
|
125 | };
|
126 | |
127 |
|
128 |
|
129 | Context.prototype.destroy = function () {
|
130 |
|
131 | each(this.actions.slice(), function (action) {
|
132 | action.destroy();
|
133 | });
|
134 | this.view = null;
|
135 | this.event = null;
|
136 | this.actions = null;
|
137 | this.cacheMap = null;
|
138 | };
|
139 | return Context;
|
140 | }());
|
141 | export default Context;
|
142 |
|
\ | No newline at end of file |