u2ui 1.5.18 → 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.
@@ -1,3 +1,5 @@
1
+ // todo: shadowrootreferencetarget
2
+
1
3
  import U2Element from '../../u2/Element.js';
2
4
  export default class U2Fields extends U2Element {
3
5
  constructor() {
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
+ }
@@ -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() { U2Tree.dnd ??= import('./ext/dnd.js'); } // load DnD lazily once draggable appears
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();
@@ -1,4 +1,3 @@
1
-
2
1
  class Menu {
3
2
  constructor(menuItem) {
4
3
  this.menuItem = menuItem;
@@ -323,6 +322,9 @@ document.documentElement.addEventListener('contextmenu', e=>{
323
322
  e.preventDefault();
324
323
 
325
324
  rootEl.showPopover();
325
+ // pointerup still pending (not on Windows): capture it, so it does not light-dismiss the popover
326
+ if (e.buttons) rootEl.setPointerCapture(e.pointerId);
327
+
326
328
  rootEl.querySelector('button').focus();
327
329
 
328
330
  let top = e.clientY + 2;
@@ -402,8 +402,8 @@ its replacement, as a promise or directly. `surface` comes along rather than a
402
402
  digest of it: `surface.config` says what the field allows — with `null` where
403
403
  nothing is restricted — because an answer using anything else is cleaned away on
404
404
  the way back in, and `surface.element` is where an application reads whatever
405
- else its own fields carry; a rejection is shown in the answer
406
- pane and leaves the field untouched. `prompts` fills a datalist beside the
405
+ else its own fields carry; a rejection is shown as an alert
406
+ (`js/dialog`) and leaves the panes and the field untouched. `prompts` fills a datalist beside the
407
407
  input. Without `diff` the dialog shows two panes — comparing two HTML strings is
408
408
  a library question, not an editor one, so none is pulled in. Answers that arrive
409
409
  after the next prompt, or after the dialog closed, are dropped.
@@ -148,7 +148,11 @@ async function ask(state) {
148
148
  parts.apply.disabled = false;
149
149
  await update(state, pending, run);
150
150
  } catch (error) {
151
- if (state.pending === pending && pending.run === run) parts.answer.textContent = String(error?.message ?? error);
151
+ // A failure is no answer: it comes as an alert, and the panes keep what they had.
152
+ if (state.pending === pending && pending.run === run) {
153
+ const {alert} = await import('../../../dialog/dialog.js');
154
+ alert(String(error?.message ?? error), {root: state.dialog.parentNode});
155
+ }
152
156
  } finally {
153
157
  if (state.pending === pending && pending.run === run) parts.busy.disabled = false;
154
158
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "u2ui",
3
- "version": "1.5.18",
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",