-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathNodeModel.js
179 lines (143 loc) · 5.25 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import constants from './constants';
const { CheckModel } = constants;
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,
isChild: parent.value !== undefined,
isParent,
isLeaf: !isParent,
showCheckbox: node.showCheckbox !== undefined ? node.showCheckbox : true,
disabled: this.getDisabledState(node, parent, disabled, noCascade),
treeDepth: depth,
index,
extraKeys: Object.entries(node)
.filter(([key]) => !CheckModel.flatKeys.includes(key))
.reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {}),
};
this.flattenNodes(node.children, node, depth + 1);
});
}
nodeHasChildren(node) {
return Array.isArray(node.children);
}
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, checkModel, noCascade, percolateUpward = true) {
const flatNode = this.flatNodes[node.value];
const modelHasParents = [CheckModel.PARENT, CheckModel.ALL].indexOf(checkModel) > -1;
const modelHasLeaves = [CheckModel.LEAF, CheckModel.ALL].indexOf(checkModel) > -1;
if (flatNode.isLeaf || noCascade) {
if (node.disabled) {
return this;
}
this.toggleNode(node.value, 'checked', isChecked);
} else {
if (modelHasParents) {
this.toggleNode(node.value, 'checked', isChecked);
}
if (modelHasLeaves) {
// Percolate check status down to all children
flatNode.children.forEach((child) => {
this.toggleChecked(child, isChecked, checkModel, noCascade, false);
});
}
}
// Percolate check status up to parent
// The check model must include parent nodes and we must not have already covered the
// parent (relevant only when percolating through children)
if (percolateUpward && !noCascade && flatNode.isChild && modelHasParents) {
this.toggleParentStatus(flatNode.parent, checkModel);
}
return this;
}
toggleParentStatus(node, checkModel) {
const flatNode = this.flatNodes[node.value];
if (flatNode.isChild) {
if (checkModel === CheckModel.ALL) {
this.toggleNode(node.value, 'checked', this.isEveryChildChecked(flatNode));
}
this.toggleParentStatus(flatNode.parent, checkModel);
} else {
this.toggleNode(node.value, 'checked', this.isEveryChildChecked(flatNode));
}
}
isEveryChildChecked(node) {
return node.children.every((child) => this.getNode(child.value).checked);
}
toggleNode(nodeValue, key, toggleValue) {
this.flatNodes[nodeValue][key] = toggleValue;
return this;
}
}
export default NodeModel;