UNPKG

4.61 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var util_1 = require("@antv/util");
5var dependents_1 = require("../../dependents");
6/**
7 * coordinate controller,职责:
8 * 1. 创建实例
9 * 2. 暂存配置
10 */
11var CoordinateController = /** @class */ (function () {
12 function CoordinateController(option) {
13 // 设置默认值,并存储配置
14 this.option = this.wrapperOption(option);
15 }
16 /**
17 * 更新配置
18 * @param option
19 */
20 CoordinateController.prototype.update = function (option) {
21 this.option = this.wrapperOption(option);
22 return this;
23 };
24 /**
25 * 是否存在某一个 action
26 * @param actionName
27 */
28 CoordinateController.prototype.hasAction = function (actionName) {
29 var actions = this.option.actions;
30 return util_1.some(actions, function (action) { return action[0] === actionName; });
31 };
32 /**
33 * 创建坐标系对象
34 * @param start 起始位置
35 * @param end 结束位置
36 * @return 坐标系实例
37 */
38 CoordinateController.prototype.create = function (start, end) {
39 var _a = this.option, type = _a.type, cfg = _a.cfg;
40 var isTheta = type === 'theta';
41 // 1. 起始位置
42 var props = tslib_1.__assign({ start: start,
43 end: end }, cfg);
44 // 2. 创建实例
45 var C = dependents_1.getCoordinate(isTheta ? 'polar' : type);
46 this.coordinate = new C(props);
47 // @ts-ignore FIXME coordinate 包问题导致 type 不正确
48 this.coordinate.type = type;
49 // 3. 添加默认 action
50 if (isTheta) {
51 // 不存在 transpose,为其自动设置一个 action
52 if (!this.hasAction('transpose')) {
53 this.transpose();
54 }
55 }
56 // 4. 执行 action
57 this.execActions();
58 return this.coordinate;
59 };
60 /**
61 * 更新坐标系对象
62 * @param start 起始位置
63 * @param end 结束位置
64 * @return 坐标系实例
65 */
66 CoordinateController.prototype.adjust = function (start, end) {
67 this.coordinate.update({
68 start: start,
69 end: end,
70 });
71 // 更新坐标系大小的时候,需要:
72 // 1. 重置 matrix
73 // 2. 重新执行作用于 matrix 的 action
74 this.coordinate.resetMatrix();
75 this.execActions(['scale', 'rotate', 'translate']);
76 return this.coordinate;
77 };
78 /**
79 * 旋转弧度
80 * @param angle
81 */
82 CoordinateController.prototype.rotate = function (angle) {
83 this.option.actions.push(['rotate', angle]);
84 return this;
85 };
86 /**
87 * 镜像
88 * @param dim
89 */
90 CoordinateController.prototype.reflect = function (dim) {
91 this.option.actions.push(['reflect', dim]);
92 return this;
93 };
94 /**
95 * scale
96 * @param sx
97 * @param sy
98 */
99 CoordinateController.prototype.scale = function (sx, sy) {
100 this.option.actions.push(['scale', sx, sy]);
101 return this;
102 };
103 /**
104 * 对角变换
105 */
106 CoordinateController.prototype.transpose = function () {
107 this.option.actions.push(['transpose']);
108 return this;
109 };
110 /**
111 * 获取配置
112 */
113 CoordinateController.prototype.getOption = function () {
114 return this.option;
115 };
116 /**
117 * 获得 coordinate 实例
118 */
119 CoordinateController.prototype.getCoordinate = function () {
120 return this.coordinate;
121 };
122 /**
123 * 包装配置的默认值
124 * @param option
125 */
126 CoordinateController.prototype.wrapperOption = function (option) {
127 return tslib_1.__assign({ type: 'rect', actions: [], cfg: {} }, option);
128 };
129 /**
130 * coordinate 实例执行 actions
131 * @params includeActions 如果没有指定,则执行全部,否则,执行指定的 action
132 */
133 CoordinateController.prototype.execActions = function (includeActions) {
134 var _this = this;
135 var actions = this.option.actions;
136 util_1.each(actions, function (action) {
137 var _a;
138 var actionName = action[0], args = action.slice(1);
139 var shouldExec = util_1.isNil(includeActions) ? true : includeActions.includes(actionName);
140 if (shouldExec) {
141 (_a = _this.coordinate)[actionName].apply(_a, args);
142 }
143 });
144 };
145 return CoordinateController;
146}());
147exports.default = CoordinateController;
148//# sourceMappingURL=coordinate.js.map
\No newline at end of file