@yuneta/gobj-ui 7.27.29 → 7.27.31

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.
@@ -77,6 +77,8 @@ import {
77
77
  gobj_read_pointer_attr,
78
78
  gobj_parent,
79
79
  gobj_subscribe_event,
80
+ gobj_unsubscribe_event,
81
+ gobj_is_destroying,
80
82
  gobj_read_attr,
81
83
  gobj_write_attr,
82
84
  gobj_read_str_attr,
@@ -102,6 +104,7 @@ import {json_text_dump} from "./json_view_helpers.js";
102
104
  import {yui_toolbar} from "./yui_toolbar.js";
103
105
  import {attach_clear} from "./yui_inputs.js";
104
106
  import {yui_copy_json, yui_button_mark_done} from "./yui_clipboard.js";
107
+ import {yui_shell_of} from "./c_yui_shell.js";
105
108
 
106
109
  import {t} from "i18next";
107
110
 
@@ -211,6 +214,7 @@ let PRIVATE_DATA = {
211
214
  $raw_body: null, // the <pre> inside it
212
215
  $search: null, // the search input
213
216
  $mode_btns: null, // Map<mode, HTMLElement> of the view switch
217
+ lang_shell: null, // the shell whose EV_LANGUAGE_CHANGED we hear
214
218
  };
215
219
 
216
220
  let __gclass__ = null;
@@ -256,6 +260,21 @@ function mt_create(gobj)
256
260
  ***************************************************************/
257
261
  function mt_start(gobj)
258
262
  {
263
+ let priv = gobj.priv;
264
+
265
+ /* The counted words ("4 instances", "show all 25", the machine's
266
+ * "3 states · 9 events") carry no key on purpose -- a key would
267
+ * make refresh_language() write the bare word over the count --
268
+ * so only a re-render changes their language. The viewer is
269
+ * created by open_gclass_view() as a service with no subscriber,
270
+ * and its host does not forward the language switch: it hears
271
+ * the shell's itself, as C_YUI_JSON does. */
272
+ let shell = yui_shell_of(gobj);
273
+ if(shell) {
274
+ gobj_subscribe_event(shell, "EV_LANGUAGE_CHANGED", {}, gobj);
275
+ priv.lang_shell = shell;
276
+ }
277
+
259
278
  render_view(gobj);
260
279
  }
261
280
 
@@ -264,6 +283,15 @@ function mt_start(gobj)
264
283
  ***************************************************************/
265
284
  function mt_stop(gobj)
266
285
  {
286
+ let priv = gobj.priv;
287
+
288
+ /* The shell it subscribed to, if it is still there: a logout may
289
+ * have destroyed it, and its subscriptions with it. */
290
+ if(priv.lang_shell && !gobj_is_destroying(priv.lang_shell)) {
291
+ gobj_unsubscribe_event(priv.lang_shell, "EV_LANGUAGE_CHANGED", {}, gobj);
292
+ }
293
+ priv.lang_shell = null;
294
+
267
295
  /*
268
296
  * The graph child dies with this gobj's RUNNING state, not with
269
297
  * its destruction: gobj_destroy() destroys the children BEFORE
@@ -1737,6 +1765,10 @@ function ac_language_changed(gobj, event, kw, src)
1737
1765
  apply_view_mode(gobj);
1738
1766
  }
1739
1767
  render_view(gobj);
1768
+ /* The machine's graph, when it is built, draws its own words. */
1769
+ if(priv.graph_gobj && gobj_is_running(priv.graph_gobj)) {
1770
+ gobj_send_event(priv.graph_gobj, "EV_LANGUAGE_CHANGED", {}, gobj);
1771
+ }
1740
1772
  return 0;
1741
1773
  }
1742
1774
 
@@ -295,7 +295,10 @@ function restore_view_state(gobj)
295
295
  done = true;
296
296
  }
297
297
  if(position !== null) {
298
- graph.translateTo(position);
298
+ /* What was saved is getPosition(), and translateTo() does
299
+ * not take it back: see translate_of_position(). */
300
+ graph.translateTo(translate_of_position(graph, position,
301
+ (zoom !== null)? zoom: graph.getZoom()));
299
302
  done = true;
300
303
  }
301
304
  if(state.anchor) {
@@ -317,14 +320,38 @@ function restore_view_state(gobj)
317
320
  return done;
318
321
  }
319
322
 
323
+ /* The translateTo() argument that puts the camera back where
324
+ * getPosition() answered `position`, at zoom `zoom` (the one the
325
+ * camera has when the translate is applied).
326
+ *
327
+ * The two are each other's inverse only at zoom 1. In G6 5.x, with C
328
+ * the canvas centre, Z the zoom and P the camera's own position,
329
+ * getPosition() answers S = C/Z - P, and an absolute translateTo(T)
330
+ * leaves P = C - T/Z. So the same camera is T = (S + C) * Z - C.
331
+ * Handing S to translateTo() as it came brought the zoom back and put
332
+ * the graph elsewhere, further on every close and open (and a camera
333
+ * saved at 0.3 was judged to show nothing, and framed again). The
334
+ * treedb graph met the same pair (c_g6_nodes_tree.js, "A camera is
335
+ * saved as a NODE at a VIEWPORT PIXEL"). */
336
+ function translate_of_position(graph, position, zoom)
337
+ {
338
+ let z = (zoom > 0)? zoom: 1;
339
+ let c = graph.getCanvasCenter();
340
+ return [
341
+ (position[0] + c[0]) * z - c[0],
342
+ (position[1] + c[1]) * z - c[1]
343
+ ];
344
+ }
345
+
320
346
  /* Would the centre of at least one card land inside the canvas under
321
347
  * this camera? `zoom` and `position` are what getZoom()/getPosition()
322
- * answer (a null one: the current). A camera absolute-translated to T
323
- * at zoom Z puts a point n of the graph at (n - C) * Z + T + C on the
324
- * canvas, C its centre -- the closed form of G6's own
325
- * getTranslateOptions(), the one yui_graph_center_on() relies on.
326
- * True when there is nothing to show or it cannot be computed: only a
327
- * camera KNOWN to show nothing is refused. */
348
+ * answer (a null one: the current). Under that camera a point n of the
349
+ * graph lands at (n + S) * Z on the canvas: from G6's closed forms
350
+ * (translate_of_position() above), v = (n - P) * Z + C with
351
+ * P = C/Z - S. NOT (n - C) * Z + S + C, which takes S for a translate
352
+ * and is right only at zoom 1. True when there is nothing to show or
353
+ * it cannot be computed: only a camera KNOWN to show nothing is
354
+ * refused. */
328
355
  function camera_shows_a_card(gobj, zoom, position)
329
356
  {
330
357
  let priv = gobj.priv;
@@ -335,9 +362,8 @@ function camera_shows_a_card(gobj, zoom, position)
335
362
  }
336
363
  try {
337
364
  let size = graph.getSize();
338
- let c = graph.getCanvasCenter();
339
365
  let z = (zoom !== null)? zoom: graph.getZoom();
340
- let t = (position !== null)? position: graph.getPosition();
366
+ let s = (position !== null)? position: graph.getPosition();
341
367
  let w = size[0];
342
368
  let h = size[1];
343
369
  if(!(w > 0 && h > 0) || !(z > 0)) {
@@ -345,8 +371,8 @@ function camera_shows_a_card(gobj, zoom, position)
345
371
  }
346
372
  for(let id of ids) {
347
373
  let n = graph.getElementPosition(id);
348
- let vx = (n[0] - c[0]) * z + t[0] + c[0];
349
- let vy = (n[1] - c[1]) * z + t[1] + c[1];
374
+ let vx = (n[0] + s[0]) * z;
375
+ let vy = (n[1] + s[1]) * z;
350
376
  if(vx >= 0 && vx <= w && vy >= 0 && vy <= h) {
351
377
  return true;
352
378
  }
@@ -470,7 +496,9 @@ let PRIVATE_DATA = {
470
496
  trace_info: {}, // gclass -> {gclass_levels, global_levels, gclass_on, global_on, error}
471
497
  trace_pending: false, // a trace was set: the block is locked until the host answers
472
498
  popover_mode: "", // what the popover shows: "node" (a card) | "yuno" (the yuno's traces)
499
+ viewer_awaits: "", // the gclass whose answer unlocks the open viewer ("" = not locked)
473
500
  viewer_stale: "", // a gclass whose open viewer must be asked again after a yuno set
501
+ set_in_flight: null, // {gclass, scope}: the set whose answer (EV_TRACE_INFO of gclass) is owed
474
502
  view_refused: false, // the saved camera showed no card: the opening's framing replaces it
475
503
  };
476
504
 
@@ -3568,6 +3596,13 @@ function open_gclass_window(gobj, gclass, description, d)
3568
3596
 
3569
3597
  close_gclass_view(priv.gclass_view);
3570
3598
  priv.gclass_view = null;
3599
+ priv.viewer_awaits = "";
3600
+ priv.viewer_stale = "";
3601
+ /* A viewer opened while a set that moves what it shows is still
3602
+ * in flight opens locked, as the one it replaces was. */
3603
+ if(priv.set_in_flight) {
3604
+ lock_viewer_for_set(gobj, gclass, priv.set_in_flight);
3605
+ }
3571
3606
 
3572
3607
  let opts = {
3573
3608
  title_prefix: is_remote(gobj)?
@@ -3657,6 +3692,9 @@ function ac_trace_info(gobj, event, kw, src)
3657
3692
  error: kw.error || "",
3658
3693
  };
3659
3694
  priv.trace_pending = false;
3695
+ if(priv.set_in_flight && priv.set_in_flight.gclass === kw.gclass) {
3696
+ priv.set_in_flight = null;
3697
+ }
3660
3698
 
3661
3699
  let d = priv.popover_node;
3662
3700
  let root = priv.remote_root;
@@ -3668,16 +3706,27 @@ function ac_trace_info(gobj, event, kw, src)
3668
3706
  let viewer = gclass_viewer_gobj(gobj);
3669
3707
  if(viewer) {
3670
3708
  let shown = gobj_read_str_attr(viewer, "gclass_name");
3709
+ /* Only the answer the viewer waits for unlocks it: an answer
3710
+ * for another gclass (another card's set) is none of its
3711
+ * business. */
3712
+ if(priv.viewer_awaits === kw.gclass) {
3713
+ priv.viewer_awaits = "";
3714
+ if(priv.viewer_stale === shown && shown !== kw.gclass) {
3715
+ /* A set of the YUNO's scope: what the viewer shows as
3716
+ * held by the yuno moved too, and the host refreshed
3717
+ * only the root's gclass. Locked until its own. */
3718
+ priv.viewer_stale = "";
3719
+ priv.viewer_awaits = shown;
3720
+ /* Owed like a set of its own: a viewer of it reopened
3721
+ * before the answer opens locked too. */
3722
+ priv.set_in_flight = {gclass: shown, scope: "gclass"};
3723
+ gobj_publish_event(gobj, "EV_TRACE_INFO_WANTED", {gclass: shown});
3724
+ }
3725
+ }
3671
3726
  if(shown === kw.gclass) {
3672
3727
  gobj_send_event(viewer, "EV_SET_TRACES", viewer_traces(gobj, shown), gobj);
3673
- } else if(priv.viewer_stale === shown) {
3674
- /* A set of the YUNO's scope: what the viewer shows as held by
3675
- * the yuno moved too, and the host refreshed only the root's
3676
- * gclass. */
3677
- gobj_publish_event(gobj, "EV_TRACE_INFO_WANTED", {gclass: shown});
3678
3728
  }
3679
3729
  }
3680
- priv.viewer_stale = "";
3681
3730
  return 0;
3682
3731
  }
3683
3732
 
@@ -3693,14 +3742,15 @@ function gclass_viewer_gobj(gobj)
3693
3742
  /************************************************************
3694
3743
  * What the gclass viewer needs to draw the traces of `gclass`
3695
3744
  * for every gobj of it: the levels the host read, and whether a
3696
- * set is still in flight. Plain json: it rides in a kw.
3745
+ * set that concerns it is still in flight. Plain json: it rides
3746
+ * in a kw.
3697
3747
  ************************************************************/
3698
3748
  function viewer_traces(gobj, gclass)
3699
3749
  {
3700
3750
  let priv = gobj.priv;
3701
3751
  return {
3702
3752
  info: priv.trace_info[gclass] || null,
3703
- pending: !!priv.trace_pending,
3753
+ pending: !!priv.viewer_awaits,
3704
3754
  };
3705
3755
  }
3706
3756
 
@@ -3805,13 +3855,19 @@ function set_trace_levels(gobj, target, scope, levels, set)
3805
3855
  priv.trace_pending = true;
3806
3856
  render_current_popover(gobj);
3807
3857
 
3858
+ /* The viewer is locked only by a set that moves what it shows:
3859
+ * one of its own gclass, or one of the yuno's scope. A gobj or
3860
+ * gclass set of ANOTHER gclass leaves it alone -- the answer that
3861
+ * ends that set is not one the viewer would ever be sent. The set
3862
+ * is remembered for a viewer opened before its answer, and only
3863
+ * the LAST one: the host overtakes an earlier set in flight. */
3864
+ priv.set_in_flight = {gclass: target.gclass, scope: scope};
3808
3865
  let viewer = gclass_viewer_gobj(gobj);
3809
3866
  if(viewer) {
3810
3867
  let shown = gobj_read_str_attr(viewer, "gclass_name");
3811
- if(scope === "global" && shown !== target.gclass) {
3812
- priv.viewer_stale = shown;
3868
+ if(lock_viewer_for_set(gobj, shown, priv.set_in_flight)) {
3869
+ gobj_send_event(viewer, "EV_SET_TRACES", viewer_traces(gobj, shown), gobj);
3813
3870
  }
3814
- gobj_send_event(viewer, "EV_SET_TRACES", viewer_traces(gobj, shown), gobj);
3815
3871
  }
3816
3872
 
3817
3873
  gobj_publish_event(gobj, "EV_SET_TRACE_LEVEL", {
@@ -3823,6 +3879,22 @@ function set_trace_levels(gobj, target, scope, levels, set)
3823
3879
  });
3824
3880
  }
3825
3881
 
3882
+ /* The viewer of `shown` waits for the answer of `set` when the set
3883
+ * moves what it shows: one of its own gclass, or one of the yuno's
3884
+ * scope (then its own levels are asked for again after the yuno's,
3885
+ * see ac_trace_info). Returns whether it was locked. */
3886
+ function lock_viewer_for_set(gobj, shown, set)
3887
+ {
3888
+ let priv = gobj.priv;
3889
+ let concerned = (shown === set.gclass) || set.scope === "global";
3890
+ if(!concerned) {
3891
+ return false;
3892
+ }
3893
+ priv.viewer_awaits = set.gclass;
3894
+ priv.viewer_stale = (shown !== set.gclass)? shown: "";
3895
+ return true;
3896
+ }
3897
+
3826
3898
  /************************************************************
3827
3899
  * The toolbar's "traces of the yuno": the popover shows the
3828
3900
  * yuno's scope, read from the answer for the root's gclass.
package/src/c_yui_node.js CHANGED
@@ -79,7 +79,9 @@
79
79
  * Only the ROOT node (the one whose parent is not a C_YUI_NODE)
80
80
  * talks to the shell: it subscribes to EV_ROUTE_CHANGED, resolves
81
81
  * the subpath and activates the branch. Inner nodes only ever
82
- * hear their parent.
82
+ * hear their parent. A route of ANOTHER stage (a secondary stage,
83
+ * SHELL.md §3) is not the root's business: the shell keeps showing
84
+ * the tree in its own stage, so the tree stays on its path.
83
85
  *
84
86
  * A path segment that names no living child is not silently
85
87
  * swallowed: it logs and the URL is rewritten (replace) to the
@@ -198,6 +200,7 @@ let PRIVATE_DATA = {
198
200
  distance: 0, /* segments from me down to the tip */
199
201
  chain: null, /* nodes from my child down to the tip */
200
202
  is_root: false,
203
+ stage: "", /* root: the shell stage that shows me ("" = not shown yet) */
201
204
  $chrome: null,
202
205
  $body: null
203
206
  };
@@ -1177,6 +1180,14 @@ function activate(gobj, subpath, inherited_depth)
1177
1180
  * `keep_alive` view, so the content may want to read again
1178
1181
  * what it read when it was created. Only a content that
1179
1182
  * declares EV_VIEW_SHOWN is told.
1183
+ *
1184
+ * This is the ONLY way it is told: a tree the shell shows
1185
+ * again comes back here too (the shell hides it only for a
1186
+ * route of its own stage, which takes the root to ST_OFF, and
1187
+ * each node on the path leaves ST_OFF with the shell's
1188
+ * EV_ROUTE_CHANGED that shows it again), so the node
1189
+ * does not declare EV_VIEW_SHOWN and the shell does not send
1190
+ * the tree a second one.
1180
1191
  ************************************************************/
1181
1192
  function tell_content_shown(gobj)
1182
1193
  {
@@ -1296,17 +1307,25 @@ function notify_tree_changed(gobj)
1296
1307
  ************************************************************/
1297
1308
  function ac_route_changed(gobj, event, kw, src)
1298
1309
  {
1310
+ let priv = gobj.priv;
1299
1311
  let base = kw.base || "";
1300
1312
  let my_base = gobj_read_attr(gobj, "base_route");
1313
+ let stage = kw.stage || "main";
1301
1314
 
1302
1315
  if(base !== my_base) {
1303
- /* Another view owns the URL now. A keep_alive tree stays
1316
+ /* A route of ANOTHER stage changes what that stage shows, not
1317
+ * mine: the shell keeps showing me, so I stay on my path. */
1318
+ if(priv.stage && stage !== priv.stage) {
1319
+ return 0;
1320
+ }
1321
+ /* Another view owns my stage now. A keep_alive tree stays
1304
1322
  * created but must not keep painting a stale branch. */
1305
1323
  deactivate(gobj);
1306
1324
  gobj_change_state(gobj, "ST_OFF");
1307
1325
  return 0;
1308
1326
  }
1309
1327
 
1328
+ priv.stage = stage;
1310
1329
  gobj_change_state(gobj, activate(gobj, kw.subpath || "", null));
1311
1330
  return 0;
1312
1331
  }
@@ -1320,24 +1339,6 @@ function ac_activate(gobj, event, kw, src)
1320
1339
  return 0;
1321
1340
  }
1322
1341
 
1323
- /************************************************************
1324
- * The shell (or my parent) shows the tree again after hiding
1325
- * it: down the path to the node on screen, which tells its
1326
- * content.
1327
- ************************************************************/
1328
- function ac_view_shown(gobj, event, kw, src)
1329
- {
1330
- let priv = gobj.priv;
1331
- if(gobj_current_state(gobj) === "ST_CHILD") {
1332
- if(priv.active_child) {
1333
- gobj_send_event(priv.active_child, event, kw, gobj);
1334
- }
1335
- return 0;
1336
- }
1337
- tell_content_shown(gobj);
1338
- return 0;
1339
- }
1340
-
1341
1342
  /************************************************************
1342
1343
  * My parent took me off the path.
1343
1344
  ************************************************************/
@@ -1855,7 +1856,6 @@ function create_gclass(gclass_name)
1855
1856
  ["EV_SET_NAV_MODE", ac_set_nav_mode, null],
1856
1857
  ["EV_SET_CONTENT", ac_set_content, null],
1857
1858
  ["EV_NODE_TREE_CHANGED", ac_tree_changed, null],
1858
- ["EV_VIEW_SHOWN", ac_view_shown, null],
1859
1859
  ["EV_DESTROY_NAVS", ac_destroy_navs, null]
1860
1860
  ]],
1861
1861
  ["ST_CHILD", [
@@ -1872,7 +1872,6 @@ function create_gclass(gclass_name)
1872
1872
  ["EV_SET_NAV_MODE", ac_set_nav_mode, null],
1873
1873
  ["EV_SET_CONTENT", ac_set_content, null],
1874
1874
  ["EV_NODE_TREE_CHANGED", ac_tree_changed, null],
1875
- ["EV_VIEW_SHOWN", ac_view_shown, null],
1876
1875
  ["EV_DESTROY_NAVS", ac_destroy_navs, null]
1877
1876
  ]]
1878
1877
  ];
@@ -1894,8 +1893,6 @@ function create_gclass(gclass_name)
1894
1893
  ["EV_SET_NAV_MODE", 0],
1895
1894
  ["EV_SET_CONTENT", 0],
1896
1895
  ["EV_NODE_TREE_CHANGED", 0],
1897
- /* the shell or the parent shows the tree again (keep_alive) */
1898
- ["EV_VIEW_SHOWN", 0],
1899
1896
  /* internal: posted to itself, the navs a render left */
1900
1897
  ["EV_DESTROY_NAVS", 0]
1901
1898
  ];
@@ -42,6 +42,13 @@
42
42
  * next login) in document.body. It follows the shell across a
43
43
  * logout (yui_shell_watch_changes()), as the dock does.
44
44
  *
45
+ * Its language: it hears the live shell's EV_LANGUAGE_CHANGED
46
+ * (yui_shell_language_changed()). With NO shell there is nobody
47
+ * to publish that, so an app whose login switches language
48
+ * calls yui_update_watch_language_changed() after switching its
49
+ * i18next -- it sends EV_LANGUAGE_CHANGED to every live watch.
50
+ * Calling it with a shell up too is harmless.
51
+ *
45
52
  * Usage (an app, once, after creating the yuno):
46
53
  *
47
54
  * register_c_yui_update_watch();
@@ -53,12 +60,21 @@
53
60
  * // on its backend's EV_ON_OPEN:
54
61
  * gobj_send_event(watch, "EV_CHECK_BUILD", {}, gobj);
55
62
  *
63
+ * // the login's language switch (no shell yet):
64
+ * switch_locale(locale);
65
+ * yui_update_watch_language_changed();
66
+ *
56
67
  * Input events:
57
68
  * EV_CHECK_BUILD {force?} ask the server (force skips
58
69
  * the minimum interval)
59
70
  * EV_RELOAD {} the bar's reload button
60
71
  * EV_DISMISS {} the bar's ✕: hidden for THIS
61
72
  * build, a later one shows it
73
+ * EV_LANGUAGE_CHANGED {} the app's language changed:
74
+ * the bar is re-translated
75
+ * (the live shell publishes it;
76
+ * with no shell,
77
+ * yui_update_watch_language_changed())
62
78
  *
63
79
  * Output events (optional subscribers):
64
80
  * EV_NEW_BUILD {running, deployed, version}
@@ -144,6 +160,11 @@ let PRIVATE_DATA = {
144
160
 
145
161
  let __gclass__ = null;
146
162
 
163
+ /* The watches alive (mt_create .. mt_destroy): what
164
+ * yui_update_watch_language_changed() reaches when no shell can
165
+ * publish the language switch. */
166
+ const __live_watches__ = [];
167
+
147
168
 
148
169
 
149
170
 
@@ -163,6 +184,7 @@ function mt_create(gobj)
163
184
 
164
185
  priv.running = running_build(typeof document !== "undefined" ? document : null);
165
186
  priv.dom_handlers = [];
187
+ __live_watches__.push(gobj);
166
188
 
167
189
  /*
168
190
  * Create children
@@ -247,6 +269,10 @@ function mt_destroy(gobj)
247
269
  {
248
270
  let priv = gobj.priv;
249
271
 
272
+ let idx = __live_watches__.indexOf(gobj);
273
+ if(idx >= 0) {
274
+ __live_watches__.splice(idx, 1);
275
+ }
250
276
  yui_shell_unwatch_changes(gobj);
251
277
  if(priv.lang_shell) {
252
278
  if(!gobj_is_destroying(priv.lang_shell)) {
@@ -719,4 +745,25 @@ function register_c_yui_update_watch()
719
745
  return create_gclass(GCLASS_NAME);
720
746
  }
721
747
 
722
- export { register_c_yui_update_watch };
748
+ /***************************************************************
749
+ * The app switched its language (call it AFTER switching the
750
+ * i18next): every live watch re-translates its bar.
751
+ *
752
+ * For the screens with no shell -- the login, the gap between a
753
+ * logout and the next login -- where nobody publishes the
754
+ * shell's EV_LANGUAGE_CHANGED that the watch hears otherwise.
755
+ * It is an EVENT to each watch, so the switch is in its
756
+ * machine trace. `src`: the gobj that switched, if the caller
757
+ * has one.
758
+ ***************************************************************/
759
+ function yui_update_watch_language_changed(src)
760
+ {
761
+ for(let watch of __live_watches__.slice()) {
762
+ if(gobj_is_destroying(watch)) {
763
+ continue;
764
+ }
765
+ gobj_send_event(watch, "EV_LANGUAGE_CHANGED", {}, src || watch);
766
+ }
767
+ }
768
+
769
+ export { register_c_yui_update_watch, yui_update_watch_language_changed };
@@ -5,8 +5,11 @@
5
5
  * with the server played by a fake fetch: the bar appears for
6
6
  * another build and not for the same one, the ✕ hides it for THAT
7
7
  * build only, a rollback takes it away, a check too soon or
8
- * offline asks nothing, a failure is said once, and the reload
9
- * asks the host first.
8
+ * offline asks nothing (navigator.onLine is stubbed: vitest has
9
+ * none, and the guard never ran) and coming back online asks at
10
+ * once, a failure is said once, the reload asks the host first,
11
+ * and with no shell (the login) the app's language switch reaches
12
+ * the bar through yui_update_watch_language_changed().
10
13
  *
11
14
  * Copyright (c) 2026, ArtGins.
12
15
  * All Rights Reserved.
@@ -22,7 +25,8 @@ const {
22
25
  gobj_destroy, gobj_is_running, gobj_send_event, gobj_current_state,
23
26
  set_log_callback, register_c_timer,
24
27
  } = await import("@yuneta/gobj-js");
25
- const {register_c_yui_update_watch} = await import("./c_yui_update_watch.js");
28
+ const {register_c_yui_update_watch, yui_update_watch_language_changed} =
29
+ await import("./c_yui_update_watch.js");
26
30
 
27
31
  const RUNNING = "aaaaaaaaaaaaaaaa";
28
32
 
@@ -32,6 +36,15 @@ let yuno = null;
32
36
  let host = null;
33
37
  let watch = null;
34
38
  let seq = 0;
39
+ let i18next = null;
40
+
41
+ /* navigator.onLine as the browser says it: vitest has no such
42
+ * property, so `undefined` unless a test sets it. */
43
+ const network = {online: undefined};
44
+ Object.defineProperty(globalThis.navigator, "onLine", {
45
+ configurable: true,
46
+ get: () => network.online,
47
+ });
35
48
 
36
49
  /* The server: what the next fetches answer, and what was asked. */
37
50
  const server = {
@@ -85,8 +98,14 @@ function make_watch(kw)
85
98
  }
86
99
 
87
100
  beforeAll(async () => {
88
- const i18next = (await import("i18next")).default;
89
- await i18next.init({lng: "en", resources: {}});
101
+ i18next = (await import("i18next")).default;
102
+ await i18next.init({lng: "en", resources: {
103
+ es: {translation: {
104
+ "new version available": "hay una versión nueva",
105
+ "reload": "recargar",
106
+ "close": "cerrar",
107
+ }},
108
+ }});
90
109
 
91
110
  const $meta = document.createElement("meta");
92
111
  $meta.setAttribute("name", "yuneta-build");
@@ -131,6 +150,7 @@ beforeEach(() => {
131
150
  server.version = "1.0.0";
132
151
  server.fail = null;
133
152
  server.asked = 0;
153
+ network.online = undefined;
134
154
  });
135
155
 
136
156
  afterEach(() => {
@@ -239,6 +259,61 @@ describe("C_YUI_UPDATE_WATCH", () => {
239
259
  expect(server.asked).toBeGreaterThanOrEqual(3);
240
260
  });
241
261
 
262
+ test("offline asks nothing, not even forced", async () => {
263
+ network.online = false;
264
+ make_watch({min_interval_ms: 0});
265
+ await settle();
266
+ gobj_send_event(watch, "EV_CHECK_BUILD", {force: true}, watch);
267
+ window.dispatchEvent(fake_event("focus"));
268
+ await settle();
269
+ expect(server.asked).toBe(0);
270
+ });
271
+
272
+ test("back online asks at once, inside min_interval_ms", async () => {
273
+ network.online = true;
274
+ make_watch({min_interval_ms: 60000});
275
+ await settle();
276
+ expect(server.asked).toBe(1);
277
+ window.dispatchEvent(fake_event("focus"));
278
+ await settle();
279
+ expect(server.asked).toBe(1);
280
+
281
+ network.online = false;
282
+ gobj_send_event(watch, "EV_CHECK_BUILD", {force: true}, watch);
283
+ await settle();
284
+ expect(server.asked).toBe(1);
285
+
286
+ network.online = true;
287
+ window.dispatchEvent(fake_event("online"));
288
+ await settle();
289
+ expect(server.asked).toBe(2);
290
+ });
291
+
292
+ test("with no shell, the app's language switch re-translates the bar", async () => {
293
+ server.stamp = "bbbbbbbbbbbbbbbb";
294
+ make_watch();
295
+ await settle();
296
+ const $bar = bar();
297
+ const $reload = $bar.querySelector(".UPDATE_RELOAD");
298
+ expect($reload.getAttribute("aria-label")).toBe("reload");
299
+
300
+ /* The texts carry their key; the document double's text nodes
301
+ * have no nodeValue for refresh_language() to write, so what it
302
+ * rewrites is read from the attributes. */
303
+ expect($bar.querySelector(".UPDATE_TEXT").getAttribute("data-i18n")).toBe("new version available");
304
+ await i18next.changeLanguage("es");
305
+ try {
306
+ expect($reload.getAttribute("aria-label")).toBe("reload");
307
+ yui_update_watch_language_changed();
308
+ expect([$reload.getAttribute("title"), $reload.getAttribute("aria-label")])
309
+ .toEqual(["recargar", "recargar"]);
310
+ expect($bar.querySelector(".UPDATE_DISMISS").getAttribute("aria-label")).toBe("cerrar");
311
+ expect($bar.querySelector(".UPDATE_VERSION").textContent).toBe("1.0.0");
312
+ } finally {
313
+ await i18next.changeLanguage("en");
314
+ }
315
+ });
316
+
242
317
  test("stopped, the page events ask nothing", async () => {
243
318
  make_watch({min_interval_ms: 0});
244
319
  await settle();