UNPKG

4.68 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 (0, 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, end: end }, cfg);
43 // 2. 创建实例
44 var C = (0, dependents_1.getCoordinate)(isTheta ? 'polar' : type);
45 this.coordinate = new C(props);
46 // @ts-ignore FIXME coordinate 包问题导致 type 不正确
47 this.coordinate.type = type;
48 // 3. 添加默认 action
49 if (isTheta) {
50 // 不存在 transpose,为其自动设置一个 action
51 if (!this.hasAction('transpose')) {
52 this.transpose();
53 }
54 }
55 // 4. 执行 action
56 this.execActions();
57 return this.coordinate;
58 };
59 /**
60 * 更新坐标系对象
61 * @param start 起始位置
62 * @param end 结束位置
63 * @return 坐标系实例
64 */
65 CoordinateController.prototype.adjust = function (start, end) {
66 this.coordinate.update({
67 start: start,
68 end: end,
69 });
70 // 更新坐标系大小的时候,需要:
71 // 1. 重置 matrix
72 // 2. 重新执行作用于 matrix 的 action
73 this.coordinate.resetMatrix();
74 this.execActions(['scale', 'rotate', 'translate']);
75 return this.coordinate;
76 };
77 /**
78 * 旋转弧度
79 * @param angle
80 */
81 CoordinateController.prototype.rotate = function (angle) {
82 this.option.actions.push(['rotate', angle]);
83 return this;
84 };
85 /**
86 * 镜像
87 * @param dim
88 */
89 CoordinateController.prototype.reflect = function (dim) {
90 this.option.actions.push(['reflect', dim]);
91 return this;
92 };
93 /**
94 * scale
95 * @param sx
96 * @param sy
97 */
98 CoordinateController.prototype.scale = function (sx, sy) {
99 this.option.actions.push(['scale', sx, sy]);
100 return this;
101 };
102 /**
103 * 对角变换
104 */
105 CoordinateController.prototype.transpose = function () {
106 this.option.actions.push(['transpose']);
107 return this;
108 };
109 /**
110 * 获取配置
111 */
112 CoordinateController.prototype.getOption = function () {
113 return this.option;
114 };
115 /**
116 * 获得 coordinate 实例
117 */
118 CoordinateController.prototype.getCoordinate = function () {
119 return this.coordinate;
120 };
121 /**
122 * 包装配置的默认值
123 * @param option
124 */
125 CoordinateController.prototype.wrapperOption = function (option) {
126 return tslib_1.__assign({ type: 'rect', actions: [], cfg: {} }, option);
127 };
128 /**
129 * coordinate 实例执行 actions
130 * @params includeActions 如果没有指定,则执行全部,否则,执行指定的 action
131 */
132 CoordinateController.prototype.execActions = function (includeActions) {
133 var _this = this;
134 var actions = this.option.actions;
135 (0, util_1.each)(actions, function (action) {
136 var _a;
137 var _b = tslib_1.__read(action), actionName = _b[0], args = _b.slice(1);
138 var shouldExec = (0, util_1.isNil)(includeActions) ? true : includeActions.includes(actionName);
139 if (shouldExec) {
140 (_a = _this.coordinate)[actionName].apply(_a, tslib_1.__spreadArray([], tslib_1.__read(args), false));
141 }
142 });
143 };
144 return CoordinateController;
145}());
146exports.default = CoordinateController;
147//# sourceMappingURL=coordinate.js.map
\No newline at end of file