@yuneta/gobj-ui 7.32.1 → 7.32.2

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
@@ -4401,6 +4401,23 @@ header filter:
4401
4401
  - A column with no `field`, or with a control formatter (`rowSelection`),
4402
4402
  gets no filter. Column groups are walked.
4403
4403
 
4404
+ A tree table (`dataTree`) filters each column with
4405
+ `yui_tree_filter(text_of)`. Tabulator filters a tree from its roots, so a
4406
+ parent that fails is hidden with all its children: a filter on the name of
4407
+ a yuno hid every node. `text_of(data)` gives the text that a row shows in
4408
+ the column. A row passes when its own text holds the term, or when the text
4409
+ of one of its descendants does. Tabulator then filters the children with the
4410
+ same function, so only the children that match are shown:
4411
+
4412
+ ```js
4413
+ {title: t("name"), field: "id",
4414
+ headerFilterFunc: yui_tree_filter((d) => d._type === "yuno" ? d.label : d.host)}
4415
+ ```
4416
+
4417
+ `yui_table_filters()` gives that column its text box. The second argument of
4418
+ `yui_tree_filter()` is the `dataTreeChildField` of the table, `"_children"`
4419
+ by default.
4420
+
4404
4421
  `yui_table_wire(table, t)` names each filter box ("filter by column") and
4405
4422
  adds its ✕ (`yui_table_filter_clear()`). It does this when the table is
4406
4423
  built and again each time Tabulator builds the columns again. Call it right
@@ -4561,9 +4578,12 @@ the tables of `gui_treedb` and `gui_agent`. Other tables can obey it too.
4561
4578
  it scrolls sideways with its arrows (`yui_toolbar()`). It never wraps into
4562
4579
  a second row.
4563
4580
 
4564
- There is one exception. The records of a timeranger2 topic do not obey
4565
- point 3, because a topic can hold millions of records. Those tables page
4566
- from the backend (remote pagination). They obey points 1, 2 and 4.
4581
+ There is one exception: the records of a timeranger2 topic. A topic can
4582
+ hold millions of records, so those tables do not obey point 3. They page
4583
+ from the backend (remote pagination). Their filters are the ones that the
4584
+ backend can apply, or they filter the page that is loaded, and the table
4585
+ says so. The live view of a topic holds a rolling buffer of the newest
4586
+ records, so it has no pages. These tables obey point 4.
4567
4587
 
4568
4588
  How to write it: the table with `yui_table_standard.js` (see
4569
4589
  [The standard table](#the-standard-table-yui_table_standardjs)), the
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yuneta/gobj-ui",
3
- "version": "7.32.1",
3
+ "version": "7.32.2",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "dist/gobj-ui.cjs.js",
@@ -36,6 +36,10 @@
36
36
  * timeranger2 topic), and it pages from the backend on
37
37
  * purpose.
38
38
  *
39
+ * A TREE table (`dataTree`) filters each column with
40
+ * `yui_tree_filter()`: Tabulator filters a tree from its roots,
41
+ * and a parent that fails hides the children that match.
42
+ *
39
43
  * The row count needs the key "rows" in the app (every app
40
44
  * that shows a treedb table has it already).
41
45
  *
@@ -180,6 +184,65 @@ function yui_table_filters(columns)
180
184
  });
181
185
  }
182
186
 
187
+ /***************************************************************
188
+ * The header filter of a column of a TREE table (`dataTree`).
189
+ *
190
+ * `text_of(data)` answers the text that a row shows in the column.
191
+ * A row passes when its own text holds the term (in any case), or
192
+ * when the text of one of its descendants does.
193
+ *
194
+ * Why the descendants: Tabulator filters a tree from its roots, and
195
+ * a parent that fails is hidden with all its children. A filter on
196
+ * the name of a yuno would hide every node, and the yunos with
197
+ * them. With this one the node stays, and Tabulator then filters
198
+ * its children with the same function (`dataTreeFilter`, on by
199
+ * default), so only the yunos that match are shown.
200
+ *
201
+ * `child_field` is the table's `dataTreeChildField` ("_children").
202
+ *
203
+ * {title: t("name"), field: "id",
204
+ * headerFilterFunc: yui_tree_filter((d) => d.label || d.host)}
205
+ *
206
+ * `yui_table_filters()` then gives the column its text box.
207
+ ***************************************************************/
208
+ function yui_tree_filter(text_of, child_field)
209
+ {
210
+ let field = child_field || "_children";
211
+
212
+ function matches(data, needle, depth)
213
+ {
214
+ if(!data || typeof data !== "object") {
215
+ return false;
216
+ }
217
+ let text = "";
218
+ try {
219
+ text = String(text_of(data) ?? "");
220
+ } catch(e) {
221
+ text = "";
222
+ }
223
+ if(text.toLowerCase().includes(needle)) {
224
+ return true;
225
+ }
226
+ /* A guard against a cycle in data that is not a tree. */
227
+ if(depth > 32) {
228
+ return false;
229
+ }
230
+ let children = data[field];
231
+ if(!Array.isArray(children)) {
232
+ return false;
233
+ }
234
+ return children.some((child) => matches(child, needle, depth + 1));
235
+ }
236
+
237
+ return function(term, value, data) {
238
+ let needle = String(term ?? "").trim().toLowerCase();
239
+ if(!needle) {
240
+ return true;
241
+ }
242
+ return matches(data, needle, 0);
243
+ };
244
+ }
245
+
183
246
  /***************************************************************
184
247
  * What the header filters need after the table is drawn.
185
248
  *
@@ -210,5 +273,6 @@ export {
210
273
  YUI_TABLE_FILTER_MIN_WIDTH,
211
274
  yui_table_paging,
212
275
  yui_table_filters,
276
+ yui_tree_filter,
213
277
  yui_table_wire,
214
278
  };
@@ -17,6 +17,7 @@ import {
17
17
  YUI_TABLE_FILTER_MIN_WIDTH,
18
18
  yui_table_paging,
19
19
  yui_table_filters,
20
+ yui_tree_filter,
20
21
  yui_table_wire,
21
22
  } from "./yui_table_standard.js";
22
23
 
@@ -146,6 +147,53 @@ describe("the filters", () => {
146
147
  });
147
148
  });
148
149
 
150
+ describe("the filter of a tree", () => {
151
+ const node = {
152
+ host: "artgins",
153
+ _children: [
154
+ {label: "yuneta_agent"},
155
+ {label: "controlcenter", _children: [{label: "auth_bff"}]},
156
+ ]
157
+ };
158
+ const f = yui_tree_filter((d) => d.label || d.host);
159
+
160
+ test("a row passes on its own text, in any case", () => {
161
+ expect(f("ArtG", null, node)).toBe(true);
162
+ expect(f("agent", null, node._children[0])).toBe(true);
163
+ });
164
+
165
+ test("a parent passes when a descendant matches, at any depth", () => {
166
+ expect(f("control", null, node)).toBe(true);
167
+ expect(f("bff", null, node)).toBe(true);
168
+ });
169
+
170
+ test("a row that does not match, with no descendant that does, fails", () => {
171
+ expect(f("mqtt", null, node)).toBe(false);
172
+ expect(f("control", null, node._children[0])).toBe(false);
173
+ });
174
+
175
+ test("an empty term passes everything", () => {
176
+ expect(f("", null, node)).toBe(true);
177
+ expect(f(" ", null, node)).toBe(true);
178
+ expect(f(null, null, node)).toBe(true);
179
+ });
180
+
181
+ test("another child field, and a text_of that throws", () => {
182
+ let g = yui_tree_filter((d) => d.name, "kids");
183
+ expect(g("b", null, {name: "a", kids: [{name: "b"}]})).toBe(true);
184
+ let bad = yui_tree_filter(() => {
185
+ throw new Error("x");
186
+ });
187
+ expect(bad("a", null, {})).toBe(false);
188
+ });
189
+
190
+ test("with yui_table_filters, the column gets its text box", () => {
191
+ let [c] = yui_table_filters([{field: "id", headerFilterFunc: f}]);
192
+ expect(c.headerFilter).toBe("input");
193
+ expect(c.headerFilterFunc).toBe(f);
194
+ });
195
+ });
196
+
149
197
  describe("the wiring", () => {
150
198
  test("names the filters when the table is built and on every rebuild", () => {
151
199
  let events = {};