forked from ukrbublik/react-awesome-query-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.js
89 lines (78 loc) · 3.09 KB
/
tree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import Immutable, { fromJS, Map } from "immutable";
import {validateTree} from "../utils/validation";
import {extendConfig} from "../utils/configUtils";
import {getTreeBadFields, getLightTree, _fixImmutableValue} from "../utils/treeUtils";
import {isJsonLogic} from "../utils/stuff";
export const getTree = (immutableTree, light = true, children1AsArray = true) => {
if (!immutableTree) return undefined;
let tree = immutableTree;
tree = tree.toJS();
if (light)
tree = getLightTree(tree, children1AsArray);
return tree;
};
export const loadTree = (serTree) => {
if (isImmutableTree(serTree)) {
return serTree;
} else if (isTree(serTree)) {
return jsToImmutable(serTree);
} else if (typeof serTree == "string" && serTree.startsWith('["~#iM"')) {
//tip: old versions of RAQB were saving tree with `transit.toJSON()`
// https://github.com/ukrbublik/react-awesome-query-builder/issues/69
throw "You are trying to load query in obsolete serialization format (Immutable string) which is not supported in versions starting from 2.1.17";
} else if (typeof serTree == "string") {
return jsToImmutable(JSON.parse(serTree));
} else throw "Can't load tree!";
};
export const checkTree = (tree, config) => {
if (!tree) return undefined;
const extendedConfig = extendConfig(config, undefined, true);
return validateTree(tree, null, extendedConfig, extendedConfig);
};
export const isValidTree = (tree) => {
return getTreeBadFields(tree).length == 0;
};
export const isImmutableTree = (tree) => {
return Map.isMap(tree);
};
export const isTree = (tree) => {
return typeof tree == "object" && (tree.type == "group" || tree.type == "switch_group");
};
export {isJsonLogic};
export function jsToImmutable(tree) {
const imm = fromJS(tree, function (key, value, path) {
const isFuncArg = path
&& path.length > 3
&& path[path.length-1] === "value"
&& path[path.length-3] === "args";
const isRuleValue = path
&& path.length > 3
&& path[path.length-1] === "value"
&& path[path.length-2] === "properties";
let outValue;
if (key == "properties") {
outValue = value.toOrderedMap();
// `value` should be undefined instead of null
// JSON doesn't support undefined and replaces undefined -> null
// So fix: null -> undefined
for (let i = 0 ; i < 2 ; i++) {
if (outValue.get("value")?.get(i) === null) {
outValue = outValue.setIn(["value", i], undefined);
}
}
} else if (isFuncArg) {
outValue = _fixImmutableValue(value);
} else if ((path ? isRuleValue : key == "value") && Immutable.Iterable.isIndexed(value)) {
outValue = value.map(_fixImmutableValue).toList();
} else if (key == "asyncListValues") {
// keep in JS format
outValue = value.toJS();
} else if (key == "children1" && Immutable.Iterable.isIndexed(value)) {
outValue = new Immutable.OrderedMap(value.map(child => [child.get("id"), child]));
} else {
outValue = Immutable.Iterable.isIndexed(value) ? value.toList() : value.toOrderedMap();
}
return outValue;
});
return imm;
}