@byteluck-fe/model-driven-engine 2.23.0-beta.26-zt → 2.23.0-beta.26-chrome86

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,500 +0,0 @@
1
- function _array_like_to_array(arr, len) {
2
- if (len == null || len > arr.length) len = arr.length;
3
- for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
4
- return arr2;
5
- }
6
- function _array_without_holes(arr) {
7
- if (Array.isArray(arr)) return _array_like_to_array(arr);
8
- }
9
- function _class_call_check(instance, Constructor) {
10
- if (!(instance instanceof Constructor)) {
11
- throw new TypeError("Cannot call a class as a function");
12
- }
13
- }
14
- function _defineProperties(target, props) {
15
- for(var i = 0; i < props.length; i++){
16
- var descriptor = props[i];
17
- descriptor.enumerable = descriptor.enumerable || false;
18
- descriptor.configurable = true;
19
- if ("value" in descriptor) descriptor.writable = true;
20
- Object.defineProperty(target, descriptor.key, descriptor);
21
- }
22
- }
23
- function _create_class(Constructor, protoProps, staticProps) {
24
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
25
- if (staticProps) _defineProperties(Constructor, staticProps);
26
- return Constructor;
27
- }
28
- function _define_property(obj, key, value) {
29
- if (key in obj) {
30
- Object.defineProperty(obj, key, {
31
- value: value,
32
- enumerable: true,
33
- configurable: true,
34
- writable: true
35
- });
36
- } else {
37
- obj[key] = value;
38
- }
39
- return obj;
40
- }
41
- function _iterable_to_array(iter) {
42
- if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
43
- }
44
- function _non_iterable_spread() {
45
- throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
46
- }
47
- function _to_consumable_array(arr) {
48
- return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
49
- }
50
- function _type_of(obj) {
51
- "@swc/helpers - typeof";
52
- return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
53
- }
54
- function _unsupported_iterable_to_array(o, minLen) {
55
- if (!o) return;
56
- if (typeof o === "string") return _array_like_to_array(o, minLen);
57
- var n = Object.prototype.toString.call(o).slice(8, -1);
58
- if (n === "Object" && o.constructor) n = o.constructor.name;
59
- if (n === "Map" || n === "Set") return Array.from(n);
60
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
61
- }
62
- import { CONTROL_TYPE, CALC_TOKEN_TYPE, CALC_AGGREGATE_TYPE } from '@byteluck-fe/model-driven-shared';
63
- import { format } from 'mathjs';
64
- var DisplayType;
65
- (function(DisplayType) {
66
- /**
67
- * 完全等于
68
- */ DisplayType["EQ"] = "EQ";
69
- /**
70
- * 包含
71
- * */ DisplayType["IN"] = "IN";
72
- })(DisplayType || (DisplayType = {}));
73
- export var CalcPlugin = /*#__PURE__*/ function() {
74
- "use strict";
75
- function CalcPlugin(options) {
76
- _class_call_check(this, CalcPlugin);
77
- _define_property(this, "engine", void 0);
78
- _define_property(this, "options", void 0);
79
- /**
80
- * 所有的计算公式控件
81
- * */ // @ts-ignore
82
- _define_property(this, "calcControls", []);
83
- /**
84
- * key=控件id value=控件修改后需要触发重新计算的计算公式控件组成的数组
85
- * 存储为一个Map,用于在控件change的时候,重新计算依赖的calc
86
- * */ _define_property(this, "dependenciesTriggerMap", new Map());
87
- /**
88
- * 隐藏以后需要被计算的控件
89
- * */ _define_property(this, "hideNotRememberControlIds", []);
90
- _define_property(this, "dontHasPermissionControlIds", []);
91
- _define_property(this, "cacheComputedResult", {});
92
- this.options = options;
93
- this.getDontHasPermissionControlIds();
94
- this.getNeedHideRememberControlIds();
95
- }
96
- _create_class(CalcPlugin, [
97
- {
98
- key: "getNeedHideRememberControlIds",
99
- value: /**
100
- * @description 获取显隐控制的需要被记住值的控件id
101
- * */ function getNeedHideRememberControlIds() {
102
- var _this_options;
103
- if (!((_this_options = this.options) === null || _this_options === void 0 ? void 0 : _this_options.displayBoList)) {
104
- return;
105
- }
106
- this.hideNotRememberControlIds = this.options.displayBoList.reduce(function(result, current) {
107
- if (!current.is_remember) {
108
- var _result;
109
- (_result = result).push.apply(_result, _to_consumable_array(current.hide_controls));
110
- }
111
- return result;
112
- }, []);
113
- }
114
- },
115
- {
116
- key: "getDontHasPermissionControlIds",
117
- value: /**
118
- * @description 获取权限控制的隐藏的字段,不需要参与计算
119
- * */ function getDontHasPermissionControlIds() {
120
- var _this_options;
121
- if (!((_this_options = this.options) === null || _this_options === void 0 ? void 0 : _this_options.behavior)) {
122
- return;
123
- }
124
- this.dontHasPermissionControlIds = this.options.behavior.reduce(function(result, current) {
125
- if (current.ctrlBehavior === 1) {
126
- result.push(current.ctrlId);
127
- }
128
- return result;
129
- }, []);
130
- }
131
- },
132
- {
133
- key: "controlNeedComputedValue",
134
- value: /**
135
- * @description 判断控件是否需要被计算
136
- * @param control 控件
137
- * */ function controlNeedComputedValue(control) {
138
- if (!control) {
139
- return false;
140
- }
141
- var isHide = this.getControlIsHide(control);
142
- // 权限控制的控件隐藏,并且控件已经隐藏了(不需要知道是否记住值,统一不计算,这个逻辑前置是因为可能存在显隐控制的,需要记住值,但是被权限控制隐藏的字段,这种情况下权限控制权重最高)
143
- if (this.dontHasPermissionControlIds.includes(control.id) && isHide) {
144
- return false;
145
- }
146
- // 被显隐控制的且不需要记住值,并且控件已经隐藏了(控件已经隐藏以后,因为不需要记住值,所以一律不计算)
147
- if (this.hideNotRememberControlIds.includes(control.id) && isHide) {
148
- return false;
149
- }
150
- // 其余都计算
151
- return true;
152
- }
153
- },
154
- {
155
- key: "getControlIsHide",
156
- value: /**
157
- * @description 获取控件是否被隐藏
158
- * @param control 控件
159
- * */ function getControlIsHide(control) {
160
- if (control.props.isHide) {
161
- return true;
162
- }
163
- if (control.parent === null || control.parent === undefined) {
164
- return false;
165
- }
166
- return this.getControlIsHide(control.parent);
167
- }
168
- },
169
- {
170
- key: "apply",
171
- value: function apply(engine) {
172
- var _this = this;
173
- this.engine = engine;
174
- this.watchControlChange();
175
- this.watchSubtableChange();
176
- this.watchSchemaHideChange();
177
- this.engine.on('engine-mounted', function() {
178
- requestAnimationFrame(function() {
179
- _this.resetDependencies();
180
- _this.allCalcControlComputed();
181
- });
182
- });
183
- }
184
- },
185
- {
186
- key: "resetDependencies",
187
- value: /**
188
- * @description 重置依赖,获取所有的计算公式控件,并根据计算公式获取控件依赖关系
189
- * */ function resetDependencies() {
190
- this.calcControls = [];
191
- this.dependenciesTriggerMap.clear();
192
- this.getAllCalcControl();
193
- this.getCalcDependencies();
194
- }
195
- },
196
- {
197
- key: "allCalcControlComputed",
198
- value: /**
199
- * @description 执行所有计算公式控件的计算
200
- * */ function allCalcControlComputed() {
201
- var _this = this;
202
- this.calcControls.forEach(function(item) {
203
- // this.engine.getState(item.id)
204
- if (item.props.dataBind.result.fieldCode === null || item.props.dataBind.result.fieldCode === undefined) {
205
- // formEngineModel在custom:beforeEngineMounted时调用二开赋值,表单值 set 了却没有做计算。但这个是正常的
206
- _this.computedCalcValue(item);
207
- }
208
- });
209
- }
210
- },
211
- {
212
- key: "getAllCalcControl",
213
- value: /**
214
- * @description 获取所有的计算公式控件
215
- * */ function getAllCalcControl() {
216
- this.calcControls = this.engine.runtime.flatInstances.filter(// @ts-ignore
217
- function(item) {
218
- return item.type === CONTROL_TYPE.CALC;
219
- });
220
- }
221
- },
222
- {
223
- key: "getCalcDependencies",
224
- value: /**
225
- * @description 获取计算公式控件依赖,组成 依赖=>[计算公式] 的Map结构
226
- * */ function getCalcDependencies() {
227
- var _this = this;
228
- this.calcControls.forEach(function(item) {
229
- var scriptEcho = item.props.scriptEcho;
230
- scriptEcho.forEach(function(scriptItem) {
231
- if (scriptItem.type === CALC_TOKEN_TYPE.VariableInMainTable || scriptItem.type === CALC_TOKEN_TYPE.VariableInCurrentSubTable || scriptItem.type === CALC_TOKEN_TYPE.VariableInOtherSubTable) {
232
- // 当前是一个变量,存储依赖于当前变量的计算公式
233
- _this.setDependenciesTriggerMapItem(scriptItem.id, item);
234
- }
235
- // 依赖明细子表的计算公式控件,不收集自己表内的,而是主表或者其他明细子表的控件
236
- if (scriptItem.type === CALC_TOKEN_TYPE.VariableInOtherSubTable && scriptItem.subTableId) {
237
- _this.setDependenciesTriggerMapItem(scriptItem.subTableId, item);
238
- }
239
- });
240
- });
241
- }
242
- },
243
- {
244
- key: "setDependenciesTriggerMapItem",
245
- value: function setDependenciesTriggerMapItem(id, // @ts-ignore
246
- item) {
247
- if (!this.dependenciesTriggerMap.has(id)) {
248
- this.dependenciesTriggerMap.set(id, []);
249
- }
250
- var arr = this.dependenciesTriggerMap.get(id);
251
- if (!arr.includes(item)) {
252
- arr.push(item);
253
- }
254
- }
255
- },
256
- {
257
- key: "getCalcControlsFromSubtableRows",
258
- value: function getCalcControlsFromSubtableRows(// @ts-ignore
259
- subtableRows) {
260
- var _this = this;
261
- return subtableRows.reduce(function(result, current) {
262
- // @ts-ignore
263
- current.children.forEach(function(item) {
264
- var control = item.children[0];
265
- // @ts-ignore
266
- if (control && _this.engine.assertInstance(control, CONTROL_TYPE.CALC)) {
267
- result.push(control);
268
- }
269
- });
270
- return result;
271
- // @ts-ignore
272
- }, []);
273
- }
274
- },
275
- {
276
- key: "watchSchemaHideChange",
277
- value: /**
278
- * @description 监听控件的显隐事件,把显隐会影响的控件触发一遍计算
279
- * */ function watchSchemaHideChange() {
280
- var _this = this;
281
- this.engine.on('schema-change', function(payload) {
282
- if (payload.props === 'isHide') {
283
- var _this_dependenciesTriggerMap_get;
284
- var calcControls = (_this_dependenciesTriggerMap_get = _this.dependenciesTriggerMap.get(payload.instance.id)) !== null && _this_dependenciesTriggerMap_get !== void 0 ? _this_dependenciesTriggerMap_get : [];
285
- calcControls.forEach(function(calc) {
286
- _this.computedCalcValue(calc);
287
- });
288
- }
289
- });
290
- }
291
- },
292
- {
293
- key: "watchSubtableChange",
294
- value: /**
295
- * @description 监听明细子表的change事件,把明细表会影响的控件触发一遍计算
296
- * */ function watchSubtableChange() {
297
- var _this = this;
298
- this.engine.on('list-change', function(payload) {
299
- var _payload_options;
300
- // 初始化前是批量操作。不做处理
301
- if (_this.engine.isMounted === false) return;
302
- _this.resetDependencies();
303
- var _payload_options_changed;
304
- var changedRows = (_payload_options_changed = (_payload_options = payload.options) === null || _payload_options === void 0 ? void 0 : _payload_options.changed) !== null && _payload_options_changed !== void 0 ? _payload_options_changed : [];
305
- // 对发生改变的行进行重新计算
306
- var changedCalc = _this.getCalcControlsFromSubtableRows(changedRows);
307
- // @ts-ignore
308
- changedCalc.forEach(function(calcControl) {
309
- _this.computedCalcValue(calcControl);
310
- });
311
- var _this_dependenciesTriggerMap_get;
312
- var calcControls = (_this_dependenciesTriggerMap_get = _this.dependenciesTriggerMap.get(payload.instance.id)) !== null && _this_dependenciesTriggerMap_get !== void 0 ? _this_dependenciesTriggerMap_get : [];
313
- // @ts-ignore
314
- calcControls.forEach(function(calcControl) {
315
- _this.computedCalcValue(calcControl);
316
- });
317
- });
318
- }
319
- },
320
- {
321
- key: "watchControlChange",
322
- value: /**
323
- * @description 监听控件的change事件,当控件change的时候,取到控件会影响的计算公式,然后执行对应的计算
324
- * */ function watchControlChange() {
325
- var _this = this;
326
- this.engine.on('change', function(payload) {
327
- // 初始化前是批量操作。不做处理
328
- if (_this.engine.isMounted === false) return;
329
- var instance = payload.instance;
330
- if (!_this.dependenciesTriggerMap.has(instance.id)) {
331
- return;
332
- }
333
- var _this_dependenciesTriggerMap_get;
334
- var calcControls = (_this_dependenciesTriggerMap_get = _this.dependenciesTriggerMap.get(instance.id)) !== null && _this_dependenciesTriggerMap_get !== void 0 ? _this_dependenciesTriggerMap_get : [];
335
- // 在明细子表中的控件,如果计算公式也在明细子表,那只触发同一行的计算公式的计算
336
- if (payload.rowIndex !== undefined && payload.rowIndex > -1) {
337
- // @ts-ignore
338
- calcControls.forEach(function(calcControl) {
339
- if (_this.controlInSubtable(calcControl)) {
340
- var calcIndex = _this.engine.getInstanceRowIndex(calcControl);
341
- if (calcIndex !== payload.rowIndex) {
342
- return;
343
- }
344
- }
345
- _this.computedCalcValue(calcControl);
346
- });
347
- } else {
348
- // @ts-ignore
349
- calcControls.forEach(function(calcControl) {
350
- _this.computedCalcValue(calcControl);
351
- });
352
- }
353
- });
354
- }
355
- },
356
- {
357
- key: "controlInSubtable",
358
- value: /**
359
- * @description 控件在明细子表内
360
- * */ function controlInSubtable(control) {
361
- var _control_parent;
362
- return ((_control_parent = control.parent) === null || _control_parent === void 0 ? void 0 : _control_parent.type) === CONTROL_TYPE.SUBTABLE_COLUMN;
363
- }
364
- },
365
- {
366
- key: "computedCalcValue",
367
- value: /**
368
- * @description 执行计算公式的计算
369
- * */ // @ts-ignore
370
- function computedCalcValue(control) {
371
- var _this = this;
372
- var scriptEcho = control.props.scriptEcho;
373
- if (!scriptEcho || scriptEcho.length === 0) {
374
- return;
375
- }
376
- var rowIndex = undefined;
377
- if (this.controlInSubtable(control)) {
378
- rowIndex = this.engine.getInstanceRowIndex(control);
379
- }
380
- // @ts-ignore
381
- var scriptText = scriptEcho.reduce(function(result, current) {
382
- if (current.type === CALC_TOKEN_TYPE.Operator || current.type === CALC_TOKEN_TYPE.Number) {
383
- return result + current.name;
384
- }
385
- var num = 0;
386
- var instance;
387
- switch(current.type){
388
- case CALC_TOKEN_TYPE.VariableInMainTable:
389
- {
390
- var value = _this.getNumberValue(_this.engine.getState(current.id));
391
- instance = _this.engine.getInstance(current.id);
392
- num = Number(value);
393
- break;
394
- }
395
- case CALC_TOKEN_TYPE.VariableInCurrentSubTable:
396
- {
397
- //const rowIndex = this.engine.getInstanceRowIndex(control)
398
- instance = _this.engine.getInstance(current.id, rowIndex);
399
- var state = _this.engine.getState(current.id, rowIndex);
400
- var value1 = _this.getNumberValue(state);
401
- num = Number(value1);
402
- break;
403
- }
404
- case CALC_TOKEN_TYPE.VariableInOtherSubTable:
405
- {
406
- var states = _this.engine.getState(current.id);
407
- var values = Array.isArray(states) ? states.map(function(item) {
408
- return _this.getNumberValue(item);
409
- }) : [];
410
- var value2 = _this.getAggregateTypeValue(values, current.aggregateType);
411
- instance = _this.engine.getInstance(current.subTableId);
412
- num = Number(value2);
413
- break;
414
- }
415
- }
416
- if (!Number.isNaN(num) && _this.controlNeedComputedValue(instance)) {
417
- // 在每一个数字外边包一个括号,将 --1 转为 -(-1) 确保 --1 这种情况不会出问题
418
- result += "(".concat(num, ")");
419
- } else {
420
- result += 0;
421
- }
422
- return result;
423
- }, '');
424
- var value;
425
- // 有缓存的话取缓存,没有缓存再执行eval
426
- if (this.cacheComputedResult[scriptText] !== undefined) {
427
- value = this.cacheComputedResult[scriptText];
428
- } else {
429
- // 解决js精准度问题
430
- value = Number(format(new Function("return ".concat(scriptText))(), {
431
- precision: 16
432
- }));
433
- this.cacheComputedResult[scriptText] = value;
434
- }
435
- // let rowIndex = undefined
436
- if (this.controlInSubtable(control)) {
437
- // rowIndex = this.engine.getInstanceRowIndex(control)
438
- // 控件在明细表内,但是没有行下标,说明控件被已经删除了,所以直接return
439
- if (rowIndex === undefined) {
440
- return;
441
- }
442
- }
443
- var oldValue = this.engine.getState(control.id, rowIndex);
444
- var result = !value || value === Infinity || value === -Infinity ? 0 : control.props.precision === '' ? value : Number(value.toFixed(control.props.precision));
445
- // 如果值没变的话,不触发修改
446
- if (result === (oldValue === null || oldValue === void 0 ? void 0 : oldValue.result)) {
447
- return;
448
- }
449
- var _oldValue_unit;
450
- this.engine.setState(control.id, {
451
- result: result,
452
- unit: (_oldValue_unit = oldValue === null || oldValue === void 0 ? void 0 : oldValue.unit) !== null && _oldValue_unit !== void 0 ? _oldValue_unit : ''
453
- }, rowIndex);
454
- }
455
- },
456
- {
457
- key: "getNumberValue",
458
- value: /**
459
- * @description 获取数字值,因为计算公式可以依赖计算公式和金额,所以需要从value中取到对应的值
460
- * */ function getNumberValue(value) {
461
- if ((typeof value === "undefined" ? "undefined" : _type_of(value)) === 'object' && value) {
462
- if ('result' in value) {
463
- return value.result;
464
- } else if ('amount' in value) {
465
- return value.amount;
466
- }
467
- }
468
- if (typeof value === 'number') {
469
- return value;
470
- }
471
- return 0;
472
- }
473
- },
474
- {
475
- key: "getAggregateTypeValue",
476
- value: /**
477
- * 计算明细子表最大值最小值平均值总和
478
- * */ function getAggregateTypeValue(values, type) {
479
- if (!type || !values || !values.length) return 0;
480
- switch(type){
481
- case CALC_AGGREGATE_TYPE.MAX:
482
- var _Math;
483
- return (_Math = Math).max.apply(_Math, _to_consumable_array(values));
484
- case CALC_AGGREGATE_TYPE.MIN:
485
- var _Math1;
486
- return (_Math1 = Math).min.apply(_Math1, _to_consumable_array(values));
487
- case CALC_AGGREGATE_TYPE.SUM:
488
- return values.reduce(function(a, b) {
489
- return a + b;
490
- });
491
- case CALC_AGGREGATE_TYPE.AVG:
492
- return values.reduce(function(a, b) {
493
- return a + b;
494
- }) / values.length;
495
- }
496
- }
497
- }
498
- ]);
499
- return CalcPlugin;
500
- }();