-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexamples-basic.html
More file actions
275 lines (239 loc) · 12.3 KB
/
Copy pathexamples-basic.html
File metadata and controls
275 lines (239 loc) · 12.3 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TreeView - Basic Usage</title>
<link rel="stylesheet" href="examples-shared.css">
</head>
<body>
<div class="container">
<header>
<a href="index.html" class="back-link">← Back to Examples</a>
<h1>Basic Usage</h1>
<p class="subtitle">Simple tree rendering, expand/collapse, node selection, and hierarchical data</p>
</header>
<!-- Example 1: Simple Tree -->
<div class="card">
<h2>Example 1: Simple File Tree</h2>
<p>A basic tree with hierarchical data using path-based structure. Click nodes to select, click the toggle icon to expand/collapse.</p>
<div class="tree-container" id="tree1-container">
<web-treeview id="tree1"></web-treeview>
</div>
<div class="output">
<p class="output-label">Selected node:</p>
<pre id="tree1-output">Click a node to see selection info</pre>
</div>
</div>
<!-- Example 2: Expand Level -->
<div class="card">
<h2>Example 2: Pre-expanded Tree</h2>
<p>Use <code>expand-level</code> to control how many levels are expanded on initial render. This tree starts with 2 levels expanded.</p>
<div class="tree-container" id="tree2-container">
<web-treeview id="tree2" expand-level="2"></web-treeview>
</div>
</div>
<!-- Example 3: Large Flat Dataset -->
<div class="card">
<h2>Example 3: Large Dataset with Progressive Rendering</h2>
<p>A tree with 1000+ nodes demonstrating progressive rendering. Nodes are rendered in batches to keep the UI responsive.</p>
<div class="controls">
<button onclick="loadLargeTree()">Load 1,000 nodes</button>
<button onclick="loadVeryLargeTree()">Load 5,000 nodes</button>
<button class="secondary" onclick="clearLargeTree()">Clear</button>
</div>
<div class="tree-container tree-container-lg" id="tree3-container">
<web-treeview id="tree3" progressive-render="true"></web-treeview>
</div>
<div class="output">
<p class="output-label">Render stats:</p>
<pre id="tree3-output">Load data to see render statistics</pre>
</div>
</div>
<!-- Example 4: Custom Member Mappings -->
<div class="card">
<h2>Example 4: Custom Data Mappings</h2>
<p>Map your own data shape to the tree using <code>id-member</code>, <code>path-member</code>, and <code>display-value-member</code> attributes.</p>
<div class="tree-container" id="tree4-container">
<web-treeview id="tree4"></web-treeview>
</div>
<div class="code-block">
<pre><code>// Your data can use any property names
const data = [
{ categoryId: 1, categoryPath: "1", label: "Electronics", ... },
{ categoryId: 2, categoryPath: "1.1", label: "Phones", ... }
];
// Map them to tree properties
element.setAttribute('id-member', 'categoryId');
element.setAttribute('path-member', 'categoryPath');
element.setAttribute('display-value-member', 'label');</code></pre>
</div>
</div>
<!-- Example 5: Events -->
<div class="card">
<h2>Example 5: Events</h2>
<p>Listen to tree events: <code>node-clicked</code>, <code>selected-node-changed</code>, <code>tree-changed</code>.</p>
<div class="tree-container" id="tree5-container">
<web-treeview id="tree5" expand-level="1"></web-treeview>
</div>
<div class="output">
<p class="output-label">Event log:</p>
<div class="event-log" id="tree5-log"></div>
</div>
<button onclick="document.getElementById('tree5-log').innerHTML = ''" class="btn-outline" style="margin-top: 0.5rem">Clear log</button>
</div>
</div>
<script type="module">
import '/src/index.ts';
// ── Sample Data ──────────────────────────────────────────────────
const fileTree = [
{ id: 1, path: '1', name: 'src' },
{ id: 2, path: '1.1', name: 'components' },
{ id: 3, path: '1.1.1', name: 'Button.tsx' },
{ id: 4, path: '1.1.2', name: 'Input.tsx' },
{ id: 5, path: '1.1.3', name: 'Modal.tsx' },
{ id: 6, path: '1.2', name: 'pages' },
{ id: 7, path: '1.2.1', name: 'Home.tsx' },
{ id: 8, path: '1.2.2', name: 'About.tsx' },
{ id: 9, path: '1.2.3', name: 'Settings.tsx' },
{ id: 10, path: '1.3', name: 'utils' },
{ id: 11, path: '1.3.1', name: 'helpers.ts' },
{ id: 12, path: '1.3.2', name: 'constants.ts' },
{ id: 13, path: '2', name: 'public' },
{ id: 14, path: '2.1', name: 'index.html' },
{ id: 15, path: '2.2', name: 'favicon.ico' },
{ id: 16, path: '3', name: 'package.json' },
{ id: 17, path: '4', name: 'tsconfig.json' },
{ id: 18, path: '5', name: 'README.md' }
];
const orgChart = [
{ id: 1, path: '1', name: 'CEO - Alice' },
{ id: 2, path: '1.1', name: 'CTO - Bob' },
{ id: 3, path: '1.1.1', name: 'Dev Lead - Charlie' },
{ id: 4, path: '1.1.1.1', name: 'Frontend Dev - Dave' },
{ id: 5, path: '1.1.1.2', name: 'Backend Dev - Eve' },
{ id: 6, path: '1.1.1.3', name: 'DevOps - Frank' },
{ id: 7, path: '1.1.2', name: 'QA Lead - Grace' },
{ id: 8, path: '1.1.2.1', name: 'QA Engineer - Hank' },
{ id: 9, path: '1.2', name: 'CFO - Ivy' },
{ id: 10, path: '1.2.1', name: 'Accountant - Jack' },
{ id: 11, path: '1.2.2', name: 'Financial Analyst - Kim' },
{ id: 12, path: '1.3', name: 'COO - Leo' },
{ id: 13, path: '1.3.1', name: 'HR Manager - Mia' },
{ id: 14, path: '1.3.2', name: 'Office Manager - Noah' }
];
const categories = [
{ categoryId: 1, categoryPath: '1', label: 'Electronics' },
{ categoryId: 2, categoryPath: '1.1', label: 'Phones' },
{ categoryId: 3, categoryPath: '1.1.1', label: 'Smartphones' },
{ categoryId: 4, categoryPath: '1.1.2', label: 'Feature Phones' },
{ categoryId: 5, categoryPath: '1.2', label: 'Laptops' },
{ categoryId: 6, categoryPath: '1.2.1', label: 'Gaming Laptops' },
{ categoryId: 7, categoryPath: '1.2.2', label: 'Ultrabooks' },
{ categoryId: 8, categoryPath: '1.3', label: 'Tablets' },
{ categoryId: 9, categoryPath: '2', label: 'Clothing' },
{ categoryId: 10, categoryPath: '2.1', label: 'Men' },
{ categoryId: 11, categoryPath: '2.1.1', label: 'Shirts' },
{ categoryId: 12, categoryPath: '2.1.2', label: 'Pants' },
{ categoryId: 13, categoryPath: '2.2', label: 'Women' },
{ categoryId: 14, categoryPath: '2.2.1', label: 'Dresses' },
{ categoryId: 15, categoryPath: '2.2.2', label: 'Skirts' },
{ categoryId: 16, categoryPath: '3', label: 'Books' },
{ categoryId: 17, categoryPath: '3.1', label: 'Fiction' },
{ categoryId: 18, categoryPath: '3.2', label: 'Non-Fiction' }
];
// ── Generate large datasets ─────────────────────────────────────
function generateLargeTree(count) {
const data = [];
let id = 1;
const topLevelCount = Math.min(count, 20);
const perBranch = Math.floor(count / topLevelCount);
for (let i = 1; i <= topLevelCount; i++) {
data.push({ id: id++, path: `${i}`, name: `Branch ${i}` });
const childCount = Math.min(perBranch, 50);
for (let j = 1; j <= childCount; j++) {
data.push({ id: id++, path: `${i}.${j}`, name: `Item ${i}.${j}` });
const subCount = Math.floor((count - topLevelCount * childCount) / (topLevelCount * childCount));
for (let k = 1; k <= Math.min(subCount, 10); k++) {
data.push({ id: id++, path: `${i}.${j}.${k}`, name: `Detail ${i}.${j}.${k}` });
if (id > count) break;
}
if (id > count) break;
}
if (id > count) break;
}
return data;
}
// ── Initialize trees ────────────────────────────────────────────
customElements.whenDefined('web-treeview').then(() => {
// Tree 1: Simple file tree
const tree1 = document.getElementById('tree1');
tree1.data = fileTree;
tree1.idMember = 'id';
tree1.pathMember = 'path';
tree1.displayValueMember = 'name';
tree1.expandLevel = 1;
tree1.addEventListener('node-clicked', (e) => {
const node = e.detail.node;
document.getElementById('tree1-output').textContent = JSON.stringify({
id: node.id,
path: node.path,
name: node.data?.name,
level: node.level,
hasChildren: node.hasChildren,
isExpanded: node.isExpanded
}, null, 2);
});
// Tree 2: Org chart with expand-level=2
const tree2 = document.getElementById('tree2');
tree2.data = orgChart;
tree2.idMember = 'id';
tree2.pathMember = 'path';
tree2.displayValueMember = 'name';
// Tree 3: Large dataset (starts empty)
const tree3 = document.getElementById('tree3');
tree3.idMember = 'id';
tree3.pathMember = 'path';
tree3.displayValueMember = 'name';
tree3.onRenderComplete = (stats) => {
document.getElementById('tree3-output').textContent = JSON.stringify(stats, null, 2);
};
window.loadLargeTree = () => {
tree3.data = generateLargeTree(1000);
};
window.loadVeryLargeTree = () => {
tree3.data = generateLargeTree(5000);
};
window.clearLargeTree = () => {
tree3.data = [];
document.getElementById('tree3-output').textContent = 'Load data to see render statistics';
};
// Tree 4: Custom member mappings
const tree4 = document.getElementById('tree4');
tree4.data = categories;
tree4.idMember = 'categoryId';
tree4.pathMember = 'categoryPath';
tree4.displayValueMember = 'label';
tree4.expandLevel = 1;
// Tree 5: Events
const tree5 = document.getElementById('tree5');
tree5.data = fileTree;
tree5.idMember = 'id';
tree5.pathMember = 'path';
tree5.displayValueMember = 'name';
const logEl = document.getElementById('tree5-log');
function logEvent(name, detail) {
const time = new Date().toLocaleTimeString();
const info = detail?.node ? ` - ${detail.node.data?.name} (path: ${detail.node.path})` : '';
const entry = document.createElement('div');
entry.className = 'event-entry';
entry.innerHTML = `<span class="event-time">${time}</span> <span class="event-name">${name}</span>${info}`;
logEl.prepend(entry);
}
tree5.addEventListener('node-clicked', (e) => logEvent('node-clicked', e.detail));
tree5.addEventListener('selected-node-changed', (e) => logEvent('selected-node-changed', e.detail));
tree5.addEventListener('tree-changed', () => logEvent('tree-changed'));
});
</script>
</body>
</html>