UNPKG

4.07 kBJavaScriptView Raw
1import { each, get } from '@antv/util';
2import { getComponents, isInBox } from './action/util';
3/**
4 * 交互的上下文
5 */
6var Context = /** @class */ (function () {
7 function Context(view) {
8 /** 当前所有的 Action */
9 this.actions = [];
10 /** 当前事件对象 */
11 this.event = null;
12 this.cacheMap = {};
13 this.view = view;
14 }
15 /**
16 * 缓存信息
17 * @param params 缓存的字段
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 * 获取 Action
35 * @param name Action 的名称
36 */
37 Context.prototype.getAction = function (name) {
38 return this.actions.find(function (action) { return action.name === name; });
39 };
40 /**
41 * 获取 Action
42 * @param action Action 对象
43 */
44 Context.prototype.addAction = function (action) {
45 this.actions.push(action);
46 };
47 /**
48 * 移除 Action
49 * @param action Action 对象
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 * 获取当前 shape
80 * @returns current shape
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 * @param name shape 的 name
98 */
99 Context.prototype.isInShape = function (name) {
100 var shape = this.getCurrentShape(); // 不再考虑在 shape 的 parent 内的情况
101 if (shape) {
102 return shape.get('name') === name;
103 }
104 return false;
105 };
106 /**
107 * 当前的触发是组件内部
108 * @param name 组件名,可以为空
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 // 先销毁 action 再清空,一边遍历,一边删除,所以数组需要更新引用
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}());
141export default Context;
142//# sourceMappingURL=context.js.map
\No newline at end of file