1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.parseAction = void 0;
|
4 | var tslib_1 = require("tslib");
|
5 | var util_1 = require("@antv/util");
|
6 | var register_1 = require("./action/register");
|
7 | var context_1 = tslib_1.__importDefault(require("./context"));
|
8 | var interaction_1 = tslib_1.__importDefault(require("./interaction"));
|
9 |
|
10 | function parseAction(actionStr, context, arg) {
|
11 | var arr = actionStr.split(':');
|
12 | var actionName = arr[0];
|
13 |
|
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 | }
|
25 | exports.parseAction = parseAction;
|
26 |
|
27 | function 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 | }
|
36 | var STEP_NAMES = {
|
37 | START: 'start',
|
38 | SHOW_ENABLE: 'showEnable',
|
39 | END: 'end',
|
40 | ROLLBACK: 'rollback',
|
41 | PROCESSING: 'processing',
|
42 | };
|
43 |
|
44 |
|
45 |
|
46 | var GrammarInteraction = (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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
152 | return currentStepName === STEP_NAMES.START;
|
153 | }
|
154 | if (stepName === STEP_NAMES.START) {
|
155 |
|
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 |
|
164 | return currentStepName === STEP_NAMES.END;
|
165 | }
|
166 | else if (currentStepName === STEP_NAMES.START) {
|
167 |
|
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 |
|
182 | if (step.isEnable) {
|
183 | return step.isEnable(this.context);
|
184 | }
|
185 | return true;
|
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 |
|
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 |
|
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 |
|
217 | var actionCallback = function (event) {
|
218 | context.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;
|
224 | executeAction(obj);
|
225 | });
|
226 | }
|
227 | else {
|
228 | context.event = event;
|
229 | executeAction(actionObject);
|
230 | }
|
231 | _this.afterExecute(stepName, step);
|
232 | if (step.callback) {
|
233 | context.event = event;
|
234 | step.callback(context);
|
235 | }
|
236 | }
|
237 | else {
|
238 |
|
239 | context.event = null;
|
240 | }
|
241 | };
|
242 |
|
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 |
|
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));
|
288 | exports.default = GrammarInteraction;
|
289 |
|
\ | No newline at end of file |