@yuneta/gobj-ui 7.32.9 → 7.32.10

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/README.md CHANGED
@@ -919,8 +919,25 @@ Two layout facts the browser taught this component, both worth keeping:
919
919
  (`7.21.0` forwarded it and broke exactly that way); a host that wants node
920
920
  clicks mounts `C_YUI_JSON_GRAPH` itself.
921
921
  - Internal (DOM → FSM): `EV_TOGGLE_NODE`, `EV_EXPAND_COLLAPSED`, `EV_SEARCH`,
922
- `EV_EXPAND_ALL`, `EV_COLLAPSE_ALL`, `EV_COPY_ALL`. Every kw carries only a
923
- `path` string — never a DOM node or gobj.
922
+ `EV_EXPAND_ALL`, `EV_COLLAPSE_ALL`, `EV_EXPAND_LEVEL`, `EV_COLLAPSE_LEVEL`,
923
+ `EV_COPY_ALL`. Every kw carries only a `path` string — never a DOM node or
924
+ gobj.
925
+ - The tree folds by level too (7.32.10+). Beside "expand loaded" and
926
+ "collapse all", a stepper of two buttons works on one level of the whole
927
+ tree, with the tree-box icons of the icon rule (`yi-square-minus`,
928
+ `yi-square-plus`). A level is the depth of a row: 0 for an entry of the
929
+ root.
930
+ - `EV_EXPAND_LEVEL` (`+`) opens every loaded container of the shallowest
931
+ level that still has one closed. From a closed document, one press shows
932
+ the fields of each entry, with their own containers closed.
933
+ - `EV_COLLAPSE_LEVEL` (`-`) closes every container at the deepest level
934
+ open in sight, and every container under it.
935
+ - A `__collapsed__` sentinel is not opened by `+`: it opens through the
936
+ backend, as with "expand loaded".
937
+
938
+ The keys are `expand one level` and `collapse one level`, the same as the
939
+ stepper of the treedb graph. An app that mounts the viewer defines them in
940
+ every locale. The stepper hides with the tree, like the search box.
924
941
  - i18n keys the switch needs: `tree view`, `text view`, `graph view`,
925
942
  `text truncated; collapse some branches`. All spelled out inside `t()` in
926
943
  `view_label()` — never `t(VIEWS[i].key)`, which no `validate-locales` can
@@ -46710,6 +46710,8 @@ function build_ui$12(gobj) {
46710
46710
  priv.$search_ctl = $container.querySelector(".JSON_SEARCH_CONTROL");
46711
46711
  priv.$expand_btn = $container.querySelector(".EV_EXPAND_ALL");
46712
46712
  priv.$collapse_btn = $container.querySelector(".EV_COLLAPSE_ALL");
46713
+ priv.$collapse_level_btn = $container.querySelector(".EV_COLLAPSE_LEVEL");
46714
+ priv.$expand_level_btn = $container.querySelector(".EV_EXPAND_LEVEL");
46713
46715
  priv.$graph = $container.querySelector(".JSON_GRAPH");
46714
46716
  priv.$mode_btns = /* @__PURE__ */ new Map();
46715
46717
  VIEWS$1.forEach(function(v) {
@@ -46736,6 +46738,8 @@ function destroy_ui$7(gobj) {
46736
46738
  priv.$search_ctl = null;
46737
46739
  priv.$expand_btn = null;
46738
46740
  priv.$collapse_btn = null;
46741
+ priv.$collapse_level_btn = null;
46742
+ priv.$expand_level_btn = null;
46739
46743
  priv.$mode_btns = null;
46740
46744
  }
46741
46745
  /************************************************************
@@ -46783,7 +46787,7 @@ function make_toolbar$4(gobj) {
46783
46787
  [yui_toolbar_icon("yi-magnifying-glass")]
46784
46788
  ]]
46785
46789
  ]);
46786
- left_items.push(icon_button$1(gobj, "yi-angles-down", "EV_EXPAND_ALL", "expand loaded"), icon_button$1(gobj, "yi-angles-up", "EV_COLLAPSE_ALL", "collapse all"));
46790
+ left_items.push(icon_button$1(gobj, "yi-angles-down", "EV_EXPAND_ALL", "expand loaded"), icon_button$1(gobj, "yi-angles-up", "EV_COLLAPSE_ALL", "collapse all"), icon_button$1(gobj, "yi-square-minus", "EV_COLLAPSE_LEVEL", "collapse one level"), icon_button$1(gobj, "yi-square-plus", "EV_EXPAND_LEVEL", "expand one level"));
46787
46791
  attach_clear($search_control, $search_input);
46788
46792
  left_items.push($search_control);
46789
46793
  let right_items = [view_mode_switch$1(gobj), icon_button$1(gobj, "yi-copy", "EV_COPY_ALL", "copy json")];
@@ -46941,7 +46945,9 @@ function apply_view_mode$1(gobj) {
46941
46945
  [
46942
46946
  priv.$search_ctl,
46943
46947
  priv.$expand_btn,
46944
- priv.$collapse_btn
46948
+ priv.$collapse_btn,
46949
+ priv.$collapse_level_btn,
46950
+ priv.$expand_level_btn
46945
46951
  ].forEach(function($el) {
46946
46952
  if ($el) $el.classList.toggle("is-hidden", mode !== "tree");
46947
46953
  });
@@ -47412,6 +47418,70 @@ function collect_loaded_paths(value, segments, out) {
47412
47418
  });
47413
47419
  }
47414
47420
  }
47421
+ /************************************************************
47422
+ * The entries of a container, as [key, value] pairs: the keys
47423
+ * of an object, the indexes of an array (as strings, the way
47424
+ * a path spells them). None for anything else.
47425
+ ************************************************************/
47426
+ function entries_of(value) {
47427
+ let type = json_type(value);
47428
+ if (type === "object") return Object.entries(value);
47429
+ if (type === "array") return value.map((v, i) => [String(i), v]);
47430
+ return [];
47431
+ }
47432
+ /************************************************************
47433
+ * Is it a container the tree can open in place: an object or
47434
+ * an array that is loaded (not a `__collapsed__` sentinel,
47435
+ * which opens through the backend).
47436
+ ************************************************************/
47437
+ function is_open_able(value) {
47438
+ if (is_collapsed(value)) return false;
47439
+ let type = json_type(value);
47440
+ return type === "object" || type === "array";
47441
+ }
47442
+ /************************************************************
47443
+ * The shallowest level that has a loaded container still
47444
+ * closed, -1 when every loaded container is open. A level is
47445
+ * the depth of a row: 0 for an entry of the root.
47446
+ ************************************************************/
47447
+ function first_closed_level(priv, value, segments) {
47448
+ let level = -1;
47449
+ for (let [k, v] of entries_of(value)) {
47450
+ if (!is_open_able(v)) continue;
47451
+ let child = segments.concat(k);
47452
+ let found = priv.expanded.has(seg_join(child)) ? first_closed_level(priv, v, child) : child.length - 1;
47453
+ if (found >= 0 && (level < 0 || found < level)) level = found;
47454
+ }
47455
+ return level;
47456
+ }
47457
+ /************************************************************
47458
+ * The deepest level with a container open in sight (all its
47459
+ * parents open too), -1 when none is. Only what the reader
47460
+ * sees counts: an open container under a closed one is not
47461
+ * on screen, and folding it would change nothing they see.
47462
+ ************************************************************/
47463
+ function deepest_open_level(priv, value, segments) {
47464
+ let level = -1;
47465
+ for (let [k, v] of entries_of(value)) {
47466
+ if (!is_open_able(v)) continue;
47467
+ let child = segments.concat(k);
47468
+ if (!priv.expanded.has(seg_join(child))) continue;
47469
+ level = Math.max(level, child.length - 1, deepest_open_level(priv, v, child));
47470
+ }
47471
+ return level;
47472
+ }
47473
+ /************************************************************
47474
+ * Open every loaded container of the levels up to `level`.
47475
+ ************************************************************/
47476
+ function open_levels_up_to(priv, value, segments, level) {
47477
+ for (let [k, v] of entries_of(value)) {
47478
+ if (!is_open_able(v)) continue;
47479
+ let child = segments.concat(k);
47480
+ if (child.length - 1 > level) continue;
47481
+ priv.expanded.add(seg_join(child));
47482
+ open_levels_up_to(priv, v, child, level);
47483
+ }
47484
+ }
47415
47485
  /***************************
47416
47486
  * Actions
47417
47487
  ***************************/
@@ -47536,6 +47606,36 @@ function ac_collapse_all$1(gobj, event, kw, src) {
47536
47606
  return 0;
47537
47607
  }
47538
47608
  /************************************************************
47609
+ * EV_EXPAND_LEVEL — open the next level: every loaded
47610
+ * container of the shallowest level that still has one closed.
47611
+ * The levels above it are all open already, so the tree shows
47612
+ * one level more. A `__collapsed__` sentinel is not opened: it
47613
+ * asks the backend, as with "expand loaded".
47614
+ ************************************************************/
47615
+ function ac_expand_level(gobj, event, kw, src) {
47616
+ let priv = gobj.priv;
47617
+ if (priv.root === null || priv.root === void 0) return 0;
47618
+ let level = first_closed_level(priv, priv.root, []);
47619
+ if (level < 0) return 0;
47620
+ open_levels_up_to(priv, priv.root, [], level);
47621
+ render_view$1(gobj);
47622
+ return 0;
47623
+ }
47624
+ /************************************************************
47625
+ * EV_COLLAPSE_LEVEL — fold the deepest level open in sight:
47626
+ * every container open at that level or under it is closed,
47627
+ * so the tree shows one level less.
47628
+ ************************************************************/
47629
+ function ac_collapse_level(gobj, event, kw, src) {
47630
+ let priv = gobj.priv;
47631
+ if (priv.root === null || priv.root === void 0) return 0;
47632
+ let level = deepest_open_level(priv, priv.root, []);
47633
+ if (level < 0) return 0;
47634
+ for (let path of Array.from(priv.expanded)) if (seg_split(path).length - 1 >= level) priv.expanded.delete(path);
47635
+ render_view$1(gobj);
47636
+ return 0;
47637
+ }
47638
+ /************************************************************
47539
47639
  * EV_COPY_ALL — copy the working document to the clipboard
47540
47640
  ************************************************************/
47541
47641
  function ac_copy_all$1(gobj, event, kw, src) {
@@ -47729,6 +47829,16 @@ function create_gclass$13(gclass_name) {
47729
47829
  ac_collapse_all$1,
47730
47830
  null
47731
47831
  ],
47832
+ [
47833
+ "EV_EXPAND_LEVEL",
47834
+ ac_expand_level,
47835
+ null
47836
+ ],
47837
+ [
47838
+ "EV_COLLAPSE_LEVEL",
47839
+ ac_collapse_level,
47840
+ null
47841
+ ],
47732
47842
  [
47733
47843
  "EV_COPY_ALL",
47734
47844
  ac_copy_all$1,
@@ -47769,6 +47879,8 @@ function create_gclass$13(gclass_name) {
47769
47879
  ["EV_SEARCH", 0],
47770
47880
  ["EV_EXPAND_ALL", 0],
47771
47881
  ["EV_COLLAPSE_ALL", 0],
47882
+ ["EV_EXPAND_LEVEL", 0],
47883
+ ["EV_COLLAPSE_LEVEL", 0],
47772
47884
  ["EV_COPY_ALL", 0],
47773
47885
  ["EV_SET_VIEW_MODE", 0],
47774
47886
  ["EV_JSON_ITEM_CLICKED", 0],
@@ -46705,6 +46705,8 @@ function build_ui$12(gobj) {
46705
46705
  priv.$search_ctl = $container.querySelector(".JSON_SEARCH_CONTROL");
46706
46706
  priv.$expand_btn = $container.querySelector(".EV_EXPAND_ALL");
46707
46707
  priv.$collapse_btn = $container.querySelector(".EV_COLLAPSE_ALL");
46708
+ priv.$collapse_level_btn = $container.querySelector(".EV_COLLAPSE_LEVEL");
46709
+ priv.$expand_level_btn = $container.querySelector(".EV_EXPAND_LEVEL");
46708
46710
  priv.$graph = $container.querySelector(".JSON_GRAPH");
46709
46711
  priv.$mode_btns = /* @__PURE__ */ new Map();
46710
46712
  VIEWS$1.forEach(function(v) {
@@ -46731,6 +46733,8 @@ function destroy_ui$7(gobj) {
46731
46733
  priv.$search_ctl = null;
46732
46734
  priv.$expand_btn = null;
46733
46735
  priv.$collapse_btn = null;
46736
+ priv.$collapse_level_btn = null;
46737
+ priv.$expand_level_btn = null;
46734
46738
  priv.$mode_btns = null;
46735
46739
  }
46736
46740
  /************************************************************
@@ -46778,7 +46782,7 @@ function make_toolbar$4(gobj) {
46778
46782
  [yui_toolbar_icon("yi-magnifying-glass")]
46779
46783
  ]]
46780
46784
  ]);
46781
- left_items.push(icon_button$1(gobj, "yi-angles-down", "EV_EXPAND_ALL", "expand loaded"), icon_button$1(gobj, "yi-angles-up", "EV_COLLAPSE_ALL", "collapse all"));
46785
+ left_items.push(icon_button$1(gobj, "yi-angles-down", "EV_EXPAND_ALL", "expand loaded"), icon_button$1(gobj, "yi-angles-up", "EV_COLLAPSE_ALL", "collapse all"), icon_button$1(gobj, "yi-square-minus", "EV_COLLAPSE_LEVEL", "collapse one level"), icon_button$1(gobj, "yi-square-plus", "EV_EXPAND_LEVEL", "expand one level"));
46782
46786
  attach_clear($search_control, $search_input);
46783
46787
  left_items.push($search_control);
46784
46788
  let right_items = [view_mode_switch$1(gobj), icon_button$1(gobj, "yi-copy", "EV_COPY_ALL", "copy json")];
@@ -46936,7 +46940,9 @@ function apply_view_mode$1(gobj) {
46936
46940
  [
46937
46941
  priv.$search_ctl,
46938
46942
  priv.$expand_btn,
46939
- priv.$collapse_btn
46943
+ priv.$collapse_btn,
46944
+ priv.$collapse_level_btn,
46945
+ priv.$expand_level_btn
46940
46946
  ].forEach(function($el) {
46941
46947
  if ($el) $el.classList.toggle("is-hidden", mode !== "tree");
46942
46948
  });
@@ -47407,6 +47413,70 @@ function collect_loaded_paths(value, segments, out) {
47407
47413
  });
47408
47414
  }
47409
47415
  }
47416
+ /************************************************************
47417
+ * The entries of a container, as [key, value] pairs: the keys
47418
+ * of an object, the indexes of an array (as strings, the way
47419
+ * a path spells them). None for anything else.
47420
+ ************************************************************/
47421
+ function entries_of(value) {
47422
+ let type = json_type(value);
47423
+ if (type === "object") return Object.entries(value);
47424
+ if (type === "array") return value.map((v, i) => [String(i), v]);
47425
+ return [];
47426
+ }
47427
+ /************************************************************
47428
+ * Is it a container the tree can open in place: an object or
47429
+ * an array that is loaded (not a `__collapsed__` sentinel,
47430
+ * which opens through the backend).
47431
+ ************************************************************/
47432
+ function is_open_able(value) {
47433
+ if (is_collapsed(value)) return false;
47434
+ let type = json_type(value);
47435
+ return type === "object" || type === "array";
47436
+ }
47437
+ /************************************************************
47438
+ * The shallowest level that has a loaded container still
47439
+ * closed, -1 when every loaded container is open. A level is
47440
+ * the depth of a row: 0 for an entry of the root.
47441
+ ************************************************************/
47442
+ function first_closed_level(priv, value, segments) {
47443
+ let level = -1;
47444
+ for (let [k, v] of entries_of(value)) {
47445
+ if (!is_open_able(v)) continue;
47446
+ let child = segments.concat(k);
47447
+ let found = priv.expanded.has(seg_join(child)) ? first_closed_level(priv, v, child) : child.length - 1;
47448
+ if (found >= 0 && (level < 0 || found < level)) level = found;
47449
+ }
47450
+ return level;
47451
+ }
47452
+ /************************************************************
47453
+ * The deepest level with a container open in sight (all its
47454
+ * parents open too), -1 when none is. Only what the reader
47455
+ * sees counts: an open container under a closed one is not
47456
+ * on screen, and folding it would change nothing they see.
47457
+ ************************************************************/
47458
+ function deepest_open_level(priv, value, segments) {
47459
+ let level = -1;
47460
+ for (let [k, v] of entries_of(value)) {
47461
+ if (!is_open_able(v)) continue;
47462
+ let child = segments.concat(k);
47463
+ if (!priv.expanded.has(seg_join(child))) continue;
47464
+ level = Math.max(level, child.length - 1, deepest_open_level(priv, v, child));
47465
+ }
47466
+ return level;
47467
+ }
47468
+ /************************************************************
47469
+ * Open every loaded container of the levels up to `level`.
47470
+ ************************************************************/
47471
+ function open_levels_up_to(priv, value, segments, level) {
47472
+ for (let [k, v] of entries_of(value)) {
47473
+ if (!is_open_able(v)) continue;
47474
+ let child = segments.concat(k);
47475
+ if (child.length - 1 > level) continue;
47476
+ priv.expanded.add(seg_join(child));
47477
+ open_levels_up_to(priv, v, child, level);
47478
+ }
47479
+ }
47410
47480
  /***************************
47411
47481
  * Actions
47412
47482
  ***************************/
@@ -47531,6 +47601,36 @@ function ac_collapse_all$1(gobj, event, kw, src) {
47531
47601
  return 0;
47532
47602
  }
47533
47603
  /************************************************************
47604
+ * EV_EXPAND_LEVEL — open the next level: every loaded
47605
+ * container of the shallowest level that still has one closed.
47606
+ * The levels above it are all open already, so the tree shows
47607
+ * one level more. A `__collapsed__` sentinel is not opened: it
47608
+ * asks the backend, as with "expand loaded".
47609
+ ************************************************************/
47610
+ function ac_expand_level(gobj, event, kw, src) {
47611
+ let priv = gobj.priv;
47612
+ if (priv.root === null || priv.root === void 0) return 0;
47613
+ let level = first_closed_level(priv, priv.root, []);
47614
+ if (level < 0) return 0;
47615
+ open_levels_up_to(priv, priv.root, [], level);
47616
+ render_view$1(gobj);
47617
+ return 0;
47618
+ }
47619
+ /************************************************************
47620
+ * EV_COLLAPSE_LEVEL — fold the deepest level open in sight:
47621
+ * every container open at that level or under it is closed,
47622
+ * so the tree shows one level less.
47623
+ ************************************************************/
47624
+ function ac_collapse_level(gobj, event, kw, src) {
47625
+ let priv = gobj.priv;
47626
+ if (priv.root === null || priv.root === void 0) return 0;
47627
+ let level = deepest_open_level(priv, priv.root, []);
47628
+ if (level < 0) return 0;
47629
+ for (let path of Array.from(priv.expanded)) if (seg_split(path).length - 1 >= level) priv.expanded.delete(path);
47630
+ render_view$1(gobj);
47631
+ return 0;
47632
+ }
47633
+ /************************************************************
47534
47634
  * EV_COPY_ALL — copy the working document to the clipboard
47535
47635
  ************************************************************/
47536
47636
  function ac_copy_all$1(gobj, event, kw, src) {
@@ -47724,6 +47824,16 @@ function create_gclass$13(gclass_name) {
47724
47824
  ac_collapse_all$1,
47725
47825
  null
47726
47826
  ],
47827
+ [
47828
+ "EV_EXPAND_LEVEL",
47829
+ ac_expand_level,
47830
+ null
47831
+ ],
47832
+ [
47833
+ "EV_COLLAPSE_LEVEL",
47834
+ ac_collapse_level,
47835
+ null
47836
+ ],
47727
47837
  [
47728
47838
  "EV_COPY_ALL",
47729
47839
  ac_copy_all$1,
@@ -47764,6 +47874,8 @@ function create_gclass$13(gclass_name) {
47764
47874
  ["EV_SEARCH", 0],
47765
47875
  ["EV_EXPAND_ALL", 0],
47766
47876
  ["EV_COLLAPSE_ALL", 0],
47877
+ ["EV_EXPAND_LEVEL", 0],
47878
+ ["EV_COLLAPSE_LEVEL", 0],
47767
47879
  ["EV_COPY_ALL", 0],
47768
47880
  ["EV_SET_VIEW_MODE", 0],
47769
47881
  ["EV_JSON_ITEM_CLICKED", 0],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yuneta/gobj-ui",
3
- "version": "7.32.9",
3
+ "version": "7.32.10",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "dist/gobj-ui.cjs.js",
package/src/c_yui_json.js CHANGED
@@ -355,6 +355,8 @@ function build_ui(gobj)
355
355
  priv.$search_ctl = $container.querySelector('.JSON_SEARCH_CONTROL');
356
356
  priv.$expand_btn = $container.querySelector('.EV_EXPAND_ALL');
357
357
  priv.$collapse_btn = $container.querySelector('.EV_COLLAPSE_ALL');
358
+ priv.$collapse_level_btn = $container.querySelector('.EV_COLLAPSE_LEVEL');
359
+ priv.$expand_level_btn = $container.querySelector('.EV_EXPAND_LEVEL');
358
360
  priv.$graph = $container.querySelector('.JSON_GRAPH');
359
361
  priv.$mode_btns = new Map();
360
362
  VIEWS.forEach(function(v) {
@@ -388,6 +390,8 @@ function destroy_ui(gobj)
388
390
  priv.$search_ctl = null;
389
391
  priv.$expand_btn = null;
390
392
  priv.$collapse_btn = null;
393
+ priv.$collapse_level_btn = null;
394
+ priv.$expand_level_btn = null;
391
395
  priv.$mode_btns = null;
392
396
  }
393
397
 
@@ -438,7 +442,13 @@ function make_toolbar(gobj)
438
442
  * per-row toggles live in the rows. */
439
443
  left_items.push(
440
444
  icon_button(gobj, "yi-angles-down", "EV_EXPAND_ALL", "expand loaded"),
441
- icon_button(gobj, "yi-angles-up", "EV_COLLAPSE_ALL", "collapse all")
445
+ icon_button(gobj, "yi-angles-up", "EV_COLLAPSE_ALL", "collapse all"),
446
+ /* The level stepper, beside the pair that does it all at once:
447
+ * `-` folds the deepest level open, `+` opens the next one. The
448
+ * tree boxes of the icon rule (one LEVEL of the whole tree), in
449
+ * the order of the treedb graph's stepper. */
450
+ icon_button(gobj, "yi-square-minus", "EV_COLLAPSE_LEVEL", "collapse one level"),
451
+ icon_button(gobj, "yi-square-plus", "EV_EXPAND_LEVEL", "expand one level")
442
452
  );
443
453
 
444
454
  attach_clear($search_control, $search_input);
@@ -592,7 +602,8 @@ function apply_view_mode(gobj)
592
602
  * worse than an absent one. A <pre> is searched with the browser's
593
603
  * own Ctrl+F, and the graph carries its own controls.
594
604
  */
595
- [priv.$search_ctl, priv.$expand_btn, priv.$collapse_btn].forEach(function($el) {
605
+ [priv.$search_ctl, priv.$expand_btn, priv.$collapse_btn,
606
+ priv.$collapse_level_btn, priv.$expand_level_btn].forEach(function($el) {
596
607
  if($el) {
597
608
  $el.classList.toggle('is-hidden', mode !== "tree");
598
609
  }
@@ -1095,6 +1106,100 @@ function collect_loaded_paths(value, segments, out)
1095
1106
  }
1096
1107
  }
1097
1108
 
1109
+ /************************************************************
1110
+ * The entries of a container, as [key, value] pairs: the keys
1111
+ * of an object, the indexes of an array (as strings, the way
1112
+ * a path spells them). None for anything else.
1113
+ ************************************************************/
1114
+ function entries_of(value)
1115
+ {
1116
+ let type = json_type(value);
1117
+ if(type === "object") {
1118
+ return Object.entries(value);
1119
+ }
1120
+ if(type === "array") {
1121
+ return value.map((v, i) => [String(i), v]);
1122
+ }
1123
+ return [];
1124
+ }
1125
+
1126
+ /************************************************************
1127
+ * Is it a container the tree can open in place: an object or
1128
+ * an array that is loaded (not a `__collapsed__` sentinel,
1129
+ * which opens through the backend).
1130
+ ************************************************************/
1131
+ function is_open_able(value)
1132
+ {
1133
+ if(is_collapsed(value)) {
1134
+ return false;
1135
+ }
1136
+ let type = json_type(value);
1137
+ return type === "object" || type === "array";
1138
+ }
1139
+
1140
+ /************************************************************
1141
+ * The shallowest level that has a loaded container still
1142
+ * closed, -1 when every loaded container is open. A level is
1143
+ * the depth of a row: 0 for an entry of the root.
1144
+ ************************************************************/
1145
+ function first_closed_level(priv, value, segments)
1146
+ {
1147
+ let level = -1;
1148
+ for(let [k, v] of entries_of(value)) {
1149
+ if(!is_open_able(v)) {
1150
+ continue;
1151
+ }
1152
+ let child = segments.concat(k);
1153
+ let found = priv.expanded.has(seg_join(child)) ?
1154
+ first_closed_level(priv, v, child) :
1155
+ child.length - 1;
1156
+ if(found >= 0 && (level < 0 || found < level)) {
1157
+ level = found;
1158
+ }
1159
+ }
1160
+ return level;
1161
+ }
1162
+
1163
+ /************************************************************
1164
+ * The deepest level with a container open in sight (all its
1165
+ * parents open too), -1 when none is. Only what the reader
1166
+ * sees counts: an open container under a closed one is not
1167
+ * on screen, and folding it would change nothing they see.
1168
+ ************************************************************/
1169
+ function deepest_open_level(priv, value, segments)
1170
+ {
1171
+ let level = -1;
1172
+ for(let [k, v] of entries_of(value)) {
1173
+ if(!is_open_able(v)) {
1174
+ continue;
1175
+ }
1176
+ let child = segments.concat(k);
1177
+ if(!priv.expanded.has(seg_join(child))) {
1178
+ continue;
1179
+ }
1180
+ level = Math.max(level, child.length - 1, deepest_open_level(priv, v, child));
1181
+ }
1182
+ return level;
1183
+ }
1184
+
1185
+ /************************************************************
1186
+ * Open every loaded container of the levels up to `level`.
1187
+ ************************************************************/
1188
+ function open_levels_up_to(priv, value, segments, level)
1189
+ {
1190
+ for(let [k, v] of entries_of(value)) {
1191
+ if(!is_open_able(v)) {
1192
+ continue;
1193
+ }
1194
+ let child = segments.concat(k);
1195
+ if(child.length - 1 > level) {
1196
+ continue;
1197
+ }
1198
+ priv.expanded.add(seg_join(child));
1199
+ open_levels_up_to(priv, v, child, level);
1200
+ }
1201
+ }
1202
+
1098
1203
 
1099
1204
 
1100
1205
 
@@ -1274,6 +1379,52 @@ function ac_collapse_all(gobj, event, kw, src)
1274
1379
  return 0;
1275
1380
  }
1276
1381
 
1382
+ /************************************************************
1383
+ * EV_EXPAND_LEVEL — open the next level: every loaded
1384
+ * container of the shallowest level that still has one closed.
1385
+ * The levels above it are all open already, so the tree shows
1386
+ * one level more. A `__collapsed__` sentinel is not opened: it
1387
+ * asks the backend, as with "expand loaded".
1388
+ ************************************************************/
1389
+ function ac_expand_level(gobj, event, kw, src)
1390
+ {
1391
+ let priv = gobj.priv;
1392
+ if(priv.root === null || priv.root === undefined) {
1393
+ return 0;
1394
+ }
1395
+ let level = first_closed_level(priv, priv.root, []);
1396
+ if(level < 0) {
1397
+ return 0; /* every loaded container is open: nothing to step */
1398
+ }
1399
+ open_levels_up_to(priv, priv.root, [], level);
1400
+ render_view(gobj);
1401
+ return 0;
1402
+ }
1403
+
1404
+ /************************************************************
1405
+ * EV_COLLAPSE_LEVEL — fold the deepest level open in sight:
1406
+ * every container open at that level or under it is closed,
1407
+ * so the tree shows one level less.
1408
+ ************************************************************/
1409
+ function ac_collapse_level(gobj, event, kw, src)
1410
+ {
1411
+ let priv = gobj.priv;
1412
+ if(priv.root === null || priv.root === undefined) {
1413
+ return 0;
1414
+ }
1415
+ let level = deepest_open_level(priv, priv.root, []);
1416
+ if(level < 0) {
1417
+ return 0; /* nothing open: nothing to step */
1418
+ }
1419
+ for(let path of Array.from(priv.expanded)) {
1420
+ if(seg_split(path).length - 1 >= level) {
1421
+ priv.expanded.delete(path);
1422
+ }
1423
+ }
1424
+ render_view(gobj);
1425
+ return 0;
1426
+ }
1427
+
1277
1428
  /************************************************************
1278
1429
  * EV_COPY_ALL — copy the working document to the clipboard
1279
1430
  ************************************************************/
@@ -1468,6 +1619,8 @@ function create_gclass(gclass_name)
1468
1619
  ["EV_SEARCH", ac_search, null],
1469
1620
  ["EV_EXPAND_ALL", ac_expand_all, null],
1470
1621
  ["EV_COLLAPSE_ALL", ac_collapse_all, null],
1622
+ ["EV_EXPAND_LEVEL", ac_expand_level, null],
1623
+ ["EV_COLLAPSE_LEVEL", ac_collapse_level, null],
1471
1624
  ["EV_COPY_ALL", ac_copy_all, null],
1472
1625
  ["EV_SET_VIEW_MODE", ac_set_view_mode, null],
1473
1626
  ["EV_LANGUAGE_CHANGED", ac_language_changed, null],
@@ -1489,6 +1642,8 @@ function create_gclass(gclass_name)
1489
1642
  ["EV_SEARCH", 0],
1490
1643
  ["EV_EXPAND_ALL", 0],
1491
1644
  ["EV_COLLAPSE_ALL", 0],
1645
+ ["EV_EXPAND_LEVEL", 0],
1646
+ ["EV_COLLAPSE_LEVEL", 0],
1492
1647
  ["EV_COPY_ALL", 0],
1493
1648
  ["EV_SET_VIEW_MODE", 0],
1494
1649
  ["EV_JSON_ITEM_CLICKED", 0],
@@ -0,0 +1,186 @@
1
+ /***********************************************************************
2
+ * json_level_stepper.wiring.test.js
3
+ *
4
+ * The tree of C_YUI_JSON opens and folds one LEVEL at a time
5
+ * (EV_EXPAND_LEVEL / EV_COLLAPSE_LEVEL), beside the pair that does
6
+ * it all at once (EV_EXPAND_ALL / EV_COLLAPSE_ALL):
7
+ *
8
+ * - `+` opens every loaded container of the shallowest level
9
+ * that still has one closed;
10
+ * - `-` folds the deepest level open in sight, and everything
11
+ * under it.
12
+ *
13
+ * Asked for on the json popup of a treedb cell: from a closed
14
+ * document, one press shows the fields of each entry with their
15
+ * own containers still closed.
16
+ *
17
+ * Driven through the real gclass on a document double.
18
+ *
19
+ * Copyright (c) 2026, ArtGins.
20
+ * All Rights Reserved.
21
+ ***********************************************************************/
22
+ import {test, expect, beforeAll, beforeEach} from "vitest";
23
+ import {install_dom_double} from "../test/dom_double.js";
24
+
25
+ install_dom_double();
26
+
27
+ if(!globalThis.ResizeObserver) {
28
+ globalThis.ResizeObserver = class {
29
+ observe() {
30
+ }
31
+ unobserve() {
32
+ }
33
+ disconnect() {
34
+ }
35
+ };
36
+ }
37
+
38
+ const {
39
+ SDATA_END, gclass_create, gobj_start_up, gobj_create_yuno, gobj_create,
40
+ gobj_create_pure_child, gobj_start, gobj_read_attr, gobj_send_event,
41
+ set_log_callback,
42
+ } = await import("@yuneta/gobj-js");
43
+ const {register_c_yui_json} = await import("./c_yui_json.js");
44
+
45
+ const logged = [];
46
+ let yuno = null;
47
+ let seq = 0;
48
+
49
+ /* The shape of a template_settings cell: entries of fields, some of
50
+ * them containers in turn. */
51
+ const DOC = {
52
+ template_version: {type: "integer", default: 1, flag: ["persistent", "writable"]},
53
+ top_measures: {type: "array", default: ["temperature"], flag: ["persistent", "writable", "enum"]},
54
+ coordinates: {type: "dict", default: {geometry: {type: "Point", coordinates: [0, 0]}}},
55
+ note: "a leaf at the top",
56
+ };
57
+
58
+ beforeAll(async () => {
59
+ const i18next = (await import("i18next")).default;
60
+ await i18next.init({lng: "en", resources: {}});
61
+ gobj_start_up(null, null, null, null, null, null, null);
62
+ set_log_callback((level, msg) => {
63
+ logged.push({level: String(level), msg: String(msg)});
64
+ });
65
+ gclass_create("C_TEST_LEVEL_HOST", [], [["ST_IDLE", []]], {}, 0, [SDATA_END()], {}, 0, 0, 0, 0);
66
+ gclass_create(
67
+ "C_TEST_LEVEL_APP",
68
+ [["EV_EXPAND_PATH", 0]],
69
+ [["ST_IDLE", [["EV_EXPAND_PATH", () => 0, null]]]],
70
+ {}, 0, [SDATA_END()], {}, 0, 0, 0, 0
71
+ );
72
+ register_c_yui_json();
73
+ yuno = gobj_create_yuno("level_yuno", "C_TEST_LEVEL_HOST", {});
74
+ gobj_start(yuno);
75
+ });
76
+
77
+ beforeEach(() => {
78
+ logged.length = 0;
79
+ });
80
+
81
+ function new_viewer(doc)
82
+ {
83
+ const app = gobj_create_pure_child(`level_app_${++seq}`, "C_TEST_LEVEL_APP", {}, yuno);
84
+ gobj_start(app);
85
+ const viewer = gobj_create(`level_json_${seq}`, "C_YUI_JSON", {view_mode: "tree"}, app);
86
+ gobj_start(viewer);
87
+ gobj_send_event(viewer, "EV_SET_JSON", {json: doc}, app);
88
+ return viewer;
89
+ }
90
+
91
+ /* The keys of the container rows on screen that are open */
92
+ function open_keys(viewer)
93
+ {
94
+ const $c = gobj_read_attr(viewer, "$container");
95
+ return Array.from($c.querySelectorAll(".JSON_CONTAINER"))
96
+ .filter(($r) => $r.querySelector(".JSON_TOGGLE").classList.contains("is-open"))
97
+ .map(($r) => $r.querySelector(".JSON_KEY").textContent);
98
+ }
99
+
100
+ /* How many rows the tree shows */
101
+ function row_count(viewer)
102
+ {
103
+ return gobj_read_attr(viewer, "$container").querySelectorAll(".JSON_ROW").length;
104
+ }
105
+
106
+ function step(viewer, event)
107
+ {
108
+ gobj_send_event(viewer, event, {}, viewer);
109
+ }
110
+
111
+ function errors()
112
+ {
113
+ return logged.filter((l) => l.level === "error").map((l) => l.msg);
114
+ }
115
+
116
+ test("`+` opens one level at a time, `-` folds the deepest one", () => {
117
+ const v = new_viewer(DOC);
118
+ expect(open_keys(v)).toEqual([]);
119
+ expect(row_count(v)).toBe(4);
120
+
121
+ step(v, "EV_EXPAND_LEVEL");
122
+ expect(open_keys(v)).toEqual(["template_version", "top_measures", "coordinates"]);
123
+
124
+ step(v, "EV_EXPAND_LEVEL");
125
+ expect(open_keys(v)).toEqual(["template_version", "flag", "top_measures", "default", "flag",
126
+ "coordinates", "default"]);
127
+
128
+ step(v, "EV_EXPAND_LEVEL");
129
+ expect(open_keys(v)).toContain("geometry");
130
+
131
+ step(v, "EV_COLLAPSE_LEVEL");
132
+ expect(open_keys(v)).toEqual(["template_version", "flag", "top_measures", "default", "flag",
133
+ "coordinates", "default"]);
134
+
135
+ step(v, "EV_COLLAPSE_LEVEL");
136
+ step(v, "EV_COLLAPSE_LEVEL");
137
+ expect(open_keys(v)).toEqual([]);
138
+ expect(row_count(v)).toBe(4);
139
+ expect(errors()).toEqual([]);
140
+ });
141
+
142
+ test("`+` opens the shallowest level that still has a container closed", () => {
143
+ const v = new_viewer(DOC);
144
+ /* The reader opened one branch by hand, two levels deep. */
145
+ gobj_send_event(v, "EV_TOGGLE_NODE", {path: "coordinates"}, v);
146
+ expect(open_keys(v)).toEqual(["coordinates"]);
147
+
148
+ step(v, "EV_EXPAND_LEVEL");
149
+ expect(open_keys(v)).toEqual(["template_version", "top_measures", "coordinates"]);
150
+ });
151
+
152
+ test("`-` folds what is in sight, not a branch hidden under a closed one", () => {
153
+ const v = new_viewer(DOC);
154
+ step(v, "EV_EXPAND_LEVEL");
155
+ step(v, "EV_EXPAND_LEVEL");
156
+ /* Close `coordinates` by hand: its open `default` is out of sight. */
157
+ gobj_send_event(v, "EV_TOGGLE_NODE", {path: "coordinates"}, v);
158
+
159
+ step(v, "EV_COLLAPSE_LEVEL");
160
+ expect(open_keys(v)).toEqual(["template_version", "top_measures"]);
161
+ });
162
+
163
+ test("a step with nothing to step does nothing, and says no error", () => {
164
+ const v = new_viewer({a: 1, b: "x"});
165
+ step(v, "EV_EXPAND_LEVEL");
166
+ step(v, "EV_COLLAPSE_LEVEL");
167
+ expect(row_count(v)).toBe(2);
168
+ expect(errors()).toEqual([]);
169
+ });
170
+
171
+ test("the stepper is a tree control: it hides with the tree", () => {
172
+ const v = new_viewer(DOC);
173
+ const $c = gobj_read_attr(v, "$container");
174
+ const $minus = $c.querySelector(".EV_COLLAPSE_LEVEL");
175
+ const $plus = $c.querySelector(".EV_EXPAND_LEVEL");
176
+ expect($minus.querySelector("i.yi-square-minus")).not.toBe(null);
177
+ expect($plus.querySelector("i.yi-square-plus")).not.toBe(null);
178
+ expect($minus.getAttribute("aria-label")).toBe("collapse one level");
179
+ expect($plus.getAttribute("data-i18n-aria-label")).toBe("expand one level");
180
+
181
+ gobj_send_event(v, "EV_SET_VIEW_MODE", {mode: "text"}, v);
182
+ expect($minus.classList.contains("is-hidden")).toBe(true);
183
+ expect($plus.classList.contains("is-hidden")).toBe(true);
184
+ gobj_send_event(v, "EV_SET_VIEW_MODE", {mode: "tree"}, v);
185
+ expect($plus.classList.contains("is-hidden")).toBe(false);
186
+ });