@yuneta/gobj-ui 7.26.51 → 7.26.52
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 +1267 -1025
- package/dist/gobj-ui.es.js +1258 -1017
- 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_time.js +37 -0
- 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
|
+
});
|
package/src/yui_time.js
CHANGED
|
@@ -241,6 +241,42 @@ function fmt_epoch(value, ms)
|
|
|
241
241
|
}
|
|
242
242
|
}
|
|
243
243
|
|
|
244
|
+
/* The clock is always 24 hours: `hourCycle`, never `hour12: false`,
|
|
245
|
+
* which some engines read as "h24" and show midnight as 24:00:00. */
|
|
246
|
+
const DATE_TIME_OPTIONS = {
|
|
247
|
+
day: "numeric", month: "short", year: "numeric",
|
|
248
|
+
hour: "2-digit", minute: "2-digit", second: "2-digit",
|
|
249
|
+
hourCycle: "h23"
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
/***************************************************************
|
|
253
|
+
* A moment as a VIEW shows it as text: the day, the month and the
|
|
254
|
+
* year, then the clock of 24 hours with its seconds, in the APP's
|
|
255
|
+
* language -- `locale` is the language t() answers in (the views
|
|
256
|
+
* pass `i18next.language`), never the browser's:
|
|
257
|
+
*
|
|
258
|
+
* fmt_date_time(d, "es") -> "14 oct 2026, 15:05:09"
|
|
259
|
+
* fmt_date_time(d, "en") -> "Oct 14, 2026, 15:05:09"
|
|
260
|
+
*
|
|
261
|
+
* "" for anything that is not a valid Date. A locale Intl refuses
|
|
262
|
+
* gives the language-free "2026-10-14 15:05:09".
|
|
263
|
+
*
|
|
264
|
+
* The time cells of C_YUI_FORM and C_YUI_TREEDB_TOPIC_WITH_FORM and
|
|
265
|
+
* the reading C_YUI_JSON writes beside a time field all come from
|
|
266
|
+
* here, so the same moment reads the same in the three.
|
|
267
|
+
***************************************************************/
|
|
268
|
+
function fmt_date_time(date, locale)
|
|
269
|
+
{
|
|
270
|
+
if(!(date instanceof Date) || isNaN(date.getTime())) {
|
|
271
|
+
return "";
|
|
272
|
+
}
|
|
273
|
+
try {
|
|
274
|
+
return new Intl.DateTimeFormat(safe_locale(locale), DATE_TIME_OPTIONS).format(date);
|
|
275
|
+
} catch(e) {
|
|
276
|
+
return epoch_to_local_input(date.getTime(), true).replace("T", " ");
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
244
280
|
|
|
245
281
|
/***************************
|
|
246
282
|
* Calendar atoms
|
|
@@ -663,6 +699,7 @@ export {
|
|
|
663
699
|
epoch_to_local_input,
|
|
664
700
|
local_input_to_epoch,
|
|
665
701
|
fmt_epoch,
|
|
702
|
+
fmt_date_time,
|
|
666
703
|
|
|
667
704
|
day_number,
|
|
668
705
|
start_of_iso_week,
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/***********************************************************************
|
|
2
|
+
* yui_uplot_time.js
|
|
3
|
+
*
|
|
4
|
+
* The texts uPlot writes for a moment -- the ticks of a time axis
|
|
5
|
+
* and the reading of the legend -- in the APP's language, with a
|
|
6
|
+
* clock of 24 hours, as every other time a gobj-ui view shows
|
|
7
|
+
* (fmt_date_time of yui_time.js).
|
|
8
|
+
*
|
|
9
|
+
* uPlot writes them from templates of its own, in English and
|
|
10
|
+
* 12 hours: "3pm", "3:05pm", "10/14" (month first), "Oct", and
|
|
11
|
+
* "2026-10-14 3:05pm" in the legend. Its `fmtDate` option takes
|
|
12
|
+
* the template and returns the function that writes a Date with
|
|
13
|
+
* it; this one is given to it:
|
|
14
|
+
*
|
|
15
|
+
* legend "{YYYY}-{MM}-{DD} {h}:{mm}{aa}" -> fmt_date_time()
|
|
16
|
+
* "14 oct 2026, 15:05:09"
|
|
17
|
+
* "{h}{aa}" "3pm" -> "15:00"
|
|
18
|
+
* "{h}:{mm}{aa}" "3:05pm" -> "15:05"
|
|
19
|
+
* "{M}/{D}" "10/14" -> es "14/10", en "10/14"
|
|
20
|
+
* "{M}/{D}/{YY}" "10/14/26"-> es "14/10/26", en "10/14/26"
|
|
21
|
+
* "{MMM}" "Oct" -> es "oct", en "Oct"
|
|
22
|
+
*
|
|
23
|
+
* The language is asked for on every call, so a redraw after a
|
|
24
|
+
* language change writes the new one (C_YUI_UPLOT redraws its
|
|
25
|
+
* axes on EV_LANGUAGE_CHANGED).
|
|
26
|
+
*
|
|
27
|
+
* Copyright (c) 2026, ArtGins.
|
|
28
|
+
* All Rights Reserved.
|
|
29
|
+
***********************************************************************/
|
|
30
|
+
import {fmt_date_time, safe_locale} from "./yui_time.js";
|
|
31
|
+
|
|
32
|
+
/* The legend's stamp, as uPlot 1.6 writes it (_timeSeriesStamp). */
|
|
33
|
+
const LEGEND_STAMP = "{YYYY}-{MM}-{DD} {h}:{mm}{aa}";
|
|
34
|
+
|
|
35
|
+
const pad2 = (n) => String(n).padStart(2, "0");
|
|
36
|
+
|
|
37
|
+
/* Intl formatters by locale and options: a redraw writes a dozen
|
|
38
|
+
* ticks, and a formatter is not cheap to build. */
|
|
39
|
+
const __formatters__ = new Map();
|
|
40
|
+
|
|
41
|
+
function intl(locale, opts)
|
|
42
|
+
{
|
|
43
|
+
let key = locale + "|" + JSON.stringify(opts);
|
|
44
|
+
let f = __formatters__.get(key);
|
|
45
|
+
if(!f) {
|
|
46
|
+
try {
|
|
47
|
+
f = new Intl.DateTimeFormat(locale, opts);
|
|
48
|
+
} catch(e) {
|
|
49
|
+
f = new Intl.DateTimeFormat("en", opts);
|
|
50
|
+
}
|
|
51
|
+
__formatters__.set(key, f);
|
|
52
|
+
}
|
|
53
|
+
return f;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/* A template fragment that has no word for a language is rewritten
|
|
57
|
+
* whole: the order of day and month is the language's. */
|
|
58
|
+
const WHOLE = [
|
|
59
|
+
["{M}/{D}/{YY}", (d, loc) => intl(loc, {day: "numeric", month: "numeric", year: "2-digit"}).format(d)],
|
|
60
|
+
["{M}/{D}", (d, loc) => intl(loc, {day: "numeric", month: "numeric"}).format(d)],
|
|
61
|
+
["{h}{aa}", (d) => `${pad2(d.getHours())}:00`],
|
|
62
|
+
];
|
|
63
|
+
|
|
64
|
+
const TOKENS = {
|
|
65
|
+
YYYY: (d) => String(d.getFullYear()),
|
|
66
|
+
YY: (d) => String(d.getFullYear()).slice(2),
|
|
67
|
+
MMMM: (d, loc) => intl(loc, {month: "long"}).format(d),
|
|
68
|
+
MMM: (d, loc) => intl(loc, {month: "short"}).format(d),
|
|
69
|
+
MM: (d) => pad2(d.getMonth() + 1),
|
|
70
|
+
M: (d) => String(d.getMonth() + 1),
|
|
71
|
+
DD: (d) => pad2(d.getDate()),
|
|
72
|
+
D: (d) => String(d.getDate()),
|
|
73
|
+
WWWW: (d, loc) => intl(loc, {weekday: "long"}).format(d),
|
|
74
|
+
WWW: (d, loc) => intl(loc, {weekday: "short"}).format(d),
|
|
75
|
+
HH: (d) => pad2(d.getHours()),
|
|
76
|
+
H: (d) => String(d.getHours()),
|
|
77
|
+
hh: (d) => pad2(d.getHours()),
|
|
78
|
+
h: (d) => pad2(d.getHours()),
|
|
79
|
+
AA: () => "",
|
|
80
|
+
aa: () => "",
|
|
81
|
+
a: () => "",
|
|
82
|
+
mm: (d) => pad2(d.getMinutes()),
|
|
83
|
+
m: (d) => String(d.getMinutes()),
|
|
84
|
+
ss: (d) => pad2(d.getSeconds()),
|
|
85
|
+
s: (d) => String(d.getSeconds()),
|
|
86
|
+
fff: (d) => String(d.getMilliseconds()).padStart(3, "0"),
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/***************************************************************
|
|
90
|
+
* A template -> its parts: literal text, or a function of
|
|
91
|
+
* (date, locale).
|
|
92
|
+
***************************************************************/
|
|
93
|
+
function template_parts(tpl)
|
|
94
|
+
{
|
|
95
|
+
let parts = [];
|
|
96
|
+
let rest = String(tpl);
|
|
97
|
+
while(rest.length > 0) {
|
|
98
|
+
let whole = WHOLE.find(([frag]) => rest.startsWith(frag));
|
|
99
|
+
if(whole) {
|
|
100
|
+
parts.push(whole[1]);
|
|
101
|
+
rest = rest.slice(whole[0].length);
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
let m = /^\{([a-z]+)\}/i.exec(rest);
|
|
105
|
+
if(m && TOKENS[m[1]]) {
|
|
106
|
+
parts.push(TOKENS[m[1]]);
|
|
107
|
+
rest = rest.slice(m[0].length);
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
let next = rest.indexOf("{", 1);
|
|
111
|
+
let text = (next < 0)? rest : rest.slice(0, next);
|
|
112
|
+
parts.push(text);
|
|
113
|
+
rest = rest.slice(text.length);
|
|
114
|
+
}
|
|
115
|
+
return parts;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/***************************************************************
|
|
119
|
+
* uPlot's `fmtDate` option. `get_locale` answers the app's
|
|
120
|
+
* language at the moment of the call (the views pass
|
|
121
|
+
* `() => i18next.language`).
|
|
122
|
+
***************************************************************/
|
|
123
|
+
function yui_uplot_fmt_date(get_locale)
|
|
124
|
+
{
|
|
125
|
+
let locale = () => safe_locale(typeof get_locale === "function"? get_locale() : null);
|
|
126
|
+
return function(tpl) {
|
|
127
|
+
if(tpl === LEGEND_STAMP) {
|
|
128
|
+
return (date) => fmt_date_time(date, locale());
|
|
129
|
+
}
|
|
130
|
+
let parts = template_parts(tpl);
|
|
131
|
+
return function(date) {
|
|
132
|
+
let loc = locale();
|
|
133
|
+
let out = "";
|
|
134
|
+
for(let part of parts) {
|
|
135
|
+
out += (typeof part === "string")? part : part(date, loc);
|
|
136
|
+
}
|
|
137
|
+
return out;
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export {yui_uplot_fmt_date};
|