Tree View¶
Nested rows that open and close, on a List with the collapsible pattern. Children can load when a node opens, through a promise. With selectable each node has a checkbox through the selection core, and a parent is checked, partly checked, or clear from its children. A node can show a thumbnail through the thumb core.
Decision¶
| When | Use |
|---|---|
| Picking from a hierarchy, such as libraries, series, seasons, and episodes, or folders | Tree View |
| Picking one server folder | Folder Picker |
| A flat list | Checkbox List |
Usage¶
import { createTreeView } from './configurationpage?name=myplugin_stolon.js';
var tree = createTreeView({
nodes: libraries.map(function (l) { return { id: l.Id, label: l.Name, icon: 'video_library', hasChildren: true }; }),
loadChildren: function (node) {
return shared.apiRequest('Items?parentId=' + node.id, 'GET').then(function (r) {
return r.Items.map(function (i) { return { id: i.Id, label: i.Name, image: itemImage(i), imageShape: 'portrait', hasChildren: i.IsFolder }; });
});
},
selectable: true,
value: config.ItemIds,
onChange: function (ids) { config.ItemIds = ids; }
});
view.querySelector('#treeHost').appendChild(tree.element);
Classes: stolon-tree, stolon-tree-row, stolon-tree-toggle, stolon-tree-spacer, stolon-tree-check, stolon-tree-icon, stolon-tree-thumb, stolon-tree-text, stolon-tree-loading. The handle is { element, getValue, setValue, setNodes, expand, collapse, destroy }.
States and Modifiers¶
- A node is
{ id, label, secondary, icon, image, imageShape, children, hasChildren }.childrenare known children.hasChildrenmarks a node whose childrenloadChildren(node)fetches when it first opens, while the node says "Loading" byloadingText. A failed load closes it again. - The open button turns its
expand_morechevron byaria-expanded, and is named "Expand {value}" or "Collapse {value}". - With
selectable, checking a node checks every loaded node under it. A parent is checked when all its children are, partly checked when some are, and clear otherwise, and is ingetValue()only while checked. Children that load under a checked parent start checked. A value may name a parent alone. - The tree is
role="tree"withtreeitemrows that carryaria-level,aria-expanded, andaria-checked, and one row takes focus at a time. Up and Down move, Right opens or moves to the first child, Left closes or moves to the parent, Home and End go to the ends, Space checks, and Enter callsonOpen(node), or opens and closes without it. - A click on a row calls
onOpen, or opens it, or checks a leaf. expandednames the nodes that start open, andexpand(id)resolves once the children show.labelnames the tree, "Tree" by default.selectLabelnames each checkbox, "Select {value}" by default.emptyTextshows while there is no node, "Nothing to show." by default, andheight,maxHeight, andminHeightwork as a List's do.
Reference¶
Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.
createTreeView¶
function createTreeView(options?: TreeViewOptions): TreeViewHandle
Nested rows that open and close, on a List with the collapsible pattern. Children can load when a node opens, through a promise. With selectable each node has a checkbox through the selection core, and a parent is checked, partly checked, or clear from its children. Arrow keys move and open, Enter opens, Space checks, and a node can show a thumbnail through the thumb core.
| Option | Type | What it does |
|---|---|---|
nodes |
TreeNode[] | null |
array of { id, label, secondary, icon, image, imageShape, children, hasChildren } [optional] |
loadChildren |
(node: TreeNode): Promise<TreeNode[]> | TreeNode[] |
function(node) returning the node's children, or a promise of them, when a node with hasChildren opens [optional] |
selectable |
boolean |
true draws a checkbox on each node (default false) |
value |
unknown[] | null |
the ids that start checked [optional] |
expanded |
unknown[] | null |
the ids that start open [optional] |
label |
string |
aria-label of the tree (default 'Tree') |
emptyText |
string |
shown while there is no node (default 'Nothing to show.') |
loadingText |
string |
shown on a node while its children load (default 'Loading') |
expandLabel |
string |
|
collapseLabel |
string |
|
selectLabel |
string |
aria-label of a checkbox, where {value} is the label (default 'Select {value}') |
height |
ListHeight |
|
maxHeight |
number | string | null |
|
minHeight |
number | string | null |
|
onChange |
(ids: string[]): void |
function(ids) after the checked ids change [optional] |
onOpen |
(node: TreeNode): void |
function(node) when a node is clicked or gets Enter [optional] |
| Handle member | Type |
|---|---|
getValue |
(): string[] |
setValue |
(ids: unknown[] | null): void |
setNodes |
(nodes: TreeNode[] | null): void |
expand |
(id: unknown): Promise<void> |
collapse |
(id: unknown): void |
destroy |
(): void |
| Type | What it is |
|---|---|
TreeNode |
One node of the tree. children are known children, and hasChildren marks a node whose children loadChildren fetches when it opens. image is a thumbnail address. |
| Class | What it looks like |
|---|---|
stolon-tree.stolon-list.stolon-plain |
A List of nested rows that open and close, one row per visible node |
stolon-tree-row |
One node: indented by its level, then its open button, its checkbox, its picture, and its text. The focused row takes an accent ring, and a checked one the shared selection |
stolon-tree-toggle |
The open button, a row button whose chevron points down while closed and turns up while open |
stolon-tree-check |
The checkbox, the thumbnail a text line tall, the icon, and the text with its dimmed secondary part |
stolon-tree-loading |
The line a node shows while its children load |
Example¶
Example Code¶
<!-- title: Tree View -->
<div id="tree"></div>
<script type="module">
const base = await window.__baseReady;
const tree = base.createTreeView({
nodes: [
{ id: 'shows', label: 'Shows', icon: 'tv', children: [
{ id: 'alpha', label: 'Alpha Station', secondary: '2 seasons', children: [
{ id: 'a1', label: 'Season 1', secondary: '10 episodes' },
{ id: 'a2', label: 'Season 2', secondary: 'Loads when opened', hasChildren: true }
] },
{ id: 'beta', label: 'Beta Coast', secondary: '1 season' }
] },
{ id: 'movies', label: 'Movies', icon: 'movie', children: [
{ id: 'm1', label: 'Alien', secondary: '1979', image: '', imageShape: 'portrait' },
{ id: 'm2', label: 'Heat', secondary: '1995', image: '', imageShape: 'portrait' }
] }
],
expanded: ['shows', 'alpha'],
value: ['a1'],
selectable: true,
loadChildren: function () {
return new Promise(function (resolve) {
setTimeout(function () { resolve([{ id: 'a2e1', label: 'Return' }, { id: 'a2e2', label: 'Departure' }]); }, 600);
});
}
});
document.getElementById('tree').appendChild(tree.element);
</script>