@yuneta/gobj-ui 7.26.12 → 7.26.14

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.
@@ -60,4 +60,43 @@ describe("C_YUI_GCLASS header", () => {
60
60
  expect($c.querySelector(".GCLASS_CURRENT").textContent).toBe(EVIL);
61
61
  expect($c.querySelector("img")).toBe(null);
62
62
  });
63
+
64
+ test("the method chips are text (V3.1 #4)", () => {
65
+ const view = gobj_create("gclass_methods_text", "C_YUI_GCLASS", {
66
+ description: {id: "C_X", gcflag: [], attrs: [], commands: [],
67
+ gclass_methods: [EVIL], internal_methods: [EVIL],
68
+ FSM: {events: [], states: {}}},
69
+ }, yuno);
70
+ gobj_start(view);
71
+ const $c = gobj_read_attr(view, "$container");
72
+ const chips = $c.querySelectorAll(".GCLASS_METHOD");
73
+ expect(chips.length).toBe(2);
74
+ for(const chip of chips) {
75
+ expect(chip.textContent).toBe(EVIL);
76
+ }
77
+ expect($c.querySelector("img")).toBe(null);
78
+ });
79
+
80
+ test("the error of the raw dump is text (V3.1 #4)", () => {
81
+ const description = {
82
+ id: "C_X", gcflag: [], attrs: [], commands: [],
83
+ gclass_methods: [], internal_methods: [],
84
+ FSM: {events: [], states: {}},
85
+ toJSON() {
86
+ /* A bare string thrown is what reaches the screen as is:
87
+ * String() of an Error starts with "Error:", never "<". */
88
+ throw EVIL;
89
+ },
90
+ };
91
+ const view = gobj_create("gclass_raw_error_text", "C_YUI_GCLASS", {
92
+ description: description, view_mode: "raw",
93
+ }, yuno);
94
+ gobj_start(view);
95
+ const $c = gobj_read_attr(view, "$container");
96
+ const $err = $c.querySelector(".GCLASS_RAW_ERROR");
97
+ expect($err).not.toBe(null);
98
+ expect($err.textContent.includes(EVIL)).toBe(true);
99
+ expect($c.querySelector("img")).toBe(null);
100
+ });
63
101
  });
102
+
@@ -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,6 +37,7 @@ const logged = [];
37
37
  let veto = true;
38
38
  let warning = null;
39
39
  let yuno = null;
40
+ const shells = [];
40
41
 
41
42
  /* The session history of one tab. Each entry carries its hash, as a
42
43
  * browser's does: a traversal lands on the entry's hash, and fires a
@@ -49,6 +50,48 @@ function url_hash(url)
49
50
  url.slice(url.indexOf("#")) : null;
50
51
  }
51
52
 
53
+ /* The model's own writes of the fragment: never a "link". */
54
+ function set_hash(h)
55
+ {
56
+ if(session.raw_hash_set) {
57
+ session.raw_hash_set(h);
58
+ } else {
59
+ window.location.hash = h;
60
+ }
61
+ }
62
+
63
+ /* As a browser does with `location.hash = x` -- what the push of
64
+ * yui_shell_navigate() does: a new entry, popstate, then the queued
65
+ * hashchange. Opt-in, and undone by links_on_hash_assign_off(): the
66
+ * other tests assign the hash to set their page up. */
67
+ function links_on_hash_assign()
68
+ {
69
+ let raw = window.location.hash;
70
+ session.raw_hash_set = (h) => {
71
+ raw = h;
72
+ };
73
+ Object.defineProperty(window.location, "hash", {
74
+ configurable: true,
75
+ get() {
76
+ return raw;
77
+ },
78
+ set(v) {
79
+ if(v === raw) {
80
+ return;
81
+ }
82
+ link_to(v);
83
+ }
84
+ });
85
+ }
86
+
87
+ function links_on_hash_assign_off()
88
+ {
89
+ const h = window.location.hash;
90
+ delete window.location.hash;
91
+ session.raw_hash_set = null;
92
+ window.location.hash = h;
93
+ }
94
+
52
95
  function install_history_model()
53
96
  {
54
97
  session.entries = [{state: null, hash: window.location.hash}];
@@ -63,7 +106,7 @@ function install_history_model()
63
106
  pushState(state, title, url) {
64
107
  const h = url_hash(url);
65
108
  if(h !== null) {
66
- window.location.hash = h;
109
+ set_hash(h);
67
110
  }
68
111
  session.entries.splice(session.index + 1);
69
112
  session.entries.push({state: state, hash: window.location.hash});
@@ -72,7 +115,7 @@ function install_history_model()
72
115
  replaceState(state, title, url) {
73
116
  const h = url_hash(url);
74
117
  if(h !== null) {
75
- window.location.hash = h;
118
+ set_hash(h);
76
119
  }
77
120
  session.entries[session.index] = {state: state, hash: window.location.hash};
78
121
  },
@@ -97,17 +140,39 @@ function fire_popstate()
97
140
  window.dispatchEvent(ev);
98
141
  }
99
142
 
143
+ /* As a browser does: popstate is fired during the traversal, and a
144
+ * hashchange -- when the fragment moved -- is QUEUED as a task of its own,
145
+ * so it runs after popstate and after whatever popstate's handlers did. */
146
+ function queue_hashchange()
147
+ {
148
+ setTimeout(() => {
149
+ window.dispatchEvent(new Event("hashchange"));
150
+ }, 0);
151
+ }
152
+
100
153
  function traverse_to(index)
101
154
  {
102
155
  const left = window.location.hash;
103
156
  session.index = index;
104
- window.location.hash = session.entries[index].hash;
157
+ set_hash(session.entries[index].hash);
105
158
  fire_popstate();
106
- if(window.location.hash !== left) {
107
- window.dispatchEvent(new Event("hashchange"));
159
+ if(session.entries[index].hash !== left) {
160
+ queue_hashchange();
108
161
  }
109
162
  }
110
163
 
164
+ /* A click on a fragment link: a new entry, then popstate, then the
165
+ * queued hashchange -- what Chromium and Firefox do. */
166
+ function link_to(hash)
167
+ {
168
+ session.entries.splice(session.index + 1);
169
+ session.entries.push({state: null, hash: hash});
170
+ session.index++;
171
+ set_hash(hash);
172
+ fire_popstate();
173
+ queue_hashchange();
174
+ }
175
+
111
176
  /* The user's Back: the browser's own traversal. */
112
177
  function user_back()
113
178
  {
@@ -141,6 +206,18 @@ beforeAll(async () => {
141
206
  ]]],
142
207
  {}, 0, [SDATA_END()], {}, 0, 0, 0, 0
143
208
  );
209
+ gclass_create(
210
+ "C_TEST_MARKER_VIEW", [], [["ST_IDLE", []]], {
211
+ mt_create: (gobj) => {
212
+ gobj_write_attr(gobj, "$container",
213
+ document.createElement("div"));
214
+ }
215
+ }, 0, [
216
+ SDATA(data_type_t.DTP_POINTER, "$container", 0, null, "view root"),
217
+ SDATA(data_type_t.DTP_POINTER, "subscriber", 0, null, "subscriber"),
218
+ SDATA_END()
219
+ ], {}, 0, 0, 0, 0
220
+ );
144
221
  register_c_yui_shell();
145
222
  register_c_yui_window();
146
223
  yuno = gobj_create_yuno("marker_yuno", "C_TEST_MARKER_HOST", {});
@@ -154,6 +231,53 @@ beforeEach(() => {
154
231
  install_history_model();
155
232
  });
156
233
 
234
+
235
+ /* A shell WITH a route table, so a change of resting route drains the
236
+ * overlays (navigate_to() -> drain_overlays()), as in an app. */
237
+ function routed_config()
238
+ {
239
+ const target = (route) => ({stage: "main", gclass: "C_TEST_MARKER_VIEW", kw: {}});
240
+ return {
241
+ shell: {
242
+ zones: {center: {host: "stage.main"}},
243
+ stages: {main: {zone: "center", default_route: "/home"}}
244
+ },
245
+ menu: {
246
+ primary: {
247
+ items: [
248
+ {id: "home", route: "/home", target: target("/home")},
249
+ {id: "prev", route: "/prev", target: target("/prev")},
250
+ {id: "other", route: "/other", target: target("/other")}
251
+ ]
252
+ }
253
+ }
254
+ };
255
+ }
256
+
257
+ function routed_shell(name, app)
258
+ {
259
+ const shell = gobj_create(name, "C_YUI_SHELL",
260
+ {config: routed_config(), subscriber: app}, app);
261
+ gobj_start(shell);
262
+ shells.push(shell);
263
+ return shell;
264
+ }
265
+
266
+ /* A test leaves no shell listening and no queued hashchange behind: the
267
+ * next test's shell would otherwise hear the previous test's traversal. */
268
+ afterEach(async () => {
269
+ if(session.raw_hash_set) {
270
+ links_on_hash_assign_off(); // a test that failed half-way
271
+ }
272
+ await next_task();
273
+ await next_task();
274
+ for(const sh of shells.splice(0)) {
275
+ if(gobj_is_running(sh)) {
276
+ gobj_stop(sh);
277
+ }
278
+ }
279
+ });
280
+
157
281
  function errors()
158
282
  {
159
283
  return logged.filter((l) => l.level === "error" || l.level === "warning").map((l) => l.msg);
@@ -166,6 +290,7 @@ describe("an overlay registered while a back() of ours is in flight", () => {
166
290
  const shell = gobj_create("mshell", "C_YUI_SHELL",
167
291
  {config: {shell: {}}, default_route: "/home", subscriber: app}, app);
168
292
  gobj_start(shell);
293
+ shells.push(shell);
169
294
  logged.length = 0; /* the shell's own start: no route table here */
170
295
  install_history_model();
171
296
 
@@ -213,6 +338,7 @@ describe("an overlay registered while a back() of ours is in flight", () => {
213
338
  const shell = gobj_create("mshell2", "C_YUI_SHELL",
214
339
  {config: {shell: {}}, default_route: "/home", subscriber: app}, app);
215
340
  gobj_start(shell);
341
+ shells.push(shell);
216
342
  logged.length = 0;
217
343
  install_history_model();
218
344
 
@@ -241,6 +367,7 @@ describe("an overlay registered while a back() of ours is in flight", () => {
241
367
  const shell = gobj_create("mshell3", "C_YUI_SHELL",
242
368
  {config: {shell: {}}, default_route: "/home", subscriber: app}, app);
243
369
  gobj_start(shell);
370
+ shells.push(shell);
244
371
  logged.length = 0;
245
372
  install_history_model();
246
373
 
@@ -266,6 +393,7 @@ describe("an overlay registered while a back() of ours is in flight", () => {
266
393
  const shell = gobj_create("mshell4", "C_YUI_SHELL",
267
394
  {config: {shell: {}}, default_route: "/home", subscriber: app}, app);
268
395
  gobj_start(shell);
396
+ shells.push(shell);
269
397
  logged.length = 0;
270
398
  install_history_model();
271
399
  window.location.hash = "#/home";
@@ -281,10 +409,10 @@ describe("an overlay registered while a back() of ours is in flight", () => {
281
409
  {keep_on_navigate: true});
282
410
  expect(second.marker_pending).toBe(true);
283
411
 
284
- /* A navigation: the hash moves (a new entry, as a link does), our
285
- * back() will never land. */
286
- window.history.pushState(null, "", "#/other");
287
- window.dispatchEvent(new Event("hashchange"));
412
+ /* A click on a link: a new entry, popstate, then the queued
413
+ * hashchange. Our back() will never land. */
414
+ link_to("#/other");
415
+ await next_task();
288
416
  expect(shell.priv.expected_pops).toBe(0);
289
417
  expect(second.marker_pending).toBe(false);
290
418
  expect(window.history.state).toEqual({__yui_overlay__: second.id});
@@ -302,6 +430,7 @@ describe("an overlay registered while a back() of ours is in flight", () => {
302
430
  const shell = gobj_create("mshell5", "C_YUI_SHELL",
303
431
  {config: {shell: {}}, default_route: "/home", subscriber: app}, app);
304
432
  gobj_start(shell);
433
+ shells.push(shell);
305
434
  install_history_model();
306
435
  const {yui_shell_register_overlay, yui_shell_overlay_dismissed} =
307
436
  await import("./c_yui_shell.js");
@@ -321,6 +450,7 @@ describe("an overlay registered while a back() of ours is in flight", () => {
321
450
  const shell = gobj_create("mshell6", "C_YUI_SHELL",
322
451
  {config: {shell: {}}, default_route: "/home", subscriber: app}, app);
323
452
  gobj_start(shell);
453
+ shells.push(shell);
324
454
  window.location.hash = "#/prev";
325
455
  install_history_model();
326
456
  window.history.pushState(null, "", "#/home"); // a route entry
@@ -340,6 +470,7 @@ describe("an overlay registered while a back() of ours is in flight", () => {
340
470
  * history.go(-2)): from the dead marker straight to #/prev, a hash
341
471
  * that is not the landing ours expected. */
342
472
  traverse_to(0);
473
+ await next_task(); // the queued hashchange
343
474
  expect(window.location.hash).toBe("#/prev");
344
475
  expect(closed).toBe(0);
345
476
  expect(shell.priv.overlay_stack).toEqual([second]);
@@ -359,6 +490,7 @@ describe("an overlay registered while a back() of ours is in flight", () => {
359
490
  const shell = gobj_create("mshell7", "C_YUI_SHELL",
360
491
  {config: {shell: {}}, default_route: "/home", subscriber: app}, app);
361
492
  gobj_start(shell);
493
+ shells.push(shell);
362
494
  window.location.hash = "#/home";
363
495
  install_history_model();
364
496
 
@@ -374,8 +506,153 @@ describe("an overlay registered while a back() of ours is in flight", () => {
374
506
  await next_task();
375
507
  await next_task();
376
508
  expect(shell.priv.expected_pops).toBe(0);
377
- expect(window.location.hash).toBe("#/home");
378
- expect(logged.some((l) => l.msg.includes("did not land"))).toBe(false);
509
+ /* Since 7.26.13 the position written with replace is put back. */
510
+ expect(window.location.hash).toBe("#/home/pos");
511
+ expect(logged.filter((l) => l.msg.includes("did not land")).map(l => l.msg)).toEqual([]);
512
+ });
513
+ });
514
+
515
+ describe("a routed shell, a realistic history (V5)", () => {
516
+ test("Back closes a keep_on_navigate panel after a replace navigation", async () => {
517
+ const app = gobj_create("vapp1", "C_TEST_MARKER_APP", {}, yuno);
518
+ gobj_start(app);
519
+ window.location.hash = "#/home";
520
+ install_history_model();
521
+ const shell = routed_shell("vshell1", app);
522
+ const {yui_shell_register_overlay, yui_shell_navigate} = await import("./c_yui_shell.js");
523
+ let closed = 0;
524
+ yui_shell_register_overlay(shell, () => { closed++; }, {keep_on_navigate: true});
525
+
526
+ /* The view under the panel writes its position with replace. */
527
+ yui_shell_navigate(shell, "/home/pos", {replace: true});
528
+ expect(window.location.hash).toBe("#/home/pos");
529
+
530
+ user_back();
531
+ await next_task();
532
+ await next_task();
533
+ expect(closed).toBe(1);
534
+ expect(shell.priv.overlay_stack).toEqual([]);
535
+ /* Back closed the panel; it did not move the view. */
536
+ expect(window.location.hash).toBe("#/home/pos");
537
+ expect(gobj_read_attr(shell, "current_route")).toBe("/home/pos");
538
+ });
539
+
540
+ test("a dialog's action that closes it and navigates in the same turn: the navigation survives", async () => {
541
+ const app = gobj_create("vapp_go", "C_TEST_MARKER_APP", {}, yuno);
542
+ gobj_start(app);
543
+ window.location.hash = "#/home";
544
+ install_history_model();
545
+ const shell = routed_shell("vshell_go", app);
546
+ const {yui_shell_register_overlay, yui_shell_overlay_dismissed, yui_shell_navigate} =
547
+ await import("./c_yui_shell.js");
548
+ const entry = yui_shell_register_overlay(shell, () => {});
549
+ await next_task();
550
+ links_on_hash_assign();
551
+
552
+ yui_shell_overlay_dismissed(shell, entry); // the close: a back() of ours
553
+ yui_shell_navigate(shell, "/prev"); // and the navigation, same turn
554
+ for(let i = 0; i < 4; i++) {
555
+ await next_task();
556
+ }
557
+ expect(shell.priv.expected_pops).toBe(0);
558
+ expect(window.location.hash).toBe("#/prev");
559
+ expect(gobj_read_attr(shell, "current_route")).toBe("/prev");
560
+ expect(window.history.state).toBe(null); // no marker left
561
+
562
+ user_back();
563
+ await next_task();
564
+ await next_task();
565
+ expect(window.location.hash).toBe("#/home"); // not swallowed
566
+ });
567
+
568
+ test("a button close after a replace navigation keeps the position", async () => {
569
+ const app = gobj_create("vapp2", "C_TEST_MARKER_APP", {}, yuno);
570
+ gobj_start(app);
571
+ window.location.hash = "#/home";
572
+ install_history_model();
573
+ const shell = routed_shell("vshell2", app);
574
+ const {yui_shell_register_overlay, yui_shell_overlay_dismissed, yui_shell_navigate} =
575
+ await import("./c_yui_shell.js");
576
+ const entry = yui_shell_register_overlay(shell, () => {}, {keep_on_navigate: true});
577
+ yui_shell_navigate(shell, "/home/pos", {replace: true});
578
+ logged.length = 0;
579
+
580
+ yui_shell_overlay_dismissed(shell, entry); // the close button
581
+ await next_task();
582
+ await next_task();
583
+ expect(shell.priv.expected_pops).toBe(0);
584
+ expect(window.location.hash).toBe("#/home/pos");
585
+ expect(gobj_read_attr(shell, "current_route")).toBe("/home/pos");
586
+ expect(window.history.state).toBe(null);
587
+ expect(errors()).toEqual([]);
588
+ });
589
+
590
+ test("a dropped back() and a real link: a drained overlay leaves no orphan marker", async () => {
591
+ const app = gobj_create("vapp3", "C_TEST_MARKER_APP", {}, yuno);
592
+ gobj_start(app);
593
+ window.location.hash = "#/home";
594
+ install_history_model();
595
+ const shell = routed_shell("vshell3", app);
596
+ const {yui_shell_register_overlay, yui_shell_overlay_dismissed} = await import("./c_yui_shell.js");
597
+ const first = yui_shell_register_overlay(shell, () => {});
598
+ window.history.back = () => {}; // the browser drops it
599
+ yui_shell_overlay_dismissed(shell, first);
600
+ let closed = 0;
601
+ const second = yui_shell_register_overlay(shell, () => { closed++; }); // drained by a navigation
602
+ expect(second.marker_pending).toBe(true);
603
+
604
+ link_to("#/other"); // popstate, then the queued hashchange
605
+ await next_task();
606
+ await next_task();
607
+ expect(closed).toBe(1);
608
+ expect(shell.priv.overlay_stack).toEqual([]);
609
+ expect(window.history.state).toBe(null); // no marker of the drained overlay
610
+ expect(session.entries.length).toBe(3); // #/home, the dead first marker, #/other
611
+ });
612
+
613
+ test("Forward onto a dead marker does not close the overlay under it", async () => {
614
+ const app = gobj_create("vapp4", "C_TEST_MARKER_APP", {}, yuno);
615
+ gobj_start(app);
616
+ window.location.hash = "#/home";
617
+ install_history_model();
618
+ const shell = routed_shell("vshell4", app);
619
+ const {yui_shell_register_overlay, yui_shell_overlay_dismissed} = await import("./c_yui_shell.js");
620
+ let closed_a = 0;
621
+ const a = yui_shell_register_overlay(shell, () => { closed_a++; });
622
+ const b = yui_shell_register_overlay(shell, () => {});
623
+ yui_shell_overlay_dismissed(shell, b); // its button: back() to A's marker
624
+ await next_task();
625
+ expect(window.history.state).toEqual({__yui_overlay__: a.id});
626
+
627
+ traverse_to(session.index + 1); // Forward, onto B's dead marker
628
+ expect(closed_a).toBe(0);
629
+ expect(shell.priv.overlay_stack).toEqual([a]);
630
+
631
+ user_back(); // back on A's own marker: absorbed
632
+ expect(closed_a).toBe(0);
633
+ user_back(); // off it: closes A
634
+ expect(closed_a).toBe(1);
635
+ });
636
+
637
+ test("mt_stop leaves no overlay waiting for a marker that will never come", async () => {
638
+ const app = gobj_create("vapp5", "C_TEST_MARKER_APP", {}, yuno);
639
+ gobj_start(app);
640
+ window.location.hash = "#/home";
641
+ install_history_model();
642
+ const shell = routed_shell("vshell5", app);
643
+ const {yui_shell_register_overlay, yui_shell_overlay_dismissed} = await import("./c_yui_shell.js");
644
+ const first = yui_shell_register_overlay(shell, () => {});
645
+ let backs = 0;
646
+ window.history.back = () => { backs++; };
647
+ yui_shell_overlay_dismissed(shell, first);
648
+ expect(backs).toBe(1);
649
+ const second = yui_shell_register_overlay(shell, () => {});
650
+ expect(second.marker_pending).toBe(true);
651
+
652
+ gobj_stop(shell);
653
+ expect(second.marker_pending).toBe(false);
654
+ yui_shell_overlay_dismissed(shell, second); // it has no marker: nothing to retire
655
+ expect(backs).toBe(1);
379
656
  });
380
657
  });
381
658
 
@@ -98,12 +98,12 @@ describe("EV_SHOW", () => {
98
98
  expect($w.style.getPropertyValue("display")).toBe("");
99
99
  expect(errors().some((m) => m.includes("is not the src"))).toBe(true);
100
100
 
101
- /* The old form (the gobj in the kw) still works, with a warning. */
101
+ /* The old form (the gobj in the kw) is refused since 7.26.14,
102
+ * even from the window itself. */
102
103
  logged.length = 0;
103
104
  gobj_send_event(dock, "EV_MINIMIZE_WINDOW", {window: w}, w);
104
- expect($w.style.getPropertyValue("display")).toBe("none");
105
- expect(errors()).toEqual([]);
106
- expect(logged.some((l) => l.level === "warning" && l.msg.includes("gobj_full_name"))).toBe(true);
105
+ expect($w.style.getPropertyValue("display")).toBe("");
106
+ expect(errors().some((m) => m.includes("carries the window gobj"))).toBe(true);
107
107
  });
108
108
 
109
109
  test("with plain strings as TEXT (set_strings_are_text) the controls are still svg", () => {
@@ -148,7 +148,7 @@ describe("EV_SHOW", () => {
148
148
  /* The OLD form (the gobj in the kw), sent by another gobj. */
149
149
  logged.length = 0;
150
150
  gobj_send_event(dock, ev, {window: w}, other);
151
- expect(errors().some((m) => m.includes("is not the src"))).toBe(true);
151
+ expect(errors().some((m) => m.includes("carries the window gobj"))).toBe(true);
152
152
  }
153
153
  expect(dock.priv.windows.length).toBe(n_windows); // nothing unregistered
154
154
  expect(dock.priv.windows.some((e) => e.gobj === other)).toBe(false); // nothing registered