@yuneta/gobj-ui 7.26.51 → 7.26.53
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 +88 -2
- package/dist/gobj-ui.cjs.js +330 -71
- package/dist/gobj-ui.es.js +321 -63
- package/index.js +1 -0
- package/package.json +1 -1
- package/src/c_yui_form.js +48 -5
- package/src/c_yui_json.js +36 -3
- package/src/c_yui_treedb_topic_with_form.js +13 -28
- package/src/c_yui_uplot.js +51 -2
- package/src/fmt_date_time.test.js +138 -0
- package/src/form_table_rows.wiring.test.js +5 -4
- package/src/form_time_value.js +9 -6
- package/src/form_time_value.test.js +1 -1
- package/src/json_view_helpers.js +8 -3
- package/src/time_text.wiring.test.js +489 -0
- package/src/yui_date_text.js +82 -0
- package/src/yui_time.js +6 -22
- package/src/yui_uplot_time.js +142 -0
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
/***********************************************************************
|
|
2
|
+
* time_text.wiring.test.js
|
|
3
|
+
*
|
|
4
|
+
* A time value a view shows as text is shown in the APP's language
|
|
5
|
+
* (i18next's, never the browser's) with a clock of 24 hours, the
|
|
6
|
+
* same in the four views that show one (fmt_date_time of
|
|
7
|
+
* yui_time.js), and it changes language with the app: on the
|
|
8
|
+
* shell's EV_LANGUAGE_CHANGED.
|
|
9
|
+
*
|
|
10
|
+
* Up to gobj-ui 7.26.51:
|
|
11
|
+
* - C_YUI_TREEDB_TOPIC_WITH_FORM wrote its time cells in the
|
|
12
|
+
* BROWSER's language with `hour12: true` ("14 oct 2026,
|
|
13
|
+
* 03:05:09 p. m."), so the app's language button changed
|
|
14
|
+
* nothing in them;
|
|
15
|
+
* - C_YUI_FORM wrote "2026-10-14 15:05:09";
|
|
16
|
+
* - C_YUI_JSON wrote toLocaleString() beside a time field, and
|
|
17
|
+
* never heard the language change: it did not subscribe to
|
|
18
|
+
* the shell, and no host forwarded the event;
|
|
19
|
+
* - C_YUI_UPLOT let uPlot write its English 12 hours ("3pm",
|
|
20
|
+
* legend "2026-03-14 3:26pm"), and never heard the change.
|
|
21
|
+
*
|
|
22
|
+
* Driven through the real gclasses and the real shell on a document
|
|
23
|
+
* double, with a Tabulator double that draws a cell by running its
|
|
24
|
+
* column's formatter, and draws every row again on setColumns(), as
|
|
25
|
+
* Tabulator does. The real Tabulator is checked in the browser.
|
|
26
|
+
*
|
|
27
|
+
* Copyright (c) 2026, ArtGins.
|
|
28
|
+
* All Rights Reserved.
|
|
29
|
+
***********************************************************************/
|
|
30
|
+
import {describe, test, expect, beforeAll, beforeEach, vi} from "vitest";
|
|
31
|
+
import {install_dom_double} from "../test/dom_double.js";
|
|
32
|
+
|
|
33
|
+
install_dom_double();
|
|
34
|
+
|
|
35
|
+
if(typeof globalThis.ResizeObserver === "undefined") {
|
|
36
|
+
globalThis.ResizeObserver = class {
|
|
37
|
+
observe() {
|
|
38
|
+
}
|
|
39
|
+
unobserve() {
|
|
40
|
+
}
|
|
41
|
+
disconnect() {
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
if(typeof globalThis.matchMedia === "undefined") {
|
|
46
|
+
globalThis.matchMedia = () => ({matches: false, addEventListener() {}, removeEventListener() {}});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const tables = vi.hoisted(() => []);
|
|
50
|
+
vi.mock("tabulator-tables", () => {
|
|
51
|
+
class FakeTabulator {
|
|
52
|
+
constructor($el, settings) {
|
|
53
|
+
this.element = $el;
|
|
54
|
+
this.settings = settings;
|
|
55
|
+
this.options = Object.assign({}, settings);
|
|
56
|
+
this.columns = (settings && settings.columns) || [];
|
|
57
|
+
this.handlers = {};
|
|
58
|
+
this.initialized = false;
|
|
59
|
+
this.destroyed = false;
|
|
60
|
+
this.rows = [];
|
|
61
|
+
this.calls = {setColumns: 0};
|
|
62
|
+
this.modules = {
|
|
63
|
+
localize: {
|
|
64
|
+
installLang: () => {
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
edit: {currentCell: false}
|
|
68
|
+
};
|
|
69
|
+
tables.push(this);
|
|
70
|
+
setTimeout(() => {
|
|
71
|
+
if(!this.destroyed) {
|
|
72
|
+
for(const record of (settings && settings.data) || []) {
|
|
73
|
+
this.add(record);
|
|
74
|
+
}
|
|
75
|
+
this.initialized = true;
|
|
76
|
+
this.emit("tableBuilt");
|
|
77
|
+
}
|
|
78
|
+
}, 0);
|
|
79
|
+
return new Proxy(this, {
|
|
80
|
+
get(target, prop) {
|
|
81
|
+
if(prop in target) {
|
|
82
|
+
return target[prop];
|
|
83
|
+
}
|
|
84
|
+
if(typeof prop === "string" && prop.startsWith("_")) {
|
|
85
|
+
return undefined; // a property a view hangs on it
|
|
86
|
+
}
|
|
87
|
+
return () => [];
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
on(ev, fn) {
|
|
92
|
+
(this.handlers[ev] = this.handlers[ev] || []).push(fn);
|
|
93
|
+
}
|
|
94
|
+
off(ev) {
|
|
95
|
+
delete this.handlers[ev];
|
|
96
|
+
}
|
|
97
|
+
emit(ev, ...args) {
|
|
98
|
+
for(const fn of (this.handlers[ev] || [])) {
|
|
99
|
+
fn(...args);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
leaf_columns() {
|
|
103
|
+
const out = [];
|
|
104
|
+
const walk = (defs) => {
|
|
105
|
+
for(const def of defs || []) {
|
|
106
|
+
if(def.columns) {
|
|
107
|
+
walk(def.columns);
|
|
108
|
+
} else if(def.field) {
|
|
109
|
+
out.push(def);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
walk(this.columns);
|
|
114
|
+
return out;
|
|
115
|
+
}
|
|
116
|
+
/* A cell as Tabulator draws it: its element holds what the
|
|
117
|
+
* column's formatter returns. */
|
|
118
|
+
draw_cell(row, def) {
|
|
119
|
+
const cell = row.cells[def.field] || {
|
|
120
|
+
$el: document.createElement("div"),
|
|
121
|
+
getValue: () => row.data[def.field],
|
|
122
|
+
getData: () => row.data,
|
|
123
|
+
getField: () => def.field,
|
|
124
|
+
getRow: () => row.component,
|
|
125
|
+
getElement: () => cell.$el,
|
|
126
|
+
};
|
|
127
|
+
row.cells[def.field] = cell;
|
|
128
|
+
cell.$el.className = "tabulator-cell";
|
|
129
|
+
while(cell.$el.firstChild) {
|
|
130
|
+
cell.$el.removeChild(cell.$el.firstChild);
|
|
131
|
+
}
|
|
132
|
+
const out = (typeof def.formatter === "function")?
|
|
133
|
+
def.formatter(cell, def.formatterParams || {}, () => {}) : row.data[def.field];
|
|
134
|
+
if(out && typeof out === "object") {
|
|
135
|
+
cell.$el.appendChild(out);
|
|
136
|
+
} else if(out !== undefined && out !== null) {
|
|
137
|
+
cell.$el.textContent = String(out);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
draw_row(row) {
|
|
141
|
+
for(const def of this.leaf_columns()) {
|
|
142
|
+
this.draw_cell(row, def);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
add(record) {
|
|
146
|
+
const row = {data: record, cells: {}};
|
|
147
|
+
row.component = {
|
|
148
|
+
getData: () => row.data,
|
|
149
|
+
getElement: () => document.createElement("div"),
|
|
150
|
+
getCells: () => Object.values(row.cells),
|
|
151
|
+
};
|
|
152
|
+
this.rows.push(row);
|
|
153
|
+
this.draw_row(row);
|
|
154
|
+
return row.component;
|
|
155
|
+
}
|
|
156
|
+
addRow(record) {
|
|
157
|
+
return Promise.resolve(this.add(record));
|
|
158
|
+
}
|
|
159
|
+
addData(records) {
|
|
160
|
+
return Promise.resolve(records.map((record) => this.add(record)));
|
|
161
|
+
}
|
|
162
|
+
setData(records) {
|
|
163
|
+
this.rows = [];
|
|
164
|
+
for(const record of records || []) {
|
|
165
|
+
this.add(record);
|
|
166
|
+
}
|
|
167
|
+
return Promise.resolve();
|
|
168
|
+
}
|
|
169
|
+
clearData() {
|
|
170
|
+
this.rows = [];
|
|
171
|
+
}
|
|
172
|
+
getRows() {
|
|
173
|
+
return this.rows.map((row) => row.component);
|
|
174
|
+
}
|
|
175
|
+
getData() {
|
|
176
|
+
return this.rows.map((row) => structuredClone(row.data));
|
|
177
|
+
}
|
|
178
|
+
getDataCount() {
|
|
179
|
+
return this.rows.length;
|
|
180
|
+
}
|
|
181
|
+
getColumns(structured) {
|
|
182
|
+
if(structured) {
|
|
183
|
+
return [];
|
|
184
|
+
}
|
|
185
|
+
return this.leaf_columns().map((def) => ({
|
|
186
|
+
getDefinition: () => def,
|
|
187
|
+
getField: () => def.field,
|
|
188
|
+
getElement: () => null,
|
|
189
|
+
getCells: () => this.rows.filter((row) => row.cells[def.field])
|
|
190
|
+
.map((row) => row.cells[def.field]),
|
|
191
|
+
}));
|
|
192
|
+
}
|
|
193
|
+
getColumnDefinitions() {
|
|
194
|
+
return this.columns;
|
|
195
|
+
}
|
|
196
|
+
/* Tabulator draws every row again with the new columns. */
|
|
197
|
+
setColumns(defs) {
|
|
198
|
+
this.calls.setColumns++;
|
|
199
|
+
this.columns = defs;
|
|
200
|
+
for(const row of this.rows) {
|
|
201
|
+
this.draw_row(row);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
cell_text(index, field) {
|
|
205
|
+
return this.rows[index].cells[field].$el.textContent;
|
|
206
|
+
}
|
|
207
|
+
validate() {
|
|
208
|
+
return true;
|
|
209
|
+
}
|
|
210
|
+
destroy() {
|
|
211
|
+
this.destroyed = true;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return {TabulatorFull: FakeTabulator, Tabulator: FakeTabulator};
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
/* uPlot draws on a canvas the document double has not: this one keeps
|
|
218
|
+
* its options, and writes a tick and the legend with them as uPlot
|
|
219
|
+
* does -- through `fmtDate` -- on a build, a redraw and a setLegend. */
|
|
220
|
+
const plots = vi.hoisted(() => []);
|
|
221
|
+
vi.mock("uplot", async () => {
|
|
222
|
+
if(typeof globalThis.devicePixelRatio === "undefined") {
|
|
223
|
+
globalThis.devicePixelRatio = 1;
|
|
224
|
+
}
|
|
225
|
+
const real = (await vi.importActual("uplot")).default;
|
|
226
|
+
class FakeUPlot {
|
|
227
|
+
constructor(opts, data, $el) {
|
|
228
|
+
this.opts = opts;
|
|
229
|
+
this.series = [{}];
|
|
230
|
+
this.data = data;
|
|
231
|
+
this.root = $el;
|
|
232
|
+
this.cursor = {idx: 0};
|
|
233
|
+
const fmt = opts.fmtDate || ((tpl) => real.fmtDate(tpl));
|
|
234
|
+
this.tick_of = fmt("{h}{aa}");
|
|
235
|
+
this.legend_of = fmt("{YYYY}-{MM}-{DD} {h}:{mm}{aa}");
|
|
236
|
+
this.calls = {redraw: 0, setLegend: 0};
|
|
237
|
+
plots.push(this);
|
|
238
|
+
this.draw();
|
|
239
|
+
}
|
|
240
|
+
draw() {
|
|
241
|
+
this.tick = this.tick_of(this.at);
|
|
242
|
+
this.legend = this.legend_of(this.at);
|
|
243
|
+
}
|
|
244
|
+
redraw() {
|
|
245
|
+
this.calls.redraw++;
|
|
246
|
+
this.draw();
|
|
247
|
+
}
|
|
248
|
+
setLegend() {
|
|
249
|
+
this.calls.setLegend++;
|
|
250
|
+
this.legend = this.legend_of(this.at);
|
|
251
|
+
}
|
|
252
|
+
setSize() {
|
|
253
|
+
}
|
|
254
|
+
destroy() {
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
FakeUPlot.prototype.at = new Date(2026, 2, 14, 15, 26, 53);
|
|
258
|
+
return {default: FakeUPlot};
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
const {
|
|
262
|
+
SDATA_END, gclass_create,
|
|
263
|
+
gobj_start_up, gobj_create_yuno, gobj_create, gobj_create_pure_child,
|
|
264
|
+
gobj_create_service, gobj_start, gobj_stop, gobj_destroy,
|
|
265
|
+
gobj_read_attr, gobj_send_event,
|
|
266
|
+
set_log_callback,
|
|
267
|
+
} = await import("@yuneta/gobj-js");
|
|
268
|
+
const {register_c_yui_shell, yui_shell_language_changed} = await import("./c_yui_shell.js");
|
|
269
|
+
const {register_c_yui_form} = await import("./c_yui_form.js");
|
|
270
|
+
const {register_c_yui_json} = await import("./c_yui_json.js");
|
|
271
|
+
const {register_c_yui_treedb_topic_with_form} = await import("./c_yui_treedb_topic_with_form.js");
|
|
272
|
+
const {register_c_yui_uplot} = await import("./c_yui_uplot.js");
|
|
273
|
+
|
|
274
|
+
const logged = [];
|
|
275
|
+
let yuno = null;
|
|
276
|
+
let shell = null;
|
|
277
|
+
let i18next = null;
|
|
278
|
+
|
|
279
|
+
/* A local wall time and its epoch, whatever the zone of the run. */
|
|
280
|
+
const WALL = new Date(2026, 2, 14, 9, 26, 53);
|
|
281
|
+
const EPOCH = Math.floor(WALL.getTime() / 1000);
|
|
282
|
+
const ISO = WALL.toISOString();
|
|
283
|
+
const ES = "14 mar 2026, 09:26:53";
|
|
284
|
+
const EN = "Mar 14, 2026, 09:26:53";
|
|
285
|
+
|
|
286
|
+
beforeAll(async () => {
|
|
287
|
+
i18next = (await import("i18next")).default;
|
|
288
|
+
await i18next.init({lng: "es", resources: {}});
|
|
289
|
+
gobj_start_up(null, null, null, null, null, null, null);
|
|
290
|
+
set_log_callback((level, msg) => {
|
|
291
|
+
logged.push({level: String(level), msg: String(msg)});
|
|
292
|
+
});
|
|
293
|
+
gclass_create("C_TEST_TT_HOST", [], [["ST_IDLE", []]], {}, 0, [SDATA_END()], {}, 0, 0, 0, 0);
|
|
294
|
+
gclass_create(
|
|
295
|
+
"C_TEST_TT_APP",
|
|
296
|
+
[["EV_EXPAND_PATH", 0]],
|
|
297
|
+
[["ST_IDLE", [["EV_EXPAND_PATH", () => 0, null]]]],
|
|
298
|
+
{}, 0, [SDATA_END()], {}, 0, 0, 0, 0
|
|
299
|
+
);
|
|
300
|
+
register_c_yui_shell();
|
|
301
|
+
register_c_yui_form();
|
|
302
|
+
register_c_yui_json();
|
|
303
|
+
register_c_yui_treedb_topic_with_form();
|
|
304
|
+
register_c_yui_uplot();
|
|
305
|
+
yuno = gobj_create_yuno("time_text_yuno", "C_TEST_TT_HOST", {});
|
|
306
|
+
gobj_start(yuno);
|
|
307
|
+
shell = gobj_create("time_text_shell", "C_YUI_SHELL", {config: {
|
|
308
|
+
shell: {
|
|
309
|
+
zones: {center: {host: "stage.main"}},
|
|
310
|
+
stages: {main: {zone: "center", default_route: "/home"}}
|
|
311
|
+
},
|
|
312
|
+
menu: {primary: {items: []}}
|
|
313
|
+
}}, yuno);
|
|
314
|
+
gobj_start(shell);
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
beforeEach(async () => {
|
|
318
|
+
logged.length = 0;
|
|
319
|
+
await i18next.changeLanguage("es");
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
function errors()
|
|
323
|
+
{
|
|
324
|
+
return logged.filter(l => /error/i.test(l.level) || /ERROR/.test(l.msg));
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/* What the app does when the reader picks a language. */
|
|
328
|
+
async function switch_language(lng)
|
|
329
|
+
{
|
|
330
|
+
await i18next.changeLanguage(lng);
|
|
331
|
+
yui_shell_language_changed(shell);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function settle()
|
|
335
|
+
{
|
|
336
|
+
return new Promise((resolve) => setTimeout(resolve, 0));
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
describe("the time cells of C_YUI_FORM", () => {
|
|
340
|
+
test("are the app's language and 24 hours, and change with it", async () => {
|
|
341
|
+
const first = tables.length;
|
|
342
|
+
const app = gobj_create_pure_child("tt_app_form", "C_TEST_TT_APP", {}, yuno);
|
|
343
|
+
gobj_start(app);
|
|
344
|
+
const form = gobj_create_pure_child("tt_form", "C_YUI_FORM", {
|
|
345
|
+
template: {
|
|
346
|
+
events: {
|
|
347
|
+
type: "array",
|
|
348
|
+
flag: ["writable", "persistent", "table"],
|
|
349
|
+
table: {
|
|
350
|
+
when: {type: "integer", flag: ["writable", "persistent", "time"]},
|
|
351
|
+
at: {type: "string", flag: ["writable", "persistent", "time"]},
|
|
352
|
+
noon: {type: "integer", flag: ["writable", "persistent", "time"]}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
},
|
|
356
|
+
record: {events: [{
|
|
357
|
+
when: EPOCH, at: ISO,
|
|
358
|
+
noon: Math.floor(new Date(2026, 2, 14, 12, 0, 0).getTime() / 1000)
|
|
359
|
+
}]},
|
|
360
|
+
editable: true,
|
|
361
|
+
}, app);
|
|
362
|
+
gobj_start(form);
|
|
363
|
+
await settle();
|
|
364
|
+
await settle();
|
|
365
|
+
const table = tables[first];
|
|
366
|
+
expect(table.rows.length).toBe(1);
|
|
367
|
+
|
|
368
|
+
expect(table.cell_text(0, "when")).toBe(ES);
|
|
369
|
+
expect(table.cell_text(0, "at")).toBe(ES);
|
|
370
|
+
expect(table.cell_text(0, "noon")).toBe("14 mar 2026, 12:00:00");
|
|
371
|
+
|
|
372
|
+
/* A cell being edited is its editor's: left alone. */
|
|
373
|
+
const $editing = table.rows[0].cells.at.$el;
|
|
374
|
+
$editing.classList.add("tabulator-editing");
|
|
375
|
+
|
|
376
|
+
await switch_language("en");
|
|
377
|
+
expect(table.cell_text(0, "when")).toBe(EN);
|
|
378
|
+
expect(table.cell_text(0, "noon")).toBe("Mar 14, 2026, 12:00:00");
|
|
379
|
+
expect(table.cell_text(0, "at")).toBe(ES);
|
|
380
|
+
|
|
381
|
+
$editing.classList.remove("tabulator-editing");
|
|
382
|
+
await switch_language("en");
|
|
383
|
+
expect(table.cell_text(0, "at")).toBe(EN);
|
|
384
|
+
await switch_language("es");
|
|
385
|
+
expect(table.cell_text(0, "when")).toBe(ES);
|
|
386
|
+
expect(table.cell_text(0, "at")).toBe(ES);
|
|
387
|
+
|
|
388
|
+
gobj_stop(form);
|
|
389
|
+
gobj_destroy(form);
|
|
390
|
+
expect(errors()).toEqual([]);
|
|
391
|
+
});
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
describe("the time cells of C_YUI_TREEDB_TOPIC_WITH_FORM", () => {
|
|
395
|
+
test("are the app's language and 24 hours, not the browser's 12, and change with it", async () => {
|
|
396
|
+
const first = tables.length;
|
|
397
|
+
const view = gobj_create_service("tt_topic", "C_YUI_TREEDB_TOPIC_WITH_FORM", {
|
|
398
|
+
treedb_name: "treedb_test",
|
|
399
|
+
topic_name: "events",
|
|
400
|
+
desc: {id: "events", pkey: "id", cols: [
|
|
401
|
+
{id: "id", header: "Id", type: "string", flag: ["persistent", "required"]},
|
|
402
|
+
{id: "at", header: "at", type: "string", flag: ["persistent", "time"]},
|
|
403
|
+
{id: "when", header: "when", type: "integer", flag: ["persistent", "time"]},
|
|
404
|
+
{id: "midnight", header: "midnight", type: "integer", flag: ["persistent", "time"]}
|
|
405
|
+
]}
|
|
406
|
+
}, yuno);
|
|
407
|
+
gobj_start(view);
|
|
408
|
+
await settle();
|
|
409
|
+
const table = tables[first];
|
|
410
|
+
expect(table._ready).toBe(true);
|
|
411
|
+
gobj_send_event(view, "EV_LOAD_NODES", [{
|
|
412
|
+
id: "a", at: ISO, when: EPOCH,
|
|
413
|
+
midnight: Math.floor(new Date(2026, 2, 14, 0, 0, 0).getTime() / 1000)
|
|
414
|
+
}, {
|
|
415
|
+
id: "b", at: "not a time", when: null, midnight: undefined
|
|
416
|
+
}], view);
|
|
417
|
+
expect(table.rows.length).toBe(2);
|
|
418
|
+
/* What a text column holds when it is not a time is shown as
|
|
419
|
+
* it is, as in the form; no time is nothing. */
|
|
420
|
+
expect(table.cell_text(1, "at")).toBe("not a time");
|
|
421
|
+
expect(table.cell_text(1, "when")).toBe("");
|
|
422
|
+
expect(table.cell_text(1, "midnight")).toBe("");
|
|
423
|
+
|
|
424
|
+
expect(table.cell_text(0, "when")).toBe(ES);
|
|
425
|
+
expect(table.cell_text(0, "at")).toBe(ES);
|
|
426
|
+
expect(table.cell_text(0, "midnight")).toBe("14 mar 2026, 00:00:00");
|
|
427
|
+
|
|
428
|
+
const before = table.calls.setColumns;
|
|
429
|
+
await switch_language("en");
|
|
430
|
+
expect(table.calls.setColumns).toBe(before + 1);
|
|
431
|
+
expect(table.cell_text(0, "when")).toBe(EN);
|
|
432
|
+
expect(table.cell_text(0, "at")).toBe(EN);
|
|
433
|
+
expect(table.cell_text(0, "midnight")).toBe("Mar 14, 2026, 00:00:00");
|
|
434
|
+
|
|
435
|
+
gobj_stop(view);
|
|
436
|
+
gobj_destroy(view);
|
|
437
|
+
expect(errors()).toEqual([]);
|
|
438
|
+
});
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
describe("the reading C_YUI_JSON writes beside a time field", () => {
|
|
442
|
+
test("is the app's language and 24 hours, and changes with it", async () => {
|
|
443
|
+
const app = gobj_create_pure_child("tt_app_json", "C_TEST_TT_APP", {}, yuno);
|
|
444
|
+
gobj_start(app);
|
|
445
|
+
const viewer = gobj_create("tt_json", "C_YUI_JSON", {view_mode: "tree"}, app);
|
|
446
|
+
gobj_start(viewer);
|
|
447
|
+
gobj_send_event(viewer, "EV_SET_JSON", {json: {t: EPOCH, tm: EPOCH * 1000 + 250}}, app);
|
|
448
|
+
const $c = gobj_read_attr(viewer, "$container");
|
|
449
|
+
const readings = () => Array.from($c.querySelectorAll(".JSON_TIME")).map(($s) => $s.textContent);
|
|
450
|
+
|
|
451
|
+
expect(readings()).toEqual([ES, ES]);
|
|
452
|
+
|
|
453
|
+
await switch_language("en");
|
|
454
|
+
expect(readings()).toEqual([EN, EN]);
|
|
455
|
+
|
|
456
|
+
/* Stopped, it hears the shell no more. */
|
|
457
|
+
gobj_stop(viewer);
|
|
458
|
+
await switch_language("es");
|
|
459
|
+
expect(readings()).toEqual([EN, EN]);
|
|
460
|
+
|
|
461
|
+
gobj_destroy(viewer);
|
|
462
|
+
expect(errors()).toEqual([]);
|
|
463
|
+
});
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
describe("the moments C_YUI_UPLOT writes", () => {
|
|
467
|
+
test("a tick and the legend are the app's language and 24 hours, and change with it", async () => {
|
|
468
|
+
const first = plots.length;
|
|
469
|
+
const app = gobj_create_pure_child("tt_app_plot", "C_TEST_TT_APP", {}, yuno);
|
|
470
|
+
gobj_start(app);
|
|
471
|
+
const chart = gobj_create("tt_plot", "C_YUI_UPLOT", {}, app);
|
|
472
|
+
gobj_start(chart);
|
|
473
|
+
const plot = plots[first];
|
|
474
|
+
expect(plot.tick).toBe("15:00");
|
|
475
|
+
expect(plot.legend).toBe("14 mar 2026, 15:26:53");
|
|
476
|
+
|
|
477
|
+
await switch_language("en");
|
|
478
|
+
expect(plot.calls.redraw).toBe(1);
|
|
479
|
+
expect(plot.calls.setLegend).toBe(1);
|
|
480
|
+
expect(plot.legend).toBe("Mar 14, 2026, 15:26:53");
|
|
481
|
+
|
|
482
|
+
gobj_stop(chart);
|
|
483
|
+
await switch_language("es");
|
|
484
|
+
expect(plot.calls.redraw).toBe(1);
|
|
485
|
+
|
|
486
|
+
gobj_destroy(chart);
|
|
487
|
+
expect(errors()).toEqual([]);
|
|
488
|
+
});
|
|
489
|
+
});
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/***********************************************************************
|
|
2
|
+
* yui_date_text.js
|
|
3
|
+
*
|
|
4
|
+
* A moment as a VIEW shows it as text, and the locale to show it
|
|
5
|
+
* in. A module of its own, with no i18n key, on purpose: the views
|
|
6
|
+
* that show a time (C_YUI_FORM, C_YUI_TREEDB_TOPIC_WITH_FORM,
|
|
7
|
+
* C_YUI_JSON, C_YUI_UPLOT) import it, and validate-locales follows
|
|
8
|
+
* the modules an app mounts -- importing yui_time.js for this made
|
|
9
|
+
* every app that shows a time owe the keys of the period picker
|
|
10
|
+
* (gobj-ui 7.26.52). yui_time.js re-exports both.
|
|
11
|
+
*
|
|
12
|
+
* Copyright (c) 2026, ArtGins.
|
|
13
|
+
* All Rights Reserved.
|
|
14
|
+
***********************************************************************/
|
|
15
|
+
|
|
16
|
+
/***************************************************************
|
|
17
|
+
* The locale to format with.
|
|
18
|
+
*
|
|
19
|
+
* NEVER at module top level, and never `navigator.language` raw:
|
|
20
|
+
* Firefox can report the literal STRING "undefined", and every Intl
|
|
21
|
+
* constructor throws RangeError on it — a module that built its
|
|
22
|
+
* formatter on import took the whole bundle down with it (that is why
|
|
23
|
+
* gui_agent refuses to import the gobj-ui barrel).
|
|
24
|
+
***************************************************************/
|
|
25
|
+
function safe_locale(locale)
|
|
26
|
+
{
|
|
27
|
+
if(typeof locale === "string" && locale && locale !== "undefined") {
|
|
28
|
+
return locale;
|
|
29
|
+
}
|
|
30
|
+
let nav = (typeof navigator !== "undefined") ? navigator.language : null;
|
|
31
|
+
if(typeof nav === "string" && nav && nav !== "undefined") {
|
|
32
|
+
return nav;
|
|
33
|
+
}
|
|
34
|
+
return "en";
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/* The clock is always 24 hours: `hourCycle`, never `hour12: false`,
|
|
38
|
+
* which some engines read as "h24" and show midnight as 24:00:00. */
|
|
39
|
+
const DATE_TIME_OPTIONS = {
|
|
40
|
+
day: "numeric", month: "short", year: "numeric",
|
|
41
|
+
hour: "2-digit", minute: "2-digit", second: "2-digit",
|
|
42
|
+
hourCycle: "h23"
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/***************************************************************
|
|
46
|
+
* A moment as a VIEW shows it as text: the day, the month and the
|
|
47
|
+
* year, then the clock of 24 hours with its seconds, in the APP's
|
|
48
|
+
* language -- `locale` is the language t() answers in (the views
|
|
49
|
+
* pass `i18next.language`), never the browser's:
|
|
50
|
+
*
|
|
51
|
+
* fmt_date_time(d, "es") -> "14 oct 2026, 15:05:09"
|
|
52
|
+
* fmt_date_time(d, "en") -> "Oct 14, 2026, 15:05:09"
|
|
53
|
+
*
|
|
54
|
+
* "" for anything that is not a valid Date. A locale Intl refuses
|
|
55
|
+
* gives the language-free "2026-10-14 15:05:09".
|
|
56
|
+
*
|
|
57
|
+
* The time cells of C_YUI_FORM and C_YUI_TREEDB_TOPIC_WITH_FORM and
|
|
58
|
+
* the reading C_YUI_JSON writes beside a time field all come from
|
|
59
|
+
* here, so the same moment reads the same in the three.
|
|
60
|
+
***************************************************************/
|
|
61
|
+
function fmt_date_time(date, locale)
|
|
62
|
+
{
|
|
63
|
+
if(!(date instanceof Date) || isNaN(date.getTime())) {
|
|
64
|
+
return "";
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
return new Intl.DateTimeFormat(safe_locale(locale), DATE_TIME_OPTIONS).format(date);
|
|
68
|
+
} catch(e) {
|
|
69
|
+
let pad = (n) => String(n).padStart(2, "0");
|
|
70
|
+
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ` +
|
|
71
|
+
`${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
/***************************************************************
|
|
77
|
+
* Exports
|
|
78
|
+
***************************************************************/
|
|
79
|
+
export {
|
|
80
|
+
safe_locale,
|
|
81
|
+
fmt_date_time,
|
|
82
|
+
};
|
package/src/yui_time.js
CHANGED
|
@@ -44,6 +44,11 @@
|
|
|
44
44
|
* All Rights Reserved.
|
|
45
45
|
***********************************************************************/
|
|
46
46
|
|
|
47
|
+
/* The text of a moment lives in a module of its own, which carries no
|
|
48
|
+
* i18n key: a view that formats a time must not make the validator ask
|
|
49
|
+
* an app for the keys of the period picker, which live here. */
|
|
50
|
+
import {safe_locale, fmt_date_time} from "./yui_date_text.js";
|
|
51
|
+
|
|
47
52
|
/***************************************************************
|
|
48
53
|
* Constants
|
|
49
54
|
***************************************************************/
|
|
@@ -111,27 +116,6 @@ export const YUI_TIME_I18N_KEYS = [
|
|
|
111
116
|
***************************/
|
|
112
117
|
|
|
113
118
|
|
|
114
|
-
/***************************************************************
|
|
115
|
-
* The locale to format with.
|
|
116
|
-
*
|
|
117
|
-
* NEVER at module top level, and never `navigator.language` raw:
|
|
118
|
-
* Firefox can report the literal STRING "undefined", and every Intl
|
|
119
|
-
* constructor throws RangeError on it — a module that built its
|
|
120
|
-
* formatter on import took the whole bundle down with it (that is why
|
|
121
|
-
* gui_agent refuses to import the gobj-ui barrel).
|
|
122
|
-
***************************************************************/
|
|
123
|
-
function safe_locale(locale)
|
|
124
|
-
{
|
|
125
|
-
if(typeof locale === "string" && locale && locale !== "undefined") {
|
|
126
|
-
return locale;
|
|
127
|
-
}
|
|
128
|
-
let nav = (typeof navigator !== "undefined") ? navigator.language : null;
|
|
129
|
-
if(typeof nav === "string" && nav && nav !== "undefined") {
|
|
130
|
-
return nav;
|
|
131
|
-
}
|
|
132
|
-
return "en";
|
|
133
|
-
}
|
|
134
|
-
|
|
135
119
|
/***************************************************************
|
|
136
120
|
* Translate through the APP's translator when it gave us one, and
|
|
137
121
|
* degrade to the key itself otherwise — interpolating `{{x}}` by hand,
|
|
@@ -241,7 +225,6 @@ function fmt_epoch(value, ms)
|
|
|
241
225
|
}
|
|
242
226
|
}
|
|
243
227
|
|
|
244
|
-
|
|
245
228
|
/***************************
|
|
246
229
|
* Calendar atoms
|
|
247
230
|
***************************/
|
|
@@ -663,6 +646,7 @@ export {
|
|
|
663
646
|
epoch_to_local_input,
|
|
664
647
|
local_input_to_epoch,
|
|
665
648
|
fmt_epoch,
|
|
649
|
+
fmt_date_time,
|
|
666
650
|
|
|
667
651
|
day_number,
|
|
668
652
|
start_of_iso_week,
|