@flowerforce/flower-core 3.0.0 → 3.0.1-beta.1
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.
- package/dist/index.cjs.js +127 -112
- package/dist/index.esm.js +117 -102
- package/dist/src/RulesMatcher.d.ts +0 -1
- package/package.json +6 -1
- package/dist/src/rules-matcher/operators.d.ts +0 -3
package/dist/index.cjs.js
CHANGED
@@ -1,15 +1,32 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
-
var
|
3
|
+
var _get = require('lodash/get');
|
4
|
+
var isBuffer = require('lodash/isBuffer');
|
4
5
|
var tinyEmitter = require('tiny-emitter');
|
6
|
+
var _set = require('lodash/set');
|
7
|
+
var _unset = require('lodash/unset');
|
8
|
+
var _last = require('lodash/last');
|
9
|
+
var _slice = require('lodash/slice');
|
10
|
+
var _cloneDeep = require('lodash/cloneDeep');
|
11
|
+
var lastIndexOf = require('lodash/lastIndexOf');
|
12
|
+
var find = require('lodash/find');
|
13
|
+
var keyBy = require('lodash/keyBy');
|
14
|
+
var has = require('lodash/has');
|
15
|
+
var omit = require('lodash/omit');
|
16
|
+
var isEmpty = require('lodash/isEmpty');
|
17
|
+
var isPlainObject = require('lodash/isPlainObject');
|
18
|
+
var mapKeys = require('lodash/mapKeys');
|
19
|
+
var mapValues = require('lodash/mapValues');
|
20
|
+
var _trimStart = require('lodash/trimStart');
|
21
|
+
var _intersection = require('lodash/intersection');
|
5
22
|
|
6
23
|
const keyIdentity = key => key;
|
7
24
|
const flatten = (target, opts) => {
|
8
25
|
const options = opts ?? {};
|
9
|
-
const safe =
|
10
|
-
const maxDepth =
|
11
|
-
const delimiter =
|
12
|
-
const transformKey =
|
26
|
+
const safe = _get(options, 'safe', false);
|
27
|
+
const maxDepth = _get(options, 'maxDepth', 0);
|
28
|
+
const delimiter = _get(options, 'delimiter', '.');
|
29
|
+
const transformKey = _get(options, 'transformKey', keyIdentity);
|
13
30
|
const output = {};
|
14
31
|
const step = (object, prev, currentDepth) => {
|
15
32
|
const depth = currentDepth || 1;
|
@@ -17,7 +34,7 @@ const flatten = (target, opts) => {
|
|
17
34
|
const value = object[key];
|
18
35
|
const isarray = safe && Array.isArray(value);
|
19
36
|
const type = Object.prototype.toString.call(value);
|
20
|
-
const isbuffer =
|
37
|
+
const isbuffer = isBuffer(value);
|
21
38
|
const isobject = type === '[object Object]' || type === '[object Array]';
|
22
39
|
const newKey = prev ? prev + delimiter + transformKey(key) : transformKey(key);
|
23
40
|
if (!isarray && !isbuffer && isobject && Object.keys(value).length && (!maxDepth || depth < maxDepth)) {
|
@@ -31,12 +48,12 @@ const flatten = (target, opts) => {
|
|
31
48
|
};
|
32
49
|
const unflatten = (target, opts) => {
|
33
50
|
const options = opts ?? {};
|
34
|
-
const object =
|
35
|
-
const overwrite =
|
36
|
-
const delimiter =
|
37
|
-
const transformKey =
|
51
|
+
const object = _get(options, 'object', false);
|
52
|
+
const overwrite = _get(options, 'overwrite', false);
|
53
|
+
const delimiter = _get(options, 'delimiter', '.');
|
54
|
+
const transformKey = _get(options, 'transformKey', keyIdentity);
|
38
55
|
const result = {};
|
39
|
-
const isbuffer =
|
56
|
+
const isbuffer = isBuffer(target);
|
40
57
|
if (isbuffer || Object.prototype.toString.call(target) !== '[object Object]') {
|
41
58
|
return target;
|
42
59
|
}
|
@@ -79,7 +96,7 @@ const unflatten = (target, opts) => {
|
|
79
96
|
let key2 = getkey(split[0]);
|
80
97
|
let recipient = result;
|
81
98
|
while (key2 !== undefined) {
|
82
|
-
const recipient_key_1 = key1 &&
|
99
|
+
const recipient_key_1 = key1 && _get(recipient, key1);
|
83
100
|
const type = Object.prototype.toString.call(recipient_key_1);
|
84
101
|
const isobject = type === '[object Object]' || type === '[object Array]';
|
85
102
|
if (!overwrite && !isobject && typeof recipient_key_1 !== 'undefined') {
|
@@ -88,7 +105,7 @@ const unflatten = (target, opts) => {
|
|
88
105
|
if (overwrite && !isobject || !overwrite && recipient_key_1 == null) {
|
89
106
|
recipient[key1] = typeof key2 === 'number' && !object ? [] : {};
|
90
107
|
}
|
91
|
-
recipient = key1 &&
|
108
|
+
recipient = key1 && _get(recipient, key1);
|
92
109
|
if (split.length > 0) {
|
93
110
|
key1 = getkey(split.shift());
|
94
111
|
key2 = getkey(split[0]);
|
@@ -105,24 +122,6 @@ const flat = {
|
|
105
122
|
|
106
123
|
const Emitter = new tinyEmitter.TinyEmitter();
|
107
124
|
|
108
|
-
const operators = {
|
109
|
-
$exists: (a, b) => !rulesMatcherUtils.isEmpty(a) === b,
|
110
|
-
$eq: (a, b) => a === b,
|
111
|
-
$ne: (a, b) => a !== b,
|
112
|
-
$gt: (a, b) => rulesMatcherUtils.forceNumber(a) > parseFloat(b),
|
113
|
-
$gte: (a, b) => rulesMatcherUtils.forceNumber(a) >= parseFloat(b),
|
114
|
-
$lt: (a, b) => rulesMatcherUtils.forceNumber(a) < parseFloat(b),
|
115
|
-
$lte: (a, b) => rulesMatcherUtils.forceNumber(a) <= parseFloat(b),
|
116
|
-
$strGt: (a, b) => String(a || '').length > parseFloat(b),
|
117
|
-
$strGte: (a, b) => String(a || '').length >= parseFloat(b),
|
118
|
-
$strLt: (a, b) => String(a || '').length < parseFloat(b),
|
119
|
-
$strLte: (a, b) => String(a || '').length <= parseFloat(b),
|
120
|
-
$in: (a, b) => rulesMatcherUtils.forceArray(b).some(c => lodash.intersection(rulesMatcherUtils.forceArray(a), rulesMatcherUtils.forceArray(c)).length),
|
121
|
-
$nin: (a, b) => !rulesMatcherUtils.forceArray(b).some(c => lodash.intersection(rulesMatcherUtils.forceArray(a), rulesMatcherUtils.forceArray(c)).length),
|
122
|
-
$all: (a, b) => rulesMatcherUtils.forceArray(b).every(c => lodash.intersection(rulesMatcherUtils.forceArray(a), rulesMatcherUtils.forceArray(c)).length),
|
123
|
-
$regex: (a, b, opt) => rulesMatcherUtils.forceArray(b).some(c => c instanceof RegExp ? c.test(a) : new RegExp(c, opt).test(a))
|
124
|
-
};
|
125
|
-
|
126
125
|
const EMPTY_STRING_REGEXP = /^\s*$/;
|
127
126
|
const rulesMatcherUtils = {
|
128
127
|
isNumber: el => {
|
@@ -136,16 +135,16 @@ const rulesMatcherUtils = {
|
|
136
135
|
const path = Object.keys(val)[0];
|
137
136
|
const valueBlock = val;
|
138
137
|
const pathWithPrefix = rulesMatcherUtils.getPath(path, prefix);
|
139
|
-
const valueForKey =
|
138
|
+
const valueForKey = _get(data, pathWithPrefix, undefined);
|
140
139
|
const {
|
141
140
|
name
|
142
|
-
} =
|
141
|
+
} = _get(valueBlock, [path], {}) || {};
|
143
142
|
const {
|
144
143
|
op,
|
145
144
|
value,
|
146
145
|
opt
|
147
146
|
} = rulesMatcherUtils.getDefaultRule(valueBlock[path]);
|
148
|
-
const valueRef = value && String(value).indexOf('$ref:') === 0 ?
|
147
|
+
const valueRef = value && String(value).indexOf('$ref:') === 0 ? _get(data, rulesMatcherUtils.getPath(value.replace('$ref:', ''), prefix), undefined) : value;
|
149
148
|
if (!operators[op]) {
|
150
149
|
throw new Error(`Error missing operator:${op}`);
|
151
150
|
}
|
@@ -262,7 +261,7 @@ const rulesMatcherUtils = {
|
|
262
261
|
forceArray: a => Array.isArray(a) ? a : [a],
|
263
262
|
getPath: (path, prefix) => {
|
264
263
|
if (path.indexOf('^') === 0) {
|
265
|
-
return
|
264
|
+
return _trimStart(path, '^');
|
266
265
|
}
|
267
266
|
if (path.indexOf('$') === 0) {
|
268
267
|
return path;
|
@@ -301,6 +300,23 @@ const rulesMatcherUtils = {
|
|
301
300
|
return Object.keys(keys);
|
302
301
|
}
|
303
302
|
};
|
303
|
+
const operators = {
|
304
|
+
$exists: (a, b) => !rulesMatcherUtils.isEmpty(a) === b,
|
305
|
+
$eq: (a, b) => a === b,
|
306
|
+
$ne: (a, b) => a !== b,
|
307
|
+
$gt: (a, b) => rulesMatcherUtils.forceNumber(a) > parseFloat(b),
|
308
|
+
$gte: (a, b) => rulesMatcherUtils.forceNumber(a) >= parseFloat(b),
|
309
|
+
$lt: (a, b) => rulesMatcherUtils.forceNumber(a) < parseFloat(b),
|
310
|
+
$lte: (a, b) => rulesMatcherUtils.forceNumber(a) <= parseFloat(b),
|
311
|
+
$strGt: (a, b) => String(a || '').length > parseFloat(b),
|
312
|
+
$strGte: (a, b) => String(a || '').length >= parseFloat(b),
|
313
|
+
$strLt: (a, b) => String(a || '').length < parseFloat(b),
|
314
|
+
$strLte: (a, b) => String(a || '').length <= parseFloat(b),
|
315
|
+
$in: (a, b) => rulesMatcherUtils.forceArray(b).some(c => _intersection(rulesMatcherUtils.forceArray(a), rulesMatcherUtils.forceArray(c)).length),
|
316
|
+
$nin: (a, b) => !rulesMatcherUtils.forceArray(b).some(c => _intersection(rulesMatcherUtils.forceArray(a), rulesMatcherUtils.forceArray(c)).length),
|
317
|
+
$all: (a, b) => rulesMatcherUtils.forceArray(b).every(c => _intersection(rulesMatcherUtils.forceArray(a), rulesMatcherUtils.forceArray(c)).length),
|
318
|
+
$regex: (a, b, opt) => rulesMatcherUtils.forceArray(b).some(c => c instanceof RegExp ? c.test(a) : new RegExp(c, opt).test(a))
|
319
|
+
};
|
304
320
|
|
305
321
|
const rulesMatcher = (rules, formValue = {}, apply = true, options) => {
|
306
322
|
if (!rules) return [apply];
|
@@ -312,7 +328,6 @@ const rulesMatcher = (rules, formValue = {}, apply = true, options) => {
|
|
312
328
|
};
|
313
329
|
const MatchRules = {
|
314
330
|
rulesMatcher,
|
315
|
-
operators,
|
316
331
|
utils: rulesMatcherUtils
|
317
332
|
};
|
318
333
|
|
@@ -359,21 +374,21 @@ const CoreUtils = {
|
|
359
374
|
if (Array.isArray(obj)) {
|
360
375
|
return obj.map(item => CoreUtils.mapKeysDeepLodash(item, cb, true));
|
361
376
|
}
|
362
|
-
if (!
|
377
|
+
if (!isPlainObject(obj)) {
|
363
378
|
return obj;
|
364
379
|
}
|
365
|
-
const result =
|
366
|
-
return
|
380
|
+
const result = mapKeys(obj, cb);
|
381
|
+
return mapValues(result, value => CoreUtils.mapKeysDeepLodash(value, cb, true));
|
367
382
|
},
|
368
|
-
generateNodes: nodes =>
|
383
|
+
generateNodes: nodes => keyBy(nodes.map(s => omit(s, 'nextRules')), 'nodeId'),
|
369
384
|
makeObjectRules: nodes => nodes.reduce((acc, inc) => ({
|
370
385
|
...acc,
|
371
386
|
[inc.nodeId]: inc.nextRules
|
372
387
|
}), {}),
|
373
|
-
hasNode: (state, name, node) =>
|
388
|
+
hasNode: (state, name, node) => has(state, [name, 'nodes', node]),
|
374
389
|
isEmptyRules: rules => {
|
375
|
-
if (
|
376
|
-
if (
|
390
|
+
if (isEmpty(rules)) return true;
|
391
|
+
if (isEmpty(_get(rules, 'rules'))) return true;
|
377
392
|
if (Object.keys(flattenRules(rules)).every(key => key.endsWith('$and') || key.endsWith('$or'))) {
|
378
393
|
return true;
|
379
394
|
}
|
@@ -398,14 +413,14 @@ const CoreUtils = {
|
|
398
413
|
return {
|
399
414
|
nodeId: e.props.id,
|
400
415
|
nodeType: e.type.displayName || e.props.as || 'FlowerNode',
|
401
|
-
nodeTitle:
|
416
|
+
nodeTitle: _get(e.props, 'data.title'),
|
402
417
|
children,
|
403
418
|
nextRules,
|
404
419
|
retain: e.props.retain,
|
405
420
|
disabled: e.props.disabled
|
406
421
|
};
|
407
422
|
}),
|
408
|
-
cleanPath: (name, char = '^') =>
|
423
|
+
cleanPath: (name, char = '^') => _trimStart(name, char),
|
409
424
|
getPath: idValue => {
|
410
425
|
if (!idValue) {
|
411
426
|
return {
|
@@ -429,7 +444,7 @@ const CoreUtils = {
|
|
429
444
|
};
|
430
445
|
},
|
431
446
|
allEqual: (arr, arr2) => arr.length === arr2.length && arr.every(v => arr2.includes(v)),
|
432
|
-
findValidRule: (nextRules, value, prefix) =>
|
447
|
+
findValidRule: (nextRules, value, prefix) => find(nextRules, rule => {
|
433
448
|
if (typeof rule.rules === 'string') {
|
434
449
|
return false;
|
435
450
|
}
|
@@ -454,16 +469,16 @@ const FlowerStateUtils = {
|
|
454
469
|
...acc,
|
455
470
|
[k]: v.data
|
456
471
|
}), {}),
|
457
|
-
selectFlowerFormNode: (name, id) => state =>
|
472
|
+
selectFlowerFormNode: (name, id) => state => _get(state, [name, 'form', id]),
|
458
473
|
makeSelectCurrentNextRules: name => state => {
|
459
|
-
const nextRules =
|
474
|
+
const nextRules = _get(state, [name, 'nextRules']);
|
460
475
|
const currentNodeId = FlowerStateUtils.makeSelectCurrentNodeId(name)(state);
|
461
|
-
return
|
476
|
+
return _get(nextRules, [currentNodeId]);
|
462
477
|
},
|
463
478
|
makeSelectCurrentNodeId: name => state => {
|
464
|
-
const subState =
|
465
|
-
const startId =
|
466
|
-
return
|
479
|
+
const subState = _get(state, [name]);
|
480
|
+
const startId = _get(state, ['startId']);
|
481
|
+
return _get(subState, ['current']) || startId;
|
467
482
|
},
|
468
483
|
makeSelectNodeErrors: (name, currentNodeId) => state => {
|
469
484
|
const form = FlowerStateUtils.selectFlowerFormNode(name, currentNodeId)(state);
|
@@ -489,29 +504,29 @@ const FlowerCoreReducers = {
|
|
489
504
|
}) => {
|
490
505
|
if (hasNode(state, payload.name, payload.node)) {
|
491
506
|
state[payload.name].history.push(payload.node);
|
492
|
-
|
507
|
+
_set(state, [payload.name, 'current'], payload.node);
|
493
508
|
}
|
494
509
|
return state;
|
495
510
|
},
|
496
511
|
historyPrevToNode: (state, {
|
497
512
|
payload
|
498
513
|
}) => {
|
499
|
-
const history =
|
500
|
-
const lastIndex =
|
514
|
+
const history = _get(state[typeof payload === 'string' ? payload : payload.name], ['history'], []);
|
515
|
+
const lastIndex = lastIndexOf(history, typeof payload === 'string' ? payload : payload.node);
|
501
516
|
if (lastIndex >= 0) {
|
502
|
-
const newHistory =
|
503
|
-
|
504
|
-
|
517
|
+
const newHistory = _slice(history, 0, lastIndex + 1);
|
518
|
+
_set(state, [typeof payload === 'string' ? payload : payload.name, 'history'], newHistory);
|
519
|
+
_set(state, [typeof payload === 'string' ? payload : payload.name, 'current'], typeof payload === 'string' ? payload : payload.node);
|
505
520
|
}
|
506
521
|
return state;
|
507
522
|
},
|
508
523
|
setFormTouched: (state, {
|
509
524
|
payload
|
510
525
|
}) => {
|
511
|
-
if (!
|
526
|
+
if (!_get(state, [typeof payload === 'string' ? payload : payload.flowName, 'nodes', typeof payload === 'string' ? payload : payload.currentNode])) {
|
512
527
|
return state;
|
513
528
|
}
|
514
|
-
|
529
|
+
_set(state, [typeof payload === 'string' ? payload : payload.flowName, 'form', typeof payload === 'string' ? payload : payload.currentNode, 'touched'], true);
|
515
530
|
return state;
|
516
531
|
},
|
517
532
|
forceAddHistory: (state, {
|
@@ -530,37 +545,37 @@ const FlowerCoreReducers = {
|
|
530
545
|
historyPop: (state, {
|
531
546
|
payload
|
532
547
|
}) => {
|
533
|
-
const container =
|
534
|
-
const startId =
|
535
|
-
const history =
|
536
|
-
const originHistory = [...
|
548
|
+
const container = _get(state, [payload.name]);
|
549
|
+
const startId = _get(container, ['startId']);
|
550
|
+
const history = _get(container, ['history']);
|
551
|
+
const originHistory = [..._get(container, ['history'], [])];
|
537
552
|
if (originHistory.length < 2) {
|
538
|
-
|
539
|
-
|
553
|
+
_set(state, [payload.name, 'current'], startId);
|
554
|
+
_set(state, [payload.name, 'history'], [startId]);
|
540
555
|
return state;
|
541
556
|
}
|
542
557
|
history.pop();
|
543
558
|
const total = originHistory.length - 2;
|
544
559
|
for (let index = total; index > 0; index--) {
|
545
560
|
const curr = originHistory[index];
|
546
|
-
const nodeType =
|
547
|
-
const nodeDisabled =
|
561
|
+
const nodeType = _get(container, ['nodes', curr, 'nodeType']);
|
562
|
+
const nodeDisabled = _get(container, ['nodes', curr, 'disabled']);
|
548
563
|
if (nodeDisabled || nodeType === 'FlowerAction' || nodeType === 'FlowerServer' || !nodeType) {
|
549
564
|
history.pop();
|
550
565
|
} else {
|
551
566
|
break;
|
552
567
|
}
|
553
568
|
}
|
554
|
-
const lastId =
|
555
|
-
|
569
|
+
const lastId = _last(history);
|
570
|
+
_set(state, [payload.name, 'current'], lastId);
|
556
571
|
return state;
|
557
572
|
},
|
558
573
|
restoreHistory: (state, {
|
559
574
|
payload
|
560
575
|
}) => {
|
561
|
-
const startId =
|
562
|
-
|
563
|
-
|
576
|
+
const startId = _get(state, [payload.name, 'startId']);
|
577
|
+
_set(state, [payload.name, 'current'], startId);
|
578
|
+
_set(state, [payload.name, 'history'], [startId]);
|
564
579
|
return state;
|
565
580
|
},
|
566
581
|
replaceNode: (state, {
|
@@ -572,8 +587,8 @@ const FlowerCoreReducers = {
|
|
572
587
|
node
|
573
588
|
} = payload;
|
574
589
|
if (hasNode(state, name || flowName || '', node)) {
|
575
|
-
|
576
|
-
|
590
|
+
_set(state, [name || flowName || '', 'current'], node);
|
591
|
+
_set(state, [name || flowName || '', 'history'], [node]);
|
577
592
|
}
|
578
593
|
return state;
|
579
594
|
},
|
@@ -586,9 +601,9 @@ const FlowerCoreReducers = {
|
|
586
601
|
node
|
587
602
|
} = payload;
|
588
603
|
if (hasNode(state, name || flowName || '', node)) {
|
589
|
-
|
590
|
-
|
591
|
-
|
604
|
+
_set(state, [name || flowName || '', 'startId'], node);
|
605
|
+
_set(state, [name || flowName || '', 'current'], node);
|
606
|
+
_set(state, [name || flowName || '', 'history'], [node]);
|
592
607
|
}
|
593
608
|
return state;
|
594
609
|
},
|
@@ -600,24 +615,24 @@ const FlowerCoreReducers = {
|
|
600
615
|
flowName
|
601
616
|
} = payload;
|
602
617
|
if (!name && !flowName) return state;
|
603
|
-
|
618
|
+
_set(state, [name || flowName || '', 'history'], []);
|
604
619
|
return state;
|
605
620
|
},
|
606
621
|
destroy: (state, {
|
607
622
|
payload
|
608
623
|
}) => {
|
609
|
-
|
624
|
+
_set(state, [payload.name], {});
|
610
625
|
},
|
611
626
|
initNodes: (state, {
|
612
627
|
payload
|
613
628
|
}) => {
|
614
|
-
if (payload.persist &&
|
615
|
-
const startId = payload.startId ||
|
629
|
+
if (payload.persist && _get(state, [payload.name, 'persist'])) return;
|
630
|
+
const startId = payload.startId || _get(payload, 'nodes.0.nodeId');
|
616
631
|
if (!startId) {
|
617
632
|
console.warn('Flower is empty');
|
618
633
|
return;
|
619
634
|
}
|
620
|
-
|
635
|
+
_set(state, payload.name, {
|
621
636
|
persist: payload.persist,
|
622
637
|
startId,
|
623
638
|
current: startId,
|
@@ -631,31 +646,31 @@ const FlowerCoreReducers = {
|
|
631
646
|
payload
|
632
647
|
}) => {
|
633
648
|
if (hasNode(state, payload.name, payload.node)) {
|
634
|
-
const startId =
|
649
|
+
const startId = _get(state, [payload.name, 'startId']);
|
635
650
|
if (payload.node === startId) {
|
636
|
-
|
637
|
-
|
651
|
+
_set(state, [payload.name, 'current'], startId);
|
652
|
+
_set(state, [payload.name, 'history'], [startId]);
|
638
653
|
} else {
|
639
|
-
|
654
|
+
_set(state, [payload.name, 'current'], payload.node);
|
640
655
|
}
|
641
656
|
}
|
642
657
|
},
|
643
658
|
formAddErrors: (state, {
|
644
659
|
payload
|
645
660
|
}) => {
|
646
|
-
|
661
|
+
_set(state, [payload.name, 'form', payload.currentNode, 'errors', payload.id], payload.errors);
|
647
662
|
},
|
648
663
|
formRemoveErrors: (state, {
|
649
664
|
payload
|
650
665
|
}) => {
|
651
|
-
|
652
|
-
|
666
|
+
_unset(state, [payload.name, 'form', payload.currentNode, 'errors', payload.id]);
|
667
|
+
_unset(state, [payload.name, 'form', payload.currentNode, 'isValidating']);
|
653
668
|
},
|
654
669
|
addData: (state, {
|
655
670
|
payload
|
656
671
|
}) => {
|
657
|
-
const prevData =
|
658
|
-
|
672
|
+
const prevData = _get(state, [payload.flowName, 'data']);
|
673
|
+
_set(state, [payload.flowName, 'data'], {
|
659
674
|
...prevData,
|
660
675
|
...payload.value
|
661
676
|
});
|
@@ -664,23 +679,23 @@ const FlowerCoreReducers = {
|
|
664
679
|
payload
|
665
680
|
}) => {
|
666
681
|
if (payload.id && payload.id.length) {
|
667
|
-
|
682
|
+
_set(state, [payload.flowName, 'data', ...payload.id], payload.value);
|
668
683
|
}
|
669
684
|
},
|
670
685
|
replaceData: (state, {
|
671
686
|
payload
|
672
687
|
}) => {
|
673
|
-
|
688
|
+
_set(state, [payload.flowName, 'data'], payload.value);
|
674
689
|
},
|
675
690
|
unsetData: (state, {
|
676
691
|
payload
|
677
692
|
}) => {
|
678
|
-
|
693
|
+
_unset(state, [payload.flowName, 'data', ...payload.id]);
|
679
694
|
},
|
680
695
|
setFormIsValidating: (state, {
|
681
696
|
payload
|
682
697
|
}) => {
|
683
|
-
|
698
|
+
_set(state, [payload.name, 'form', payload.currentNode, 'isValidating'], payload.isValidating);
|
684
699
|
},
|
685
700
|
node: (state, {
|
686
701
|
payload
|
@@ -691,8 +706,8 @@ const FlowerCoreReducers = {
|
|
691
706
|
} = payload;
|
692
707
|
const node = payload.nodeId || payload.node || '';
|
693
708
|
const flowName = name || payload.flowName || '';
|
694
|
-
const startNode =
|
695
|
-
const currentNodeId =
|
709
|
+
const startNode = _get(state, [payload.name, 'startId']);
|
710
|
+
const currentNodeId = _get(state, [payload.name, 'current'], startNode);
|
696
711
|
FlowerCoreReducers.setFormTouched(state, {
|
697
712
|
type: 'setFormTouched',
|
698
713
|
payload: {
|
@@ -700,7 +715,7 @@ const FlowerCoreReducers = {
|
|
700
715
|
currentNode: currentNodeId
|
701
716
|
}
|
702
717
|
});
|
703
|
-
if (global.window &&
|
718
|
+
if (global.window && _get(global.window, '__FLOWER_DEVTOOLS__') && history) {
|
704
719
|
FlowerCoreReducers.forceAddHistory(state, {
|
705
720
|
type: 'forceAddHistory',
|
706
721
|
payload: {
|
@@ -746,7 +761,7 @@ const FlowerCoreReducers = {
|
|
746
761
|
const currentNodeId = FlowerStateUtils.makeSelectCurrentNodeId(flowName)(state);
|
747
762
|
const currentNextRules = FlowerStateUtils.makeSelectCurrentNextRules(flowName)(state);
|
748
763
|
const form = FlowerStateUtils.makeSelectNodeErrors(flowName, currentNodeId)(state);
|
749
|
-
const clonedData =
|
764
|
+
const clonedData = _cloneDeep(FlowerStateUtils.getAllData(state));
|
750
765
|
const stateWithNodeData = {
|
751
766
|
$in: data,
|
752
767
|
$form: form,
|
@@ -777,7 +792,7 @@ const FlowerCoreReducers = {
|
|
777
792
|
return;
|
778
793
|
}
|
779
794
|
const validRule = findValidRule(currentNextRules, stateWithNodeData, flowName);
|
780
|
-
const nextNumberNode =
|
795
|
+
const nextNumberNode = _get(validRule, 'nodeId');
|
781
796
|
if (!nextNumberNode) {
|
782
797
|
return;
|
783
798
|
}
|
@@ -821,16 +836,16 @@ const FlowerCoreReducers = {
|
|
821
836
|
|
822
837
|
const FlowerCoreStateSelectors = {
|
823
838
|
selectGlobal: state => state && state.flower,
|
824
|
-
selectFlower: name => state =>
|
825
|
-
selectFlowerFormNode: id => state =>
|
826
|
-
selectFlowerHistory: flower =>
|
827
|
-
makeSelectNodesIds: flower =>
|
828
|
-
makeSelectStartNodeId: flower =>
|
829
|
-
getDataByFlow: flower =>
|
830
|
-
getDataFromState: id => data => id === '*' ? data :
|
839
|
+
selectFlower: name => state => _get(state, [name]),
|
840
|
+
selectFlowerFormNode: id => state => _get(state, ['form', id]),
|
841
|
+
selectFlowerHistory: flower => _get(flower, ['history'], []),
|
842
|
+
makeSelectNodesIds: flower => _get(flower, ['nodes']),
|
843
|
+
makeSelectStartNodeId: flower => _get(flower, ['startId']),
|
844
|
+
getDataByFlow: flower => _get(flower, ['data']) ?? {},
|
845
|
+
getDataFromState: id => data => id === '*' ? data : _get(data, id),
|
831
846
|
makeSelectNodeFormTouched: form => form && form.touched,
|
832
|
-
makeSelectCurrentNodeId: (flower, startNodeId) =>
|
833
|
-
makeSelectCurrentNodeDisabled: (nodes, current) => !!
|
847
|
+
makeSelectCurrentNodeId: (flower, startNodeId) => _get(flower, ['current']) || startNodeId,
|
848
|
+
makeSelectCurrentNodeDisabled: (nodes, current) => !!_get(nodes, [current, 'disabled']),
|
834
849
|
makeSelectPrevNodeRetain: (nodes, history, current) => {
|
835
850
|
if (!nodes) return;
|
836
851
|
const prevFlowerNode = [...history].reverse().find(el => {
|
@@ -874,14 +889,14 @@ const FlowerCoreStateSelectors = {
|
|
874
889
|
$form: form
|
875
890
|
};
|
876
891
|
const state = Object.assign(newState, id ? {
|
877
|
-
$self:
|
892
|
+
$self: _get(newState, [flowName, ...id.split('.')])
|
878
893
|
} : {});
|
879
894
|
if (!rules) return false;
|
880
895
|
if (!keys) return false;
|
881
896
|
const res = keys.reduce((acc, inc) => {
|
882
897
|
const k = inc;
|
883
898
|
return Object.assign(acc, {
|
884
|
-
[k]:
|
899
|
+
[k]: _get(state, k)
|
885
900
|
});
|
886
901
|
}, {});
|
887
902
|
const [disabled] = MatchRules.rulesMatcher(rules, {
|
package/dist/index.esm.js
CHANGED
@@ -1,13 +1,30 @@
|
|
1
|
-
import
|
1
|
+
import _get from 'lodash/get';
|
2
|
+
import isBuffer from 'lodash/isBuffer';
|
2
3
|
import { TinyEmitter } from 'tiny-emitter';
|
4
|
+
import _set from 'lodash/set';
|
5
|
+
import _unset from 'lodash/unset';
|
6
|
+
import _last from 'lodash/last';
|
7
|
+
import _slice from 'lodash/slice';
|
8
|
+
import _cloneDeep from 'lodash/cloneDeep';
|
9
|
+
import lastIndexOf from 'lodash/lastIndexOf';
|
10
|
+
import find from 'lodash/find';
|
11
|
+
import keyBy from 'lodash/keyBy';
|
12
|
+
import has from 'lodash/has';
|
13
|
+
import omit from 'lodash/omit';
|
14
|
+
import isEmpty from 'lodash/isEmpty';
|
15
|
+
import isPlainObject from 'lodash/isPlainObject';
|
16
|
+
import mapKeys from 'lodash/mapKeys';
|
17
|
+
import mapValues from 'lodash/mapValues';
|
18
|
+
import _trimStart from 'lodash/trimStart';
|
19
|
+
import _intersection from 'lodash/intersection';
|
3
20
|
|
4
21
|
const keyIdentity = key => key;
|
5
22
|
const flatten = (target, opts) => {
|
6
23
|
const options = opts ?? {};
|
7
|
-
const safe =
|
8
|
-
const maxDepth =
|
9
|
-
const delimiter =
|
10
|
-
const transformKey =
|
24
|
+
const safe = _get(options, 'safe', false);
|
25
|
+
const maxDepth = _get(options, 'maxDepth', 0);
|
26
|
+
const delimiter = _get(options, 'delimiter', '.');
|
27
|
+
const transformKey = _get(options, 'transformKey', keyIdentity);
|
11
28
|
const output = {};
|
12
29
|
const step = (object, prev, currentDepth) => {
|
13
30
|
const depth = currentDepth || 1;
|
@@ -29,10 +46,10 @@ const flatten = (target, opts) => {
|
|
29
46
|
};
|
30
47
|
const unflatten = (target, opts) => {
|
31
48
|
const options = opts ?? {};
|
32
|
-
const object =
|
33
|
-
const overwrite =
|
34
|
-
const delimiter =
|
35
|
-
const transformKey =
|
49
|
+
const object = _get(options, 'object', false);
|
50
|
+
const overwrite = _get(options, 'overwrite', false);
|
51
|
+
const delimiter = _get(options, 'delimiter', '.');
|
52
|
+
const transformKey = _get(options, 'transformKey', keyIdentity);
|
36
53
|
const result = {};
|
37
54
|
const isbuffer = isBuffer(target);
|
38
55
|
if (isbuffer || Object.prototype.toString.call(target) !== '[object Object]') {
|
@@ -77,7 +94,7 @@ const unflatten = (target, opts) => {
|
|
77
94
|
let key2 = getkey(split[0]);
|
78
95
|
let recipient = result;
|
79
96
|
while (key2 !== undefined) {
|
80
|
-
const recipient_key_1 = key1 &&
|
97
|
+
const recipient_key_1 = key1 && _get(recipient, key1);
|
81
98
|
const type = Object.prototype.toString.call(recipient_key_1);
|
82
99
|
const isobject = type === '[object Object]' || type === '[object Array]';
|
83
100
|
if (!overwrite && !isobject && typeof recipient_key_1 !== 'undefined') {
|
@@ -86,7 +103,7 @@ const unflatten = (target, opts) => {
|
|
86
103
|
if (overwrite && !isobject || !overwrite && recipient_key_1 == null) {
|
87
104
|
recipient[key1] = typeof key2 === 'number' && !object ? [] : {};
|
88
105
|
}
|
89
|
-
recipient = key1 &&
|
106
|
+
recipient = key1 && _get(recipient, key1);
|
90
107
|
if (split.length > 0) {
|
91
108
|
key1 = getkey(split.shift());
|
92
109
|
key2 = getkey(split[0]);
|
@@ -103,24 +120,6 @@ const flat = {
|
|
103
120
|
|
104
121
|
const Emitter = new TinyEmitter();
|
105
122
|
|
106
|
-
const operators = {
|
107
|
-
$exists: (a, b) => !rulesMatcherUtils.isEmpty(a) === b,
|
108
|
-
$eq: (a, b) => a === b,
|
109
|
-
$ne: (a, b) => a !== b,
|
110
|
-
$gt: (a, b) => rulesMatcherUtils.forceNumber(a) > parseFloat(b),
|
111
|
-
$gte: (a, b) => rulesMatcherUtils.forceNumber(a) >= parseFloat(b),
|
112
|
-
$lt: (a, b) => rulesMatcherUtils.forceNumber(a) < parseFloat(b),
|
113
|
-
$lte: (a, b) => rulesMatcherUtils.forceNumber(a) <= parseFloat(b),
|
114
|
-
$strGt: (a, b) => String(a || '').length > parseFloat(b),
|
115
|
-
$strGte: (a, b) => String(a || '').length >= parseFloat(b),
|
116
|
-
$strLt: (a, b) => String(a || '').length < parseFloat(b),
|
117
|
-
$strLte: (a, b) => String(a || '').length <= parseFloat(b),
|
118
|
-
$in: (a, b) => rulesMatcherUtils.forceArray(b).some(c => intersection(rulesMatcherUtils.forceArray(a), rulesMatcherUtils.forceArray(c)).length),
|
119
|
-
$nin: (a, b) => !rulesMatcherUtils.forceArray(b).some(c => intersection(rulesMatcherUtils.forceArray(a), rulesMatcherUtils.forceArray(c)).length),
|
120
|
-
$all: (a, b) => rulesMatcherUtils.forceArray(b).every(c => intersection(rulesMatcherUtils.forceArray(a), rulesMatcherUtils.forceArray(c)).length),
|
121
|
-
$regex: (a, b, opt) => rulesMatcherUtils.forceArray(b).some(c => c instanceof RegExp ? c.test(a) : new RegExp(c, opt).test(a))
|
122
|
-
};
|
123
|
-
|
124
123
|
const EMPTY_STRING_REGEXP = /^\s*$/;
|
125
124
|
const rulesMatcherUtils = {
|
126
125
|
isNumber: el => {
|
@@ -134,16 +133,16 @@ const rulesMatcherUtils = {
|
|
134
133
|
const path = Object.keys(val)[0];
|
135
134
|
const valueBlock = val;
|
136
135
|
const pathWithPrefix = rulesMatcherUtils.getPath(path, prefix);
|
137
|
-
const valueForKey =
|
136
|
+
const valueForKey = _get(data, pathWithPrefix, undefined);
|
138
137
|
const {
|
139
138
|
name
|
140
|
-
} =
|
139
|
+
} = _get(valueBlock, [path], {}) || {};
|
141
140
|
const {
|
142
141
|
op,
|
143
142
|
value,
|
144
143
|
opt
|
145
144
|
} = rulesMatcherUtils.getDefaultRule(valueBlock[path]);
|
146
|
-
const valueRef = value && String(value).indexOf('$ref:') === 0 ?
|
145
|
+
const valueRef = value && String(value).indexOf('$ref:') === 0 ? _get(data, rulesMatcherUtils.getPath(value.replace('$ref:', ''), prefix), undefined) : value;
|
147
146
|
if (!operators[op]) {
|
148
147
|
throw new Error(`Error missing operator:${op}`);
|
149
148
|
}
|
@@ -260,7 +259,7 @@ const rulesMatcherUtils = {
|
|
260
259
|
forceArray: a => Array.isArray(a) ? a : [a],
|
261
260
|
getPath: (path, prefix) => {
|
262
261
|
if (path.indexOf('^') === 0) {
|
263
|
-
return
|
262
|
+
return _trimStart(path, '^');
|
264
263
|
}
|
265
264
|
if (path.indexOf('$') === 0) {
|
266
265
|
return path;
|
@@ -299,6 +298,23 @@ const rulesMatcherUtils = {
|
|
299
298
|
return Object.keys(keys);
|
300
299
|
}
|
301
300
|
};
|
301
|
+
const operators = {
|
302
|
+
$exists: (a, b) => !rulesMatcherUtils.isEmpty(a) === b,
|
303
|
+
$eq: (a, b) => a === b,
|
304
|
+
$ne: (a, b) => a !== b,
|
305
|
+
$gt: (a, b) => rulesMatcherUtils.forceNumber(a) > parseFloat(b),
|
306
|
+
$gte: (a, b) => rulesMatcherUtils.forceNumber(a) >= parseFloat(b),
|
307
|
+
$lt: (a, b) => rulesMatcherUtils.forceNumber(a) < parseFloat(b),
|
308
|
+
$lte: (a, b) => rulesMatcherUtils.forceNumber(a) <= parseFloat(b),
|
309
|
+
$strGt: (a, b) => String(a || '').length > parseFloat(b),
|
310
|
+
$strGte: (a, b) => String(a || '').length >= parseFloat(b),
|
311
|
+
$strLt: (a, b) => String(a || '').length < parseFloat(b),
|
312
|
+
$strLte: (a, b) => String(a || '').length <= parseFloat(b),
|
313
|
+
$in: (a, b) => rulesMatcherUtils.forceArray(b).some(c => _intersection(rulesMatcherUtils.forceArray(a), rulesMatcherUtils.forceArray(c)).length),
|
314
|
+
$nin: (a, b) => !rulesMatcherUtils.forceArray(b).some(c => _intersection(rulesMatcherUtils.forceArray(a), rulesMatcherUtils.forceArray(c)).length),
|
315
|
+
$all: (a, b) => rulesMatcherUtils.forceArray(b).every(c => _intersection(rulesMatcherUtils.forceArray(a), rulesMatcherUtils.forceArray(c)).length),
|
316
|
+
$regex: (a, b, opt) => rulesMatcherUtils.forceArray(b).some(c => c instanceof RegExp ? c.test(a) : new RegExp(c, opt).test(a))
|
317
|
+
};
|
302
318
|
|
303
319
|
const rulesMatcher = (rules, formValue = {}, apply = true, options) => {
|
304
320
|
if (!rules) return [apply];
|
@@ -310,7 +326,6 @@ const rulesMatcher = (rules, formValue = {}, apply = true, options) => {
|
|
310
326
|
};
|
311
327
|
const MatchRules = {
|
312
328
|
rulesMatcher,
|
313
|
-
operators,
|
314
329
|
utils: rulesMatcherUtils
|
315
330
|
};
|
316
331
|
|
@@ -371,7 +386,7 @@ const CoreUtils = {
|
|
371
386
|
hasNode: (state, name, node) => has(state, [name, 'nodes', node]),
|
372
387
|
isEmptyRules: rules => {
|
373
388
|
if (isEmpty(rules)) return true;
|
374
|
-
if (isEmpty(
|
389
|
+
if (isEmpty(_get(rules, 'rules'))) return true;
|
375
390
|
if (Object.keys(flattenRules(rules)).every(key => key.endsWith('$and') || key.endsWith('$or'))) {
|
376
391
|
return true;
|
377
392
|
}
|
@@ -396,14 +411,14 @@ const CoreUtils = {
|
|
396
411
|
return {
|
397
412
|
nodeId: e.props.id,
|
398
413
|
nodeType: e.type.displayName || e.props.as || 'FlowerNode',
|
399
|
-
nodeTitle:
|
414
|
+
nodeTitle: _get(e.props, 'data.title'),
|
400
415
|
children,
|
401
416
|
nextRules,
|
402
417
|
retain: e.props.retain,
|
403
418
|
disabled: e.props.disabled
|
404
419
|
};
|
405
420
|
}),
|
406
|
-
cleanPath: (name, char = '^') =>
|
421
|
+
cleanPath: (name, char = '^') => _trimStart(name, char),
|
407
422
|
getPath: idValue => {
|
408
423
|
if (!idValue) {
|
409
424
|
return {
|
@@ -452,16 +467,16 @@ const FlowerStateUtils = {
|
|
452
467
|
...acc,
|
453
468
|
[k]: v.data
|
454
469
|
}), {}),
|
455
|
-
selectFlowerFormNode: (name, id) => state =>
|
470
|
+
selectFlowerFormNode: (name, id) => state => _get(state, [name, 'form', id]),
|
456
471
|
makeSelectCurrentNextRules: name => state => {
|
457
|
-
const nextRules =
|
472
|
+
const nextRules = _get(state, [name, 'nextRules']);
|
458
473
|
const currentNodeId = FlowerStateUtils.makeSelectCurrentNodeId(name)(state);
|
459
|
-
return
|
474
|
+
return _get(nextRules, [currentNodeId]);
|
460
475
|
},
|
461
476
|
makeSelectCurrentNodeId: name => state => {
|
462
|
-
const subState =
|
463
|
-
const startId =
|
464
|
-
return
|
477
|
+
const subState = _get(state, [name]);
|
478
|
+
const startId = _get(state, ['startId']);
|
479
|
+
return _get(subState, ['current']) || startId;
|
465
480
|
},
|
466
481
|
makeSelectNodeErrors: (name, currentNodeId) => state => {
|
467
482
|
const form = FlowerStateUtils.selectFlowerFormNode(name, currentNodeId)(state);
|
@@ -487,29 +502,29 @@ const FlowerCoreReducers = {
|
|
487
502
|
}) => {
|
488
503
|
if (hasNode(state, payload.name, payload.node)) {
|
489
504
|
state[payload.name].history.push(payload.node);
|
490
|
-
|
505
|
+
_set(state, [payload.name, 'current'], payload.node);
|
491
506
|
}
|
492
507
|
return state;
|
493
508
|
},
|
494
509
|
historyPrevToNode: (state, {
|
495
510
|
payload
|
496
511
|
}) => {
|
497
|
-
const history =
|
512
|
+
const history = _get(state[typeof payload === 'string' ? payload : payload.name], ['history'], []);
|
498
513
|
const lastIndex = lastIndexOf(history, typeof payload === 'string' ? payload : payload.node);
|
499
514
|
if (lastIndex >= 0) {
|
500
|
-
const newHistory =
|
501
|
-
|
502
|
-
|
515
|
+
const newHistory = _slice(history, 0, lastIndex + 1);
|
516
|
+
_set(state, [typeof payload === 'string' ? payload : payload.name, 'history'], newHistory);
|
517
|
+
_set(state, [typeof payload === 'string' ? payload : payload.name, 'current'], typeof payload === 'string' ? payload : payload.node);
|
503
518
|
}
|
504
519
|
return state;
|
505
520
|
},
|
506
521
|
setFormTouched: (state, {
|
507
522
|
payload
|
508
523
|
}) => {
|
509
|
-
if (!
|
524
|
+
if (!_get(state, [typeof payload === 'string' ? payload : payload.flowName, 'nodes', typeof payload === 'string' ? payload : payload.currentNode])) {
|
510
525
|
return state;
|
511
526
|
}
|
512
|
-
|
527
|
+
_set(state, [typeof payload === 'string' ? payload : payload.flowName, 'form', typeof payload === 'string' ? payload : payload.currentNode, 'touched'], true);
|
513
528
|
return state;
|
514
529
|
},
|
515
530
|
forceAddHistory: (state, {
|
@@ -528,37 +543,37 @@ const FlowerCoreReducers = {
|
|
528
543
|
historyPop: (state, {
|
529
544
|
payload
|
530
545
|
}) => {
|
531
|
-
const container =
|
532
|
-
const startId =
|
533
|
-
const history =
|
534
|
-
const originHistory = [...
|
546
|
+
const container = _get(state, [payload.name]);
|
547
|
+
const startId = _get(container, ['startId']);
|
548
|
+
const history = _get(container, ['history']);
|
549
|
+
const originHistory = [..._get(container, ['history'], [])];
|
535
550
|
if (originHistory.length < 2) {
|
536
|
-
|
537
|
-
|
551
|
+
_set(state, [payload.name, 'current'], startId);
|
552
|
+
_set(state, [payload.name, 'history'], [startId]);
|
538
553
|
return state;
|
539
554
|
}
|
540
555
|
history.pop();
|
541
556
|
const total = originHistory.length - 2;
|
542
557
|
for (let index = total; index > 0; index--) {
|
543
558
|
const curr = originHistory[index];
|
544
|
-
const nodeType =
|
545
|
-
const nodeDisabled =
|
559
|
+
const nodeType = _get(container, ['nodes', curr, 'nodeType']);
|
560
|
+
const nodeDisabled = _get(container, ['nodes', curr, 'disabled']);
|
546
561
|
if (nodeDisabled || nodeType === 'FlowerAction' || nodeType === 'FlowerServer' || !nodeType) {
|
547
562
|
history.pop();
|
548
563
|
} else {
|
549
564
|
break;
|
550
565
|
}
|
551
566
|
}
|
552
|
-
const lastId =
|
553
|
-
|
567
|
+
const lastId = _last(history);
|
568
|
+
_set(state, [payload.name, 'current'], lastId);
|
554
569
|
return state;
|
555
570
|
},
|
556
571
|
restoreHistory: (state, {
|
557
572
|
payload
|
558
573
|
}) => {
|
559
|
-
const startId =
|
560
|
-
|
561
|
-
|
574
|
+
const startId = _get(state, [payload.name, 'startId']);
|
575
|
+
_set(state, [payload.name, 'current'], startId);
|
576
|
+
_set(state, [payload.name, 'history'], [startId]);
|
562
577
|
return state;
|
563
578
|
},
|
564
579
|
replaceNode: (state, {
|
@@ -570,8 +585,8 @@ const FlowerCoreReducers = {
|
|
570
585
|
node
|
571
586
|
} = payload;
|
572
587
|
if (hasNode(state, name || flowName || '', node)) {
|
573
|
-
|
574
|
-
|
588
|
+
_set(state, [name || flowName || '', 'current'], node);
|
589
|
+
_set(state, [name || flowName || '', 'history'], [node]);
|
575
590
|
}
|
576
591
|
return state;
|
577
592
|
},
|
@@ -584,9 +599,9 @@ const FlowerCoreReducers = {
|
|
584
599
|
node
|
585
600
|
} = payload;
|
586
601
|
if (hasNode(state, name || flowName || '', node)) {
|
587
|
-
|
588
|
-
|
589
|
-
|
602
|
+
_set(state, [name || flowName || '', 'startId'], node);
|
603
|
+
_set(state, [name || flowName || '', 'current'], node);
|
604
|
+
_set(state, [name || flowName || '', 'history'], [node]);
|
590
605
|
}
|
591
606
|
return state;
|
592
607
|
},
|
@@ -598,24 +613,24 @@ const FlowerCoreReducers = {
|
|
598
613
|
flowName
|
599
614
|
} = payload;
|
600
615
|
if (!name && !flowName) return state;
|
601
|
-
|
616
|
+
_set(state, [name || flowName || '', 'history'], []);
|
602
617
|
return state;
|
603
618
|
},
|
604
619
|
destroy: (state, {
|
605
620
|
payload
|
606
621
|
}) => {
|
607
|
-
|
622
|
+
_set(state, [payload.name], {});
|
608
623
|
},
|
609
624
|
initNodes: (state, {
|
610
625
|
payload
|
611
626
|
}) => {
|
612
|
-
if (payload.persist &&
|
613
|
-
const startId = payload.startId ||
|
627
|
+
if (payload.persist && _get(state, [payload.name, 'persist'])) return;
|
628
|
+
const startId = payload.startId || _get(payload, 'nodes.0.nodeId');
|
614
629
|
if (!startId) {
|
615
630
|
console.warn('Flower is empty');
|
616
631
|
return;
|
617
632
|
}
|
618
|
-
|
633
|
+
_set(state, payload.name, {
|
619
634
|
persist: payload.persist,
|
620
635
|
startId,
|
621
636
|
current: startId,
|
@@ -629,31 +644,31 @@ const FlowerCoreReducers = {
|
|
629
644
|
payload
|
630
645
|
}) => {
|
631
646
|
if (hasNode(state, payload.name, payload.node)) {
|
632
|
-
const startId =
|
647
|
+
const startId = _get(state, [payload.name, 'startId']);
|
633
648
|
if (payload.node === startId) {
|
634
|
-
|
635
|
-
|
649
|
+
_set(state, [payload.name, 'current'], startId);
|
650
|
+
_set(state, [payload.name, 'history'], [startId]);
|
636
651
|
} else {
|
637
|
-
|
652
|
+
_set(state, [payload.name, 'current'], payload.node);
|
638
653
|
}
|
639
654
|
}
|
640
655
|
},
|
641
656
|
formAddErrors: (state, {
|
642
657
|
payload
|
643
658
|
}) => {
|
644
|
-
|
659
|
+
_set(state, [payload.name, 'form', payload.currentNode, 'errors', payload.id], payload.errors);
|
645
660
|
},
|
646
661
|
formRemoveErrors: (state, {
|
647
662
|
payload
|
648
663
|
}) => {
|
649
|
-
|
650
|
-
|
664
|
+
_unset(state, [payload.name, 'form', payload.currentNode, 'errors', payload.id]);
|
665
|
+
_unset(state, [payload.name, 'form', payload.currentNode, 'isValidating']);
|
651
666
|
},
|
652
667
|
addData: (state, {
|
653
668
|
payload
|
654
669
|
}) => {
|
655
|
-
const prevData =
|
656
|
-
|
670
|
+
const prevData = _get(state, [payload.flowName, 'data']);
|
671
|
+
_set(state, [payload.flowName, 'data'], {
|
657
672
|
...prevData,
|
658
673
|
...payload.value
|
659
674
|
});
|
@@ -662,23 +677,23 @@ const FlowerCoreReducers = {
|
|
662
677
|
payload
|
663
678
|
}) => {
|
664
679
|
if (payload.id && payload.id.length) {
|
665
|
-
|
680
|
+
_set(state, [payload.flowName, 'data', ...payload.id], payload.value);
|
666
681
|
}
|
667
682
|
},
|
668
683
|
replaceData: (state, {
|
669
684
|
payload
|
670
685
|
}) => {
|
671
|
-
|
686
|
+
_set(state, [payload.flowName, 'data'], payload.value);
|
672
687
|
},
|
673
688
|
unsetData: (state, {
|
674
689
|
payload
|
675
690
|
}) => {
|
676
|
-
|
691
|
+
_unset(state, [payload.flowName, 'data', ...payload.id]);
|
677
692
|
},
|
678
693
|
setFormIsValidating: (state, {
|
679
694
|
payload
|
680
695
|
}) => {
|
681
|
-
|
696
|
+
_set(state, [payload.name, 'form', payload.currentNode, 'isValidating'], payload.isValidating);
|
682
697
|
},
|
683
698
|
node: (state, {
|
684
699
|
payload
|
@@ -689,8 +704,8 @@ const FlowerCoreReducers = {
|
|
689
704
|
} = payload;
|
690
705
|
const node = payload.nodeId || payload.node || '';
|
691
706
|
const flowName = name || payload.flowName || '';
|
692
|
-
const startNode =
|
693
|
-
const currentNodeId =
|
707
|
+
const startNode = _get(state, [payload.name, 'startId']);
|
708
|
+
const currentNodeId = _get(state, [payload.name, 'current'], startNode);
|
694
709
|
FlowerCoreReducers.setFormTouched(state, {
|
695
710
|
type: 'setFormTouched',
|
696
711
|
payload: {
|
@@ -698,7 +713,7 @@ const FlowerCoreReducers = {
|
|
698
713
|
currentNode: currentNodeId
|
699
714
|
}
|
700
715
|
});
|
701
|
-
if (global.window &&
|
716
|
+
if (global.window && _get(global.window, '__FLOWER_DEVTOOLS__') && history) {
|
702
717
|
FlowerCoreReducers.forceAddHistory(state, {
|
703
718
|
type: 'forceAddHistory',
|
704
719
|
payload: {
|
@@ -744,7 +759,7 @@ const FlowerCoreReducers = {
|
|
744
759
|
const currentNodeId = FlowerStateUtils.makeSelectCurrentNodeId(flowName)(state);
|
745
760
|
const currentNextRules = FlowerStateUtils.makeSelectCurrentNextRules(flowName)(state);
|
746
761
|
const form = FlowerStateUtils.makeSelectNodeErrors(flowName, currentNodeId)(state);
|
747
|
-
const clonedData =
|
762
|
+
const clonedData = _cloneDeep(FlowerStateUtils.getAllData(state));
|
748
763
|
const stateWithNodeData = {
|
749
764
|
$in: data,
|
750
765
|
$form: form,
|
@@ -775,7 +790,7 @@ const FlowerCoreReducers = {
|
|
775
790
|
return;
|
776
791
|
}
|
777
792
|
const validRule = findValidRule(currentNextRules, stateWithNodeData, flowName);
|
778
|
-
const nextNumberNode =
|
793
|
+
const nextNumberNode = _get(validRule, 'nodeId');
|
779
794
|
if (!nextNumberNode) {
|
780
795
|
return;
|
781
796
|
}
|
@@ -819,16 +834,16 @@ const FlowerCoreReducers = {
|
|
819
834
|
|
820
835
|
const FlowerCoreStateSelectors = {
|
821
836
|
selectGlobal: state => state && state.flower,
|
822
|
-
selectFlower: name => state =>
|
823
|
-
selectFlowerFormNode: id => state =>
|
824
|
-
selectFlowerHistory: flower =>
|
825
|
-
makeSelectNodesIds: flower =>
|
826
|
-
makeSelectStartNodeId: flower =>
|
827
|
-
getDataByFlow: flower =>
|
828
|
-
getDataFromState: id => data => id === '*' ? data :
|
837
|
+
selectFlower: name => state => _get(state, [name]),
|
838
|
+
selectFlowerFormNode: id => state => _get(state, ['form', id]),
|
839
|
+
selectFlowerHistory: flower => _get(flower, ['history'], []),
|
840
|
+
makeSelectNodesIds: flower => _get(flower, ['nodes']),
|
841
|
+
makeSelectStartNodeId: flower => _get(flower, ['startId']),
|
842
|
+
getDataByFlow: flower => _get(flower, ['data']) ?? {},
|
843
|
+
getDataFromState: id => data => id === '*' ? data : _get(data, id),
|
829
844
|
makeSelectNodeFormTouched: form => form && form.touched,
|
830
|
-
makeSelectCurrentNodeId: (flower, startNodeId) =>
|
831
|
-
makeSelectCurrentNodeDisabled: (nodes, current) => !!
|
845
|
+
makeSelectCurrentNodeId: (flower, startNodeId) => _get(flower, ['current']) || startNodeId,
|
846
|
+
makeSelectCurrentNodeDisabled: (nodes, current) => !!_get(nodes, [current, 'disabled']),
|
832
847
|
makeSelectPrevNodeRetain: (nodes, history, current) => {
|
833
848
|
if (!nodes) return;
|
834
849
|
const prevFlowerNode = [...history].reverse().find(el => {
|
@@ -872,14 +887,14 @@ const FlowerCoreStateSelectors = {
|
|
872
887
|
$form: form
|
873
888
|
};
|
874
889
|
const state = Object.assign(newState, id ? {
|
875
|
-
$self:
|
890
|
+
$self: _get(newState, [flowName, ...id.split('.')])
|
876
891
|
} : {});
|
877
892
|
if (!rules) return false;
|
878
893
|
if (!keys) return false;
|
879
894
|
const res = keys.reduce((acc, inc) => {
|
880
895
|
const k = inc;
|
881
896
|
return Object.assign(acc, {
|
882
|
-
[k]:
|
897
|
+
[k]: _get(state, k)
|
883
898
|
});
|
884
899
|
}, {});
|
885
900
|
const [disabled] = MatchRules.rulesMatcher(rules, {
|
@@ -1,5 +1,4 @@
|
|
1
1
|
export declare const MatchRules: {
|
2
2
|
rulesMatcher: (rules?: Record<string, any> | Record<string, any>[], formValue?: Record<string, any>, apply?: boolean, options?: Record<string, any>) => boolean[];
|
3
|
-
operators: import("./rules-matcher/interface").Operators;
|
4
3
|
utils: import("./rules-matcher/interface").RulesMatcherUtils;
|
5
4
|
};
|
package/package.json
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
{
|
2
2
|
"name": "@flowerforce/flower-core",
|
3
|
-
"version": "3.0.
|
3
|
+
"version": "3.0.1-beta.1",
|
4
4
|
"description": "Core functions for flowerJS",
|
5
|
+
"repository": {
|
6
|
+
"type": "git",
|
7
|
+
"url": "git+https://github.com/flowerforce/flower.git"
|
8
|
+
},
|
9
|
+
"homepage": "https://www.flowerjs.it",
|
5
10
|
"publishConfig": {
|
6
11
|
"access": "public"
|
7
12
|
},
|