UNPKG

4.19 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var util_1 = require("@antv/util");
4var util_2 = require("./action/util");
5/**
6 * 交互的上下文
7 */
8var Context = /** @class */ (function () {
9 function Context(view) {
10 /** 当前所有的 Action */
11 this.actions = [];
12 /** 当前事件对象 */
13 this.event = null;
14 this.cacheMap = {};
15 this.view = view;
16 }
17 /**
18 * 缓存信息
19 * @param params 缓存的字段
20 * - 如果一个字段则获取缓存
21 * - 两个字段则设置缓存
22 */
23 Context.prototype.cache = function () {
24 var params = [];
25 for (var _i = 0; _i < arguments.length; _i++) {
26 params[_i] = arguments[_i];
27 }
28 if (params.length === 1) {
29 return this.cacheMap[params[0]];
30 }
31 else if (params.length === 2) {
32 this.cacheMap[params[0]] = params[1];
33 }
34 };
35 /**
36 * 获取 Action
37 * @param name Action 的名称
38 */
39 Context.prototype.getAction = function (name) {
40 return this.actions.find(function (action) { return action.name === name; });
41 };
42 /**
43 * 获取 Action
44 * @param action Action 对象
45 */
46 Context.prototype.addAction = function (action) {
47 this.actions.push(action);
48 };
49 /**
50 * 移除 Action
51 * @param action Action 对象
52 */
53 Context.prototype.removeAction = function (action) {
54 var actions = this.actions;
55 var index = this.actions.indexOf(action);
56 if (index >= 0) {
57 actions.splice(index, 1);
58 }
59 };
60 /**
61 * 获取当前的点
62 */
63 Context.prototype.getCurrentPoint = function () {
64 var event = this.event;
65 if (event) {
66 if (event.target instanceof HTMLElement) {
67 var canvas = this.view.getCanvas();
68 var point = canvas.getPointByClient(event.clientX, event.clientY);
69 return point;
70 }
71 else {
72 return {
73 x: event.x,
74 y: event.y,
75 };
76 }
77 }
78 return null;
79 };
80 /**
81 * 获取当前 shape
82 * @returns current shape
83 */
84 Context.prototype.getCurrentShape = function () {
85 return (0, util_1.get)(this.event, ['gEvent', 'shape']);
86 };
87 /**
88 * 当前的触发是否在 View 内
89 */
90 Context.prototype.isInPlot = function () {
91 var point = this.getCurrentPoint();
92 if (point) {
93 return this.view.isPointInPlot(point);
94 }
95 return false;
96 };
97 /**
98 * 是否在指定的图形内
99 * @param name shape 的 name
100 */
101 Context.prototype.isInShape = function (name) {
102 var shape = this.getCurrentShape(); // 不再考虑在 shape 的 parent 内的情况
103 if (shape) {
104 return shape.get('name') === name;
105 }
106 return false;
107 };
108 /**
109 * 当前的触发是组件内部
110 * @param name 组件名,可以为空
111 */
112 Context.prototype.isInComponent = function (name) {
113 var components = (0, util_2.getComponents)(this.view);
114 var point = this.getCurrentPoint();
115 if (point) {
116 return !!components.find(function (component) {
117 var bbox = component.getBBox();
118 if (name) {
119 return component.get('name') === name && (0, util_2.isInBox)(bbox, point);
120 }
121 else {
122 return (0, util_2.isInBox)(bbox, point);
123 }
124 });
125 }
126 return false;
127 };
128 /**
129 * 销毁
130 */
131 Context.prototype.destroy = function () {
132 // 先销毁 action 再清空,一边遍历,一边删除,所以数组需要更新引用
133 (0, util_1.each)(this.actions.slice(), function (action) {
134 action.destroy();
135 });
136 this.view = null;
137 this.event = null;
138 this.actions = null;
139 this.cacheMap = null;
140 };
141 return Context;
142}());
143exports.default = Context;
144//# sourceMappingURL=context.js.map
\No newline at end of file