UNPKG

11.8 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.parseAction = void 0;
4var tslib_1 = require("tslib");
5var util_1 = require("@antv/util");
6var register_1 = require("./action/register");
7var context_1 = tslib_1.__importDefault(require("./context"));
8var interaction_1 = tslib_1.__importDefault(require("./interaction"));
9// 将字符串转换成 action
10function parseAction(actionStr, context, arg) {
11 var arr = actionStr.split(':');
12 var actionName = arr[0];
13 // 如果已经初始化过 action ,则直接引用之前的 action
14 var action = context.getAction(actionName) || (0, register_1.createAction)(actionName, context);
15 if (!action) {
16 throw new Error("There is no action named ".concat(actionName));
17 }
18 var methodName = arr[1];
19 return {
20 action: action,
21 methodName: methodName,
22 arg: arg,
23 };
24}
25exports.parseAction = parseAction;
26// 执行 Action
27function executeAction(actionObject) {
28 var action = actionObject.action, methodName = actionObject.methodName, arg = actionObject.arg;
29 if (action[methodName]) {
30 action[methodName](arg);
31 }
32 else {
33 throw new Error("Action(".concat(action.name, ") doesn't have a method called ").concat(methodName));
34 }
35}
36var STEP_NAMES = {
37 START: 'start',
38 SHOW_ENABLE: 'showEnable',
39 END: 'end',
40 ROLLBACK: 'rollback',
41 PROCESSING: 'processing',
42};
43/**
44 * 支持语法的交互类
45 */
46var GrammarInteraction = /** @class */ (function (_super) {
47 tslib_1.__extends(GrammarInteraction, _super);
48 function GrammarInteraction(view, steps) {
49 var _this = _super.call(this, view, steps) || this;
50 _this.callbackCaches = {};
51 // 某个触发和反馈在本环节是否执行或
52 _this.emitCaches = {};
53 _this.steps = steps;
54 return _this;
55 }
56 /**
57 * 初始化
58 */
59 GrammarInteraction.prototype.init = function () {
60 this.initContext();
61 _super.prototype.init.call(this);
62 };
63 /**
64 * 清理资源
65 */
66 GrammarInteraction.prototype.destroy = function () {
67 _super.prototype.destroy.call(this); // 先清理事件
68 this.steps = null;
69 if (this.context) {
70 this.context.destroy();
71 this.context = null;
72 }
73 this.callbackCaches = null;
74 this.view = null;
75 };
76 /**
77 * 绑定事件
78 */
79 GrammarInteraction.prototype.initEvents = function () {
80 var _this = this;
81 (0, util_1.each)(this.steps, function (stepArr, stepName) {
82 (0, util_1.each)(stepArr, function (step) {
83 var callback = _this.getActionCallback(stepName, step);
84 if (callback) {
85 // 如果存在 callback,才绑定,有时候会出现无 callback 的情况
86 _this.bindEvent(step.trigger, callback);
87 }
88 });
89 });
90 };
91 /**
92 * 清理绑定的事件
93 */
94 GrammarInteraction.prototype.clearEvents = function () {
95 var _this = this;
96 (0, util_1.each)(this.steps, function (stepArr, stepName) {
97 (0, util_1.each)(stepArr, function (step) {
98 var callback = _this.getActionCallback(stepName, step);
99 if (callback) {
100 _this.offEvent(step.trigger, callback);
101 }
102 });
103 });
104 };
105 // 初始化上下文,并初始化 action
106 GrammarInteraction.prototype.initContext = function () {
107 var view = this.view;
108 var context = new context_1.default(view);
109 this.context = context;
110 var steps = this.steps;
111 // 生成具体的 Action
112 (0, util_1.each)(steps, function (subSteps) {
113 (0, util_1.each)(subSteps, function (step) {
114 if ((0, util_1.isFunction)(step.action)) {
115 // 如果传入回调函数,则直接生成 CallbackAction
116 step.actionObject = {
117 action: (0, register_1.createCallbackAction)(step.action, context),
118 methodName: 'execute',
119 };
120 }
121 else if ((0, util_1.isString)(step.action)) {
122 // 如果是字符串
123 step.actionObject = parseAction(step.action, context, step.arg);
124 }
125 else if ((0, util_1.isArray)(step.action)) {
126 // 如果是数组
127 var actionArr = step.action;
128 var argArr_1 = (0, util_1.isArray)(step.arg) ? step.arg : [step.arg];
129 step.actionObject = [];
130 (0, util_1.each)(actionArr, function (actionStr, idx) {
131 step.actionObject.push(parseAction(actionStr, context, argArr_1[idx]));
132 });
133 }
134 // 如果 action 既不是字符串,也不是函数,则不会生成 actionObject
135 });
136 });
137 };
138 // 是否允许指定阶段名称执行
139 GrammarInteraction.prototype.isAllowStep = function (stepName) {
140 var currentStepName = this.currentStepName;
141 var steps = this.steps;
142 // 相同的阶段允许同时执行
143 if (currentStepName === stepName) {
144 return true;
145 }
146 if (stepName === STEP_NAMES.SHOW_ENABLE) {
147 // 示能在整个过程中都可用
148 return true;
149 }
150 if (stepName === STEP_NAMES.PROCESSING) {
151 // 只有当前是 start 时,才允许 processing
152 return currentStepName === STEP_NAMES.START;
153 }
154 if (stepName === STEP_NAMES.START) {
155 // 如果当前是 processing,则无法 start,必须等待 end 后才能执行
156 return currentStepName !== STEP_NAMES.PROCESSING;
157 }
158 if (stepName === STEP_NAMES.END) {
159 return currentStepName === STEP_NAMES.PROCESSING || currentStepName === STEP_NAMES.START;
160 }
161 if (stepName === STEP_NAMES.ROLLBACK) {
162 if (steps[STEP_NAMES.END]) {
163 // 如果定义了 end, 只有 end 时才允许回滚
164 return currentStepName === STEP_NAMES.END;
165 }
166 else if (currentStepName === STEP_NAMES.START) {
167 // 如果未定义 end, 则判断是否是开始
168 return true;
169 }
170 }
171 return false;
172 };
173 // 具体的指定阶段是否允许执行
174 GrammarInteraction.prototype.isAllowExecute = function (stepName, step) {
175 if (this.isAllowStep(stepName)) {
176 var key = this.getKey(stepName, step);
177 // 如果是在本环节内仅允许触发一次,同时已经触发过,则不允许再触发
178 if (step.once && this.emitCaches[key]) {
179 return false;
180 }
181 // 如果是允许的阶段,则验证 isEnable 方法
182 if (step.isEnable) {
183 return step.isEnable(this.context);
184 }
185 return true; // 如果没有 isEnable 则允许执行
186 }
187 return false;
188 };
189 GrammarInteraction.prototype.enterStep = function (stepName) {
190 this.currentStepName = stepName;
191 this.emitCaches = {}; // 清除所有本环节触发的缓存
192 };
193 // 执行完某个触发和反馈(子环节)
194 GrammarInteraction.prototype.afterExecute = function (stepName, step) {
195 // show enable 不计入正常的流程,其他情况则设置当前的 step
196 if (stepName !== STEP_NAMES.SHOW_ENABLE && this.currentStepName !== stepName) {
197 this.enterStep(stepName);
198 }
199 var key = this.getKey(stepName, step);
200 // 一旦执行,则缓存标记为,一直保持到跳出改环节
201 this.emitCaches[key] = true;
202 };
203 // 获取某个环节的唯一的键值
204 GrammarInteraction.prototype.getKey = function (stepName, step) {
205 return stepName + step.trigger + step.action;
206 };
207 // 获取 step 的回调函数,如果已经生成,则直接返回,如果未生成,则创建
208 GrammarInteraction.prototype.getActionCallback = function (stepName, step) {
209 var _this = this;
210 var context = this.context;
211 var callbackCaches = this.callbackCaches;
212 var actionObject = step.actionObject;
213 if (step.action && actionObject) {
214 var key = this.getKey(stepName, step);
215 if (!callbackCaches[key]) {
216 // 动态生成执行的方法,执行对应 action 的名称
217 var actionCallback = function (event) {
218 context.event = event; // 保证检测时的 event
219 if (_this.isAllowExecute(stepName, step)) {
220 // 如果是数组时,则依次执行
221 if ((0, util_1.isArray)(actionObject)) {
222 (0, util_1.each)(actionObject, function (obj) {
223 context.event = event; // 可能触发新的事件,保证执行前的 context.event 是正确的
224 executeAction(obj);
225 });
226 }
227 else {
228 context.event = event; // 保证执行前的 context.event 是正确的
229 executeAction(actionObject);
230 }
231 _this.afterExecute(stepName, step);
232 if (step.callback) {
233 context.event = event; // 保证执行前的 context.event 是正确的
234 step.callback(context);
235 }
236 }
237 else {
238 // 如果未通过验证,则事件不要绑定在上面
239 context.event = null;
240 }
241 };
242 // 如果设置了 debounce
243 if (step.debounce) {
244 callbackCaches[key] = (0, util_1.debounce)(actionCallback, step.debounce.wait, step.debounce.immediate);
245 }
246 else if (step.throttle) {
247 // 设置 throttle
248 callbackCaches[key] = (0, util_1.throttle)(actionCallback, step.throttle.wait, {
249 leading: step.throttle.leading,
250 trailing: step.throttle.trailing,
251 });
252 }
253 else {
254 // 直接设置
255 callbackCaches[key] = actionCallback;
256 }
257 }
258 return callbackCaches[key];
259 }
260 return null;
261 };
262 GrammarInteraction.prototype.bindEvent = function (eventName, callback) {
263 var nameArr = eventName.split(':');
264 if (nameArr[0] === 'window') {
265 window.addEventListener(nameArr[1], callback);
266 }
267 else if (nameArr[0] === 'document') {
268 document.addEventListener(nameArr[1], callback);
269 }
270 else {
271 this.view.on(eventName, callback);
272 }
273 };
274 GrammarInteraction.prototype.offEvent = function (eventName, callback) {
275 var nameArr = eventName.split(':');
276 if (nameArr[0] === 'window') {
277 window.removeEventListener(nameArr[1], callback);
278 }
279 else if (nameArr[0] === 'document') {
280 document.removeEventListener(nameArr[1], callback);
281 }
282 else {
283 this.view.off(eventName, callback);
284 }
285 };
286 return GrammarInteraction;
287}(interaction_1.default));
288exports.default = GrammarInteraction;
289//# sourceMappingURL=grammar-interaction.js.map
\No newline at end of file