@yuneta/gobj-ui 7.26.50 → 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.
@@ -0,0 +1,138 @@
1
+ /***********************************************************************
2
+ * fmt_date_time.test.js
3
+ *
4
+ * fmt_date_time() of yui_time.js: a moment as a view shows it as
5
+ * text -- day, short month, year, and a clock of 24 hours with its
6
+ * seconds -- in the language it is given (the app's), and the two
7
+ * readers built on it: time_value_to_text() (the time cells of the
8
+ * forms and the treedb tables) and format_epoch() (the reading
9
+ * C_YUI_JSON writes beside a time field).
10
+ *
11
+ * Copyright (c) 2026, ArtGins.
12
+ * All Rights Reserved.
13
+ ***********************************************************************/
14
+ import {describe, test, expect} from "vitest";
15
+ import {fmt_date_time, safe_locale} from "./yui_time.js";
16
+ import {time_value_to_text} from "./form_time_value.js";
17
+ import {format_epoch} from "./json_view_helpers.js";
18
+ import {yui_uplot_fmt_date} from "./yui_uplot_time.js";
19
+
20
+ const D = new Date(2026, 9, 14, 15, 5, 9);
21
+
22
+ describe("fmt_date_time", () => {
23
+ test("es and en, 24 hours", () => {
24
+ expect(fmt_date_time(D, "es")).toBe("14 oct 2026, 15:05:09");
25
+ expect(fmt_date_time(D, "en")).toBe("Oct 14, 2026, 15:05:09");
26
+ /* A region does not bring back the 12 hours of en-US. */
27
+ expect(fmt_date_time(D, "en-US")).toBe("Oct 14, 2026, 15:05:09");
28
+ expect(fmt_date_time(D, "es-ES")).toBe("14 oct 2026, 15:05:09");
29
+ });
30
+
31
+ test("midnight is 00, never 24 nor 12 a. m.; noon is 12, with no p. m.", () => {
32
+ const midnight = new Date(2026, 9, 14, 0, 0, 0);
33
+ const noon = new Date(2026, 9, 14, 12, 0, 0);
34
+ const last = new Date(2026, 9, 14, 23, 59, 59);
35
+ expect(fmt_date_time(midnight, "es")).toBe("14 oct 2026, 00:00:00");
36
+ expect(fmt_date_time(midnight, "en")).toBe("Oct 14, 2026, 00:00:00");
37
+ expect(fmt_date_time(noon, "es")).toBe("14 oct 2026, 12:00:00");
38
+ expect(fmt_date_time(noon, "en")).toBe("Oct 14, 2026, 12:00:00");
39
+ expect(fmt_date_time(last, "en")).toBe("Oct 14, 2026, 23:59:59");
40
+ });
41
+
42
+ test("the hour that repeats in autumn reads 02:30 in both passes", () => {
43
+ const tz = process.env.TZ;
44
+ process.env.TZ = "Europe/Madrid";
45
+ try {
46
+ /* 00:30Z and 01:30Z on 2026-10-25 are both 02:30 in Madrid. */
47
+ expect(fmt_date_time(new Date(1792888200 * 1000), "es")).toBe("25 oct 2026, 02:30:00");
48
+ expect(fmt_date_time(new Date(1792891800 * 1000), "es")).toBe("25 oct 2026, 02:30:00");
49
+ expect(fmt_date_time(new Date(1792895400 * 1000), "es")).toBe("25 oct 2026, 03:30:00");
50
+ } finally {
51
+ if(tz === undefined) {
52
+ delete process.env.TZ;
53
+ } else {
54
+ process.env.TZ = tz;
55
+ }
56
+ }
57
+ });
58
+
59
+ test("no date is no text", () => {
60
+ expect(fmt_date_time(null, "es")).toBe("");
61
+ expect(fmt_date_time(undefined, "es")).toBe("");
62
+ expect(fmt_date_time(new Date("not a date"), "es")).toBe("");
63
+ expect(fmt_date_time(1792888200, "es")).toBe("");
64
+ expect(fmt_date_time("2026-10-14", "es")).toBe("");
65
+ });
66
+
67
+ test("no language, or Firefox's literal \"undefined\", is the fallback of safe_locale", () => {
68
+ const fallback = fmt_date_time(D, safe_locale(undefined));
69
+ expect(fallback).toMatch(/15:05:09/);
70
+ expect(fmt_date_time(D, undefined)).toBe(fallback);
71
+ expect(fmt_date_time(D, "undefined")).toBe(fallback);
72
+ expect(fmt_date_time(D, "")).toBe(fallback);
73
+ });
74
+
75
+ test("a locale Intl refuses gives the language-free clock, not a broken screen", () => {
76
+ expect(fmt_date_time(D, "xx_YY")).toBe("2026-10-14 15:05:09");
77
+ });
78
+ });
79
+
80
+ describe("what the views read through it", () => {
81
+ const epoch = Math.floor(D.getTime() / 1000);
82
+
83
+ test("time_value_to_text: an epoch, an ISO text, no time, not a time", () => {
84
+ expect(time_value_to_text(epoch, "integer", "es")).toBe("14 oct 2026, 15:05:09");
85
+ expect(time_value_to_text(D.toISOString(), "string", "en")).toBe("Oct 14, 2026, 15:05:09");
86
+ expect(time_value_to_text(null, "integer", "es")).toBe("");
87
+ expect(time_value_to_text(undefined, "integer", "es")).toBe("");
88
+ expect(time_value_to_text("", "string", "es")).toBe("");
89
+ expect(time_value_to_text("garbage", "string", "es")).toBe("garbage");
90
+ });
91
+
92
+ test("format_epoch: seconds and milliseconds read the same", () => {
93
+ expect(format_epoch(epoch, "es")).toBe("14 oct 2026, 15:05:09");
94
+ expect(format_epoch(epoch * 1000 + 999, "en")).toBe("Oct 14, 2026, 15:05:09");
95
+ expect(format_epoch(0, "es")).toBe(null);
96
+ expect(format_epoch(null, "es")).toBe(null);
97
+ });
98
+ });
99
+
100
+ describe("the moments uPlot writes (yui_uplot_time.js)", () => {
101
+ let lng = "es";
102
+ const fmt = (tpl) => yui_uplot_fmt_date(() => lng)(tpl);
103
+ const AFTERNOON = new Date(2026, 9, 14, 15, 5, 9, 374);
104
+ const MIDNIGHT = new Date(2026, 9, 14, 0, 0, 0);
105
+
106
+ test("the legend reads as every other view: fmt_date_time()", () => {
107
+ const legend = fmt("{YYYY}-{MM}-{DD} {h}:{mm}{aa}");
108
+ lng = "es";
109
+ expect(legend(AFTERNOON)).toBe("14 oct 2026, 15:05:09");
110
+ lng = "en";
111
+ expect(legend(AFTERNOON)).toBe("Oct 14, 2026, 15:05:09");
112
+ expect(legend(MIDNIGHT)).toBe("Oct 14, 2026, 00:00:00");
113
+ });
114
+
115
+ test("the ticks of uPlot's own templates: 24 hours, the language's names and order", () => {
116
+ lng = "es";
117
+ expect(fmt("{h}{aa}")(AFTERNOON)).toBe("15:00");
118
+ expect(fmt("{h}{aa}")(MIDNIGHT)).toBe("00:00");
119
+ expect(fmt("{h}:{mm}{aa}")(AFTERNOON)).toBe("15:05");
120
+ expect(fmt(":{ss}")(AFTERNOON)).toBe(":09");
121
+ expect(fmt(":{ss}.{fff}")(AFTERNOON)).toBe(":09.374");
122
+ expect(fmt("{M}/{D}")(AFTERNOON)).toBe("14/10");
123
+ expect(fmt("\n{M}/{D}/{YY} {h}:{mm}{aa}")(AFTERNOON)).toBe("\n14/10/26 15:05");
124
+ expect(fmt("{MMM}")(AFTERNOON)).toBe("oct");
125
+ expect(fmt("\n{YYYY}")(AFTERNOON)).toBe("\n2026");
126
+ lng = "en";
127
+ expect(fmt("{M}/{D}")(AFTERNOON)).toBe("10/14");
128
+ expect(fmt("{MMM}")(AFTERNOON)).toBe("Oct");
129
+ expect(fmt("{h}:{mm}{aa}")(AFTERNOON)).toBe("15:05");
130
+ });
131
+
132
+ test("an unknown token is left as text; Firefox's \"undefined\" is no language", () => {
133
+ lng = "es";
134
+ expect(fmt("{nope} {HH}")(AFTERNOON)).toBe("{nope} 15");
135
+ lng = "undefined";
136
+ expect(fmt("{h}:{mm}{aa}")(AFTERNOON)).toBe("15:05");
137
+ });
138
+ });
@@ -869,8 +869,9 @@ describe("a time column of a table", () => {
869
869
  expect(errors()).toEqual([]);
870
870
  });
871
871
 
872
- /* Up to 7.26.46 the cell showed the raw epoch (1700000000). */
873
- test("shows its time as the local clock the editor opens on, not the epoch", async () => {
872
+ /* Up to 7.26.46 the cell showed the raw epoch (1700000000); up
873
+ * to 7.26.51 "2026-03-14 09:26:53". The app's language is "en". */
874
+ test("shows its time as the local clock, in the app's language, not the epoch", async () => {
874
875
  const first = tables.length;
875
876
  const app = gobj_create_pure_child(`trapp${++seq}`, "C_TEST_TR_APP", {}, yuno);
876
877
  gobj_start(app);
@@ -898,8 +899,8 @@ describe("a time column of a table", () => {
898
899
  };
899
900
  const wall = "2026-03-14T09:26:53";
900
901
  const epoch = Math.floor(new Date(wall).getTime() / 1000);
901
- expect(text("when", epoch)).toBe("2026-03-14 09:26:53");
902
- expect(text("at", new Date(epoch * 1000).toISOString())).toBe("2026-03-14 09:26:53");
902
+ expect(text("when", epoch)).toBe("Mar 14, 2026, 09:26:53");
903
+ expect(text("at", new Date(epoch * 1000).toISOString())).toBe("Mar 14, 2026, 09:26:53");
903
904
  expect(text("when", null)).toBe("");
904
905
  expect(text("at", "not a time")).toBe("not a time");
905
906
  gobj_stop(form);
@@ -187,3 +187,130 @@ describe("the time field of a form", () => {
187
187
  expect(errors()).toEqual([]);
188
188
  });
189
189
  });
190
+
191
+ /* Chromium and Firefox write a `datetime-local` value without its
192
+ * seconds when they are :00: "…T02:30:00" reads back "…T02:30". */
193
+ function with_browser_box(fn)
194
+ {
195
+ const create = document.createElement;
196
+ document.createElement = function(tag) {
197
+ const el = create.call(document, tag);
198
+ if(String(tag).toLowerCase() === "input") {
199
+ let raw = "";
200
+ Object.defineProperty(el, "value", {
201
+ configurable: true,
202
+ get: () => raw,
203
+ set: (v) => {
204
+ raw = (v === null || v === undefined)? "" :
205
+ String(v).replace(/^(\d{4}-\d\d-\d\dT\d\d:\d\d):00$/, "$1");
206
+ },
207
+ });
208
+ }
209
+ return el;
210
+ };
211
+ try {
212
+ fn();
213
+ } finally {
214
+ document.createElement = create;
215
+ }
216
+ }
217
+
218
+ function in_madrid(fn)
219
+ {
220
+ const tz = process.env.TZ;
221
+ process.env.TZ = "Europe/Madrid";
222
+ try {
223
+ fn();
224
+ } finally {
225
+ if(tz === undefined) {
226
+ delete process.env.TZ;
227
+ } else {
228
+ process.env.TZ = tz;
229
+ }
230
+ }
231
+ }
232
+
233
+ /* 01:30Z and 00:30Z on 2026-10-25 are both 02:30 in Madrid: the
234
+ * second and the first pass of the hour the clocks go back. A wall
235
+ * time read back is the FIRST one. */
236
+ const SECOND_PASS = Date.UTC(2026, 9, 25, 1, 30, 0) / 1000;
237
+ const FIRST_PASS = SECOND_PASS - 3600;
238
+ const SECOND_PASS_S = Date.UTC(2026, 9, 25, 1, 30, 15) / 1000;
239
+
240
+ describe("a time in the repeated hour of autumn, saved with nothing touched", () => {
241
+ test("goes back as the record had it: integer, string, template spec, with and without seconds", () => {
242
+ in_madrid(() => {
243
+ with_browser_box(() => {
244
+ const app = gobj_create_pure_child("tapp4", "C_TEST_TIME_APP", {}, yuno);
245
+ gobj_start(app);
246
+ const form = gobj_create_pure_child("tform4", "C_YUI_FORM", {
247
+ template: {
248
+ when: {type: "integer", flag: ["writable", "persistent", "time"]},
249
+ when_s: {type: "string", flag: ["writable", "persistent", "time"]},
250
+ bare: "time.writable",
251
+ secs: "time.writable",
252
+ },
253
+ record: {
254
+ when: SECOND_PASS,
255
+ when_s: new Date(SECOND_PASS * 1000).toISOString(),
256
+ bare: SECOND_PASS,
257
+ secs: SECOND_PASS_S,
258
+ },
259
+ editable: true,
260
+ }, app);
261
+ gobj_start(form);
262
+
263
+ expect(box(form, "when").value).toBe("2026-10-25T02:30");
264
+ expect(box(form, "secs").value).toBe("2026-10-25T02:30:15");
265
+ expect(gobj_read_attr(form, "edited")).toBe(false);
266
+
267
+ gobj_send_event(form, "EV_SAVE_RECORD", {}, form);
268
+ expect(saved.length).toBe(1);
269
+ expect(saved[0].when).toBe(SECOND_PASS);
270
+ expect(saved[0].when_s).toBe("2026-10-25T01:30:00.000Z");
271
+ expect(saved[0].bare).toBe(SECOND_PASS);
272
+ expect(saved[0].secs).toBe(SECOND_PASS_S);
273
+ expect(errors()).toEqual([]);
274
+ });
275
+ });
276
+ });
277
+
278
+ test("a box the reader changed is read, and a record loaded after another one is ITS value", () => {
279
+ in_madrid(() => {
280
+ with_browser_box(() => {
281
+ const app = gobj_create_pure_child("tapp5", "C_TEST_TIME_APP", {}, yuno);
282
+ gobj_start(app);
283
+ const form = gobj_create_pure_child("tform5", "C_YUI_FORM", {
284
+ template: {when: "time.writable", other: "time.writable"},
285
+ record: {when: SECOND_PASS, other: SECOND_PASS},
286
+ editable: true,
287
+ }, app);
288
+ gobj_start(form);
289
+
290
+ /* The same wall time, the other instant: the box shows
291
+ * the same text, and the save must be the NEW record's. */
292
+ gobj_send_event(form, "EV_LOAD_RECORD", {when: FIRST_PASS, other: null}, form);
293
+ expect(box(form, "when").value).toBe("2026-10-25T02:30");
294
+ expect(box(form, "other").value).toBe("");
295
+ gobj_send_event(form, "EV_SAVE_RECORD", {}, form);
296
+ expect(saved[0].when).toBe(FIRST_PASS);
297
+ expect(saved[0].other).toBe(null);
298
+
299
+ gobj_send_event(form, "EV_LOAD_RECORD", {when: SECOND_PASS, other: SECOND_PASS}, form);
300
+ box(form, "when").value = "2026-10-25T04:00:00";
301
+ box(form, "other").value = "";
302
+ gobj_send_event(form, "EV_SAVE_RECORD", {}, form);
303
+ expect(saved[1].when).toBe(Date.UTC(2026, 9, 25, 3, 0, 0) / 1000);
304
+ expect(saved[1].other).toBe(null);
305
+
306
+ /* Cleared by the form's own Clear: nothing kept. */
307
+ gobj_send_event(form, "EV_LOAD_RECORD", {when: SECOND_PASS, other: SECOND_PASS}, form);
308
+ gobj_send_event(form, "EV_CLEAR_RECORD", {}, form);
309
+ gobj_send_event(form, "EV_SAVE_RECORD", {}, form);
310
+ expect(saved[2].when).toBe(null);
311
+ expect(saved[2].other).toBe(null);
312
+ expect(errors()).toEqual([]);
313
+ });
314
+ });
315
+ });
316
+ });
@@ -15,8 +15,11 @@
15
15
  * `new Date("YYYY-MM-DDT02:30:00")` answers the EARLIER one
16
16
  * (ECMAScript's "compatible" disambiguation). An epoch in the
17
17
  * SECOND pass of that hour is shown as 02:30 and comes back
18
- * one hour earlier: saving the record -- any field of it --
19
- * moves a writable time column that holds it by -3600 s.
18
+ * one hour earlier. So neither the form's field nor the cell
19
+ * editor reads a box that still shows what it was filled
20
+ * with: they give back the value it was filled from (up to
21
+ * gobj-ui 7.26.48 the cell, and 7.26.50 the field, wrote
22
+ * -3600 s on a save of any field of the record).
20
23
  * - the night the clocks go forward (spring), one hour does not
21
24
  * exist. No epoch is shown in it, so nothing read comes back
22
25
  * wrong; a wall time TYPED in it is moved forward by the
@@ -26,12 +29,13 @@
26
29
  * carry. What is safe: a read-only time column never goes back
27
30
  * (treedb_write_plan.js, col_goes_back_to_treedb), and a time
28
31
  * pkey2 goes back as the record had it, never through this
29
- * widget (instance_keys_of). A writable time column edited or
30
- * merely saved inside the repeated hour is the exposed case.
32
+ * widget (instance_keys_of). A wall time TYPED inside the
33
+ * repeated hour is the exposed case: it is the first pass.
31
34
  *
32
35
  * Copyright (c) 2026, ArtGins.
33
36
  * All Rights Reserved.
34
37
  ****************************************************************************/
38
+ import {fmt_date_time} from "./yui_time.js";
35
39
 
36
40
  /************************************************************
37
41
  * A Date -> the value of a `datetime-local` input, in local time,
@@ -107,18 +111,20 @@ function epoch_to_time_value(epoch, real_type)
107
111
 
108
112
  /************************************************************
109
113
  * The value of a time column as a table cell shows it: the
110
- * local clock the editor opens on, "YYYY-MM-DD HH:mm:ss", as
111
- * fmt_epoch() of yui_time.js writes a timestamp in a table. ""
112
- * for no time; a value that is not one is shown as it is, so the
113
- * reader sees what the column holds.
114
+ * local clock, 24 hours, in the APP's language `locale`
115
+ * (fmt_date_time() of yui_time.js: es "14 oct 2026, 15:05:09").
116
+ * Up to gobj-ui 7.26.51 the form wrote "2026-10-14 15:05:09" and
117
+ * the treedb table "14 oct 2026, 03:05:09 p. m.", in the
118
+ * browser's language. "" for no time; a value that is not one is
119
+ * shown as it is, so the reader sees what the column holds.
114
120
  ************************************************************/
115
- function time_value_to_text(value, real_type)
121
+ function time_value_to_text(value, real_type, locale)
116
122
  {
117
123
  let date = time_value_to_date(value, real_type);
118
124
  if(!date) {
119
125
  return (value === null || value === undefined)? "" : String(value);
120
126
  }
121
- return date_to_datetime_local(date).replace("T", " ");
127
+ return fmt_date_time(date, locale);
122
128
  }
123
129
 
124
130
  export {
@@ -40,7 +40,7 @@ describe("form_time_value", () => {
40
40
  it("a time cell shows the local clock, and what is not a time as it is", () => {
41
41
  const wall = "2026-03-14T09:26:53";
42
42
  const epoch = Math.floor(new Date(wall).getTime() / 1000);
43
- expect(time_value_to_text(epoch, "integer")).toBe("2026-03-14 09:26:53");
43
+ expect(time_value_to_text(epoch, "integer", "es")).toBe("14 mar 2026, 09:26:53");
44
44
  expect(time_value_to_text(null, "integer")).toBe("");
45
45
  expect(time_value_to_text("garbage", "string")).toBe("garbage");
46
46
  });
@@ -16,6 +16,7 @@
16
16
  * All Rights Reserved.
17
17
  ***********************************************************************/
18
18
  import {json2flat, flat_diff} from "@yuneta/gobj-js";
19
+ import {fmt_date_time} from "./yui_time.js";
19
20
 
20
21
  /*
21
22
  * Path delimiter, identical to the kernel's `delimiter` in kwid.c.
@@ -214,11 +215,15 @@ export function is_time_field(field)
214
215
  export {TIME_FIELDS};
215
216
 
216
217
  /************************************************************
217
- * Format an epoch value as a local wall-clock string.
218
+ * Format an epoch value as a local wall-clock string, 24
219
+ * hours, in the APP's language `locale` (fmt_date_time() of
220
+ * yui_time.js, as the time cells of the forms and tables;
221
+ * up to gobj-ui 7.26.51 it was toLocaleString(): the
222
+ * browser's language, and 12 hours in English).
218
223
  * Accepts seconds or milliseconds; returns null when the
219
224
  * value is not a usable positive timestamp (0 == "unset").
220
225
  ************************************************************/
221
- export function format_epoch(value)
226
+ export function format_epoch(value, locale)
222
227
  {
223
228
  if(typeof value !== "number" || !isFinite(value) || value <= 0) {
224
229
  return null;
@@ -228,7 +233,7 @@ export function format_epoch(value)
228
233
  if(isNaN(d.getTime())) {
229
234
  return null;
230
235
  }
231
- return d.toLocaleString();
236
+ return fmt_date_time(d, locale);
232
237
  }
233
238
 
234
239
 
@@ -14,8 +14,14 @@
14
14
  * the Navigation API: a list and an index, each entry with a key that
15
15
  * a replaceState keeps and a push makes new, pushState truncating
16
16
  * what is ahead, and a traversal (back(), go()) run on a later task
17
- * from whatever entry is current THEN. Real browsers: qa-history
18
- * cases (h) to (l).
17
+ * from whatever entry is current THEN, announced by a
18
+ * `currententrychange` (with the entry it came FROM) before its
19
+ * popstate, as Chromium and Firefox do. Each entry belongs to a
20
+ * document: a reload makes every entry the new document's (both
21
+ * browsers say `sameDocument` of all of them after F5), and a
22
+ * cross-document navigation with replace keeps the entry's key
23
+ * for a NEW document, the entries under it staying the old one's.
24
+ * Real browsers: qa-history cases (h) to (n).
19
25
  *
20
26
  * Copyright (c) 2026, ArtGins.
21
27
  * All Rights Reserved.
@@ -40,7 +46,7 @@ let yuno = null;
40
46
  let key_seq = 0;
41
47
  const shells = [];
42
48
 
43
- const session = {entries: [], index: 0, gos: []};
49
+ const session = {entries: [], index: 0, gos: [], doc: 1, nav_listeners: []};
44
50
 
45
51
  function url_hash(url)
46
52
  {
@@ -51,14 +57,16 @@ function url_hash(url)
51
57
  function new_entry(state, hash)
52
58
  {
53
59
  key_seq++;
54
- return {state: state, hash: hash, key: `k${key_seq}`};
60
+ return {state: state, hash: hash, key: `k${key_seq}`, doc: session.doc};
55
61
  }
56
62
 
57
63
  function install_history_model(with_navigation)
58
64
  {
65
+ session.doc++;
59
66
  session.entries = [new_entry(null, window.location.hash)];
60
67
  session.index = 0;
61
68
  session.gos = [];
69
+ session.nav_listeners = [];
62
70
  window.history = {
63
71
  get state() {
64
72
  return session.entries[session.index].state;
@@ -88,6 +96,7 @@ function install_history_model(with_navigation)
88
96
  this.go(-1);
89
97
  },
90
98
  forward() {
99
+ this.go(1);
91
100
  },
92
101
  go(delta) {
93
102
  session.gos.push(delta);
@@ -103,12 +112,21 @@ function install_history_model(with_navigation)
103
112
  window.navigation = {
104
113
  get currentEntry() {
105
114
  const e = session.entries[session.index];
106
- return {key: e.key, index: session.index};
115
+ return {key: e.key, index: session.index, sameDocument: true};
107
116
  },
108
117
  entries() {
118
+ const doc = session.entries[session.index].doc;
109
119
  return session.entries.map((e, i) => ({
110
- key: e.key, index: i, url: `http://localhost/${e.hash}`
120
+ key: e.key, index: i, url: `http://localhost/${e.hash}`,
121
+ sameDocument: e.doc === doc
111
122
  }));
123
+ },
124
+ addEventListener(type, fn) {
125
+ session.nav_listeners.push({type, fn});
126
+ },
127
+ removeEventListener(type, fn) {
128
+ session.nav_listeners = session.nav_listeners.filter(
129
+ (l) => !(l.type === type && l.fn === fn));
112
130
  }
113
131
  };
114
132
  } else {
@@ -119,16 +137,24 @@ function install_history_model(with_navigation)
119
137
  function traverse_to(index)
120
138
  {
121
139
  const left = window.location.hash;
140
+ const from = {key: session.entries[session.index].key, index: session.index};
122
141
  session.index = index;
142
+ for(const l of session.nav_listeners.slice()) {
143
+ if(l.type === "currententrychange") {
144
+ l.fn({type: "currententrychange", navigationType: "traverse", from: from});
145
+ }
146
+ }
123
147
  window.location.hash = session.entries[index].hash;
124
- const ev = new Event("popstate");
125
- ev.state = window.history.state;
126
- window.dispatchEvent(ev);
148
+ /* The hashchange is queued BEFORE the popstate runs: a traversal
149
+ * the popstate handler starts lands after it (both browsers). */
127
150
  if(session.entries[index].hash !== left) {
128
151
  setTimeout(() => {
129
152
  window.dispatchEvent(new Event("hashchange"));
130
153
  }, 0);
131
154
  }
155
+ const ev = new Event("popstate");
156
+ ev.state = window.history.state;
157
+ window.dispatchEvent(ev);
132
158
  }
133
159
 
134
160
  function user_back()
@@ -136,6 +162,24 @@ function user_back()
136
162
  traverse_to(session.index - 1);
137
163
  }
138
164
 
165
+ function user_forward()
166
+ {
167
+ traverse_to(session.index + 1);
168
+ }
169
+
170
+ /* A navigation to another document with replace (location.replace):
171
+ * the entry keeps its key and is a new document's, and the entries
172
+ * under it are no longer the current document's. */
173
+ function cross_document_replace(hash)
174
+ {
175
+ session.doc++;
176
+ const e = session.entries[session.index];
177
+ e.doc = session.doc;
178
+ e.state = null;
179
+ e.hash = hash;
180
+ window.location.hash = hash;
181
+ }
182
+
139
183
  function next_task()
140
184
  {
141
185
  return new Promise(resolve => setTimeout(resolve, 0));
@@ -441,6 +485,145 @@ describe("a shell stopped with overlays up leaves its markers to the next one",
441
485
  expect(shell_warnings()).toEqual([]);
442
486
  });
443
487
 
488
+ /* location.replace() to another page and back to the app keeps
489
+ * the key of the entry it replaced: the new document sits on the
490
+ * key of a dead marker, and the entries under it are the earlier
491
+ * document's. Up to gobj-ui 7.26.50 the new shell took that key
492
+ * for dead and went back into them: the earlier document was
493
+ * loaded again. */
494
+ test("a dead key a later document sits on is not followed into the document before it", async () => {
495
+ window.location.hash = "#/prev";
496
+ install_history_model(true);
497
+ window.history.pushState(null, "", "#/home");
498
+ let shell = new_shell();
499
+ yui_shell_register_overlay(shell, () => {}, {keep_on_navigate: true});
500
+ await settle();
501
+ yui_shell_confirm_yesno(shell, "leave?");
502
+ expect(session.entries.length).toBe(4);
503
+ stop_shell(shell);
504
+ cross_document_replace("#/elsewhere");
505
+ cross_document_replace("#/home");
506
+
507
+ shell = new_shell();
508
+ await settle();
509
+ expect(session.gos).toEqual([]);
510
+ expect(session.index).toBe(3);
511
+
512
+ /* Nor when a Back of the reader's lands on it. */
513
+ window.history.pushState(null, "", "#/prev");
514
+ window.dispatchEvent(new Event("hashchange"));
515
+ await settle();
516
+ user_back();
517
+ await settle();
518
+ expect(session.gos).toEqual([]);
519
+ expect(session.index).toBe(3);
520
+ expect(window.location.hash).toBe("#/home");
521
+ expect(shell_warnings()).toEqual([]);
522
+ });
523
+
524
+ /* A login page between the logout and the login, and the reader
525
+ * goes somewhere in it: an entry of the reader's over the dead
526
+ * markers, and the next shell starts on it. Up to gobj-ui 7.26.50
527
+ * nothing took them off: two dead Backs under it. */
528
+ test("a Back that lands on dead entries under a later one takes them off; Forward skips them", async () => {
529
+ window.location.hash = "#/prev";
530
+ install_history_model(true);
531
+ window.history.pushState(null, "", "#/home");
532
+ let shell = new_shell();
533
+ yui_shell_register_overlay(shell, () => {}, {keep_on_navigate: true});
534
+ await settle();
535
+ yui_shell_confirm_yesno(shell, "log out?");
536
+ stop_shell(shell);
537
+ window.history.pushState(null, "", "#/prev");
538
+
539
+ shell = new_shell();
540
+ await settle();
541
+ expect(session.gos).toEqual([]);
542
+ expect(session.index).toBe(4);
543
+
544
+ user_back();
545
+ await settle();
546
+ expect(session.gos).toEqual([-2]);
547
+ expect(session.index).toBe(1);
548
+ expect(window.location.hash).toBe("#/home");
549
+
550
+ /* Forward goes over them to the reader's entry, not onto them,
551
+ * and not back down. */
552
+ user_forward();
553
+ await settle();
554
+ expect(session.gos).toEqual([-2, 2]);
555
+ expect(session.index).toBe(4);
556
+ expect(window.location.hash).toBe("#/prev");
557
+
558
+ user_back();
559
+ await settle();
560
+ expect(session.index).toBe(1);
561
+ const r = await count_backs_to_start(() => false);
562
+ expect(r).toEqual({backs: 1, dead: 0});
563
+ expect(window.location.hash).toBe("#/prev");
564
+ expect(shell_warnings()).toEqual([]);
565
+ });
566
+
567
+ /* The dead entries on another hash than the live one under them:
568
+ * the landing on them is not navigated -- it would fire the
569
+ * action again and bring the preferences up. */
570
+ test("a Back over dead entries of an action route does not run the action", async () => {
571
+ window.location.hash = "#/prev";
572
+ install_history_model(true);
573
+ window.history.pushState(null, "", "#/home");
574
+ let shell = new_shell();
575
+ window.history.pushState(null, "", "#/prefs");
576
+ window.dispatchEvent(new Event("hashchange"));
577
+ yui_shell_show_modal(shell, "the prefs", {dialog: true, title: "preferences"});
578
+ expect(session.entries.map(e => e.hash)).toEqual(["#/prev", "#/home", "#/prefs", "#/prefs"]);
579
+ /* An app that does not unpark: the URL stays on the action. */
580
+ stop_shell(shell);
581
+ window.history.pushState(null, "", "#/prev");
582
+ shell = new_shell();
583
+ await settle();
584
+ expect(session.gos).toEqual([]);
585
+ const actions = () => logged.filter((l) => l.msg.includes("EV_OPEN_PREFS")).length;
586
+ const before = actions();
587
+
588
+ user_back();
589
+ await settle();
590
+ expect(session.gos).toEqual([-2]);
591
+ expect(session.index).toBe(1);
592
+ expect(window.location.hash).toBe("#/home");
593
+ expect(actions()).toBe(before);
594
+ expect(shell_warnings()).toEqual([]);
595
+ });
596
+
597
+ test("a Forward onto dead entries with nothing of the reader's above them moves nothing", async () => {
598
+ window.location.hash = "#/prev";
599
+ install_history_model(true);
600
+ window.history.pushState(null, "", "#/home");
601
+ let shell = new_shell();
602
+ yui_shell_register_overlay(shell, () => {}, {keep_on_navigate: true});
603
+ await settle();
604
+ yui_shell_confirm_yesno(shell, "log out?");
605
+ stop_shell(shell);
606
+ shell = new_shell();
607
+ await settle();
608
+ expect(session.gos).toEqual([-2]);
609
+ expect(session.index).toBe(1);
610
+
611
+ user_forward();
612
+ await settle();
613
+ user_forward();
614
+ await settle();
615
+ expect(session.gos).toEqual([-2]);
616
+ expect(session.index).toBe(3);
617
+
618
+ /* Back from there goes down to the reader's entry in ONE. */
619
+ user_back();
620
+ await settle();
621
+ expect(session.gos).toEqual([-2, -1]);
622
+ expect(session.index).toBe(1);
623
+ expect(window.location.hash).toBe("#/home");
624
+ expect(shell_warnings()).toEqual([]);
625
+ });
626
+
444
627
  test("without the Navigation API nothing moves, as before", async () => {
445
628
  window.location.hash = "#/home";
446
629
  install_history_model(false);