-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathNodeModel.js
142 lines (114 loc) · 3.83 KB
/
NodeModel.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
class NodeModel {
constructor(props, nodes = {}) {
this.props = props;
this.flatNodes = nodes;
}
setProps(props) {
this.props = props;
}
clone() {
const clonedNodes = {};
// Re-construct nodes one level deep to avoid shallow copy of mutable characteristics
Object.keys(this.flatNodes).forEach((value) => {
const node = this.flatNodes[value];
clonedNodes[value] = { ...node };
});
return new NodeModel(this.props, clonedNodes);
}
getNode(value) {
return this.flatNodes[value];
}
flattenNodes(nodes, parent = {}, depth = 0) {
if (!Array.isArray(nodes) || nodes.length === 0) {
return;
}
const { disabled, noCascade } = this.props;
// Flatten the `node` property for internal lookups
nodes.forEach((node, index) => {
const isParent = this.nodeHasChildren(node);
this.flatNodes[node.value] = {
label: node.label,
value: node.value,
children: node.children,
parent,
isParent,
isLeaf: !isParent,
showCheckbox: node.showCheckbox !== undefined ? node.showCheckbox : true,
disabled: this.getDisabledState(node, parent, disabled, noCascade),
treeDepth: depth,
index,
};
this.flattenNodes(node.children, node, depth + 1);
});
}
nodeHasChildren(node) {
return Array.isArray(node.children) && node.children.length > 0;
}
getDisabledState(node, parent, disabledProp, noCascade) {
if (disabledProp) {
return true;
}
if (!noCascade && parent.disabled) {
return true;
}
return Boolean(node.disabled);
}
deserializeLists(lists) {
const listKeys = ['checked', 'expanded'];
// Reset values to false
Object.keys(this.flatNodes).forEach((value) => {
listKeys.forEach((listKey) => {
this.flatNodes[value][listKey] = false;
});
});
// Deserialize values and set their nodes to true
listKeys.forEach((listKey) => {
lists[listKey].forEach((value) => {
if (this.flatNodes[value] !== undefined) {
this.flatNodes[value][listKey] = true;
}
});
});
}
serializeList(key) {
const list = [];
Object.keys(this.flatNodes).forEach((value) => {
if (this.flatNodes[value][key]) {
list.push(value);
}
});
return list;
}
expandAllNodes(expand) {
Object.keys(this.flatNodes).forEach((value) => {
if (this.flatNodes[value].isParent) {
this.flatNodes[value].expanded = expand;
}
});
return this;
}
toggleChecked(node, isChecked, noCascade, useTopNode) {
const flatNode = this.flatNodes[node.value];
if (flatNode.isLeaf || noCascade) {
if (node.disabled) {
return this;
}
// Set the check status of a leaf node or an uncoupled parent
this.toggleNode(node.value, 'checked', isChecked);
} else {
if (useTopNode) {
this.toggleNode(node.value, 'checked', isChecked);
}
// Percolate check status down to all children
flatNode.children.forEach((child) => {
this.toggleChecked(child, useTopNode ? false : isChecked, noCascade, useTopNode);
});
}
return this;
}
toggleNode(nodeValue, key, toggleValue) {
this.flatNodes[nodeValue][key] = toggleValue;
return this;
}
}
export default NodeModel;