Repository navigation
Expand file tree
/
Copy pathexamples-basic.html
More file actions
505 lines (444 loc) · 23.8 KB
/
Copy pathexamples-basic.html
File metadata and controls
505 lines (444 loc) · 23.8 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
<meta name="kmwc:title" content="Basic Usage">
<meta name="kmwc:subtitle" content="Simple tree rendering, expand/collapse, node selection, and hierarchical data">
<link rel="stylesheet" href="examples-local.css">
<!-- Example 1: Simple Tree -->
<div class="card">
<h2>BU01 · 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="controls">
<span class="muted">Display:</span>
<button id="indent-condensed" onclick="applyPreset('condensed', this)">Condensed</button>
<button id="indent-compact" onclick="applyPreset('compact', this)">Compact</button>
<button id="indent-generous" onclick="applyPreset('generous', this)">Generous</button>
</div>
<div class="display-sliders">
<label for="sl-indent">--wtv-indent-size</label>
<input type="range" id="sl-indent" min="0" max="48" step="1" value="32">
<output id="out-indent">32px</output>
<label for="sl-column">--wtv-toggle-icon-width</label>
<input type="range" id="sl-column" min="0" max="48" step="1" value="24">
<output id="out-column">24px</output>
<label for="sl-padding">--wtv-node-padding (left)</label>
<input type="range" id="sl-padding" min="0" max="24" step="1" value="8">
<output id="out-padding">8px</output>
</div>
<div id="indent-readout" class="css-readout"></div>
<div class="tree-container" id="tree1-container">
<web-treeview id="tree1"></web-treeview>
</div>
<div class="note">
<p class="note-title">What makes a row's left offset</p>
<p style="margin-bottom:0.5rem">
A row sits in by <b>toggle gutter + content padding + (level − 1) × indent</b>. The
indentation buttons only move <code>--wtv-indent-size</code> — multiplied by
<code>(level − 1)</code>, so at the <em>root</em> that's × 0 and the root rows never move.
The fixed part left of every label (highlighted amber above) is the <strong>toggle column</strong>
(<code>--wtv-toggle-icon-width</code>, reserved on every row so leaf labels align under branch
labels) plus the content's left <code>--wtv-node-padding</code> — independent of indent.
</p>
<p style="margin-bottom:0">
To tighten the root gap, lower <code>--wtv-toggle-icon-width</code> (and/or
<code>--wtv-node-padding</code>); to change nesting density, use <code>--wtv-indent-size</code>.
All inherit through the Shadow DOM when set inline on <code><web-treeview></code>, so rows
re-flow instantly.
</p>
</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>BU02 · 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="controls">
<label for="tree2-expand-level">expand-level:</label>
<input type="number" id="tree2-expand-level" min="0" max="10" step="1" value="2" class="w-sm">
<span class="muted">levels expanded (0 = roots only)</span>
</div>
<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>BU03 · 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" enable-progressive-render="true"></web-treeview>
</div>
<div class="output">
<p class="output-label">Render stats:</p>
<pre id="tree3-output">Load data to see node counts</pre>
</div>
</div>
<!-- Example 4: Custom Member Mappings -->
<div class="card">
<h2>BU04 · 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" data-language="js">
<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>BU05 · Events</h2>
<p>Listen to tree events: <code>node-click</code>, <code>focus-change</code>, <code>tree-change</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="log-panel log-panel--dark" id="tree5-log" data-empty="Click a node or move the focus…"></div>
</div>
<button onclick="document.getElementById('tree5-log').innerHTML = ''" class="btn-outline mt-1">Clear log</button>
</div>
<!-- NEW (rc03): Accordion Expand -->
<div class="card">
<h2>BU06 · Accordion Expand</h2>
<p>With <code>expand-as-accordion="true"</code>, opening one branch automatically collapses its siblings at the same level. Useful when you want only one section open at a time (e.g. file managers, settings panels). User-driven toggles honor it; programmatic <code>expandNodes()</code> calls are unaffected.</p>
<div class="controls">
<label>
<input type="checkbox" id="acc-enabled" onchange="applyAccordion()" checked>
Accordion mode
</label>
<button class="btn-outline" onclick="document.getElementById('tree-acc').collapseAll()">Collapse all</button>
</div>
<div class="tree-container">
<web-treeview id="tree-acc" expand-as-accordion="true" expand-level="1"></web-treeview>
</div>
<p class="form-text mt-1">
Try expanding "src", then "public" — with accordion on, opening "public" auto-collapses "src". Untick the box and the two sections can stay open together.
</p>
</div>
<!-- Row animation -->
<div class="card">
<h2>BU07 · Row Animation</h2>
<p>Rows an expand reveals fade and slide in; rows a collapse hides shrink and fade out, then the node really collapses. It is on by default and part of <code>motion</code>: the <code>motion</code> attribute, a <code>data-motion</code> section, the page-wide <code>window.components.motion</code> or the OS reduced-motion setting turn it off together with every other animation. There is no on/off attribute — to turn off just the rows, set <code>--wtv-row-animation-duration: 0s</code>.</p>
<div class="controls">
<label>Duration (<code>--wtv-row-animation-duration</code>)
<select id="ra-speed">
<option value="normal">--wtv-transition-normal (default)</option>
<option value="slow" selected>--wtv-transition-slow</option>
<option value="off">0s (rows off)</option>
</select>
</label>
<label>
<input type="checkbox" id="ra-fade">
Plain fade (<code>--wtv-row-animation-distance: 0</code>)
</label>
<label><code>row-animation-limit</code>
<select id="ra-limit">
<option value="50" selected>50 (default)</option>
<option value="5">5</option>
<option value="0">0 (none)</option>
</select>
</label>
<label><code>motion</code>
<select id="ra-motion">
<option value="auto" selected>auto</option>
<option value="smooth">smooth</option>
<option value="snappy">snappy</option>
<option value="none">none</option>
</select>
</label>
</div>
<div class="controls">
<button onclick="document.getElementById('tree-ra').expandAll()">Expand all</button>
<button class="btn-outline" onclick="collapseRowTree()">await collapseAll()</button>
<button onclick="document.getElementById('tree-ra').expandNodes('1')">Expand "src" (1)</button>
<button class="btn-outline" onclick="document.getElementById('tree-ra').collapseNodes('1')">Collapse "src" (1)</button>
</div>
<div class="tree-container">
<web-treeview id="tree-ra" expand-level="1"></web-treeview>
</div>
<p class="form-text mt-1" id="tree-ra-status"></p>
<div class="code-block" data-language="css">
<pre><code>/* Rows only — everything else keeps animating */
web-treeview.my-tree { --wtv-row-animation-duration: 0s; }
/* Slower rows, plain fade (no slide) */
web-treeview.my-tree {
--wtv-row-animation-duration: var(--wtv-transition-slow);
--wtv-row-animation-distance: 0;
}</code></pre>
</div>
<div class="code-block" data-language="html">
<pre><code><!-- motion="none" (or window.components.motion = 'none') = no animation at all -->
<web-treeview motion="none"></web-treeview>
<!-- At most 5 rows animate per expand / collapse; the rest appear / go at once -->
<web-treeview row-animation-limit="5"></web-treeview></code></pre>
</div>
<div class="code-block" data-language="js">
<pre><code>// The collapse applies after the rows animated out — await it before reading state
await tree.collapseAll();
await tree.collapseNodes('1');</code></pre>
</div>
<p class="form-text mt-1">
Only expands and collapses animate — never the first render, a reload, a filter or a re-render. Toggling a node again while its rows are animating out cancels the collapse (it stays open). <code>row-animation-limit</code> is a performance cap: expanding a node with thousands of children animates just the first rows.
</p>
</div>
<script type="module">
import { appendLogLine, attachControlsPersistence } from '@keenmate/web-components-devkit/examples';
// ── 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.nodes = fileTree;
tree1.idMember = 'id';
tree1.pathMember = 'path';
tree1.displayValueMember = 'name';
tree1.expandLevel = 1;
// Display demo: three sliders drive the CSS knobs that compose a row's
// left offset. --wtv-indent-size is the per-level indent; --wtv-toggle-icon-width
// is the fixed toggle gutter reserved on every row; --wtv-node-padding's
// horizontal part is the content's left pad. Setting them inline on the host
// inherits into the Shadow DOM and overrides the stylesheet default, so rows
// re-flow with no re-render. Presets just snap all three sliders at once.
const slIndent = document.getElementById('sl-indent');
const slColumn = document.getElementById('sl-column');
const slPadding = document.getElementById('sl-padding');
const PRESETS = {
condensed: { indent: 8, column: 16, padding: 4 },
compact: { indent: 16, column: 20, padding: 6 },
generous: { indent: 32, column: 24, padding: 8 }
};
// Push the three slider values onto the host as inline CSS variables and
// refresh the readout (including the derived root gap = column + left pad).
function applySliders() {
const indent = +slIndent.value;
const column = +slColumn.value;
const padding = +slPadding.value;
tree1.style.setProperty('--wtv-indent-size', `${indent}px`);
tree1.style.setProperty('--wtv-toggle-icon-width', `${column}px`);
// Only the horizontal (left/right) part — keep the vertical pad intact.
tree1.style.setProperty('--wtv-node-padding', `var(--wtv-spacing-sm) ${padding}px`);
document.getElementById('out-indent').textContent = `${indent}px`;
document.getElementById('out-column').textContent = `${column}px`;
document.getElementById('out-padding').textContent = `${padding}px`;
document.getElementById('indent-readout').innerHTML = [
`<code>--wtv-indent-size: <b>${indent}px</b></code>`,
`<code>--wtv-toggle-icon-width: <b>${column}px</b></code>`,
`<code>--wtv-node-padding (left): <b>${padding}px</b></code>`,
`<code class="is-gap">root gap (gutter + padding): <b>${column + padding}px</b></code>`
].join('');
}
for (const sl of [slIndent, slColumn, slPadding]) {
sl.addEventListener('input', applySliders);
}
window.applyPreset = (preset, btn) => {
const p = PRESETS[preset];
slIndent.value = p.indent;
slColumn.value = p.column;
slPadding.value = p.padding;
for (const id of ['indent-condensed', 'indent-compact', 'indent-generous']) {
document.getElementById(id).classList.toggle('secondary', document.getElementById(id) !== btn);
}
applySliders();
};
// Start on the stated defaults (indent 32 / column 24 / pad 8 → root gap 32 = Generous).
window.applyPreset('generous', document.getElementById('indent-generous'));
tree1.addEventListener('node-click', (e) => {
const { node } = e.detail;
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.nodes = orgChart;
tree2.idMember = 'id';
tree2.pathMember = 'path';
tree2.displayValueMember = 'name';
// Live expand-level control: the number field re-drives expandLevel, which
// re-expands the tree from the root down to the requested depth.
document.getElementById('tree2-expand-level').addEventListener('input', (e) => {
const level = Math.max(0, Math.min(10, parseInt(e.target.value, 10) || 0));
tree2.expandLevel = level;
});
// Tree 3: Large dataset (starts empty)
const tree3 = document.getElementById('tree3');
tree3.idMember = 'id';
tree3.pathMember = 'path';
tree3.displayValueMember = 'name';
const showLargeTreeStats = (count) => {
document.getElementById('tree3-output').textContent =
`${count} nodes loaded · ${tree3.controller.getVisibleFlatNodes().length} visible`;
};
window.loadLargeTree = async () => {
tree3.nodes = generateLargeTree(1000);
await tree3.whenSettled();
showLargeTreeStats(1000);
};
window.loadVeryLargeTree = async () => {
tree3.nodes = generateLargeTree(5000);
await tree3.whenSettled();
showLargeTreeStats(5000);
};
window.clearLargeTree = () => {
tree3.nodes = [];
document.getElementById('tree3-output').textContent = 'Load data to see node counts';
};
// Tree 4: Custom member mappings
const tree4 = document.getElementById('tree4');
tree4.nodes = categories;
tree4.idMember = 'categoryId';
tree4.pathMember = 'categoryPath';
tree4.displayValueMember = 'label';
tree4.expandLevel = 1;
// Tree 5: Events
const tree5 = document.getElementById('tree5');
tree5.nodes = fileTree;
tree5.idMember = 'id';
tree5.pathMember = 'path';
tree5.displayValueMember = 'name';
const logEl = document.getElementById('tree5-log');
function logEvent(name, detail) {
const node = detail?.node ?? detail?.focusedNode;
appendLogLine(logEl, { name, detail: node ? `${node.data?.name} (path: ${node.path})` : undefined });
}
tree5.addEventListener('node-click', (e) => logEvent('node-click', e.detail));
tree5.addEventListener('focus-change', (e) => logEvent('focus-change', e.detail));
tree5.addEventListener('tree-change', () => logEvent('tree-change'));
// Example 6: Accordion expand
const accTree = document.getElementById('tree-acc');
accTree.nodes = fileTree;
accTree.idMember = 'id';
accTree.pathMember = 'path';
accTree.displayValueMember = 'name';
window.applyAccordion = function () {
const on = document.getElementById('acc-enabled').checked;
if (on) accTree.setAttribute('expand-as-accordion', 'true');
else accTree.removeAttribute('expand-as-accordion');
};
// Example 7: Row animation. The tokens are set inline on the host (they inherit into
// the Shadow DOM and override the stylesheet default, like the display sliders above).
const raTree = document.getElementById('tree-ra');
raTree.nodes = fileTree;
raTree.idMember = 'id';
raTree.pathMember = 'path';
raTree.displayValueMember = 'name';
const RA_SPEED = { normal: 'var(--wtv-transition-normal)', slow: 'var(--wtv-transition-slow)', off: '0s' };
const applyRowAnimation = () => {
raTree.style.setProperty('--wtv-row-animation-duration', RA_SPEED[document.getElementById('ra-speed').value]);
if (document.getElementById('ra-fade').checked) raTree.style.setProperty('--wtv-row-animation-distance', '0');
else raTree.style.removeProperty('--wtv-row-animation-distance');
raTree.setAttribute('row-animation-limit', document.getElementById('ra-limit').value);
raTree.setAttribute('motion', document.getElementById('ra-motion').value);
};
for (const id of ['ra-speed', 'ra-fade', 'ra-limit', 'ra-motion'])
document.getElementById(id).addEventListener('change', applyRowAnimation);
applyRowAnimation();
// collapseAll() returns a Promise that settles once the rows animated out and the
// tree really collapsed (node-collapse / expanded-change fire then).
window.collapseRowTree = async () => {
const status = document.getElementById('tree-ra-status');
const start = performance.now();
status.textContent = 'collapsing…';
await raTree.collapseAll();
status.textContent = `collapsed — the Promise settled after ${Math.round(performance.now() - start)} ms`;
};
// Persist .controls across reloads (shared helper — parity with web-multiselect).
attachControlsPersistence('basic');
});
</script>