@yuneta/gobj-ui 7.26.11 → 7.26.13

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.
@@ -19,15 +19,15 @@
19
19
  * Copyright (c) 2026, ArtGins.
20
20
  * All Rights Reserved.
21
21
  ***********************************************************************/
22
- import {describe, test, expect, beforeAll, beforeEach} from "vitest";
22
+ import {describe, test, expect, beforeAll, beforeEach, afterEach} from "vitest";
23
23
  import {install_dom_double} from "../test/dom_double.js";
24
24
 
25
25
  install_dom_double();
26
26
 
27
27
  const {
28
- SDATA_END, gclass_create,
28
+ SDATA, SDATA_END, data_type_t, gclass_create, gobj_write_attr,
29
29
  gobj_start_up, gobj_create_yuno, gobj_create, gobj_create_service,
30
- gobj_start, gobj_read_attr, gobj_is_destroying,
30
+ gobj_start, gobj_stop, gobj_is_running, gobj_read_attr, gobj_is_destroying,
31
31
  set_log_callback,
32
32
  } = await import("@yuneta/gobj-js");
33
33
  const {register_c_yui_shell} = await import("./c_yui_shell.js");
@@ -37,13 +37,22 @@ const logged = [];
37
37
  let veto = true;
38
38
  let warning = null;
39
39
  let yuno = null;
40
+ const shells = [];
40
41
 
41
- /* The session history of one tab. */
42
- const session = {entries: [{state: null}], index: 0};
42
+ /* The session history of one tab. Each entry carries its hash, as a
43
+ * browser's does: a traversal lands on the entry's hash, and fires a
44
+ * hashchange after the popstate when that hash is not the one it left. */
45
+ const session = {entries: [{state: null, hash: ""}], index: 0};
46
+
47
+ function url_hash(url)
48
+ {
49
+ return (typeof url === "string" && url.includes("#"))?
50
+ url.slice(url.indexOf("#")) : null;
51
+ }
43
52
 
44
53
  function install_history_model()
45
54
  {
46
- session.entries = [{state: null}];
55
+ session.entries = [{state: null, hash: window.location.hash}];
47
56
  session.index = 0;
48
57
  window.history = {
49
58
  get state() {
@@ -52,19 +61,26 @@ function install_history_model()
52
61
  get length() {
53
62
  return session.entries.length;
54
63
  },
55
- pushState(state) {
64
+ pushState(state, title, url) {
65
+ const h = url_hash(url);
66
+ if(h !== null) {
67
+ window.location.hash = h;
68
+ }
56
69
  session.entries.splice(session.index + 1);
57
- session.entries.push({state: state});
70
+ session.entries.push({state: state, hash: window.location.hash});
58
71
  session.index++;
59
72
  },
60
- replaceState(state) {
61
- session.entries[session.index] = {state: state};
73
+ replaceState(state, title, url) {
74
+ const h = url_hash(url);
75
+ if(h !== null) {
76
+ window.location.hash = h;
77
+ }
78
+ session.entries[session.index] = {state: state, hash: window.location.hash};
62
79
  },
63
80
  back() {
64
81
  setTimeout(() => {
65
82
  if(session.index > 0) {
66
- session.index--;
67
- fire_popstate();
83
+ traverse_to(session.index - 1);
68
84
  }
69
85
  }, 0);
70
86
  },
@@ -82,11 +98,43 @@ function fire_popstate()
82
98
  window.dispatchEvent(ev);
83
99
  }
84
100
 
101
+ /* As a browser does: popstate is fired during the traversal, and a
102
+ * hashchange -- when the fragment moved -- is QUEUED as a task of its own,
103
+ * so it runs after popstate and after whatever popstate's handlers did. */
104
+ function queue_hashchange()
105
+ {
106
+ setTimeout(() => {
107
+ window.dispatchEvent(new Event("hashchange"));
108
+ }, 0);
109
+ }
110
+
111
+ function traverse_to(index)
112
+ {
113
+ const left = window.location.hash;
114
+ session.index = index;
115
+ window.location.hash = session.entries[index].hash;
116
+ fire_popstate();
117
+ if(session.entries[index].hash !== left) {
118
+ queue_hashchange();
119
+ }
120
+ }
121
+
122
+ /* A click on a fragment link: a new entry, then popstate, then the
123
+ * queued hashchange -- what Chromium and Firefox do. */
124
+ function link_to(hash)
125
+ {
126
+ session.entries.splice(session.index + 1);
127
+ session.entries.push({state: null, hash: hash});
128
+ session.index++;
129
+ window.location.hash = hash;
130
+ fire_popstate();
131
+ queue_hashchange();
132
+ }
133
+
85
134
  /* The user's Back: the browser's own traversal. */
86
135
  function user_back()
87
136
  {
88
- session.index--;
89
- fire_popstate();
137
+ traverse_to(session.index - 1);
90
138
  }
91
139
 
92
140
  function next_task()
@@ -116,6 +164,18 @@ beforeAll(async () => {
116
164
  ]]],
117
165
  {}, 0, [SDATA_END()], {}, 0, 0, 0, 0
118
166
  );
167
+ gclass_create(
168
+ "C_TEST_MARKER_VIEW", [], [["ST_IDLE", []]], {
169
+ mt_create: (gobj) => {
170
+ gobj_write_attr(gobj, "$container",
171
+ document.createElement("div"));
172
+ }
173
+ }, 0, [
174
+ SDATA(data_type_t.DTP_POINTER, "$container", 0, null, "view root"),
175
+ SDATA(data_type_t.DTP_POINTER, "subscriber", 0, null, "subscriber"),
176
+ SDATA_END()
177
+ ], {}, 0, 0, 0, 0
178
+ );
119
179
  register_c_yui_shell();
120
180
  register_c_yui_window();
121
181
  yuno = gobj_create_yuno("marker_yuno", "C_TEST_MARKER_HOST", {});
@@ -129,6 +189,50 @@ beforeEach(() => {
129
189
  install_history_model();
130
190
  });
131
191
 
192
+
193
+ /* A shell WITH a route table, so a change of resting route drains the
194
+ * overlays (navigate_to() -> drain_overlays()), as in an app. */
195
+ function routed_config()
196
+ {
197
+ const target = (route) => ({stage: "main", gclass: "C_TEST_MARKER_VIEW", kw: {}});
198
+ return {
199
+ shell: {
200
+ zones: {center: {host: "stage.main"}},
201
+ stages: {main: {zone: "center", default_route: "/home"}}
202
+ },
203
+ menu: {
204
+ primary: {
205
+ items: [
206
+ {id: "home", route: "/home", target: target("/home")},
207
+ {id: "prev", route: "/prev", target: target("/prev")},
208
+ {id: "other", route: "/other", target: target("/other")}
209
+ ]
210
+ }
211
+ }
212
+ };
213
+ }
214
+
215
+ function routed_shell(name, app)
216
+ {
217
+ const shell = gobj_create(name, "C_YUI_SHELL",
218
+ {config: routed_config(), subscriber: app}, app);
219
+ gobj_start(shell);
220
+ shells.push(shell);
221
+ return shell;
222
+ }
223
+
224
+ /* A test leaves no shell listening and no queued hashchange behind: the
225
+ * next test's shell would otherwise hear the previous test's traversal. */
226
+ afterEach(async () => {
227
+ await next_task();
228
+ await next_task();
229
+ for(const sh of shells.splice(0)) {
230
+ if(gobj_is_running(sh)) {
231
+ gobj_stop(sh);
232
+ }
233
+ }
234
+ });
235
+
132
236
  function errors()
133
237
  {
134
238
  return logged.filter((l) => l.level === "error" || l.level === "warning").map((l) => l.msg);
@@ -141,6 +245,7 @@ describe("an overlay registered while a back() of ours is in flight", () => {
141
245
  const shell = gobj_create("mshell", "C_YUI_SHELL",
142
246
  {config: {shell: {}}, default_route: "/home", subscriber: app}, app);
143
247
  gobj_start(shell);
248
+ shells.push(shell);
144
249
  logged.length = 0; /* the shell's own start: no route table here */
145
250
  install_history_model();
146
251
 
@@ -188,6 +293,7 @@ describe("an overlay registered while a back() of ours is in flight", () => {
188
293
  const shell = gobj_create("mshell2", "C_YUI_SHELL",
189
294
  {config: {shell: {}}, default_route: "/home", subscriber: app}, app);
190
295
  gobj_start(shell);
296
+ shells.push(shell);
191
297
  logged.length = 0;
192
298
  install_history_model();
193
299
 
@@ -216,6 +322,7 @@ describe("an overlay registered while a back() of ours is in flight", () => {
216
322
  const shell = gobj_create("mshell3", "C_YUI_SHELL",
217
323
  {config: {shell: {}}, default_route: "/home", subscriber: app}, app);
218
324
  gobj_start(shell);
325
+ shells.push(shell);
219
326
  logged.length = 0;
220
327
  install_history_model();
221
328
 
@@ -241,6 +348,7 @@ describe("an overlay registered while a back() of ours is in flight", () => {
241
348
  const shell = gobj_create("mshell4", "C_YUI_SHELL",
242
349
  {config: {shell: {}}, default_route: "/home", subscriber: app}, app);
243
350
  gobj_start(shell);
351
+ shells.push(shell);
244
352
  logged.length = 0;
245
353
  install_history_model();
246
354
  window.location.hash = "#/home";
@@ -256,9 +364,10 @@ describe("an overlay registered while a back() of ours is in flight", () => {
256
364
  {keep_on_navigate: true});
257
365
  expect(second.marker_pending).toBe(true);
258
366
 
259
- /* A navigation: the hash moves, our back() will never land. */
260
- window.location.hash = "#/other";
261
- window.dispatchEvent(new Event("hashchange"));
367
+ /* A click on a link: a new entry, popstate, then the queued
368
+ * hashchange. Our back() will never land. */
369
+ link_to("#/other");
370
+ await next_task();
262
371
  expect(shell.priv.expected_pops).toBe(0);
263
372
  expect(second.marker_pending).toBe(false);
264
373
  expect(window.history.state).toEqual({__yui_overlay__: second.id});
@@ -276,6 +385,7 @@ describe("an overlay registered while a back() of ours is in flight", () => {
276
385
  const shell = gobj_create("mshell5", "C_YUI_SHELL",
277
386
  {config: {shell: {}}, default_route: "/home", subscriber: app}, app);
278
387
  gobj_start(shell);
388
+ shells.push(shell);
279
389
  install_history_model();
280
390
  const {yui_shell_register_overlay, yui_shell_overlay_dismissed} =
281
391
  await import("./c_yui_shell.js");
@@ -288,4 +398,184 @@ describe("an overlay registered while a back() of ours is in flight", () => {
288
398
  expect(shell.priv.expected_pops).toBe(0);
289
399
  expect(shell.priv.pending_markers).toEqual([]);
290
400
  });
401
+
402
+ test("a dropped back() and a Back to another hash: the waiting overlay is not closed, no orphan marker", async () => {
403
+ const app = gobj_create("mapp6", "C_TEST_MARKER_APP", {}, yuno);
404
+ gobj_start(app);
405
+ const shell = gobj_create("mshell6", "C_YUI_SHELL",
406
+ {config: {shell: {}}, default_route: "/home", subscriber: app}, app);
407
+ gobj_start(shell);
408
+ shells.push(shell);
409
+ window.location.hash = "#/prev";
410
+ install_history_model();
411
+ window.history.pushState(null, "", "#/home"); // a route entry
412
+
413
+ const {yui_shell_register_overlay, yui_shell_overlay_dismissed} =
414
+ await import("./c_yui_shell.js");
415
+ const first = yui_shell_register_overlay(shell, () => {});
416
+ window.history.back = () => {}; // the browser drops it
417
+ yui_shell_overlay_dismissed(shell, first);
418
+ expect(shell.priv.expected_pops).toBe(1);
419
+ let closed = 0;
420
+ const second = yui_shell_register_overlay(shell, () => { closed++; },
421
+ {keep_on_navigate: true});
422
+ expect(second.marker_pending).toBe(true);
423
+
424
+ /* The reader goes back two entries at once (a long press on Back,
425
+ * history.go(-2)): from the dead marker straight to #/prev, a hash
426
+ * that is not the landing ours expected. */
427
+ traverse_to(0);
428
+ await next_task(); // the queued hashchange
429
+ expect(window.location.hash).toBe("#/prev");
430
+ expect(closed).toBe(0);
431
+ expect(shell.priv.overlay_stack).toEqual([second]);
432
+ expect(second.marker_pending).toBe(false);
433
+ expect(window.history.state).toEqual({__yui_overlay__: second.id});
434
+ expect(second.hash).toBe("#/prev");
435
+
436
+ /* And its marker is a real one: the next Back closes it. */
437
+ user_back();
438
+ expect(closed).toBe(1);
439
+ expect(shell.priv.overlay_stack).toEqual([]);
440
+ });
441
+
442
+ test("a button close after a replace navigation lands where expected, without a 'dropped' warning", async () => {
443
+ const app = gobj_create("mapp7", "C_TEST_MARKER_APP", {}, yuno);
444
+ gobj_start(app);
445
+ const shell = gobj_create("mshell7", "C_YUI_SHELL",
446
+ {config: {shell: {}}, default_route: "/home", subscriber: app}, app);
447
+ gobj_start(shell);
448
+ shells.push(shell);
449
+ window.location.hash = "#/home";
450
+ install_history_model();
451
+
452
+ const {yui_shell_register_overlay, yui_shell_overlay_dismissed} =
453
+ await import("./c_yui_shell.js");
454
+ const entry = yui_shell_register_overlay(shell, () => {}, {keep_on_navigate: true});
455
+ /* The view under it writes its position into the URL with replace:
456
+ * the marker's own entry moves to another hash. */
457
+ window.history.replaceState(window.history.state, "", "#/home/pos");
458
+ logged.length = 0;
459
+
460
+ yui_shell_overlay_dismissed(shell, entry); // the close button
461
+ await next_task();
462
+ await next_task();
463
+ expect(shell.priv.expected_pops).toBe(0);
464
+ /* Since 7.26.13 the position written with replace is put back. */
465
+ expect(window.location.hash).toBe("#/home/pos");
466
+ expect(logged.filter((l) => l.msg.includes("did not land")).map(l => l.msg)).toEqual([]);
467
+ });
468
+ });
469
+
470
+ describe("a routed shell, a realistic history (V5)", () => {
471
+ test("Back closes a keep_on_navigate panel after a replace navigation", async () => {
472
+ const app = gobj_create("vapp1", "C_TEST_MARKER_APP", {}, yuno);
473
+ gobj_start(app);
474
+ window.location.hash = "#/home";
475
+ install_history_model();
476
+ const shell = routed_shell("vshell1", app);
477
+ const {yui_shell_register_overlay, yui_shell_navigate} = await import("./c_yui_shell.js");
478
+ let closed = 0;
479
+ yui_shell_register_overlay(shell, () => { closed++; }, {keep_on_navigate: true});
480
+
481
+ /* The view under the panel writes its position with replace. */
482
+ yui_shell_navigate(shell, "/home/pos", {replace: true});
483
+ expect(window.location.hash).toBe("#/home/pos");
484
+
485
+ user_back();
486
+ await next_task();
487
+ expect(closed).toBe(1);
488
+ expect(shell.priv.overlay_stack).toEqual([]);
489
+ });
490
+
491
+ test("a button close after a replace navigation keeps the position", async () => {
492
+ const app = gobj_create("vapp2", "C_TEST_MARKER_APP", {}, yuno);
493
+ gobj_start(app);
494
+ window.location.hash = "#/home";
495
+ install_history_model();
496
+ const shell = routed_shell("vshell2", app);
497
+ const {yui_shell_register_overlay, yui_shell_overlay_dismissed, yui_shell_navigate} =
498
+ await import("./c_yui_shell.js");
499
+ const entry = yui_shell_register_overlay(shell, () => {}, {keep_on_navigate: true});
500
+ yui_shell_navigate(shell, "/home/pos", {replace: true});
501
+ logged.length = 0;
502
+
503
+ yui_shell_overlay_dismissed(shell, entry); // the close button
504
+ await next_task();
505
+ await next_task();
506
+ expect(shell.priv.expected_pops).toBe(0);
507
+ expect(window.location.hash).toBe("#/home/pos");
508
+ expect(gobj_read_attr(shell, "current_route")).toBe("/home/pos");
509
+ expect(window.history.state).toBe(null);
510
+ expect(errors()).toEqual([]);
511
+ });
512
+
513
+ test("a dropped back() and a real link: a drained overlay leaves no orphan marker", async () => {
514
+ const app = gobj_create("vapp3", "C_TEST_MARKER_APP", {}, yuno);
515
+ gobj_start(app);
516
+ window.location.hash = "#/home";
517
+ install_history_model();
518
+ const shell = routed_shell("vshell3", app);
519
+ const {yui_shell_register_overlay, yui_shell_overlay_dismissed} = await import("./c_yui_shell.js");
520
+ const first = yui_shell_register_overlay(shell, () => {});
521
+ window.history.back = () => {}; // the browser drops it
522
+ yui_shell_overlay_dismissed(shell, first);
523
+ let closed = 0;
524
+ const second = yui_shell_register_overlay(shell, () => { closed++; }); // drained by a navigation
525
+ expect(second.marker_pending).toBe(true);
526
+
527
+ link_to("#/other"); // popstate, then the queued hashchange
528
+ await next_task();
529
+ await next_task();
530
+ expect(closed).toBe(1);
531
+ expect(shell.priv.overlay_stack).toEqual([]);
532
+ expect(window.history.state).toBe(null); // no marker of the drained overlay
533
+ expect(session.entries.length).toBe(3); // #/home, the dead first marker, #/other
534
+ });
535
+
536
+ test("Forward onto a dead marker does not close the overlay under it", async () => {
537
+ const app = gobj_create("vapp4", "C_TEST_MARKER_APP", {}, yuno);
538
+ gobj_start(app);
539
+ window.location.hash = "#/home";
540
+ install_history_model();
541
+ const shell = routed_shell("vshell4", app);
542
+ const {yui_shell_register_overlay, yui_shell_overlay_dismissed} = await import("./c_yui_shell.js");
543
+ let closed_a = 0;
544
+ const a = yui_shell_register_overlay(shell, () => { closed_a++; });
545
+ const b = yui_shell_register_overlay(shell, () => {});
546
+ yui_shell_overlay_dismissed(shell, b); // its button: back() to A's marker
547
+ await next_task();
548
+ expect(window.history.state).toEqual({__yui_overlay__: a.id});
549
+
550
+ traverse_to(session.index + 1); // Forward, onto B's dead marker
551
+ expect(closed_a).toBe(0);
552
+ expect(shell.priv.overlay_stack).toEqual([a]);
553
+
554
+ user_back(); // back on A's own marker: absorbed
555
+ expect(closed_a).toBe(0);
556
+ user_back(); // off it: closes A
557
+ expect(closed_a).toBe(1);
558
+ });
559
+
560
+ test("mt_stop leaves no overlay waiting for a marker that will never come", async () => {
561
+ const app = gobj_create("vapp5", "C_TEST_MARKER_APP", {}, yuno);
562
+ gobj_start(app);
563
+ window.location.hash = "#/home";
564
+ install_history_model();
565
+ const shell = routed_shell("vshell5", app);
566
+ const {yui_shell_register_overlay, yui_shell_overlay_dismissed} = await import("./c_yui_shell.js");
567
+ const first = yui_shell_register_overlay(shell, () => {});
568
+ let backs = 0;
569
+ window.history.back = () => { backs++; };
570
+ yui_shell_overlay_dismissed(shell, first);
571
+ expect(backs).toBe(1);
572
+ const second = yui_shell_register_overlay(shell, () => {});
573
+ expect(second.marker_pending).toBe(true);
574
+
575
+ gobj_stop(shell);
576
+ expect(second.marker_pending).toBe(false);
577
+ yui_shell_overlay_dismissed(shell, second); // it has no marker: nothing to retire
578
+ expect(backs).toBe(1);
579
+ });
291
580
  });
581
+