@yuneta/gobj-ui 7.26.5 → 7.26.7

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.
@@ -66,12 +66,19 @@ import {
66
66
  import {yui_shell_show_error, yui_shell_show_modal, yui_shell_popup_layer} from "./shell_modals.js";
67
67
  import {
68
68
  yui_shell_of,
69
+ yui_shell_icons_changed,
69
70
  yui_shell_set_sub_routes,
70
71
  yui_shell_navigate,
71
72
  yui_shell_last_route_under,
72
73
  yui_shell_previous_route,
73
74
  } from "./c_yui_shell.js";
74
75
  import {nodes_answer} from "./nodes_answer.js";
76
+ import {
77
+ ICONS_TOPIC,
78
+ yui_icons_set_user,
79
+ yui_icons_put_user,
80
+ yui_icons_remove_user
81
+ } from "./lib_icons.js";
75
82
  import {yui_toolbar_icon} from "./yui_toolbar.js";
76
83
  import {
77
84
  desc_pkey2s, desc_tkey, col_key_roles, system_flag_names
@@ -1255,6 +1262,56 @@ function request_treedb_descs(gobj)
1255
1262
  }
1256
1263
  }
1257
1264
 
1265
+ /************************************************************
1266
+ * A user view shows the topics of the user and, of the system
1267
+ * topics, only __icons__: its nodes are what the user ADDS to
1268
+ * the app (the icons a column `icon` can name), so editing them
1269
+ * is the user's business. The other `__` topics are the
1270
+ * treedb's own bookkeeping.
1271
+ ************************************************************/
1272
+ function topic_is_shown(topic_name)
1273
+ {
1274
+ return topic_name.substring(0, 2) !== "__" || topic_name === ICONS_TOPIC;
1275
+ }
1276
+
1277
+ /************************************************************
1278
+ * The nodes of __icons__ are the user icons of the app: each
1279
+ * load and each write of them goes to the icon registry
1280
+ * (lib_icons.js), and the shell tells the views that decided
1281
+ * at render time whether a name was an icon.
1282
+ *
1283
+ * how: "load" (nodes = the whole topic), "put" (one node
1284
+ * created or updated) or "remove" (one node deleted).
1285
+ ************************************************************/
1286
+ function feed_user_icons(gobj, how, nodes)
1287
+ {
1288
+ switch(how) {
1289
+ case "load":
1290
+ yui_icons_set_user(nodes);
1291
+ break;
1292
+ case "put":
1293
+ for(const node of nodes) {
1294
+ yui_icons_put_user(node);
1295
+ }
1296
+ break;
1297
+ case "remove":
1298
+ for(const node of nodes) {
1299
+ if(node && typeof node.id === "string") {
1300
+ yui_icons_remove_user(node.id);
1301
+ }
1302
+ }
1303
+ break;
1304
+ default:
1305
+ log_error(`${gobj_short_name(gobj)}: feed_user_icons(): unknown '${how}'`);
1306
+ return;
1307
+ }
1308
+
1309
+ let shell = yui_shell_of(gobj);
1310
+ if(shell) {
1311
+ yui_shell_icons_changed(shell);
1312
+ }
1313
+ }
1314
+
1258
1315
  /************************************************************
1259
1316
  * Response of remote command
1260
1317
  ************************************************************/
@@ -1271,11 +1328,8 @@ function process_treedb_descs(gobj)
1271
1328
  let treedb_name = gobj_read_str_attr(gobj, "treedb_name");
1272
1329
  let readonly = gobj_read_bool_attr(gobj, "readonly");
1273
1330
  for(const [key, desc] of Object.entries(descs)) {
1274
- if(system) {
1275
- } else {
1276
- if(key.substring(0, 2) === "__") {
1277
- continue;
1278
- }
1331
+ if(!system && !topic_is_shown(key)) {
1332
+ continue;
1279
1333
  }
1280
1334
 
1281
1335
  let kw_topic_form = {
@@ -1292,7 +1346,11 @@ function process_treedb_descs(gobj)
1292
1346
  treedb_name: treedb_name,
1293
1347
  topic_name: key,
1294
1348
  desc: desc,
1295
- with_remote_paging: gobj_read_bool_attr(gobj, "with_remote_paging"),
1349
+ /* __icons__ is read WHOLE, never by pages: its nodes are the
1350
+ * user icons of the app (feed_user_icons), and a page of them
1351
+ * would leave the rest of the icons undrawn. */
1352
+ with_remote_paging: key !== ICONS_TOPIC &&
1353
+ gobj_read_bool_attr(gobj, "with_remote_paging"),
1296
1354
  page_size: gobj_read_integer_attr(gobj, "page_size"),
1297
1355
  with_selection_bar: gobj_read_bool_attr(gobj, "with_selection_bar"),
1298
1356
  /* This host answers every write of the form (see
@@ -1402,7 +1460,7 @@ function register_sub_routes(gobj)
1402
1460
  }
1403
1461
  if(is_object(descs)) {
1404
1462
  for(let topic of Object.keys(descs)) {
1405
- if(!system && topic.substring(0, 2) === "__") {
1463
+ if(!system && !topic_is_shown(topic)) {
1406
1464
  continue;
1407
1465
  }
1408
1466
  nodes.push({
@@ -2503,6 +2561,9 @@ function ac_mt_command_answer(gobj, event, kw, src)
2503
2561
  );
2504
2562
  } else {
2505
2563
  forget_topic_rereads(gobj, topic_name);
2564
+ if(topic_name === ICONS_TOPIC) {
2565
+ feed_user_icons(gobj, "load", answer.rows);
2566
+ }
2506
2567
  gobj_send_event(
2507
2568
  gobj_topic_form,
2508
2569
  "EV_LOAD_NODES",
@@ -2914,6 +2975,9 @@ function ac_treedb_node_created(gobj, event, kw, src)
2914
2975
  let node = kw_get_dict_value(gobj, kw, "node", null, 0);
2915
2976
 
2916
2977
  if(treedb_name === gobj_read_str_attr(gobj, "treedb_name")) {
2978
+ if(topic_name === ICONS_TOPIC) {
2979
+ feed_user_icons(gobj, "put", [node]);
2980
+ }
2917
2981
  let gobj_formtable = get_gobj_formtable(gobj, topic_name);
2918
2982
  gobj_send_event(
2919
2983
  gobj_formtable,
@@ -2936,6 +3000,9 @@ function ac_treedb_node_updated(gobj, event, kw, src)
2936
3000
  let node = kw_get_dict_value(gobj, kw, "node", null, 0);
2937
3001
 
2938
3002
  if(treedb_name === gobj_read_str_attr(gobj, "treedb_name")) {
3003
+ if(topic_name === ICONS_TOPIC) {
3004
+ feed_user_icons(gobj, "put", [node]);
3005
+ }
2939
3006
  let gobj_formtable = get_gobj_formtable(gobj, topic_name);
2940
3007
  gobj_send_event(
2941
3008
  gobj_formtable,
@@ -2988,6 +3055,9 @@ function ac_treedb_node_deleted(gobj, event, kw, src)
2988
3055
  let node = kw_get_dict_value(gobj, kw, "node", null, 0);
2989
3056
 
2990
3057
  if(treedb_name === gobj_read_str_attr(gobj, "treedb_name")) {
3058
+ if(topic_name === ICONS_TOPIC) {
3059
+ feed_user_icons(gobj, "remove", [node]);
3060
+ }
2991
3061
  let gobj_formtable = get_gobj_formtable(gobj, topic_name);
2992
3062
  gobj_send_event(
2993
3063
  gobj_formtable,
@@ -211,7 +211,7 @@ function build_ui(gobj)
211
211
  }
212
212
  ]
213
213
  );
214
- gobj_write_bool_attr(gobj, "$container", $container);
214
+ gobj_write_attr(gobj, "$container", $container);
215
215
  gobj_write_bool_attr(gobj, "own_container", true);
216
216
  }
217
217
 
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Window - position fixed
5
5
  *
6
- * Copyright (c) 2025, ArtGins.
6
+ * Copyright (c) 2025-2026, ArtGins.
7
7
  * All Rights Reserved.
8
8
  ***********************************************************************/
9
9
 
@@ -14,6 +14,7 @@ import {
14
14
  gclass_create,
15
15
  event_flag_t,
16
16
  log_error,
17
+ log_warning,
17
18
  gobj_read_pointer_attr,
18
19
  gobj_subscribe_event,
19
20
  gobj_write_attr,
@@ -102,7 +103,7 @@ SDATA(data_type_t.DTP_STRING, "logical_class",0, "", "Logical UPPER_SNAKE
102
103
  SDATA(data_type_t.DTP_STRING, "title_prefix", 0, "", "Optional DATA half of the title (a topic/service/marker name), shown before `title` and never translated. Use it instead of composing `${name} · ${t(key)}` into `title`, which cannot re-translate"),
103
104
  SDATA(data_type_t.DTP_STRING, "title", 0, "", "Window title, an i18n KEY: painted in the title bar (unless `header` overrides it) and on the dock chip. Pass the key, not t(key), or it cannot re-translate"),
104
105
  SDATA(data_type_t.DTP_STRING, "icon", 0, "", "Window icon (by window type), leading the title bar and the dock chip: a yi-* class name or inline SVG"),
105
- // TODO pendiente focus modal keyboard
106
+ // TODO declared, NOT implemented yet: focus, modal, keyboard (Escape does not close a window)
106
107
  SDATA(data_type_t.DTP_POINTER, "focus", 0, null, "Brings focus to the element, can be a number or selector"),
107
108
  SDATA(data_type_t.DTP_BOOLEAN, "modal", 0, false, "Enable modal mode"),
108
109
  SDATA(data_type_t.DTP_BOOLEAN, "keyboard", 0, true, "Close window on ESC if not modal"),
@@ -188,15 +189,7 @@ function mt_create(gobj)
188
189
  * is also why a navigation panel prefers a manager, and falls
189
190
  * back to `keep_on_navigate` when the app has no dock.
190
191
  * Retired in mt_destroy (covers every teardown path). */
191
- let shell = yui_shell_of(gobj);
192
- if(shell) {
193
- let overlay = yui_shell_register_overlay(
194
- shell,
195
- function() { close_window(gobj); },
196
- {keep_on_navigate: gobj_read_bool_attr(gobj, "keep_on_navigate")}
197
- );
198
- gobj_write_attr(gobj, "back_overlay", overlay);
199
- }
192
+ register_back_overlay(gobj);
200
193
  }
201
194
 
202
195
  /* Keep the window inside the viewport on a breakpoint change.
@@ -347,7 +340,7 @@ function build_default_header(gobj)
347
340
  if(!empty_string(prefix)) {
348
341
  items.push(
349
342
  ['span', {class: 'WINDOW_TITLE_PREFIX has-text-weight-semibold'},
350
- prefix]
343
+ document.createTextNode(String(prefix))] // data, not markup
351
344
  );
352
345
  }
353
346
  if(!empty_string(title)) {
@@ -400,7 +393,11 @@ function resolve_manager(gobj)
400
393
  return null;
401
394
  }
402
395
  if(typeof m === "string") {
403
- m = gobj_find_service(m, false) || null;
396
+ let name = m;
397
+ m = gobj_find_service(name, false) || null;
398
+ if(!m) {
399
+ log_warning(`${GCLASS_NAME}: window manager '${name}' not found: no dock, no minimize`);
400
+ }
404
401
  gobj_write_attr(gobj, "manager", m);
405
402
  }
406
403
  return m;
@@ -550,7 +547,9 @@ function build_ui(gobj)
550
547
  * Minimize (to dock)
551
548
  *----------------------------*/
552
549
  ['button', {
553
- class: 'WINDOW_MIN yui-wc wc-min', type: 'button', 'aria-label': t('minimize'), 'data-i18n-aria-label': 'minimize',
550
+ class: 'WINDOW_MIN yui-wc wc-min', type: 'button',
551
+ title: t('minimize'), 'data-i18n-title': 'minimize',
552
+ 'aria-label': t('minimize'), 'data-i18n-aria-label': 'minimize',
554
553
  style: (gobj_read_bool_attr(gobj, "showMin") &&
555
554
  gobj_read_pointer_attr(gobj, "manager")) ? '' : 'display:none;',
556
555
  }, WC_MIN, {
@@ -563,7 +562,9 @@ function build_ui(gobj)
563
562
  * Maximize / restore
564
563
  *----------------------------*/
565
564
  ['button', {
566
- class: 'WINDOW_MAX yui-wc wc-max', type: 'button', 'aria-label': t('maximize'), 'data-i18n-aria-label': 'maximize',
565
+ class: 'WINDOW_MAX yui-wc wc-max', type: 'button',
566
+ title: t('maximize'), 'data-i18n-title': 'maximize',
567
+ 'aria-label': t('maximize'), 'data-i18n-aria-label': 'maximize',
567
568
  style: gobj_read_bool_attr(gobj, "showMax") ? '' : 'display:none;',
568
569
  }, WC_MAX, {
569
570
  click: (evt) => {
@@ -575,11 +576,13 @@ function build_ui(gobj)
575
576
  * Close
576
577
  *----------------------------*/
577
578
  ['button', {
578
- class: 'WINDOW_CLOSE yui-wc wc-close', type: 'button', 'aria-label': t('close'), 'data-i18n-aria-label': 'close',
579
+ class: 'WINDOW_CLOSE yui-wc wc-close', type: 'button',
580
+ title: t('close'), 'data-i18n-title': 'close',
581
+ 'aria-label': t('close'), 'data-i18n-aria-label': 'close',
579
582
  }, WC_CLOSE, {
580
583
  click: (evt) => {
581
584
  evt.stopPropagation();
582
- close_window(gobj);
585
+ gobj_send_event(gobj, "EV_CLOSE_WINDOW", {}, gobj);
583
586
  }
584
587
  }]
585
588
  ]]
@@ -752,6 +755,37 @@ function destroy_ui(gobj)
752
755
  }
753
756
  }
754
757
 
758
+ /************************************************************
759
+ * Register this floating window in the shell's overlay history,
760
+ * so the browser Back closes it.
761
+ *
762
+ * The shell takes the entry off its stack BEFORE it calls the
763
+ * close function (Back, or a route change draining overlays). When
764
+ * a subscriber of EV_WINDOW_TO_CLOSE vetoes the close, the window
765
+ * stays up with no entry: Back would no longer close it, and it
766
+ * would float over the next views. So a window that survives its
767
+ * own close function registers again.
768
+ ************************************************************/
769
+ function register_back_overlay(gobj)
770
+ {
771
+ let shell = yui_shell_of(gobj);
772
+ if(!shell) {
773
+ return;
774
+ }
775
+ let overlay = yui_shell_register_overlay(
776
+ shell,
777
+ function() {
778
+ gobj_write_attr(gobj, "back_overlay", null);
779
+ close_window(gobj);
780
+ if(!gobj_is_destroying(gobj) && !gobj_read_attr(gobj, "back_overlay")) {
781
+ register_back_overlay(gobj);
782
+ }
783
+ },
784
+ {keep_on_navigate: gobj_read_bool_attr(gobj, "keep_on_navigate")}
785
+ );
786
+ gobj_write_attr(gobj, "back_overlay", overlay);
787
+ }
788
+
755
789
  /************************************************************
756
790
  *
757
791
  ************************************************************/
@@ -780,14 +814,22 @@ function close_window(gobj)
780
814
  yui_shell_of(gobj), kw_close.warning,
781
815
  {t: t, yes_label: "yes", no_label: "no", cancel_label: "cancel"}
782
816
  ).then(function(answer) {
783
- if(answer === "yes") {
784
- if(on_close) {
785
- on_close();
786
- }
817
+ if(answer !== "yes") {
818
+ return;
819
+ }
820
+ /* The question was asynchronous: the window may have been
821
+ * torn down by another path while it was open. */
822
+ if(gobj_is_destroying(gobj)) {
823
+ return;
824
+ }
825
+ if(on_close) {
826
+ on_close();
827
+ }
828
+ if(gobj_is_running(gobj)) {
787
829
  gobj_stop(gobj);
788
- gobj_stop_children(gobj);
789
- gobj_destroy(gobj);
790
830
  }
831
+ gobj_stop_children(gobj);
832
+ gobj_destroy(gobj);
791
833
  });
792
834
  }
793
835
  }
@@ -841,8 +883,12 @@ function set_max_icon(gobj, maximized)
841
883
  }
842
884
  let $btn = $container.querySelector('.wc-max');
843
885
  if($btn) {
886
+ let key = maximized ? 'restore' : 'maximize';
844
887
  $btn.innerHTML = maximized ? WC_RESTORE : WC_MAX;
845
- $btn.setAttribute('aria-label', maximized ? 'restore' : 'maximize');
888
+ $btn.setAttribute('title', t(key));
889
+ $btn.setAttribute('data-i18n-title', key);
890
+ $btn.setAttribute('aria-label', t(key));
891
+ $btn.setAttribute('data-i18n-aria-label', key);
846
892
  }
847
893
  }
848
894
 
@@ -469,7 +469,8 @@ function ac_register_window(gobj, event, kw, src)
469
469
  let prefix = kw.title_prefix || "";
470
470
  let label_items = [];
471
471
  if(prefix) {
472
- label_items.push(['span', {class: 'yui-dock-label-prefix'}, prefix]);
472
+ label_items.push(['span', {class: 'yui-dock-label-prefix'},
473
+ document.createTextNode(String(prefix))]); // data, not markup
473
474
  }
474
475
  if(title_key) {
475
476
  label_items.push(
@@ -479,7 +480,8 @@ function ac_register_window(gobj, event, kw, src)
479
480
  }
480
481
  if(label_items.length === 0) {
481
482
  /* No split halves supplied (legacy caller): plain text. */
482
- label_items.push(['span', {class: 'yui-dock-label-kind'}, title]);
483
+ label_items.push(['span', {class: 'yui-dock-label-kind'},
484
+ document.createTextNode(String(title))]); // data, not markup
483
485
  }
484
486
 
485
487
  let entry = {gobj: win, $chip: null, minimized: false};
@@ -496,7 +498,9 @@ function ac_register_window(gobj, event, kw, src)
496
498
  ), [
497
499
  chip_lead(kw.icon),
498
500
  ['span', {class: 'yui-dock-label'}, label_items],
499
- ['button', {class: 'yui-dock-close', type: 'button', 'aria-label': t('close'), 'data-i18n-aria-label': 'close'}, WC_X, {
501
+ ['button', {class: 'yui-dock-close', type: 'button',
502
+ title: t('close'), 'data-i18n-title': 'close',
503
+ 'aria-label': t('close'), 'data-i18n-aria-label': 'close'}, WC_X, {
500
504
  click: (evt) => {
501
505
  evt.stopPropagation();
502
506
  gobj_send_event(entry.gobj, "EV_CLOSE_WINDOW", {}, gobj);
@@ -156,6 +156,8 @@ function mt_destroy(gobj)
156
156
  ************************************************************/
157
157
  function build_ui(gobj)
158
158
  {
159
+ let back_label = gobj_read_attr(gobj, "back_label");
160
+ let next_label = gobj_read_attr(gobj, "next_label");
159
161
  let $container = createElement2(
160
162
  ['div', {class: 'C_YUI_WIZARD', style: 'height:100%; display:flex; flex-direction:column;'}, [
161
163
  ['div', {class: 'yui-wizard-header is-flex is-align-items-center is-flex-grow-0',
@@ -168,17 +170,21 @@ function build_ui(gobj)
168
170
  ],
169
171
  ['div', {class: 'yui-wizard-footer is-flex is-justify-content-space-between is-flex-grow-0',
170
172
  style: 'gap:.5rem; padding:.5rem;'}, [
171
- ['button', {class: 'yui-wizard-back button', 'aria-label': t('back'), 'data-i18n-aria-label': 'back'}, [
173
+ ['button', {class: 'yui-wizard-back button',
174
+ title: t(back_label), 'data-i18n-title': back_label,
175
+ 'aria-label': t(back_label), 'data-i18n-aria-label': back_label}, [
172
176
  ['span', {class: 'icon'}, ['i', {class: 'yi-arrow-left'}]],
173
- ['span', {class: 'is-hidden-mobile', i18n: 'back'}, 'back']
177
+ ['span', {class: 'is-hidden-mobile', i18n: back_label}, back_label]
174
178
  ], {
175
179
  click: function(evt) {
176
180
  evt.stopPropagation();
177
181
  gobj_send_event(gobj, "EV_PREV", {}, gobj);
178
182
  }
179
183
  }],
180
- ['button', {class: 'yui-wizard-primary button is-link', 'aria-label': t('next'), 'data-i18n-aria-label': 'next'}, [
181
- ['span', {class: 'yui-wizard-primary-label', i18n: 'next'}, 'next']
184
+ ['button', {class: 'yui-wizard-primary button is-link',
185
+ title: t(next_label), 'data-i18n-title': next_label,
186
+ 'aria-label': t(next_label), 'data-i18n-aria-label': next_label}, [
187
+ ['span', {class: 'yui-wizard-primary-label', i18n: next_label}, next_label]
182
188
  ], {
183
189
  click: function(evt) {
184
190
  evt.stopPropagation();
@@ -243,10 +249,15 @@ function render_chrome(gobj)
243
249
  let $title = $container.querySelector('.yui-wizard-title');
244
250
  let $counter = $container.querySelector('.yui-wizard-counter');
245
251
  let $back = $container.querySelector('.yui-wizard-back');
252
+ let $primary = $container.querySelector('.yui-wizard-primary');
246
253
  let $plabel = $container.querySelector('.yui-wizard-primary-label');
247
254
 
248
- $title.setAttribute('i18n', model.title);
249
- $title.textContent = model.title;
255
+ /* render_chrome runs on every step change, AFTER the host's one-time
256
+ * refresh_language($container,t): translate HERE, and stamp the
257
+ * canonical `data-i18n` (a bare `i18n` attribute is not read by
258
+ * refresh_language) so a later language switch re-translates it. */
259
+ $title.setAttribute('data-i18n', model.title);
260
+ $title.textContent = t(model.title);
250
261
 
251
262
  if(model.count > 0) {
252
263
  $counter.textContent = `${model.idx + 1} / ${model.count}`;
@@ -256,8 +267,12 @@ function render_chrome(gobj)
256
267
 
257
268
  $back.classList.toggle('is-hidden', !model.show_back);
258
269
 
259
- $plabel.setAttribute('i18n', model.primary_label);
260
- $plabel.textContent = model.primary_label;
270
+ $plabel.setAttribute('data-i18n', model.primary_label);
271
+ $plabel.textContent = t(model.primary_label);
272
+ $primary.setAttribute('title', t(model.primary_label));
273
+ $primary.setAttribute('data-i18n-title', model.primary_label);
274
+ $primary.setAttribute('aria-label', t(model.primary_label));
275
+ $primary.setAttribute('data-i18n-aria-label', model.primary_label);
261
276
 
262
277
  /*
263
278
  * Show only the current step
@@ -78,9 +78,11 @@ function delete_impact(desc, records)
78
78
  if(n === 0) {
79
79
  continue;
80
80
  }
81
- /* A column can be BOTH (a department's `users` is a hook to its
82
- * users and a fkey to the department that manages it). Counting
83
- * it on both sides is right: the delete does both things. */
81
+ /* A column is a hook OR a fkey: the treedb refuses one flagged
82
+ * both ("A column cannot be both 'hook' and 'fkey'"), and a node
83
+ * that is a child and a parent carries two columns. A column
84
+ * read from an older store may still carry both; it is counted
85
+ * on both sides, which is what its delete would do. */
84
86
  if(col.flag.indexOf("hook") >= 0) {
85
87
  out.children += n;
86
88
  }
@@ -0,0 +1,116 @@
1
+ /***********************************************************************
2
+ * form_json_field.js
3
+ *
4
+ * Pure logic of a json field of C_YUI_FORM: what a jsoneditor
5
+ * holds, read as the value its column stores.
6
+ *
7
+ * The editor answers its content in one of two shapes -- {json}
8
+ * (tree mode) or {text} (text mode, whatever the reader typed).
9
+ * A text that does not parse used to be caught and replaced by
10
+ * `{}` or `[]`, and SAVED: a trailing comma wiped the column, with
11
+ * no log and no message. A text that is not json, or json of the
12
+ * wrong shape (a list in a dict column), is an error now, and the
13
+ * form refuses to save while a field holds one.
14
+ *
15
+ * Copyright (c) 2026, ArtGins.
16
+ * All Rights Reserved.
17
+ ***********************************************************************/
18
+
19
+ /*
20
+ * The shape a column type stores. A type not listed takes any json.
21
+ */
22
+ const SHAPE_OF_TYPE = {
23
+ object: "dict",
24
+ dict: "dict",
25
+ template: "dict",
26
+ array: "list",
27
+ list: "list",
28
+ table: "list",
29
+ coordinates: "container",
30
+ };
31
+
32
+ /************************************************************
33
+ * The shape `type` stores: "dict", "list", "container" or "any".
34
+ ************************************************************/
35
+ export function json_field_shape(type)
36
+ {
37
+ return SHAPE_OF_TYPE[type] || "any";
38
+ }
39
+
40
+ /************************************************************
41
+ * The empty value of a shape: what a blank editor stores.
42
+ ************************************************************/
43
+ export function json_field_empty(shape)
44
+ {
45
+ if(shape === "list") {
46
+ return [];
47
+ }
48
+ return {};
49
+ }
50
+
51
+ function value_fits(value, shape)
52
+ {
53
+ let is_list = Array.isArray(value);
54
+ let is_dict = value !== null && typeof value === "object" && !is_list;
55
+ switch(shape) {
56
+ case "dict":
57
+ return is_dict;
58
+ case "list":
59
+ return is_list;
60
+ case "container":
61
+ return is_dict || is_list;
62
+ default:
63
+ return true;
64
+ }
65
+ }
66
+
67
+ /************************************************************
68
+ * Parse what a json field holds.
69
+ *
70
+ * `value` is a json string (what the form reads out of an editor)
71
+ * or an already parsed value; an editor's content goes through
72
+ * json_editor_value() first.
73
+ * Returns {value, error}: `error` is "" when the value is good, else
74
+ * an i18n key; `value` is then the empty value of the shape and must
75
+ * NOT be stored.
76
+ *
77
+ * A blank text is not an error: it is the empty value.
78
+ ************************************************************/
79
+ export function parse_json_field(value, type)
80
+ {
81
+ let shape = json_field_shape(type);
82
+
83
+ if(typeof value === "string") {
84
+ if(value.trim() === "") {
85
+ return {value: json_field_empty(shape), error: ""};
86
+ }
87
+ try {
88
+ value = JSON.parse(value);
89
+ } catch(e) {
90
+ return {value: json_field_empty(shape), error: "invalid json"};
91
+ }
92
+ }
93
+
94
+ if(value === undefined) {
95
+ return {value: json_field_empty(shape), error: ""};
96
+ }
97
+ if(!value_fits(value, shape)) {
98
+ return {value: json_field_empty(shape), error: "invalid json"};
99
+ }
100
+ return {value: value, error: ""};
101
+ }
102
+
103
+ /************************************************************
104
+ * What a jsoneditor content ({text} or {json}) holds: the
105
+ * text as typed, or the tree-mode json.
106
+ ************************************************************/
107
+ export function json_editor_value(content)
108
+ {
109
+ if(!content) {
110
+ return undefined;
111
+ }
112
+ if(content.text !== undefined) {
113
+ return content.text;
114
+ }
115
+ return content.json;
116
+ }
@@ -0,0 +1,43 @@
1
+ /***********************************************************************
2
+ * form_json_field.test.js
3
+ *
4
+ * Copyright (c) 2026, ArtGins.
5
+ * All Rights Reserved.
6
+ ***********************************************************************/
7
+ import {describe, it, expect} from "vitest";
8
+ import {parse_json_field, json_field_shape, json_editor_value} from "./form_json_field.js";
9
+
10
+ describe("parse_json_field", () => {
11
+ it("a text that does not parse is an error, never an empty dict to store", () => {
12
+ expect(parse_json_field('{"a": 1,}', "dict")).toEqual({value: {}, error: "invalid json"});
13
+ expect(parse_json_field(json_editor_value({text: '[1, 2'}), "list")).toEqual({value: [], error: "invalid json"});
14
+ expect(parse_json_field('{"x": }', "template").error).toBe("invalid json");
15
+ expect(parse_json_field('nope', "blob").error).toBe("invalid json");
16
+ });
17
+
18
+ it("json of the wrong shape is an error", () => {
19
+ expect(parse_json_field('[1]', "dict").error).toBe("invalid json");
20
+ expect(parse_json_field('{"a": 1}', "list").error).toBe("invalid json");
21
+ expect(parse_json_field('null', "dict").error).toBe("invalid json");
22
+ expect(parse_json_field('3', "coordinates").error).toBe("invalid json");
23
+ });
24
+
25
+ it("good json comes back parsed", () => {
26
+ expect(parse_json_field('{"a": 1}', "dict")).toEqual({value: {a: 1}, error: ""});
27
+ expect(parse_json_field(json_editor_value({json: [1, 2]}), "table")).toEqual({value: [1, 2], error: ""});
28
+ expect(parse_json_field('[0, 1]', "coordinates")).toEqual({value: [0, 1], error: ""});
29
+ expect(parse_json_field('"s"', "blob")).toEqual({value: "s", error: ""});
30
+ expect(parse_json_field({text: 1}, "dict")).toEqual({value: {text: 1}, error: ""});
31
+ });
32
+
33
+ it("a blank editor is the empty value, not an error", () => {
34
+ expect(parse_json_field("", "dict")).toEqual({value: {}, error: ""});
35
+ expect(parse_json_field(json_editor_value({text: " "}), "list")).toEqual({value: [], error: ""});
36
+ });
37
+
38
+ it("shapes", () => {
39
+ expect(json_field_shape("object")).toBe("dict");
40
+ expect(json_field_shape("table")).toBe("list");
41
+ expect(json_field_shape("blob")).toBe("any");
42
+ });
43
+ });