@yuneta/gobj-ui 7.26.6 → 7.26.8

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.
Files changed (48) hide show
  1. package/README.md +74 -25
  2. package/dist/gobj-ui.cjs.js +967 -377
  3. package/dist/gobj-ui.es.js +967 -377
  4. package/index.js +5 -1
  5. package/package.json +2 -2
  6. package/src/c_g6_nodes_tree.js +69 -24
  7. package/src/c_yui_form.js +331 -140
  8. package/src/c_yui_gclass.js +26 -21
  9. package/src/c_yui_gobj_tree_js.js +16 -5
  10. package/src/c_yui_json.js +7 -6
  11. package/src/c_yui_json_graph.js +42 -14
  12. package/src/c_yui_json_pad.js +2 -1
  13. package/src/c_yui_map.js +139 -75
  14. package/src/c_yui_nav.js +60 -20
  15. package/src/c_yui_pager.js +14 -9
  16. package/src/c_yui_period.js +16 -6
  17. package/src/c_yui_schema_editor.js +3 -2
  18. package/src/c_yui_shell.js +96 -31
  19. package/src/c_yui_treedb_topic_with_form.js +50 -56
  20. package/src/c_yui_treedb_topics.js +8 -7
  21. package/src/c_yui_uplot.js +1 -1
  22. package/src/c_yui_window.js +123 -36
  23. package/src/c_yui_window_manager.js +8 -3
  24. package/src/c_yui_wizard.js +23 -8
  25. package/src/delete_impact.js +5 -3
  26. package/src/form_json_field.js +123 -0
  27. package/src/form_json_field.test.js +49 -0
  28. package/src/form_json_field.wiring.test.js +137 -0
  29. package/src/form_table_rearm.wiring.test.js +158 -0
  30. package/src/g6_event_kw.js +36 -0
  31. package/src/nav_cards_helpers.js +2 -1
  32. package/src/nav_cards_helpers.test.js +7 -3
  33. package/src/schema_to_c.js +27 -1
  34. package/src/schema_to_c.test.js +15 -1
  35. package/src/shell_modals.js +5 -3
  36. package/src/shell_route_map.js +18 -13
  37. package/src/treedb_topics_transport.wiring.test.js +35 -1
  38. package/src/vite_plugin_yuneta_html.test.js +79 -0
  39. package/src/window_show.wiring.test.js +83 -0
  40. package/src/window_veto_back.wiring.test.js +136 -0
  41. package/src/wizard_chrome.wiring.test.js +87 -0
  42. package/src/yui_dev.js +23 -14
  43. package/src/yui_frontend_view.js +4 -4
  44. package/src/yui_gclass_view.js +3 -3
  45. package/src/yui_table_filter_clear.js +44 -44
  46. package/src/yui_text.js +31 -0
  47. package/src/yui_text.test.js +39 -0
  48. package/vite-plugin-yuneta-html.js +36 -9
package/src/c_yui_form.js CHANGED
@@ -47,6 +47,7 @@ import {
47
47
  refresh_language,
48
48
  treedb_decoder_fkey,
49
49
  gclass_find_by_name,
50
+ gobj_is_destroying,
50
51
  } from "@yuneta/gobj-js";
51
52
 
52
53
  import {t} from "i18next";
@@ -89,6 +90,8 @@ import "tabulator-tables/dist/css/tabulator_bulma.css";
89
90
  import "./tabulator.css";
90
91
  import {is_time_field} from "./json_view_helpers.js";
91
92
  import {date_to_datetime_local, datetime_local_to_epoch} from "./form_time_value.js";
93
+ import {parse_json_field, json_editor_value} from "./form_json_field.js";
94
+ import {text_node} from "./yui_text.js";
92
95
  import { TabulatorFull as Tabulator } from "tabulator-tables"; // Import Full Tabulator JS
93
96
  // import { Tabulator } from "tabulator-tables"; // Import light Tabulator JS
94
97
 
@@ -158,7 +161,15 @@ SDATA(data_type_t.DTP_BOOLEAN, "changes", 0, false, "Indicates if ch
158
161
  SDATA_END()
159
162
  ];
160
163
 
161
- let PRIVATE_DATA = {};
164
+ let PRIVATE_DATA = {
165
+ /* The deferred re-wiring of the tables' dataChanged after a
166
+ * save/undo (see set_changed_stated): cancelled in destroy_ui. */
167
+ rearm_timer: null,
168
+ /* table_id -> WeakRef of its element: EV_ADD_TABLE_ROW names a
169
+ * table by id (a kw is plain json), see table_identity(). */
170
+ tables: null,
171
+ table_seq: 0,
172
+ };
162
173
 
163
174
  let __gclass__ = null;
164
175
 
@@ -189,6 +200,7 @@ function mt_create(gobj)
189
200
  let name = clean_name(gobj_name(gobj));
190
201
  gobj_write_attr(gobj, "form_id", "form" + name);
191
202
 
203
+ gobj.priv.tables = new Map();
192
204
  build_ui(gobj);
193
205
  }
194
206
 
@@ -252,11 +264,11 @@ function set_changed_stated(gobj, changed)
252
264
  let $form = $container.querySelector('form');
253
265
 
254
266
  /*
255
- * Los dos pueden NO estar: desde que la botonera es configurable
256
- * (`toolbar`), un dialogo de una sola accion pide ["save"] y aqui no
257
- * hay undo que habilitar. Antes se daban por presentes y quitar uno
258
- * reventaba con "can't access property setAttribute, a is null" al
259
- * primer cambio en el formulario.
267
+ * Either of the two may be MISSING: since the toolbar is configurable
268
+ * (`toolbar`), a one-action dialog asks for ["save"] and there is no
269
+ * undo to enable here. They used to be taken for granted, and leaving
270
+ * one out blew up with "can't access property setAttribute, a is null"
271
+ * at the first change in the form.
260
272
  */
261
273
  const enable = ($btn, on, color) => {
262
274
  if(!$btn) {
@@ -287,12 +299,12 @@ function set_changed_stated(gobj, changed)
287
299
  enable($button_undo, true, "Magenta");
288
300
 
289
301
  $form.querySelectorAll('.yui-tabulator-table').forEach($table => {
290
- if(isEventSet($table.tabulator, "dataChanged")) {
302
+ if($table.tabulator && isEventSet($table.tabulator, "dataChanged")) {
291
303
  $table.tabulator.off("dataChanged");
292
304
  }
293
305
 
294
306
  $table.querySelectorAll('.yui-tabulator-table-nested').forEach($table_nested => {
295
- if(isEventSet($table_nested.tabulator, "dataChanged")) {
307
+ if($table_nested.tabulator && isEventSet($table_nested.tabulator, "dataChanged")) {
296
308
  $table_nested.tabulator.off("dataChanged");
297
309
  }
298
310
  });
@@ -307,25 +319,37 @@ function set_changed_stated(gobj, changed)
307
319
  enable($button_save, false, OFF_COLOR);
308
320
  enable($button_undo, false, OFF_COLOR);
309
321
 
310
- // TODO improve
311
- setTimeout(function() {
312
- $form.querySelectorAll('.yui-tabulator-table').forEach($table => {
313
- if(isEventSet($table.tabulator, "dataChanged")) {
314
- $table.tabulator.off("dataChanged");
322
+ /*
323
+ * TODO improve. The tables are re-wired a moment later, once the
324
+ * data just loaded has settled. The timer is kept so that
325
+ * destroy_ui() cancels it: a form closed right after a save (what
326
+ * a dialog does) destroyed its widgets first, and the timer then
327
+ * called .on() on a null tabulator -- an uncaught TypeError.
328
+ */
329
+ if(gobj.priv.rearm_timer) {
330
+ clearTimeout(gobj.priv.rearm_timer);
331
+ }
332
+ gobj.priv.rearm_timer = setTimeout(function() {
333
+ gobj.priv.rearm_timer = null;
334
+ if(gobj_is_destroying(gobj)) {
335
+ return; // destroy_ui() cancels it; this is the belt
336
+ }
337
+ const rearm = (tabulator) => {
338
+ if(!tabulator) {
339
+ return; // destroyed with its form
315
340
  }
316
- $table.tabulator.on("dataChanged", function(data) {
341
+ if(isEventSet(tabulator, "dataChanged")) {
342
+ tabulator.off("dataChanged");
343
+ }
344
+ tabulator.on("dataChanged", function(data) {
317
345
  //data - the updated table data
318
346
  gobj_send_event(gobj, "EV_RECORD_CHANGED", {}, gobj);
319
347
  });
320
-
348
+ };
349
+ $form.querySelectorAll('.yui-tabulator-table').forEach($table => {
350
+ rearm($table.tabulator);
321
351
  $table.querySelectorAll('.yui-tabulator-table-nested').forEach($table_nested => {
322
- if(isEventSet($table_nested.tabulator, "dataChanged")) {
323
- $table_nested.tabulator.off("dataChanged");
324
- }
325
- $table_nested.tabulator.on("dataChanged", function(data) {
326
- //data - the updated table data
327
- gobj_send_event(gobj, "EV_RECORD_CHANGED", {}, gobj);
328
- });
352
+ rearm($table_nested.tabulator);
329
353
  });
330
354
  });
331
355
  }, 200);
@@ -490,8 +514,13 @@ function build_ui(gobj)
490
514
  ************************************************************/
491
515
  function destroy_ui(gobj)
492
516
  {
517
+ if(gobj.priv && gobj.priv.rearm_timer) {
518
+ clearTimeout(gobj.priv.rearm_timer);
519
+ gobj.priv.rearm_timer = null;
520
+ }
493
521
  let $container = gobj_read_attr(gobj, "$container");
494
522
  if($container) {
523
+ destroy_widgets(gobj, $container);
495
524
  if($container.parentNode) {
496
525
  $container.parentNode.removeChild($container);
497
526
  }
@@ -499,6 +528,116 @@ function destroy_ui(gobj)
499
528
  }
500
529
  }
501
530
 
531
+ /************************************************************
532
+ * The icon a table cell draws as its control (delete the row,
533
+ * add a nested row): an element, so it carries its name. Drawn
534
+ * by Tabulator on every render, so t() at render time is
535
+ * current.
536
+ ************************************************************/
537
+ function cell_icon(icon_class, key)
538
+ {
539
+ /* A control, not a picture: role, focus and the keys of a button. A
540
+ * key press clicks it, so it reaches the same Tabulator cellClick as
541
+ * the pointer. */
542
+ let $i = document.createElement('i');
543
+ $i.className = icon_class;
544
+ $i.setAttribute('role', 'button');
545
+ $i.setAttribute('tabindex', '0');
546
+ $i.setAttribute('title', t(key));
547
+ $i.setAttribute('data-i18n-title', key);
548
+ $i.setAttribute('aria-label', t(key));
549
+ $i.setAttribute('data-i18n-aria-label', key);
550
+ $i.addEventListener('keydown', function(evt) {
551
+ if(evt.key === 'Enter' || evt.key === ' ') {
552
+ evt.preventDefault();
553
+ $i.click();
554
+ }
555
+ });
556
+ return $i;
557
+ }
558
+
559
+ /************************************************************
560
+ * The identity of a table element, for the kw of
561
+ * EV_ADD_TABLE_ROW: a kw is plain json, and an element (with its
562
+ * `.tabulator`, circular) broke the `machine` trace that dumps it.
563
+ * Given on first use and kept on the element; the registry holds
564
+ * it weakly, so a nested table re-built by its row formatter is
565
+ * not kept alive by it.
566
+ ************************************************************/
567
+ function table_identity(gobj, $table)
568
+ {
569
+ if(!$table.yui_table_id) {
570
+ let priv = gobj.priv;
571
+ priv.table_seq += 1;
572
+ $table.yui_table_id = `t${priv.table_seq}`;
573
+ priv.tables.set($table.yui_table_id, new WeakRef($table));
574
+ }
575
+ return $table.yui_table_id;
576
+ }
577
+
578
+ /************************************************************
579
+ * The element of a table identity, or null when it is gone.
580
+ ************************************************************/
581
+ function find_table(gobj, table_id)
582
+ {
583
+ let priv = gobj.priv;
584
+ let ref = priv.tables? priv.tables.get(table_id) : null;
585
+ let $table = ref? ref.deref() : null;
586
+ if(!$table) {
587
+ if(priv.tables) {
588
+ priv.tables.delete(table_id);
589
+ }
590
+ return null;
591
+ }
592
+ return $table;
593
+ }
594
+
595
+ /************************************************************
596
+ * Destroy the third-party widgets the form built. Removing
597
+ * their DOM is not enough: Tom Select keeps listeners on
598
+ * `document` and `window`, the jsoneditor is a live Svelte app
599
+ * and a Tabulator keeps its observers -- each one a closure over
600
+ * this form, alive for the life of the page. Nested tables go
601
+ * first: their parent's destroy drops the rows they hang from.
602
+ ************************************************************/
603
+ function destroy_widgets(gobj, $container)
604
+ {
605
+ let destroy = (what, widget) => {
606
+ try {
607
+ /* vanilla-jsoneditor 3 destroys asynchronously: its failure
608
+ * is a rejected promise, which no try/catch sees. */
609
+ let r = widget.destroy();
610
+ if(r && typeof r.then === "function") {
611
+ r.catch((e) => {
612
+ log_warning(`${gobj_short_name(gobj)}: cannot destroy ${what}: ${e}`);
613
+ });
614
+ }
615
+ } catch(e) {
616
+ log_warning(`${gobj_short_name(gobj)}: cannot destroy ${what}: ${e}`);
617
+ }
618
+ };
619
+ $container.querySelectorAll('.yui-tabulator-table-nested').forEach(($el) => {
620
+ if($el.tabulator) {
621
+ destroy("nested table", $el.tabulator);
622
+ $el.tabulator = null;
623
+ }
624
+ });
625
+ $container.querySelectorAll('.yui-form-data-input').forEach(($el) => {
626
+ if($el.tom_select) {
627
+ destroy("select", $el.tom_select);
628
+ $el.tom_select = null;
629
+ }
630
+ if($el.jsoneditor) {
631
+ destroy("json editor", $el.jsoneditor);
632
+ $el.jsoneditor = null;
633
+ }
634
+ if($el.tabulator) {
635
+ destroy("table", $el.tabulator);
636
+ $el.tabulator = null;
637
+ }
638
+ });
639
+ }
640
+
502
641
  /******************************************************************
503
642
  * Build form with bulma and createElement2
504
643
  ******************************************************************/
@@ -543,6 +682,19 @@ function name_form_control($field, conf)
543
682
  $ts.setAttribute("aria-label", t(key));
544
683
  $ts.setAttribute("data-i18n-aria-label", key);
545
684
  }
685
+
686
+ /* The ✕ of the clear_button plugin is drawn by Tom Select too, with a
687
+ * title written once with t() and no key: named here, after the
688
+ * render, with its key, so it re-translates. */
689
+ let $clear = $field.querySelector(".ts-wrapper .clear-button");
690
+ if($clear) {
691
+ const CLEAR_KEY = "remove all selected options";
692
+ $clear.setAttribute("role", "button");
693
+ $clear.setAttribute("title", t(CLEAR_KEY));
694
+ $clear.setAttribute("data-i18n-title", CLEAR_KEY);
695
+ $clear.setAttribute("aria-label", t(CLEAR_KEY));
696
+ $clear.setAttribute("data-i18n-aria-label", CLEAR_KEY);
697
+ }
546
698
  }
547
699
 
548
700
  function build_form(gobj)
@@ -1105,12 +1257,7 @@ function create_form_field(
1105
1257
  *
1106
1258
  *--------------------------------------*/
1107
1259
  let $element;
1108
- let kw_add_table_row = {
1109
- $row: null, // not null if it's a nested table
1110
- $table: null, // Element of tabulator
1111
- record: {},
1112
- loading: false,
1113
- };
1260
+ let $add_table = null; // the table the field's "add" button adds a row to
1114
1261
 
1115
1262
  switch (tag) {
1116
1263
  case 'input':
@@ -1119,7 +1266,7 @@ function create_form_field(
1119
1266
  $element = createElement2(
1120
1267
  ['div', {class: 'xfield field is-horizontal'}, [
1121
1268
  ['div', {class: 'field-label is-normal' }, [
1122
- ["label", {class:"label ", i18n:(label_i18n||label), for:name}, label]
1269
+ ["label", {class:"label ", i18n:(label_i18n||label), for:name}, text_node(label)]
1123
1270
  ]],
1124
1271
 
1125
1272
  ['div', {class: 'field-body'}, [
@@ -1136,7 +1283,7 @@ function create_form_field(
1136
1283
  $element = createElement2(
1137
1284
  ['div', {class: 'xfield field is-horizontal'}, [
1138
1285
  ['div', {class: 'field-label is-normal' }, [
1139
- ["label", {class:"label ", i18n:(label_i18n||label), for:name}, label]
1286
+ ["label", {class:"label ", i18n:(label_i18n||label), for:name}, text_node(label)]
1140
1287
  ]],
1141
1288
 
1142
1289
  ['div', {class: 'field-body'}, [
@@ -1159,7 +1306,7 @@ function create_form_field(
1159
1306
  $element = createElement2(
1160
1307
  ['div', {class: 'xfield field is-horizontal'}, [
1161
1308
  ['div', {class: 'field-label is-normal' }, [
1162
- ["label", {class:"label ", i18n:(label_i18n||label), for:name}, label]
1309
+ ["label", {class:"label ", i18n:(label_i18n||label), for:name}, text_node(label)]
1163
1310
  ]],
1164
1311
 
1165
1312
  ['div', {class: 'field-body'}, [
@@ -1175,7 +1322,7 @@ function create_form_field(
1175
1322
  $element = createElement2(
1176
1323
  ['div', {class: 'xfield field is-horizontal'}, [
1177
1324
  ['div', {class: 'field-label is-normal' }, [
1178
- ["label", {class:"label ", i18n:(label_i18n||label), for:name}, label]
1325
+ ["label", {class:"label ", i18n:(label_i18n||label), for:name}, text_node(label)]
1179
1326
  ]],
1180
1327
 
1181
1328
  ['div', {class: 'field-body'}, [
@@ -1194,21 +1341,33 @@ function create_form_field(
1194
1341
  $element = createElement2(
1195
1342
  ['div', {class: 'field is-horizontal tabulator-buttons'}, [
1196
1343
  ['div', {class: 'field-label is-normal' }, [
1197
- ['label', {class:'label', i18n:(label_i18n||label)}, label]
1344
+ ['label', {class:'label', i18n:(label_i18n||label)}, text_node(label)]
1198
1345
  ]],
1199
1346
 
1200
1347
  ['div', {class: 'field-body'}, [
1201
1348
  ['div', {class: 'field'}, [
1202
1349
  ['div', {class: '' }, [
1203
- ['button', {class: 'button p-1'}, [
1350
+ ['button', {class: 'button p-1',
1351
+ title: t('add'), 'data-i18n-title': 'add',
1352
+ 'aria-label': t('add'), 'data-i18n-aria-label': 'add'}, [
1204
1353
  ['span', {class: 'icon m-0'}, '<i class="yi-plus"></i>'],
1205
1354
  ['span', {class: 'p-1 pr-2 is-hidden-mobile', i18n: 'add'}, 'add'],
1206
1355
  ], {
1207
1356
  click: function(evt) {
1208
1357
  evt.stopPropagation();
1209
1358
  evt.preventDefault();
1359
+ if(!$add_table) {
1360
+ log_error(`${gobj_short_name(gobj)}: "add" of a field without a table`);
1361
+ return;
1362
+ }
1210
1363
  gobj_send_event(gobj,
1211
- "EV_ADD_TABLE_ROW", kw_add_table_row, gobj
1364
+ "EV_ADD_TABLE_ROW",
1365
+ {
1366
+ table_id: table_identity(gobj, $add_table),
1367
+ record: {},
1368
+ loading: false,
1369
+ },
1370
+ gobj
1212
1371
  );
1213
1372
  }
1214
1373
  }]
@@ -1237,7 +1396,7 @@ function create_form_field(
1237
1396
  $element = createElement2(
1238
1397
  ['div', {class: 'xfield field is-horizontal'}, [
1239
1398
  ['div', {class: 'field-label is-normal' }, [
1240
- ["label", {class:"label ", i18n:(label_i18n||label), for:name}, label]
1399
+ ["label", {class:"label ", i18n:(label_i18n||label), for:name}, text_node(label)]
1241
1400
  ]],
1242
1401
 
1243
1402
  ['div', {class: 'field-body'}, [
@@ -1356,7 +1515,8 @@ function create_form_field(
1356
1515
  name: name
1357
1516
  },
1358
1517
  opts.map(option =>
1359
- ['option', {value:option, i18n:option}, option]
1518
+ ['option', {value:option, i18n:option},
1519
+ text_node(option)] // data, not markup
1360
1520
  ),
1361
1521
  {
1362
1522
  'change': function (evt) {
@@ -1398,8 +1558,8 @@ function create_form_field(
1398
1558
  multiple: true,
1399
1559
  },
1400
1560
  opts.map(option => is_icon
1401
- ? ['option', {value:option}, option]
1402
- : ['option', {value:option, i18n:option}, option]
1561
+ ? ['option', {value:option}, text_node(option)]
1562
+ : ['option', {value:option, i18n:option}, text_node(option)]
1403
1563
  )
1404
1564
  ];
1405
1565
  $extend = createElement2(extend);
@@ -1450,19 +1610,18 @@ function create_form_field(
1450
1610
 
1451
1611
  case 'radio':
1452
1612
  {
1453
- // TODO review, not tested
1454
- // for(const option of options) {
1455
- // // WARNING here can't use data-i18n, the radio element is destroyed.
1456
- let elm = `
1457
- <label class="radio">
1458
- <input type="radio" name="${name}">
1459
- ${t(options)}
1460
- </label>
1461
- `;
1462
-
1463
- $extend = createOneHtml(elm);
1464
- $control.appendChild($extend);
1465
- // }
1613
+ // TODO review, not tested: one radio, labelled by `options`
1614
+ /* Built as nodes: the name and the label are data, and the
1615
+ * label keeps its key so it re-translates. The <label> WRAPS
1616
+ * its control, which is what names it. */
1617
+ let label_key = String(options === undefined || options === null ? "" : options);
1618
+ $extend = createElement2(
1619
+ ['label', {class: 'radio'}, [
1620
+ ['input', {type: 'radio', name: name}],
1621
+ ['span', {i18n: label_key}, text_node(label_key)]
1622
+ ]]
1623
+ );
1624
+ $control.appendChild($extend);
1466
1625
  break;
1467
1626
  }
1468
1627
 
@@ -1488,12 +1647,18 @@ function create_form_field(
1488
1647
  props: {
1489
1648
  readOnly: !!readonly,
1490
1649
  onChange: function() {
1650
+ /* A field marked wrong clears its mark as soon as
1651
+ * it holds good json again. */
1652
+ if($extend.yui_json_error) {
1653
+ check_json_editor($extend);
1654
+ mark_field_validity($extend);
1655
+ }
1491
1656
  gobj_send_event(gobj, "EV_RECORD_CHANGED", {}, gobj);
1492
1657
  },
1493
- /* La lista vive en json_view_helpers.js: el visor de
1494
- * JSON y este editor tienen que reconocer los MISMOS
1495
- * campos, y tener cada uno su copia es como dejaron de
1496
- * hacerlo. */
1658
+ /* The list lives in json_view_helpers.js: the JSON
1659
+ * viewer and this editor must recognise the SAME
1660
+ * fields, and a copy each is how they stopped
1661
+ * doing so. */
1497
1662
  timestampTag: function({field, value, path}) {
1498
1663
  return is_time_field(field);
1499
1664
  },
@@ -1531,7 +1696,7 @@ function create_form_field(
1531
1696
  $extend = createElement2(extend);
1532
1697
  $control.appendChild($extend);
1533
1698
  $extend.tabulator = create_tabulator(gobj, $extend, name, options);
1534
- kw_add_table_row["$table"] = $extend;
1699
+ $add_table = $extend;
1535
1700
  break;
1536
1701
  }
1537
1702
 
@@ -1951,7 +2116,7 @@ function create_tabulator(gobj, $extend, name, template)
1951
2116
  columns.push(
1952
2117
  {
1953
2118
  formatter: function(cell, formatterParams) {
1954
- return '<i style="" class="yi-trash has-text-danger"></i>';
2119
+ return cell_icon('yi-trash has-text-danger', 'delete');
1955
2120
  },
1956
2121
  width:40,
1957
2122
  hozAlign:"center",
@@ -2062,12 +2227,12 @@ function template2columns(gobj, columns, template, sub_elements)
2062
2227
  validator.push("required");
2063
2228
  }
2064
2229
 
2065
- // TODO añade los validator necesarios, como validator.push("integer");
2230
+ // TODO add the validators needed, like validator.push("integer");
2066
2231
 
2067
2232
  switch(field_desc.type) {
2068
2233
  case "object":
2069
2234
  case "dict":
2070
- // TODO es otro formulario, pongo un button y que abra un popup?
2235
+ // TODO it is another form: a button that opens it in a popup?
2071
2236
  // Object.entries(value).forEach(([key, value]) => {
2072
2237
  // build_html_form_field_conf_from_template(gobj, $form, key, value);
2073
2238
  // });
@@ -2075,7 +2240,7 @@ function template2columns(gobj, columns, template, sub_elements)
2075
2240
 
2076
2241
  case "template":
2077
2242
  // field_conf.tag = "fieldset";
2078
- // TODO si es para editar, tiene que ser field_conf.tag = "jsoneditor";
2243
+ // TODO when editing, it must be field_conf.tag = "jsoneditor";
2079
2244
  break;
2080
2245
 
2081
2246
  case "blob":
@@ -2091,8 +2256,8 @@ function template2columns(gobj, columns, template, sub_elements)
2091
2256
  // field_conf.tag = "table";
2092
2257
  // field_conf.options = enum_list;
2093
2258
  // column.editor = "false";
2094
- // TODO es otra tabla, poner buttons:
2095
- // TODO - para mostrar/ocultar la segunda tabla
2259
+ // TODO it is another table: give it buttons
2260
+ // TODO - to show/hide the second table
2096
2261
 
2097
2262
  let template = field_desc.enum_list;
2098
2263
  sub_elements.push({
@@ -2102,18 +2267,21 @@ function template2columns(gobj, columns, template, sub_elements)
2102
2267
 
2103
2268
  Object.assign(column, {
2104
2269
  formatter: function(cell, formatterParams) {
2105
- return '<i style="" class="yi-plus has-text-link"></i>';
2270
+ return cell_icon('yi-plus has-text-link', 'add');
2106
2271
  },
2107
2272
  hozAlign:"center",
2108
2273
  cellClick: function(evt, cell) {
2109
2274
  evt.stopPropagation();
2110
2275
  let $row = cell.getRow().getElement();
2111
2276
  let $extend = $row.querySelector(`[name="${field_desc.name}"]`);
2277
+ if(!$extend) {
2278
+ log_error(`${gobj_short_name(gobj)}: nested table '${field_desc.name}' not found in its row`);
2279
+ return;
2280
+ }
2112
2281
  gobj_send_event(gobj,
2113
2282
  "EV_ADD_TABLE_ROW",
2114
2283
  {
2115
- $row: $row, // not null if it's a nested table
2116
- $table: $extend, // Element of tabulator
2284
+ table_id: table_identity(gobj, $extend),
2117
2285
  record: {},
2118
2286
  loading: false,
2119
2287
  },
@@ -2345,11 +2513,28 @@ function template2columns(gobj, columns, template, sub_elements)
2345
2513
  ************************************************************/
2346
2514
  function field_validation_message($input)
2347
2515
  {
2516
+ let key = field_validation_key($input);
2517
+ if(key) {
2518
+ return t(key);
2519
+ }
2520
+ return $input.validationMessage;
2521
+ }
2522
+
2523
+ /************************************************************
2524
+ * The i18n key of the message, when the message is ours ("" when
2525
+ * it is the browser's): written as the help line's data-i18n, so
2526
+ * a language change re-translates the line on screen.
2527
+ ************************************************************/
2528
+ function field_validation_key($input)
2529
+ {
2530
+ if($input.yui_json_error) {
2531
+ return $input.yui_json_error;
2532
+ }
2348
2533
  let v = $input.validity;
2349
2534
  if(v && v.valueMissing) {
2350
- return t("this field is required");
2535
+ return "this field is required";
2351
2536
  }
2352
- return $input.validationMessage;
2537
+ return "";
2353
2538
  }
2354
2539
 
2355
2540
  /************************************************************
@@ -2369,12 +2554,13 @@ function mark_field_validity($input)
2369
2554
  * field. Reading `validity` asks the same question and fires nothing.
2370
2555
  * A control with no `validity` (a wrapper div) is not invalid. */
2371
2556
  let v = $input.validity;
2372
- let ok = !v || v.valid;
2557
+ let ok = (!v || v.valid) && !$input.yui_json_error;
2373
2558
 
2374
2559
  if(ok) {
2375
2560
  $input.classList.remove('is-danger');
2376
2561
  if($h) {
2377
2562
  $h.textContent = '';
2563
+ $h.removeAttribute('data-i18n');
2378
2564
  $h.style.display = 'none';
2379
2565
  }
2380
2566
  return true;
@@ -2382,6 +2568,12 @@ function mark_field_validity($input)
2382
2568
 
2383
2569
  $input.classList.add('is-danger');
2384
2570
  if($h) {
2571
+ let key = field_validation_key($input);
2572
+ if(key) {
2573
+ $h.setAttribute('data-i18n', key);
2574
+ } else {
2575
+ $h.removeAttribute('data-i18n'); // the browser's own sentence
2576
+ }
2385
2577
  $h.textContent = field_validation_message($input);
2386
2578
  $h.style.display = 'block';
2387
2579
  }
@@ -2399,6 +2591,16 @@ function validate_form(gobj, $form)
2399
2591
  validated = false;
2400
2592
  }
2401
2593
 
2594
+ /* checkValidity() does not see a jsoneditor: it is no form control.
2595
+ * A text that is not json -- or not the column's shape -- used to be
2596
+ * saved as {} or []. */
2597
+ $form.querySelectorAll('.jsoneditor.yui-form-data-input').forEach($editor => {
2598
+ if(!check_json_editor($editor)) {
2599
+ validated = false;
2600
+ mark_field_validity($editor);
2601
+ }
2602
+ });
2603
+
2402
2604
  $form.querySelectorAll('.yui-tabulator-table').forEach($table => {
2403
2605
  if(!$table.tabulator.validate()) {
2404
2606
  validated = false;
@@ -2414,6 +2616,24 @@ function validate_form(gobj, $form)
2414
2616
  return validated;
2415
2617
  }
2416
2618
 
2619
+ /************************************************************
2620
+ * Does the jsoneditor `$editor` hold json its column can store?
2621
+ * Leaves the answer on the element (`yui_json_error`, an i18n key
2622
+ * or ""), where mark_field_validity() reads it.
2623
+ ************************************************************/
2624
+ function check_json_editor($editor)
2625
+ {
2626
+ let jsoneditor = $editor.jsoneditor;
2627
+ let field_desc = $editor.field_desc;
2628
+ if(!jsoneditor || !field_desc || $editor.hasAttribute('readonly')) {
2629
+ $editor.yui_json_error = "";
2630
+ return true;
2631
+ }
2632
+ let r = parse_json_field(json_editor_value(jsoneditor.get()), field_desc.type);
2633
+ $editor.yui_json_error = r.error;
2634
+ return !r.error;
2635
+ }
2636
+
2417
2637
  /************************************************************
2418
2638
  * Function to get input value based on its type
2419
2639
  ************************************************************/
@@ -2825,8 +3045,7 @@ function load_tabulator_data(gobj, $table, value)
2825
3045
  gobj_send_event(gobj,
2826
3046
  "EV_ADD_TABLE_ROW",
2827
3047
  {
2828
- $row: null, // not null if it's a nested table
2829
- $table: $table, // Element of tabulator
3048
+ table_id: table_identity(gobj, $table),
2830
3049
  record: row,
2831
3050
  loading: true,
2832
3051
  },
@@ -2839,8 +3058,7 @@ function load_tabulator_data(gobj, $table, value)
2839
3058
  gobj_send_event(gobj,
2840
3059
  "EV_ADD_TABLE_ROW",
2841
3060
  {
2842
- $row: null, // not null if it's a nested table
2843
- $table: $table, // Element of tabulator
3061
+ table_id: table_identity(gobj, $table),
2844
3062
  record: row,
2845
3063
  loading: true,
2846
3064
  },
@@ -3000,6 +3218,23 @@ function treedb_value_2_form_value(gobj, field_desc, value)
3000
3218
  return value;
3001
3219
  }
3002
3220
 
3221
+ /************************************************************
3222
+ * A json field's value, parsed. validate_form() refuses a field
3223
+ * that does not parse, so reaching the error here is a path that
3224
+ * skipped it: logged, and the value is handed on AS IT IS. An
3225
+ * empty `{}` / `[]` in its place is a value nobody wrote, and
3226
+ * saved it wipes the column.
3227
+ ************************************************************/
3228
+ function parse_json_value(gobj, field_desc, value)
3229
+ {
3230
+ let r = parse_json_field(value, field_desc.type);
3231
+ if(r.error) {
3232
+ log_error(`${gobj_short_name(gobj)}: field '${field_desc.name}' (${field_desc.type}) holds no valid json, handed on as it is`);
3233
+ return value;
3234
+ }
3235
+ return r.value;
3236
+ }
3237
+
3003
3238
  /************************************************************
3004
3239
  * Convert from frontend to backend
3005
3240
  * operation: "create" "update"
@@ -3014,21 +3249,7 @@ function form_value_2_treedb_value(gobj, field_desc, value)
3014
3249
  * interpreted-widget conversions ('exec' mode) below. */
3015
3250
  if(gobj_read_str_attr(gobj, "render_mode") === "edit" &&
3016
3251
  (type === "template" || type === "table" || type === "coordinates")) {
3017
- if(is_string(value)) {
3018
- try {
3019
- value = JSON.parse(value);
3020
- } catch (e) {
3021
- value = (type === "table")? [] : {};
3022
- }
3023
- }
3024
- if(type === "table") {
3025
- if(!is_array(value)) {
3026
- value = [];
3027
- }
3028
- } else if(!is_object(value) && !is_array(value)) {
3029
- value = {};
3030
- }
3031
- return value;
3252
+ return parse_json_value(gobj, field_desc, value);
3032
3253
  }
3033
3254
 
3034
3255
  switch(type) {
@@ -3084,19 +3305,7 @@ function form_value_2_treedb_value(gobj, field_desc, value)
3084
3305
  case "svg":
3085
3306
  break;
3086
3307
  case "template":
3087
- if(is_string(value)) {
3088
- // Come from the form
3089
- try {
3090
- value = JSON.parse(value);
3091
- } catch (e) {
3092
- value = {};
3093
- }
3094
- } else if(is_object(value)) {
3095
- // Come from the table
3096
- }
3097
- if(!is_object(value)) {
3098
- value = {};
3099
- }
3308
+ value = parse_json_value(gobj, field_desc, value);
3100
3309
  break;
3101
3310
  case "table":
3102
3311
  case "id":
@@ -3114,22 +3323,13 @@ function form_value_2_treedb_value(gobj, field_desc, value)
3114
3323
  break;
3115
3324
  case "object":
3116
3325
  case "dict":
3326
+ /* Parsed like a list: it went out as the editor's TEXT, and a
3327
+ * backend that reads a bad text stores {} without a word. */
3328
+ value = parse_json_value(gobj, field_desc, value);
3117
3329
  break;
3118
3330
  case "array":
3119
3331
  case "list":
3120
- if(is_string(value)) {
3121
- // Come from the form
3122
- try {
3123
- value = JSON.parse(value);
3124
- } catch (e) {
3125
- value = [];
3126
- }
3127
- } else if(is_array(value)) {
3128
- // Come from the table
3129
- }
3130
- if(!is_array(value)) {
3131
- value = [];
3132
- }
3332
+ value = parse_json_value(gobj, field_desc, value);
3133
3333
  break;
3134
3334
  case "real":
3135
3335
  value = parseFloat(value) || 0.0;
@@ -3138,21 +3338,7 @@ function form_value_2_treedb_value(gobj, field_desc, value)
3138
3338
  value = parseBoolean(value);
3139
3339
  break;
3140
3340
  case "blob":
3141
- if(is_string(value)) {
3142
- // Come from the form
3143
- try {
3144
- value = JSON.parse(value);
3145
- } catch (e) {
3146
- value = {};
3147
- }
3148
-
3149
- } else if(is_object(value)) {
3150
- // Come from the table
3151
- } else if(is_array(value)) {
3152
- // Come from the table
3153
- } else {
3154
- value = {};
3155
- }
3341
+ value = parse_json_value(gobj, field_desc, value);
3156
3342
  break;
3157
3343
  case "number":
3158
3344
  break;
@@ -3343,9 +3529,13 @@ function ac_set_file_preview(gobj, event, kw, src)
3343
3529
  ************************************************************/
3344
3530
  function ac_add_table_row(gobj, event, kw, src)
3345
3531
  {
3346
- let $row = kw["$row"]; // not null if it's a nested table in this row
3347
- let $table = kw.$table; // Element of tabulator
3348
- let tabulator = $table.tabulator;
3532
+ let table_id = kw.table_id;
3533
+ let $table = find_table(gobj, table_id);
3534
+ let tabulator = $table? $table.tabulator : null;
3535
+ if(!tabulator) {
3536
+ log_error(`${gobj_short_name(gobj)}: EV_ADD_TABLE_ROW to a table that is gone: '${table_id}'`);
3537
+ return -1;
3538
+ }
3349
3539
  let record = kw.record;
3350
3540
  let loading = kw.loading;
3351
3541
 
@@ -3370,18 +3560,19 @@ function ac_add_table_row(gobj, event, kw, src)
3370
3560
  }
3371
3561
  })
3372
3562
  .catch(function(error){
3373
- //handle error updating data
3563
+ log_error(`${gobj_short_name(gobj)}: cannot add a table row: ${error}`);
3374
3564
  });
3375
3565
 
3376
3566
  // TODO improve, to use only once the timeout?
3377
3567
  setTimeout(function() {
3378
- if($row) {
3379
- let $tabulator = $row.closest('.yui-tabulator-table');
3380
- if($tabulator && $tabulator.tabulator) {
3381
- $tabulator.tabulator.redraw(); /* Delete double scrollbar */
3382
- }
3383
- } else {
3384
- tabulator.redraw(); /* Delete double scrollbar */
3568
+ if(gobj_is_destroying(gobj)) {
3569
+ return; // the form went in the meantime: nothing to redraw
3570
+ }
3571
+ /* The table itself, or -- for a nested one -- the table of the
3572
+ * row it lives in. Re-checked: it may be destroyed by now. */
3573
+ let $outer = $table.closest('.yui-tabulator-table');
3574
+ if($outer && $outer.tabulator) {
3575
+ $outer.tabulator.redraw(); /* Delete double scrollbar */
3385
3576
  }
3386
3577
  }, 30);
3387
3578