forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.js
More file actions
244 lines (235 loc) · 5.59 KB
/
Copy pathtree.js
File metadata and controls
244 lines (235 loc) · 5.59 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
import React from "react";
const { Component, createFactory, createElement } = React;
import Components from "../index";
const Tree = createFactory(Components.Tree);
import { storiesOf } from "@storybook/react";
storiesOf("Tree", module)
.add("Simple tree - autoExpand 1", () => {
return renderTree({
autoExpandDepth: 1,
});
})
.add("Simple tree - autoExpand 2", () => {
return renderTree({
autoExpandDepth: 2,
});
})
.add("Simple tree - autoExpand ∞", () => {
return renderTree({
autoExpandDepth: Infinity,
});
})
.add("Multiple root tree", () => {
return renderTree({
autoExpandDepth: Infinity,
getRoots: () => ["A", "P", "M", "Q", "W", "R"],
});
})
.add("focused node", () => {
return renderTree({
focused: "W",
getRoots: () => ["A", "W"],
expanded: new Set(["A"])
});
})
.add("variable height nodes", () => {
const nodes = Array.from({length: 10})
.map((_, i) => `item ${i + 1} - `.repeat(10 + Math.random() * 50));
return renderTree({
getRoots: () => ["ROOT"],
expanded: new Set(["ROOT"])
}, {
children: {"ROOT": nodes}
});
})
.add("scrollable tree", () => {
const nodes = Array.from({length: 500}).map((_, i) => (i + 1).toString());
class container extends Component {
constructor(props) {
super(props);
const state = {
expanded: new Set(),
focused: null
};
this.state = state;
}
render() {
return createElement("div", {},
createElement("label", {
style: {position: "fixed", right: 0},
},
"Enter node number to set focus on: ",
createElement("input", {
type: "number",
min: 1,
max: 500,
onInput: e => {
// Storing balue since `e` can be reused by the time the setState
// callback is called.
const value = e.target.value.toString();
this.setState(previousState => {
return {focused: value};
});
}
}),
),
createTreeElement({getRoots: () => nodes}, this, {})
);
}
}
return createFactory(container)();
})
.add("scrollable tree with focused node", () => {
const nodes = Array.from({length: 500}).map((_, i) => `item ${i + 1}`);
return renderTree({
focused: "item 250",
getRoots: () => nodes,
}, {});
})
.add("1000 items tree", () => {
const nodes = Array.from({length: 1000}).map((_, i) => `item-${i + 1}`);
return renderTree({
getRoots: () => ["ROOT"],
expanded: new Set()
}, {
children: {"ROOT": nodes}
});
})
.add("30,000 items tree", () => {
const nodes = Array.from({length: 1000}).map((_, i) => `item-${i + 1}`);
return renderTree({
getRoots: () => nodes,
expanded: new Set(Array.from({length: 2000}).map((_, i) => `item-${i + 1}`))
}, {
children: Array.from({length: 2000}).reduce((res, _, i) => {
res[`item-${i + 1}`] = [`item-${i + 1001}`];
return res;
}, {})
});
});
// Encoding of the following tree/forest:
//
// A
// |-- B
// | |-- E
// | | |-- K
// | | `-- L
// | |-- F
// | `-- G
// |-- C
// | |-- H
// | `-- I
// `-- D
// `-- J
// M
// `-- N
// `-- O
// P
// Q
// R
// W
// `-- X
// `-- Z
// `-- Y
const TEST_TREE = {
children: {
A: ["B", "C", "D"],
B: ["E", "F", "G"],
C: ["H", "I"],
D: ["J"],
E: ["K", "L"],
F: [],
G: [],
H: [],
I: [],
J: [],
K: [],
L: [],
M: ["N"],
N: ["O"],
O: [],
P: [],
Q: [],
R: [],
W: ["X", "Y"],
X: ["Z"],
Y: [],
Z: [],
},
parent: {
A: null,
B: "A",
C: "A",
D: "A",
E: "B",
F: "B",
G: "B",
H: "C",
I: "C",
J: "D",
K: "E",
L: "E",
M: null,
N: "M",
O: "N",
P: null,
Q: null,
R: null,
W: null,
X: "W",
Y: "W",
Z: "X",
},
};
function renderTree(props, tree = TEST_TREE) {
class container extends Component {
constructor(props2) {
super(props2);
const state = {
expanded: props2.expanded || new Set(),
focused: props2.focused
};
delete props2.focused;
this.state = state;
}
render() {
return createTreeElement(props, this, tree);
}
}
return createFactory(container)();
}
function createTreeElement(props, context, tree) {
return Tree(Object.assign({
getParent: x => tree.parent && tree.parent[x],
getChildren: x => tree.children && tree.children[x]
? tree.children[x]
: [],
renderItem: (x, depth, focused, arrow, expanded) => [arrow, x],
getRoots: () => ["A"],
getKey: x => "key-" + x,
onFocus: x => {
context.setState(previousState => {
return {focused: x};
});
},
onExpand: x => {
context.setState(previousState => {
const expanded = new Set(previousState.expanded);
expanded.add(x);
return {expanded};
});
},
onCollapse: x => {
context.setState(previousState => {
const expanded = new Set(previousState.expanded);
expanded.delete(x);
return {expanded};
});
},
isExpanded: x => context.state && context.state.expanded.has(x),
focused: context.state.focused,
}, props));
}