@yuneta/gobj-ui 7.32.1 → 7.32.3
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 +36 -6
- package/package.json +1 -1
- package/src/yui_table_standard.js +97 -0
- package/src/yui_table_standard.test.js +72 -0
package/README.md
CHANGED
|
@@ -4401,6 +4401,33 @@ 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 column whose cell shows something else than its value (a boolean drawn as
|
|
4405
|
+
"enabled", a time drawn as a date, a list drawn as names) filters with
|
|
4406
|
+
`yui_text_filter(text_of)`. `text_of(data)` gives the text that the row
|
|
4407
|
+
shows, and the row passes when that text holds the term:
|
|
4408
|
+
|
|
4409
|
+
```js
|
|
4410
|
+
{title: t("status"), field: "disabled",
|
|
4411
|
+
headerFilterFunc: yui_text_filter((d) => t(d.disabled ? "disabled" : "enabled"))}
|
|
4412
|
+
```
|
|
4413
|
+
|
|
4414
|
+
A tree table (`dataTree`) filters each column with
|
|
4415
|
+
`yui_tree_filter(text_of)`. Tabulator filters a tree from its roots, so a
|
|
4416
|
+
parent that fails is hidden with all its children: a filter on the name of
|
|
4417
|
+
a yuno hid every node. `text_of(data)` gives the text that a row shows in
|
|
4418
|
+
the column. A row passes when its own text holds the term, or when the text
|
|
4419
|
+
of one of its descendants does. Tabulator then filters the children with the
|
|
4420
|
+
same function, so only the children that match are shown:
|
|
4421
|
+
|
|
4422
|
+
```js
|
|
4423
|
+
{title: t("name"), field: "id",
|
|
4424
|
+
headerFilterFunc: yui_tree_filter((d) => d._type === "yuno" ? d.label : d.host)}
|
|
4425
|
+
```
|
|
4426
|
+
|
|
4427
|
+
`yui_table_filters()` gives that column its text box. The second argument of
|
|
4428
|
+
`yui_tree_filter()` is the `dataTreeChildField` of the table, `"_children"`
|
|
4429
|
+
by default.
|
|
4430
|
+
|
|
4404
4431
|
`yui_table_wire(table, t)` names each filter box ("filter by column") and
|
|
4405
4432
|
adds its ✕ (`yui_table_filter_clear()`). It does this when the table is
|
|
4406
4433
|
built and again each time Tabulator builds the columns again. Call it right
|
|
@@ -4412,9 +4439,9 @@ and the paginator keys of `yui_tabulator_lang()`.
|
|
|
4412
4439
|
Two things to look at when you apply it:
|
|
4413
4440
|
|
|
4414
4441
|
- A text filter compares the value of the row, not what the formatter
|
|
4415
|
-
shows. If a column shows a boolean as "Yes" or "No", give it
|
|
4416
|
-
(`headerFilter: "tickCross"`,
|
|
4417
|
-
a list.
|
|
4442
|
+
shows. If a column shows a boolean as "Yes" or "No", give it
|
|
4443
|
+
`yui_text_filter()`, a tick (`headerFilter: "tickCross"`,
|
|
4444
|
+
`headerFilterParams: {tristate: true}`) or a list.
|
|
4418
4445
|
- A column with a fixed `width` under 150 px cuts the placeholder of its
|
|
4419
4446
|
filter. Give it 150.
|
|
4420
4447
|
|
|
@@ -4561,9 +4588,12 @@ the tables of `gui_treedb` and `gui_agent`. Other tables can obey it too.
|
|
|
4561
4588
|
it scrolls sideways with its arrows (`yui_toolbar()`). It never wraps into
|
|
4562
4589
|
a second row.
|
|
4563
4590
|
|
|
4564
|
-
There is one exception
|
|
4565
|
-
|
|
4566
|
-
from the backend (remote pagination).
|
|
4591
|
+
There is one exception: the records of a timeranger2 topic. A topic can
|
|
4592
|
+
hold millions of records, so those tables do not obey point 3. They page
|
|
4593
|
+
from the backend (remote pagination). Their filters are the ones that the
|
|
4594
|
+
backend can apply, or they filter the page that is loaded, and the table
|
|
4595
|
+
says so. The live view of a topic holds a rolling buffer of the newest
|
|
4596
|
+
records, so it has no pages. These tables obey point 4.
|
|
4567
4597
|
|
|
4568
4598
|
How to write it: the table with `yui_table_standard.js` (see
|
|
4569
4599
|
[The standard table](#the-standard-table-yui_table_standardjs)), the
|
package/package.json
CHANGED
|
@@ -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,97 @@ function yui_table_filters(columns)
|
|
|
180
184
|
});
|
|
181
185
|
}
|
|
182
186
|
|
|
187
|
+
/***************************************************************
|
|
188
|
+
* The header filter of a column whose cell SHOWS something else
|
|
189
|
+
* than its value: a boolean drawn as "enabled", a time drawn as a
|
|
190
|
+
* date, a list drawn as names.
|
|
191
|
+
*
|
|
192
|
+
* `text_of(data)` answers the text that the row shows in the
|
|
193
|
+
* column. The row passes when it holds the term, in any case. A
|
|
194
|
+
* plain text filter compares the VALUE, so "enabled" finds nothing
|
|
195
|
+
* in a column of `false`.
|
|
196
|
+
*
|
|
197
|
+
* {title: t("status"), field: "disabled",
|
|
198
|
+
* headerFilterFunc: yui_text_filter((d) => t(d.disabled ? "disabled" : "enabled"))}
|
|
199
|
+
*
|
|
200
|
+
* `yui_table_filters()` then gives the column its text box.
|
|
201
|
+
***************************************************************/
|
|
202
|
+
function yui_text_filter(text_of)
|
|
203
|
+
{
|
|
204
|
+
return function(term, value, data) {
|
|
205
|
+
let needle = String(term ?? "").trim().toLowerCase();
|
|
206
|
+
if(!needle) {
|
|
207
|
+
return true;
|
|
208
|
+
}
|
|
209
|
+
let text = "";
|
|
210
|
+
try {
|
|
211
|
+
text = String(text_of(data) ?? "");
|
|
212
|
+
} catch(e) {
|
|
213
|
+
text = "";
|
|
214
|
+
}
|
|
215
|
+
return text.toLowerCase().includes(needle);
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/***************************************************************
|
|
220
|
+
* The header filter of a column of a TREE table (`dataTree`).
|
|
221
|
+
*
|
|
222
|
+
* `text_of(data)` answers the text that a row shows in the column.
|
|
223
|
+
* A row passes when its own text holds the term (in any case), or
|
|
224
|
+
* when the text of one of its descendants does.
|
|
225
|
+
*
|
|
226
|
+
* Why the descendants: Tabulator filters a tree from its roots, and
|
|
227
|
+
* a parent that fails is hidden with all its children. A filter on
|
|
228
|
+
* the name of a yuno would hide every node, and the yunos with
|
|
229
|
+
* them. With this one the node stays, and Tabulator then filters
|
|
230
|
+
* its children with the same function (`dataTreeFilter`, on by
|
|
231
|
+
* default), so only the yunos that match are shown.
|
|
232
|
+
*
|
|
233
|
+
* `child_field` is the table's `dataTreeChildField` ("_children").
|
|
234
|
+
*
|
|
235
|
+
* {title: t("name"), field: "id",
|
|
236
|
+
* headerFilterFunc: yui_tree_filter((d) => d.label || d.host)}
|
|
237
|
+
*
|
|
238
|
+
* `yui_table_filters()` then gives the column its text box.
|
|
239
|
+
***************************************************************/
|
|
240
|
+
function yui_tree_filter(text_of, child_field)
|
|
241
|
+
{
|
|
242
|
+
let field = child_field || "_children";
|
|
243
|
+
|
|
244
|
+
function matches(data, needle, depth)
|
|
245
|
+
{
|
|
246
|
+
if(!data || typeof data !== "object") {
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
let text = "";
|
|
250
|
+
try {
|
|
251
|
+
text = String(text_of(data) ?? "");
|
|
252
|
+
} catch(e) {
|
|
253
|
+
text = "";
|
|
254
|
+
}
|
|
255
|
+
if(text.toLowerCase().includes(needle)) {
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
/* A guard against a cycle in data that is not a tree. */
|
|
259
|
+
if(depth > 32) {
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
let children = data[field];
|
|
263
|
+
if(!Array.isArray(children)) {
|
|
264
|
+
return false;
|
|
265
|
+
}
|
|
266
|
+
return children.some((child) => matches(child, needle, depth + 1));
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return function(term, value, data) {
|
|
270
|
+
let needle = String(term ?? "").trim().toLowerCase();
|
|
271
|
+
if(!needle) {
|
|
272
|
+
return true;
|
|
273
|
+
}
|
|
274
|
+
return matches(data, needle, 0);
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
|
|
183
278
|
/***************************************************************
|
|
184
279
|
* What the header filters need after the table is drawn.
|
|
185
280
|
*
|
|
@@ -210,5 +305,7 @@ export {
|
|
|
210
305
|
YUI_TABLE_FILTER_MIN_WIDTH,
|
|
211
306
|
yui_table_paging,
|
|
212
307
|
yui_table_filters,
|
|
308
|
+
yui_text_filter,
|
|
309
|
+
yui_tree_filter,
|
|
213
310
|
yui_table_wire,
|
|
214
311
|
};
|
|
@@ -17,6 +17,8 @@ import {
|
|
|
17
17
|
YUI_TABLE_FILTER_MIN_WIDTH,
|
|
18
18
|
yui_table_paging,
|
|
19
19
|
yui_table_filters,
|
|
20
|
+
yui_text_filter,
|
|
21
|
+
yui_tree_filter,
|
|
20
22
|
yui_table_wire,
|
|
21
23
|
} from "./yui_table_standard.js";
|
|
22
24
|
|
|
@@ -146,6 +148,76 @@ describe("the filters", () => {
|
|
|
146
148
|
});
|
|
147
149
|
});
|
|
148
150
|
|
|
151
|
+
describe("the filter of what a cell shows", () => {
|
|
152
|
+
const f = yui_text_filter((d) => d.disabled ? "Disabled" : "Enabled");
|
|
153
|
+
|
|
154
|
+
test("compares the text shown, not the value, in any case", () => {
|
|
155
|
+
expect(f("enab", false, {disabled: false})).toBe(true);
|
|
156
|
+
expect(f("enab", true, {disabled: true})).toBe(false);
|
|
157
|
+
expect(f("DIS", true, {disabled: true})).toBe(true);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test("an empty term passes everything", () => {
|
|
161
|
+
expect(f("", true, {disabled: true})).toBe(true);
|
|
162
|
+
expect(f(undefined, true, {disabled: true})).toBe(true);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test("a text_of that throws, or answers nothing, matches nothing", () => {
|
|
166
|
+
let bad = yui_text_filter(() => {
|
|
167
|
+
throw new Error("x");
|
|
168
|
+
});
|
|
169
|
+
expect(bad("a", null, {})).toBe(false);
|
|
170
|
+
expect(yui_text_filter(() => null)("a", null, {})).toBe(false);
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
describe("the filter of a tree", () => {
|
|
175
|
+
const node = {
|
|
176
|
+
host: "artgins",
|
|
177
|
+
_children: [
|
|
178
|
+
{label: "yuneta_agent"},
|
|
179
|
+
{label: "controlcenter", _children: [{label: "auth_bff"}]},
|
|
180
|
+
]
|
|
181
|
+
};
|
|
182
|
+
const f = yui_tree_filter((d) => d.label || d.host);
|
|
183
|
+
|
|
184
|
+
test("a row passes on its own text, in any case", () => {
|
|
185
|
+
expect(f("ArtG", null, node)).toBe(true);
|
|
186
|
+
expect(f("agent", null, node._children[0])).toBe(true);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test("a parent passes when a descendant matches, at any depth", () => {
|
|
190
|
+
expect(f("control", null, node)).toBe(true);
|
|
191
|
+
expect(f("bff", null, node)).toBe(true);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test("a row that does not match, with no descendant that does, fails", () => {
|
|
195
|
+
expect(f("mqtt", null, node)).toBe(false);
|
|
196
|
+
expect(f("control", null, node._children[0])).toBe(false);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
test("an empty term passes everything", () => {
|
|
200
|
+
expect(f("", null, node)).toBe(true);
|
|
201
|
+
expect(f(" ", null, node)).toBe(true);
|
|
202
|
+
expect(f(null, null, node)).toBe(true);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
test("another child field, and a text_of that throws", () => {
|
|
206
|
+
let g = yui_tree_filter((d) => d.name, "kids");
|
|
207
|
+
expect(g("b", null, {name: "a", kids: [{name: "b"}]})).toBe(true);
|
|
208
|
+
let bad = yui_tree_filter(() => {
|
|
209
|
+
throw new Error("x");
|
|
210
|
+
});
|
|
211
|
+
expect(bad("a", null, {})).toBe(false);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test("with yui_table_filters, the column gets its text box", () => {
|
|
215
|
+
let [c] = yui_table_filters([{field: "id", headerFilterFunc: f}]);
|
|
216
|
+
expect(c.headerFilter).toBe("input");
|
|
217
|
+
expect(c.headerFilterFunc).toBe(f);
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
|
|
149
221
|
describe("the wiring", () => {
|
|
150
222
|
test("names the filters when the table is built and on every rebuild", () => {
|
|
151
223
|
let events = {};
|