@yuneta/gobj-ui 7.26.45 → 7.26.46

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.
@@ -24,7 +24,14 @@
24
24
  * that wrapped to it, did nothing and was prevented -- the
25
25
  * keyboard never got into the dialog.
26
26
  *
27
- * Pure module — no gobj / Yuneta dependencies. The optional
27
+ * A trap whose panel left the document without being released
28
+ * (its owner was torn down with it) is dropped, with its
29
+ * listener, the next time the stack is read, and said. Up to
30
+ * gobj-ui 7.26.45 it stayed the top: every window opened later
31
+ * believed itself under an overlay and took no focus, and a
32
+ * live trap under it no longer heard Tab.
33
+ *
34
+ * No gobj dependency: only gobj-js's logger. The optional
28
35
  * second argument lets callers inject a mock document object
29
36
  * for unit tests.
30
37
  *
@@ -32,6 +39,8 @@
32
39
  * All Rights Reserved.
33
40
  ***********************************************************************/
34
41
 
42
+ import {log_warning} from "@yuneta/gobj-js";
43
+
35
44
  /* Selector matching the elements typically considered focusable.
36
45
  * Kept conservative: items that are explicitly removed from the tab
37
46
  * order (`tabindex="-1"`) are excluded; everything else is in. */
@@ -50,6 +59,23 @@ export const FOCUSABLE_SELECTOR =
50
59
  * topmost trap; the rest short-circuit. */
51
60
  const __trap_stack__ = [];
52
61
 
62
+ /* Drop the traps whose panel is no longer in the document. Only a
63
+ * panel that SAYS so (isConnected === false) is detached: a stub
64
+ * that cannot say is taken as connected. */
65
+ function drop_detached_traps()
66
+ {
67
+ for(let i = __trap_stack__.length - 1; i >= 0; i--) {
68
+ let trap = __trap_stack__[i];
69
+ if(trap.$panel.isConnected !== false) {
70
+ continue;
71
+ }
72
+ __trap_stack__.splice(i, 1);
73
+ trap.detach();
74
+ log_warning("focus trap: its panel left the document without releasing it; " +
75
+ "dropped (the overlay's owner was torn down with it open)");
76
+ }
77
+ }
78
+
53
79
 
54
80
  /* Whether `el` is drawn, so focus() on it can take. An element that
55
81
  * cannot say (a stub with no getClientRects) is taken as drawn. */
@@ -100,6 +126,7 @@ export function activate_focus_trap_on($panel, doc)
100
126
  * Earlier-registered listeners fire too (capture phase),
101
127
  * but they short-circuit unless they own the top of the
102
128
  * stack. */
129
+ drop_detached_traps();
103
130
  if(__trap_stack__[__trap_stack__.length - 1] !== trap) {
104
131
  return;
105
132
  }
@@ -124,6 +151,9 @@ export function activate_focus_trap_on($panel, doc)
124
151
  }
125
152
  };
126
153
  trap.$panel = $panel;
154
+ trap.detach = function() {
155
+ doc.removeEventListener("keydown", trap, true);
156
+ };
127
157
  doc.addEventListener("keydown", trap, true);
128
158
  __trap_stack__.push(trap);
129
159
 
@@ -161,6 +191,7 @@ export function activate_focus_trap_on($panel, doc)
161
191
  ***************************************************************/
162
192
  export function focus_trap_panel()
163
193
  {
194
+ drop_detached_traps();
164
195
  let top = __trap_stack__[__trap_stack__.length - 1];
165
196
  return top ? top.$panel : null;
166
197
  }
@@ -9,6 +9,7 @@ import { test, expect } from "vitest";
9
9
  import {
10
10
  FOCUSABLE_SELECTOR,
11
11
  activate_focus_trap_on,
12
+ focus_trap_panel,
12
13
  } from "./shell_focus_trap.js";
13
14
 
14
15
 
@@ -381,3 +382,97 @@ test("Tab and Shift+Tab wrap to the first and last DRAWN controls", () => {
381
382
  expect(x.focus_calls).toEqual([]);
382
383
  release();
383
384
  });
385
+
386
+
387
+ /***************************************************************
388
+ * A trap whose panel left the document without being released
389
+ * (its owner was torn down with it: a shell destroyed with a
390
+ * drawer or a dialog open) holds the focus of nothing. Kept on
391
+ * the stack it was the "overlay" every later window opened
392
+ * under, so none of them took the focus; and above a live trap
393
+ * it left that one deaf to Tab. It is dropped, with its
394
+ * listener, the first time the stack is read.
395
+ ***************************************************************/
396
+ function connected_panel(focusables)
397
+ {
398
+ let panel = make_panel(focusables);
399
+ panel.isConnected = true;
400
+ return panel;
401
+ }
402
+
403
+ test("a trap whose panel left the document is no longer the top", () => {
404
+ let a_first = make_focusable("a-first");
405
+ let panel_a = connected_panel([a_first]);
406
+ let panel_b = connected_panel([make_focusable("b-first")]);
407
+ let doc = make_doc(null);
408
+
409
+ let release_a = activate_focus_trap_on(panel_a, doc);
410
+ activate_focus_trap_on(panel_b, doc);
411
+ expect(focus_trap_panel()).toBe(panel_b);
412
+
413
+ panel_b.isConnected = false;
414
+ expect(focus_trap_panel()).toBe(panel_a);
415
+ expect(doc.listeners.length).toBe(1);
416
+
417
+ /* And the live one under it hears Tab again. */
418
+ a_first.focus_calls.length = 0;
419
+ doc.activeElement = make_focusable("outside");
420
+ let prevented = false;
421
+ doc.listeners[0]({
422
+ key: "Tab",
423
+ shiftKey: false,
424
+ preventDefault() {
425
+ prevented = true;
426
+ }
427
+ });
428
+ expect(prevented).toBe(true);
429
+ expect(a_first.focus_calls).toEqual(["a-first.focus"]);
430
+
431
+ release_a();
432
+ expect(doc.listeners.length).toBe(0);
433
+ });
434
+
435
+ test("a detached trap above a live one does not leave the live one deaf to Tab", () => {
436
+ let a_first = make_focusable("a-first");
437
+ let panel_a = connected_panel([a_first]);
438
+ let panel_b = connected_panel([make_focusable("b-first")]);
439
+ let doc = make_doc(null);
440
+
441
+ let release_a = activate_focus_trap_on(panel_a, doc);
442
+ activate_focus_trap_on(panel_b, doc);
443
+ panel_b.isConnected = false;
444
+
445
+ /* Nobody asked for the top: the Tab itself finds the stale one. */
446
+ a_first.focus_calls.length = 0;
447
+ doc.activeElement = make_focusable("outside");
448
+ let prevented = false;
449
+ let ev = {
450
+ key: "Tab",
451
+ shiftKey: false,
452
+ preventDefault() {
453
+ prevented = true;
454
+ }
455
+ };
456
+ for(let fn of doc.listeners.slice()) {
457
+ fn(ev);
458
+ }
459
+ expect(prevented).toBe(true);
460
+ expect(a_first.focus_calls).toEqual(["a-first.focus"]);
461
+ expect(doc.listeners.length).toBe(1);
462
+
463
+ release_a();
464
+ });
465
+
466
+ test("the release of a dropped trap still gives the focus back", () => {
467
+ let prev = make_focusable("prev");
468
+ let panel = connected_panel([make_focusable("first")]);
469
+ let doc = make_doc(prev);
470
+
471
+ let release = activate_focus_trap_on(panel, doc);
472
+ panel.isConnected = false;
473
+ expect(focus_trap_panel()).not.toBe(panel);
474
+ expect(doc.listeners.length).toBe(0);
475
+
476
+ release();
477
+ expect(prev.focus_calls).toEqual(["prev.focus"]);
478
+ });
@@ -518,7 +518,7 @@ export function yui_shell_show_modal(shell, content, opts)
518
518
  };
519
519
 
520
520
  escape_handler = request_close;
521
- yui_shell_push_escape(shell, "modal", request_close);
521
+ yui_shell_push_escape(shell, "modal", request_close, do_close);
522
522
  /* Browser Back closes the top-most modal (see overlay history in
523
523
  * c_yui_shell.js). Null when history integration is off. */
524
524
  overlay = yui_shell_register_overlay(shell, on_back);
@@ -693,7 +693,7 @@ function build_dialog(shell, message, buttons, opts)
693
693
  };
694
694
  close_fn = () => close(dismiss_value);
695
695
 
696
- yui_shell_push_escape(shell, "modal", close_fn);
696
+ yui_shell_push_escape(shell, "modal", close_fn, close_fn);
697
697
  /* Browser Back dismisses the dialog with the safe-default value,
698
698
  * exactly like Escape. */
699
699
  overlay = yui_shell_register_overlay(shell, close_fn);
@@ -0,0 +1,143 @@
1
+ /***********************************************************************
2
+ * shell_stop_overlays.wiring.test.js
3
+ *
4
+ * A shell stopped with an overlay up -- a logout, a session cut
5
+ * with the drawer or a dialog open -- closes the overlay first,
6
+ * as if it had been dismissed, and releases its focus trap. Up
7
+ * to gobj-ui 7.26.45 only the toolbar dropdown was closed: the
8
+ * trap of a drawer, a modal or a question stayed on the stack,
9
+ * every window opened afterwards believed itself under an
10
+ * overlay and took no focus, and the question's promise never
11
+ * settled.
12
+ *
13
+ * Copyright (c) 2026, ArtGins.
14
+ * All Rights Reserved.
15
+ ***********************************************************************/
16
+ import {describe, test, expect, beforeAll, beforeEach} from "vitest";
17
+ import {install_dom_double} from "../test/dom_double.js";
18
+
19
+ install_dom_double();
20
+
21
+ const {
22
+ SDATA_END, gclass_create,
23
+ gobj_start_up, gobj_create_yuno, gobj_create, gobj_start, gobj_stop, gobj_destroy,
24
+ set_log_callback,
25
+ } = await import("@yuneta/gobj-js");
26
+ const {register_c_yui_shell, yui_shell_open_drawer} = await import("./c_yui_shell.js");
27
+ const {register_c_yui_nav} = await import("./c_yui_nav.js");
28
+ const {yui_shell_show_modal, yui_shell_confirm_yesno} = await import("./shell_modals.js");
29
+ const {focus_trap_panel} = await import("./shell_focus_trap.js");
30
+
31
+ const logged = [];
32
+ let yuno = null;
33
+ let seq = 0;
34
+
35
+ beforeAll(async () => {
36
+ const i18next = (await import("i18next")).default;
37
+ await i18next.init({lng: "en", resources: {}});
38
+ gobj_start_up(null, null, null, null, null, null, null);
39
+ set_log_callback((level, msg) => {
40
+ logged.push({level: String(level), msg: String(msg)});
41
+ });
42
+ gclass_create("C_TEST_STOP_HOST", [], [["ST_IDLE", []]], {}, 0, [SDATA_END()], {}, 0, 0, 0, 0);
43
+ register_c_yui_nav();
44
+ register_c_yui_shell();
45
+ yuno = gobj_create_yuno("stop_overlays_yuno", "C_TEST_STOP_HOST", {});
46
+ gobj_start(yuno);
47
+ });
48
+
49
+ beforeEach(() => {
50
+ logged.length = 0;
51
+ });
52
+
53
+ function errors()
54
+ {
55
+ return logged.filter((l) => l.level === "error").map((l) => l.msg);
56
+ }
57
+
58
+ function settle()
59
+ {
60
+ return new Promise(resolve => setTimeout(resolve, 0));
61
+ }
62
+
63
+ function make_shell()
64
+ {
65
+ seq++;
66
+ const app = gobj_create(`sapp${seq}`, "C_TEST_STOP_HOST", {}, yuno);
67
+ gobj_start(app);
68
+ const shell = gobj_create(`sshell${seq}`, "C_YUI_SHELL", {
69
+ config: {
70
+ shell: {},
71
+ menu: {
72
+ quick: {
73
+ render: {overlay: "drawer"},
74
+ items: [{id: "home", name: "home", route: "/home"}]
75
+ }
76
+ }
77
+ },
78
+ subscriber: app
79
+ }, app);
80
+ gobj_start(shell);
81
+ logged.length = 0; /* the shell's own start: no route table here */
82
+ return shell;
83
+ }
84
+
85
+ describe("a shell stopped with an overlay up", () => {
86
+ test("the drawer, a modal and a question close, and no trap is left", async () => {
87
+ const shell = make_shell();
88
+
89
+ yui_shell_open_drawer(shell, "quick");
90
+ const $drawer = document.querySelector(".is-active");
91
+ expect($drawer).not.toBe(null);
92
+ expect(focus_trap_panel()).not.toBe(null);
93
+
94
+ let modal_closed = 0;
95
+ yui_shell_show_modal(shell, "the sheet", {
96
+ dialog: true,
97
+ logical_class: "TEST_STOP_SHEET",
98
+ title: "settings",
99
+ /* A guard is for the reader's dismissals, not for the
100
+ * shell going away. */
101
+ before_close: () => false,
102
+ on_close: () => {
103
+ modal_closed++;
104
+ }
105
+ });
106
+ let answer = null;
107
+ yui_shell_confirm_yesno(shell, "really?").then((a) => {
108
+ answer = a;
109
+ });
110
+ expect(document.querySelectorAll(".TEST_STOP_SHEET").length).toBe(1);
111
+ expect(document.querySelectorAll(".CONFIRM").length).toBe(1);
112
+
113
+ gobj_stop(shell);
114
+ gobj_destroy(shell);
115
+ await settle();
116
+
117
+ expect(focus_trap_panel()).toBe(null);
118
+ expect(modal_closed).toBe(1);
119
+ expect(answer).toBe(false); /* the answer Escape gives */
120
+ expect(document.querySelectorAll(".TEST_STOP_SHEET").length).toBe(0);
121
+ expect(document.querySelectorAll(".CONFIRM").length).toBe(0);
122
+ expect($drawer.classList.contains("is-active")).toBe(false);
123
+ expect(errors()).toEqual([]);
124
+ });
125
+
126
+ test("destroyed without a stop, it closes them too", async () => {
127
+ const shell = make_shell();
128
+
129
+ yui_shell_open_drawer(shell, "quick");
130
+ yui_shell_show_modal(shell, "the sheet", {
131
+ dialog: true, logical_class: "TEST_STOP_SHEET", title: "settings"
132
+ });
133
+ expect(focus_trap_panel()).not.toBe(null);
134
+
135
+ gobj_destroy(shell);
136
+ await settle();
137
+
138
+ expect(focus_trap_panel()).toBe(null);
139
+ expect(document.querySelectorAll(".TEST_STOP_SHEET").length).toBe(0);
140
+ /* gobj-js says what the test did; nothing else is said. */
141
+ expect(errors().filter((m) => !m.includes("Destroying a RUNNING gobj"))).toEqual([]);
142
+ });
143
+ });
@@ -0,0 +1,50 @@
1
+ /***********************************************************************
2
+ * tabulator_edit_internals.test.js
3
+ *
4
+ * C_YUI_FORM closes a table's open cell editor before it wipes
5
+ * the rows (close_table_editor): Tabulator 6.6.0 closes it while
6
+ * it wipes them and throws. Tabulator has no public way to ask
7
+ * which cell is being edited, so the form reads its edit module:
8
+ * `table.modules.edit`, its `currentCell` and its `cancelEdit()`.
9
+ * This pins those names against the Tabulator INSTALLED: an
10
+ * upgrade that renames them fails here, not in a reader's
11
+ * reload. (The form also says so in the log when they are
12
+ * missing.)
13
+ *
14
+ * Copyright (c) 2026, ArtGins.
15
+ * All Rights Reserved.
16
+ ***********************************************************************/
17
+ import {test, expect} from "vitest";
18
+ import {install_dom_double} from "../test/dom_double.js";
19
+
20
+ install_dom_double(); /* Tabulator reads `document` when it loads */
21
+
22
+ const {EditModule} = await import("tabulator-tables");
23
+
24
+ /* Just what the module's constructor touches. */
25
+ function stub_table()
26
+ {
27
+ const registry = {register() {}};
28
+ return {
29
+ optionsList: registry,
30
+ columnManager: {optionsList: registry},
31
+ componentFunctionBinder: {bind() {}},
32
+ eventBus: {subscribe() {}},
33
+ externalEvents: {subscribe() {}},
34
+ };
35
+ }
36
+
37
+ /* A table keeps each module under its `moduleName`. */
38
+ test("the edit module is `modules.edit`", () => {
39
+ expect(EditModule.moduleName).toBe("edit");
40
+ });
41
+
42
+ test("it holds the cell being edited in `currentCell`, false when none", () => {
43
+ const edit = new EditModule(stub_table());
44
+ expect(Object.prototype.hasOwnProperty.call(edit, "currentCell")).toBe(true);
45
+ expect(edit.currentCell).toBe(false);
46
+ });
47
+
48
+ test("and closes it with `cancelEdit()`", () => {
49
+ expect(typeof EditModule.prototype.cancelEdit).toBe("function");
50
+ });
@@ -105,6 +105,7 @@ describe("the saved rect of a window is whole pixels", () => {
105
105
  expect(attrs_rect(win)).toEqual({x: 5, y: 6, width: 300, height: 200});
106
106
  const warned = logged.filter((l) => l.level === "warning" && l.msg.includes("rect_bad-rect"));
107
107
  expect(warned.length, "the bad entry is said").toBe(1);
108
+ expect(window.localStorage.getItem("rect_bad-rect"), "and forgotten: said once").toBe(null);
108
109
  expect(errors()).toEqual([]);
109
110
 
110
111
  /* No entry at all is nothing to say. */
@@ -116,6 +117,28 @@ describe("the saved rect of a window is whole pixels", () => {
116
117
  expect(logged.filter((l) => l.msg.includes("rect_none-rect"))).toEqual([]);
117
118
  });
118
119
 
120
+ /* A rect with no size -- what the box of a window that is not
121
+ * drawn answers -- is no place to open in: the window was drawn at
122
+ * the CSS minimum while its attrs said 0 x 0, and every open
123
+ * restored it again. It is said, forgotten, and the window opens
124
+ * where it would. (A rect only OFF the screen is kept: the attrs
125
+ * are the DESIRED place, clamped to the viewport on every resize,
126
+ * so a window saved on a larger screen goes back there.) */
127
+ test("an entry with no size is said once, forgotten, and the window opens where it would", () => {
128
+ window.localStorage.setItem("rect_zero-rect", JSON.stringify({x: 0, y: 0, width: 0, height: 0}));
129
+ logged.length = 0;
130
+ const win = gobj_create_service("rect_zero", "C_YUI_WINDOW", {
131
+ header: "zero", body: ["div", {}, "zero"], auto_save_size_and_position: true,
132
+ x: 5, y: 6, width: 300, height: 200,
133
+ }, yuno);
134
+ gobj_start(win);
135
+ expect(attrs_rect(win)).toEqual({x: 5, y: 6, width: 300, height: 200});
136
+ const warned = logged.filter((l) => l.level === "warning" && l.msg.includes("rect_zero-rect"));
137
+ expect(warned.length, "the empty entry is said").toBe(1);
138
+ expect(window.localStorage.getItem("rect_zero-rect")).toBe(null);
139
+ expect(errors()).toEqual([]);
140
+ });
141
+
119
142
  test("a move saves whole pixels, and writes them into the attrs", () => {
120
143
  const win = gobj_create_service("rect_move", "C_YUI_WINDOW", {
121
144
  header: "move", body: ["div", {}, "move"], auto_save_size_and_position: true,