u2ui 1.5.19 → 1.5.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/el/tree/README.md +13 -0
- package/el/tree/ext/tristate.js +63 -0
- package/el/tree/manifest.json +4 -1
- package/el/tree/tree.js +5 -2
- package/package.json +1 -1
package/el/tree/README.md
CHANGED
|
@@ -8,6 +8,7 @@ Treeview component `<u2-tree>Folder <u2-tree>File</u2-tree> </u2-tree>`
|
|
|
8
8
|
- Expand/collapse events
|
|
9
9
|
- Lazy loading of children
|
|
10
10
|
- Drag and drop (opt-in, loaded on demand via the `draggable` attribute)
|
|
11
|
+
- Checkbox tree (opt-in, loaded on demand via the `tristate` attribute)
|
|
11
12
|
|
|
12
13
|
## Usage
|
|
13
14
|
|
|
@@ -67,6 +68,17 @@ This is either not expandable or like aria-expanded="false" if it has children.
|
|
|
67
68
|
This indicates, that the node has to be loaded.
|
|
68
69
|
The `u2-tree-collapse` event will get a property `event.load(asyncFn)` to load their children.
|
|
69
70
|
|
|
71
|
+
#### tristate
|
|
72
|
+
`<u2-tree tristate>` — an item's checkbox (the first in its content) stands for everything below it:
|
|
73
|
+
checking it checks every checkbox below, and it follows its child items' — checked when all are,
|
|
74
|
+
indeterminate when some are. A checkbox checked in the markup checks all below it on load, and so
|
|
75
|
+
do items added later under a checked item (e.g. loaded on expand).
|
|
76
|
+
```html
|
|
77
|
+
<u2-tree tristate aria-expanded=true><label><input type=checkbox name=a value=cms> cms</label>
|
|
78
|
+
<u2-tree><label><input type=checkbox name=a value=cms/node> node</label></u2-tree>
|
|
79
|
+
</u2-tree>
|
|
80
|
+
```
|
|
81
|
+
|
|
70
82
|
### Events
|
|
71
83
|
|
|
72
84
|
#### expand / collapse
|
|
@@ -117,6 +129,7 @@ treeElement.addEventListener('u2-tree-drop', (e) => {
|
|
|
117
129
|
[dnd-shadow.html](http://gcdn.li/u2ui/u2@main/el/tree/tests/dnd-shadow.html)
|
|
118
130
|
[custom.html](http://gcdn.li/u2ui/u2@main/el/tree/tests/custom.html)
|
|
119
131
|
[test.html](http://gcdn.li/u2ui/u2@main/el/tree/tests/test.html)
|
|
132
|
+
[tristate.html](http://gcdn.li/u2ui/u2@main/el/tree/tests/tristate.html)
|
|
120
133
|
[animated.html](http://gcdn.li/u2ui/u2@main/el/tree/tests/animated.html)
|
|
121
134
|
[minimal.html](http://gcdn.li/u2ui/u2@main/el/tree/tests/minimal.html)
|
|
122
135
|
[dnd.html](http://gcdn.li/u2ui/u2@main/el/tree/tests/dnd.html)
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// u2-tree tristate — an item's checkbox stands for everything below it.
|
|
2
|
+
// Its checkbox is the first one in its content (not in a child item). Checking it checks every checkbox
|
|
3
|
+
// below; it follows its child items': checked when all are, indeterminate when some are. Items added
|
|
4
|
+
// later (e.g. loaded on expand) are checked under a checked item; else, as when items are removed,
|
|
5
|
+
// the items above follow.
|
|
6
|
+
// Loaded on demand via the `tristate` attribute.
|
|
7
|
+
|
|
8
|
+
const BOX = 'input[type=checkbox]';
|
|
9
|
+
|
|
10
|
+
/** The item's own checkbox, if it has one: searched in its content only, not below. */
|
|
11
|
+
function box(item) {
|
|
12
|
+
for (const el of item.children) {
|
|
13
|
+
if (el.localName === item.localName) continue;
|
|
14
|
+
const found = el.matches(BOX) ? el : el.querySelector(BOX);
|
|
15
|
+
if (found) return found;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Set every checkbox below the item like its own. */
|
|
20
|
+
function cascade(item, checked) {
|
|
21
|
+
for (const b of item.querySelectorAll(BOX)) {
|
|
22
|
+
b.checked = checked;
|
|
23
|
+
b.indeterminate = false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The item's checkbox follows its child items'. */
|
|
28
|
+
function follow(item) {
|
|
29
|
+
const own = box(item), boxes = [...item.children].filter(c => c.localName === item.localName).map(box).filter(Boolean);
|
|
30
|
+
if (!own || !boxes.length) return;
|
|
31
|
+
const checked = boxes.filter(b => b.checked).length;
|
|
32
|
+
own.checked = checked === boxes.length;
|
|
33
|
+
own.indeterminate = !own.checked && (checked > 0 || boxes.some(b => b.indeterminate));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function tristate(tree) {
|
|
37
|
+
const init = () => {
|
|
38
|
+
const items = [tree, ...tree.querySelectorAll(tree.localName)];
|
|
39
|
+
for (const item of items) { // a checked item: all below it, once (one above did it already)
|
|
40
|
+
if (box(item)?.checked && (item === tree || !box(item.parentNode)?.checked)) cascade(item, true);
|
|
41
|
+
}
|
|
42
|
+
for (const item of items.reverse()) follow(item); // then bottom-up
|
|
43
|
+
};
|
|
44
|
+
document.readyState === 'loading' ? document.addEventListener('DOMContentLoaded', init, { once: true }) : init();
|
|
45
|
+
|
|
46
|
+
/** The items from `item` up to the tree follow their children. */
|
|
47
|
+
const up = item => { for (; item !== tree.parentNode; item = item.parentNode) follow(item); };
|
|
48
|
+
|
|
49
|
+
tree.addEventListener('input', e => {
|
|
50
|
+
const item = e.target.closest(tree.localName);
|
|
51
|
+
if (!item || e.target !== box(item)) return;
|
|
52
|
+
cascade(item, e.target.checked);
|
|
53
|
+
if (item !== tree) up(item.parentNode);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
new MutationObserver(records => {
|
|
57
|
+
for (const { target, addedNodes, removedNodes } of records) {
|
|
58
|
+
const items = nodes => [...nodes].some(n => n.localName === tree.localName);
|
|
59
|
+
if (items(addedNodes) && box(target)?.checked) cascade(target, true);
|
|
60
|
+
else if (items(addedNodes) || items(removedNodes)) up(target);
|
|
61
|
+
}
|
|
62
|
+
}).observe(tree, { childList: true, subtree: true });
|
|
63
|
+
}
|
package/el/tree/manifest.json
CHANGED
|
@@ -25,7 +25,10 @@
|
|
|
25
25
|
"name": "u2-tree-drop",
|
|
26
26
|
"type": { "text": "Event" }
|
|
27
27
|
}],
|
|
28
|
-
"attributes": [
|
|
28
|
+
"attributes": [{
|
|
29
|
+
"name": "tristate",
|
|
30
|
+
"description": "An item's checkbox stands for everything below it: checks all below, follows its children (indeterminate when some are checked)"
|
|
31
|
+
}],
|
|
29
32
|
"cssProperties": [{
|
|
30
33
|
"name": "--indent",
|
|
31
34
|
"syntax": "<length>",
|
package/el/tree/tree.js
CHANGED
|
@@ -40,8 +40,11 @@ styleSheet.replaceSync(css);
|
|
|
40
40
|
|
|
41
41
|
export default class U2Tree extends HTMLElement {
|
|
42
42
|
|
|
43
|
-
static observedAttributes = ['draggable'];
|
|
44
|
-
attributeChangedCallback() {
|
|
43
|
+
static observedAttributes = ['draggable', 'tristate'];
|
|
44
|
+
attributeChangedCallback(name) {
|
|
45
|
+
if (name === 'draggable') U2Tree.dnd ??= import('./ext/dnd.js'); // load DnD lazily once draggable appears
|
|
46
|
+
if (name === 'tristate' && this.hasAttribute(name)) this._tristate ??= import('./ext/tristate.js').then(({tristate}) => tristate(this));
|
|
47
|
+
}
|
|
45
48
|
|
|
46
49
|
constructor() {
|
|
47
50
|
super();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "u2ui",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.20",
|
|
4
4
|
"description": "HTML Advanced: More Capabilities, Same Feel, Same Simplicity",
|
|
5
5
|
"keywords": ["web-components","pattern-library","component-library","ui-toolkit","design-system"],
|
|
6
6
|
"author": "Tobias Buschor",
|