@yuneta/gobj-ui 7.23.45 → 7.23.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.
package/README.md CHANGED
@@ -1364,6 +1364,59 @@ only while there is somewhere to go back to, spent when used, and dropped by a
1364
1364
  refresh. Undoing is another write, like the drag. The host must define the key
1365
1365
  `"undo the order"`.
1366
1366
 
1367
+ ### The bytes a node owns but cannot hold — `yui_asset_*`
1368
+
1369
+ A treedb node often owns something that is not JSON: a photo, a plan, a
1370
+ clip. Those bytes cannot live in the treedb — it is held in memory and
1371
+ timeranger2 rewrites the whole record on every update — so the SDK's
1372
+ `C_ASSETS` keeps them in a directory it owns and the node names one with an
1373
+ **fkey**.
1374
+
1375
+ `get-asset` answers in one of two shapes, and the **backend** decides which:
1376
+
1377
+ ```json
1378
+ {"mode": "url", "url": "/assets/ab/cd/<id>.jpg?e=<expires>&s=<token>"}
1379
+ {"mode": "inline", "content_type": "image/jpeg", "content64": "..."}
1380
+ ```
1381
+
1382
+ It signs a URL when a web server sits in front of the store and hands over
1383
+ the bytes when there is none, so a consumer has **one** code path and a node
1384
+ with no web server still shows its images instead of showing nothing.
1385
+
1386
+ This module is the two ends of that, and **it does not talk to the
1387
+ backend** — asking is an action and belongs in the view's own FSM:
1388
+
1389
+ | | |
1390
+ |---|---|
1391
+ | `yui_asset_id(ref)` / `yui_asset_ids(ref)` | the id(s) a column names. Reads a single-valued fkey (`"assets^<id>^as_foto"`), an array fkey, and an expanded ref (`{id}`). An empty column answers nothing rather than throwing |
1392
+ | `yui_asset_src(answer)` | the two shapes into one `src`. `null` when the answer carries neither — never an empty string, because `<img src="">` reloads the page in some browsers |
1393
+ | `yui_asset_element(answer, opts)` | the element, picked from the **content type the backend stored**: `<img>`, `<video>` or `<audio>`. Video and audio are assets too, and an `<img>` whose src is a film shows the broken box this exists to remove |
1394
+ | `yui_asset_missing(detail, opts)` | the marker, for when there is nothing to show |
1395
+
1396
+ **A missing asset is now said out loud.** It used to leave a broken box and
1397
+ no word about it, which is indistinguishable from a slow one and from a bug
1398
+ — 47 such holes in one day on `artgins.ytreedb.com` before anybody noticed.
1399
+ `yui_asset_element()` wires `onerror` so the dead element is **replaced** by
1400
+ the marker, whatever the reason: an expired signature, a blob gone from the
1401
+ store, an unsupported codec.
1402
+
1403
+ The marker's label carries its i18n key (`asset not available`, overridable
1404
+ with `opts.key`), so it follows a language change; `opts.detail` is DATA — a
1405
+ name or a path, the thing a person can act on — and is never translated.
1406
+
1407
+ ```js
1408
+ import {yui_asset_id, yui_asset_element} from "@yuneta/gobj-ui/src/yui_asset.js";
1409
+ import "@yuneta/gobj-ui/src/yui_asset.css";
1410
+
1411
+ // in the view's FSM, not in a DOM callback:
1412
+ const id = yui_asset_id(device.foto);
1413
+ if(id) {
1414
+ gobj_send_event(gobj, "EV_ASK_ASSET", {asset_id: id, slot: "foto"}, gobj);
1415
+ }
1416
+ // ...and when the answer arrives, in the action:
1417
+ $box.appendChild(yui_asset_element(answer, {detail: device.foto_name}));
1418
+ ```
1419
+
1367
1420
  ### Frontend view — `setup_frontend_view`
1368
1421
 
1369
1422
  `setup_frontend_view(self)` opens the **gobj tree of the app's own yuno** in a
@@ -67043,6 +67043,118 @@ function register_c_yui_period() {
67043
67043
  return create_gclass(GCLASS_NAME);
67044
67044
  }
67045
67045
  //#endregion
67046
+ //#region src/yui_asset.js
67047
+ function ref_to_id(ref) {
67048
+ if (!ref) return null;
67049
+ if (typeof ref === "string") {
67050
+ const parts = ref.split("^");
67051
+ return parts.length >= 2 ? parts[1] : null;
67052
+ }
67053
+ if (typeof ref === "object" && typeof ref.id === "string") return ref.id;
67054
+ return null;
67055
+ }
67056
+ /***************************************************************************
67057
+ * Every asset id a column names, in order. Always an array, possibly empty.
67058
+ ***************************************************************************/
67059
+ function yui_asset_ids(ref) {
67060
+ if (Array.isArray(ref)) return ref.map(ref_to_id).filter((id) => !!id);
67061
+ const id = ref_to_id(ref);
67062
+ return id ? [id] : [];
67063
+ }
67064
+ /***************************************************************************
67065
+ * The first asset id a column names, or null.
67066
+ ***************************************************************************/
67067
+ function yui_asset_id(ref) {
67068
+ const ids = yui_asset_ids(ref);
67069
+ return ids.length ? ids[0] : null;
67070
+ }
67071
+ /***************************************************************************
67072
+ * Turn a `get-asset` answer into something an element can load.
67073
+ *
67074
+ * Returns null when the answer carries neither -- which the caller must
67075
+ * treat as "missing", never as an empty src: an <img src=""> reloads the
67076
+ * PAGE in some browsers.
67077
+ ***************************************************************************/
67078
+ function yui_asset_src(answer) {
67079
+ if (!answer) return null;
67080
+ if (answer.mode === "url" && answer.url) return answer.url;
67081
+ if (answer.mode === "inline" && answer.content64) return `data:${answer.content_type || "application/octet-stream"};base64,${answer.content64}`;
67082
+ return null;
67083
+ }
67084
+ /***************************************************************************
67085
+ * The marker for an asset that is not there.
67086
+ *
67087
+ * A missing image used to leave a broken box and no word about it, which
67088
+ * is indistinguishable from a slow one and from a bug. This says so, and
67089
+ * it says WHICH: `detail` is data (a name, a path) and is never
67090
+ * translated, while the label carries its i18n key so a language change
67091
+ * re-translates it.
67092
+ ***************************************************************************/
67093
+ function yui_asset_missing(detail, opts = {}) {
67094
+ const key = opts.key || "asset not available";
67095
+ const children = [["span", { class: "ASSET_MISSING_ICON yi-triangle-exclamation" }], [
67096
+ "span",
67097
+ {
67098
+ class: "ASSET_MISSING_LABEL",
67099
+ i18n: key
67100
+ },
67101
+ key
67102
+ ]];
67103
+ if (detail) children.push([
67104
+ "span",
67105
+ { class: "ASSET_MISSING_DETAIL" },
67106
+ String(detail)
67107
+ ]);
67108
+ const attrs = { class: `ASSET_MISSING ${opts.class || ""}`.trim() };
67109
+ if (detail) attrs.title = String(detail);
67110
+ else attrs["data-i18n-title"] = key;
67111
+ return (0, _yuneta_gobj_js.createElement2)([
67112
+ "div",
67113
+ attrs,
67114
+ children
67115
+ ]);
67116
+ }
67117
+ /***************************************************************************
67118
+ * The element for one asset.
67119
+ *
67120
+ * The KIND comes from the content type the backend stored, not from the
67121
+ * name a person typed: video and audio are assets too, and an <img> whose
67122
+ * src is a film shows the broken box this module exists to remove.
67123
+ *
67124
+ * `detail` is what the missing marker shows -- pass the original name or
67125
+ * the source path, the thing a person can act on.
67126
+ ***************************************************************************/
67127
+ function yui_asset_element(answer, opts = {}) {
67128
+ const src = yui_asset_src(answer);
67129
+ if (!src) return yui_asset_missing(opts.detail, opts);
67130
+ const ct = (answer.content_type || "").toLowerCase();
67131
+ const cls = `ASSET_MEDIA ${opts.class || ""}`.trim();
67132
+ let el;
67133
+ if (ct.startsWith("video/")) el = (0, _yuneta_gobj_js.createElement2)(["video", {
67134
+ class: cls,
67135
+ controls: "",
67136
+ preload: "metadata"
67137
+ }]);
67138
+ else if (ct.startsWith("audio/")) el = (0, _yuneta_gobj_js.createElement2)(["audio", {
67139
+ class: cls,
67140
+ controls: "",
67141
+ preload: "metadata"
67142
+ }]);
67143
+ else {
67144
+ el = (0, _yuneta_gobj_js.createElement2)(["img", {
67145
+ class: cls,
67146
+ loading: "lazy"
67147
+ }]);
67148
+ el.alt = opts.alt || opts.detail || "";
67149
+ }
67150
+ el.src = src;
67151
+ el.onerror = () => {
67152
+ const $marker = yui_asset_missing(opts.detail, opts);
67153
+ if (el.parentNode) el.parentNode.replaceChild($marker, el);
67154
+ };
67155
+ return el;
67156
+ }
67157
+ //#endregion
67046
67158
  //#region src/dev_machine_trace.js
67047
67159
  /***********************************************************************
67048
67160
  * dev_machine_trace.js
@@ -68791,6 +68903,11 @@ exports.set_submit_state = set_submit_state;
68791
68903
  exports.setup_dev = setup_dev;
68792
68904
  exports.setup_frontend_view = setup_frontend_view;
68793
68905
  exports.toggleClasses = toggleClasses;
68906
+ exports.yui_asset_element = yui_asset_element;
68907
+ exports.yui_asset_id = yui_asset_id;
68908
+ exports.yui_asset_ids = yui_asset_ids;
68909
+ exports.yui_asset_missing = yui_asset_missing;
68910
+ exports.yui_asset_src = yui_asset_src;
68794
68911
  exports.yui_is_dark = yui_is_dark;
68795
68912
  exports.yui_mount_service_view = yui_mount_service_view;
68796
68913
  exports.yui_shell_close_drawer = yui_shell_close_drawer;
@@ -67038,6 +67038,118 @@ function register_c_yui_period() {
67038
67038
  return create_gclass(GCLASS_NAME);
67039
67039
  }
67040
67040
  //#endregion
67041
+ //#region src/yui_asset.js
67042
+ function ref_to_id(ref) {
67043
+ if (!ref) return null;
67044
+ if (typeof ref === "string") {
67045
+ const parts = ref.split("^");
67046
+ return parts.length >= 2 ? parts[1] : null;
67047
+ }
67048
+ if (typeof ref === "object" && typeof ref.id === "string") return ref.id;
67049
+ return null;
67050
+ }
67051
+ /***************************************************************************
67052
+ * Every asset id a column names, in order. Always an array, possibly empty.
67053
+ ***************************************************************************/
67054
+ function yui_asset_ids(ref) {
67055
+ if (Array.isArray(ref)) return ref.map(ref_to_id).filter((id) => !!id);
67056
+ const id = ref_to_id(ref);
67057
+ return id ? [id] : [];
67058
+ }
67059
+ /***************************************************************************
67060
+ * The first asset id a column names, or null.
67061
+ ***************************************************************************/
67062
+ function yui_asset_id(ref) {
67063
+ const ids = yui_asset_ids(ref);
67064
+ return ids.length ? ids[0] : null;
67065
+ }
67066
+ /***************************************************************************
67067
+ * Turn a `get-asset` answer into something an element can load.
67068
+ *
67069
+ * Returns null when the answer carries neither -- which the caller must
67070
+ * treat as "missing", never as an empty src: an <img src=""> reloads the
67071
+ * PAGE in some browsers.
67072
+ ***************************************************************************/
67073
+ function yui_asset_src(answer) {
67074
+ if (!answer) return null;
67075
+ if (answer.mode === "url" && answer.url) return answer.url;
67076
+ if (answer.mode === "inline" && answer.content64) return `data:${answer.content_type || "application/octet-stream"};base64,${answer.content64}`;
67077
+ return null;
67078
+ }
67079
+ /***************************************************************************
67080
+ * The marker for an asset that is not there.
67081
+ *
67082
+ * A missing image used to leave a broken box and no word about it, which
67083
+ * is indistinguishable from a slow one and from a bug. This says so, and
67084
+ * it says WHICH: `detail` is data (a name, a path) and is never
67085
+ * translated, while the label carries its i18n key so a language change
67086
+ * re-translates it.
67087
+ ***************************************************************************/
67088
+ function yui_asset_missing(detail, opts = {}) {
67089
+ const key = opts.key || "asset not available";
67090
+ const children = [["span", { class: "ASSET_MISSING_ICON yi-triangle-exclamation" }], [
67091
+ "span",
67092
+ {
67093
+ class: "ASSET_MISSING_LABEL",
67094
+ i18n: key
67095
+ },
67096
+ key
67097
+ ]];
67098
+ if (detail) children.push([
67099
+ "span",
67100
+ { class: "ASSET_MISSING_DETAIL" },
67101
+ String(detail)
67102
+ ]);
67103
+ const attrs = { class: `ASSET_MISSING ${opts.class || ""}`.trim() };
67104
+ if (detail) attrs.title = String(detail);
67105
+ else attrs["data-i18n-title"] = key;
67106
+ return createElement2([
67107
+ "div",
67108
+ attrs,
67109
+ children
67110
+ ]);
67111
+ }
67112
+ /***************************************************************************
67113
+ * The element for one asset.
67114
+ *
67115
+ * The KIND comes from the content type the backend stored, not from the
67116
+ * name a person typed: video and audio are assets too, and an <img> whose
67117
+ * src is a film shows the broken box this module exists to remove.
67118
+ *
67119
+ * `detail` is what the missing marker shows -- pass the original name or
67120
+ * the source path, the thing a person can act on.
67121
+ ***************************************************************************/
67122
+ function yui_asset_element(answer, opts = {}) {
67123
+ const src = yui_asset_src(answer);
67124
+ if (!src) return yui_asset_missing(opts.detail, opts);
67125
+ const ct = (answer.content_type || "").toLowerCase();
67126
+ const cls = `ASSET_MEDIA ${opts.class || ""}`.trim();
67127
+ let el;
67128
+ if (ct.startsWith("video/")) el = createElement2(["video", {
67129
+ class: cls,
67130
+ controls: "",
67131
+ preload: "metadata"
67132
+ }]);
67133
+ else if (ct.startsWith("audio/")) el = createElement2(["audio", {
67134
+ class: cls,
67135
+ controls: "",
67136
+ preload: "metadata"
67137
+ }]);
67138
+ else {
67139
+ el = createElement2(["img", {
67140
+ class: cls,
67141
+ loading: "lazy"
67142
+ }]);
67143
+ el.alt = opts.alt || opts.detail || "";
67144
+ }
67145
+ el.src = src;
67146
+ el.onerror = () => {
67147
+ const $marker = yui_asset_missing(opts.detail, opts);
67148
+ if (el.parentNode) el.parentNode.replaceChild($marker, el);
67149
+ };
67150
+ return el;
67151
+ }
67152
+ //#endregion
67041
67153
  //#region src/dev_machine_trace.js
67042
67154
  /***********************************************************************
67043
67155
  * dev_machine_trace.js
@@ -68721,6 +68833,6 @@ function setup_frontend_view(self) {
68721
68833
  return win;
68722
68834
  }
68723
68835
  //#endregion
68724
- export { EditControl, MarkerControl, YUI_PERIODS, YUI_PERIODS_DEFAULT, YUI_ROLLING, addClasses, apply_dev_traces, attach_clear, build_dev_panel, dev_window_was_open, disableElements, enableElements, epoch_to_local_input, epoch_to_ms, expose_view_container, fmt_epoch, getStrokeColor, infer_period, info_traffic, inject_svg_icons, is_current_period, iso_week, local_input_to_epoch, ms_to_epoch, period_bounds, period_bounds_epoch, period_label, period_name, period_shift, period_spec, period_start, refresh_clear, register_c_g6_nodes_tree, register_c_yui_form, register_c_yui_fsm_graph, register_c_yui_gclass, register_c_yui_gobj_tree_js, register_c_yui_json, register_c_yui_json_graph, register_c_yui_map, register_c_yui_nav, register_c_yui_pager, register_c_yui_period, register_c_yui_schema_editor, register_c_yui_service_view, register_c_yui_shell, register_c_yui_treedb_graph, register_c_yui_treedb_schema, register_c_yui_treedb_topic_with_form, register_c_yui_treedb_topics, register_c_yui_uplot, register_c_yui_window, register_c_yui_window_manager, register_c_yui_wizard, removeChildElements, removeClasses, rolling_bounds, safe_locale, set_active_state, set_cancel_state, set_pressed_state, set_submit_state, setup_dev, setup_frontend_view, toggleClasses, yui_is_dark, yui_mount_service_view, yui_shell_close_drawer, yui_shell_close_dropdown, yui_shell_confirm_danger, yui_shell_confirm_ok, yui_shell_confirm_yesno, yui_shell_confirm_yesnocancel, yui_shell_navigate, yui_shell_open_drawer, yui_shell_overlay_dismissed, yui_shell_pop_escape, yui_shell_push_escape, yui_shell_refresh_avatars, yui_shell_register_event_handler, yui_shell_register_overlay, yui_shell_set_avatar_provider, yui_shell_set_connection_state, yui_shell_set_sub_routes, yui_shell_set_toolbar_item_badge, yui_shell_set_toolbar_item_icon, yui_shell_set_translator, yui_shell_show_error, yui_shell_show_info, yui_shell_show_modal, yui_shell_show_route_map, yui_shell_show_warning, yui_shell_toggle_drawer, yui_shell_unpark_route, yui_theme_now, yui_toolbar, yui_watch_theme };
68836
+ export { EditControl, MarkerControl, YUI_PERIODS, YUI_PERIODS_DEFAULT, YUI_ROLLING, addClasses, apply_dev_traces, attach_clear, build_dev_panel, dev_window_was_open, disableElements, enableElements, epoch_to_local_input, epoch_to_ms, expose_view_container, fmt_epoch, getStrokeColor, infer_period, info_traffic, inject_svg_icons, is_current_period, iso_week, local_input_to_epoch, ms_to_epoch, period_bounds, period_bounds_epoch, period_label, period_name, period_shift, period_spec, period_start, refresh_clear, register_c_g6_nodes_tree, register_c_yui_form, register_c_yui_fsm_graph, register_c_yui_gclass, register_c_yui_gobj_tree_js, register_c_yui_json, register_c_yui_json_graph, register_c_yui_map, register_c_yui_nav, register_c_yui_pager, register_c_yui_period, register_c_yui_schema_editor, register_c_yui_service_view, register_c_yui_shell, register_c_yui_treedb_graph, register_c_yui_treedb_schema, register_c_yui_treedb_topic_with_form, register_c_yui_treedb_topics, register_c_yui_uplot, register_c_yui_window, register_c_yui_window_manager, register_c_yui_wizard, removeChildElements, removeClasses, rolling_bounds, safe_locale, set_active_state, set_cancel_state, set_pressed_state, set_submit_state, setup_dev, setup_frontend_view, toggleClasses, yui_asset_element, yui_asset_id, yui_asset_ids, yui_asset_missing, yui_asset_src, yui_is_dark, yui_mount_service_view, yui_shell_close_drawer, yui_shell_close_dropdown, yui_shell_confirm_danger, yui_shell_confirm_ok, yui_shell_confirm_yesno, yui_shell_confirm_yesnocancel, yui_shell_navigate, yui_shell_open_drawer, yui_shell_overlay_dismissed, yui_shell_pop_escape, yui_shell_push_escape, yui_shell_refresh_avatars, yui_shell_register_event_handler, yui_shell_register_overlay, yui_shell_set_avatar_provider, yui_shell_set_connection_state, yui_shell_set_sub_routes, yui_shell_set_toolbar_item_badge, yui_shell_set_toolbar_item_icon, yui_shell_set_translator, yui_shell_show_error, yui_shell_show_info, yui_shell_show_modal, yui_shell_show_route_map, yui_shell_show_warning, yui_shell_toggle_drawer, yui_shell_unpark_route, yui_theme_now, yui_toolbar, yui_watch_theme };
68725
68837
 
68726
68838
  //# sourceMappingURL=gobj-ui.es.js.map
package/index.js CHANGED
@@ -119,6 +119,7 @@ export {
119
119
  export { addClasses, removeClasses, toggleClasses, removeChildElements, disableElements, enableElements, set_submit_state, set_cancel_state, set_active_state, set_pressed_state, getStrokeColor } from "./src/lib_graph.js";
120
120
  export { inject_svg_icons } from "./src/lib_icons.js";
121
121
  export { EditControl, MarkerControl } from "./src/lib_maplibre.js";
122
+ export { yui_asset_id, yui_asset_ids, yui_asset_src, yui_asset_element, yui_asset_missing } from "./src/yui_asset.js";
122
123
  export { yui_toolbar } from "./src/yui_toolbar.js";
123
124
  export { yui_theme_now, yui_is_dark, yui_watch_theme } from "./src/yui_theme.js";
124
125
  export { attach_clear, refresh_clear } from "./src/yui_inputs.js";
@@ -131,6 +132,7 @@ export { setup_frontend_view } from "./src/yui_frontend_view.js";
131
132
  * import "gobj-ui/src/c_yui_shell.css"; // declarative shell
132
133
  * import "gobj-ui/src/c_yui_map.css";
133
134
  * import "gobj-ui/src/yui_toolbar.css";
135
+ * import "gobj-ui/src/yui_asset.css";
134
136
  * import "gobj-ui/src/lib_graph.css";
135
137
  * import "gobj-ui/src/yui_icons.css";
136
138
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yuneta/gobj-ui",
3
- "version": "7.23.45",
3
+ "version": "7.23.46",
4
4
  "type": "module",
5
5
  "main": "dist/gobj-ui.cjs.js",
6
6
  "module": "dist/gobj-ui.es.js",
@@ -20,7 +20,7 @@
20
20
  "src/",
21
21
  "vite-plugin-yuneta-html.js"
22
22
  ],
23
- "description": "Yuneta UI Library — the active line: declarative shell (C_YUI_SHELL+NAV+PAGER+WIZARD) + windows, TreeDB views, forms, charts and maps. Consumed by wattyzer and the yunetas JS yunos (gui_agent, gui_treedb). The frozen legacy GClass GUI stack (C_YUI_MAIN/TABS/ROUTING) lives on the v1 branch, npm dist-tag 'legacy' (consumed by estadodelaire and hidraulia).",
23
+ "description": "Yuneta UI Library \u2014 the active line: declarative shell (C_YUI_SHELL+NAV+PAGER+WIZARD) + windows, TreeDB views, forms, charts and maps. Consumed by wattyzer and the yunetas JS yunos (gui_agent, gui_treedb). The frozen legacy GClass GUI stack (C_YUI_MAIN/TABS/ROUTING) lives on the v1 branch, npm dist-tag 'legacy' (consumed by estadodelaire and hidraulia).",
24
24
  "scripts": {
25
25
  "build": "vite build",
26
26
  "test": "vitest run",
@@ -0,0 +1,43 @@
1
+ /*
2
+ * The marker of an asset that is not there.
3
+ *
4
+ * It takes the SPACE an image would have taken, so a card does not jump
5
+ * when one is missing, and it is quiet enough to sit in a gallery without
6
+ * shouting -- it reports a fact, it is not an error dialog.
7
+ */
8
+ .ASSET_MISSING {
9
+ display: flex;
10
+ flex-direction: column;
11
+ align-items: center;
12
+ justify-content: center;
13
+ gap: 0.25rem;
14
+ min-width: 5rem;
15
+ min-height: 5rem;
16
+ padding: 0.5rem;
17
+ border: 1px dashed var(--bulma-border, #dbdbdb);
18
+ border-radius: var(--bulma-radius, 4px);
19
+ color: var(--bulma-text-weak, #7a7a7a);
20
+ background: var(--bulma-scheme-main-bis, transparent);
21
+ text-align: center;
22
+ }
23
+
24
+ .ASSET_MISSING_ICON {
25
+ font-size: 1.25rem;
26
+ opacity: 0.7;
27
+ }
28
+
29
+ .ASSET_MISSING_LABEL {
30
+ font-size: 0.75rem;
31
+ }
32
+
33
+ /*
34
+ * The detail is a name or a path: it must not stretch the card, and it is
35
+ * the one thing a person can act on, so it stays readable rather than
36
+ * being cut to nothing.
37
+ */
38
+ .ASSET_MISSING_DETAIL {
39
+ font-size: 0.7rem;
40
+ opacity: 0.75;
41
+ max-width: 12rem;
42
+ overflow-wrap: anywhere;
43
+ }
@@ -0,0 +1,180 @@
1
+ /*
2
+ * The bytes a treedb node owns but cannot hold -- a photo, a plan, a clip.
3
+ *
4
+ * A node names its asset with an FKEY, and the BACKEND decides how the
5
+ * bytes travel: a signed url when a web server sits in front of the store,
6
+ * the bytes inline when there is none (the SDK's `C_ASSETS get-asset`).
7
+ * That is why a consumer has ONE code path, and why a node with no web
8
+ * server in front of it still shows its images instead of showing nothing.
9
+ *
10
+ * WHAT THIS MODULE DOES NOT DO IS TALK TO THE BACKEND. Asking is an
11
+ * ACTION and belongs in the view's own FSM; these are the two ends of it:
12
+ * read the id out of the fkey before asking, and turn the answer into an
13
+ * element afterwards.
14
+ *
15
+ * Using Bulma Framework (https://bulma.io)
16
+ */
17
+
18
+ import {
19
+ createElement2
20
+ } from "@yuneta/gobj-js";
21
+
22
+ import "./yui_asset.css";
23
+
24
+ /*
25
+ * A treedb fkey is `topic^id^hook`, and a column holds either one of them
26
+ * (a single-valued fkey, like a device's photo) or a list (an array fkey,
27
+ * like the plans of a bay). An expanded ref -- what `refs` options give
28
+ * back -- is an object carrying the id instead.
29
+ */
30
+ function ref_to_id(ref)
31
+ {
32
+ if(!ref) {
33
+ return null;
34
+ }
35
+ if(typeof ref === "string") {
36
+ const parts = ref.split("^");
37
+ return parts.length >= 2? parts[1]: null;
38
+ }
39
+ if(typeof ref === "object" && typeof ref.id === "string") {
40
+ return ref.id;
41
+ }
42
+ return null;
43
+ }
44
+
45
+ /***************************************************************************
46
+ * Every asset id a column names, in order. Always an array, possibly empty.
47
+ ***************************************************************************/
48
+ function yui_asset_ids(ref)
49
+ {
50
+ if(Array.isArray(ref)) {
51
+ return ref.map(ref_to_id).filter((id) => !!id);
52
+ }
53
+ const id = ref_to_id(ref);
54
+ return id? [id]: [];
55
+ }
56
+
57
+ /***************************************************************************
58
+ * The first asset id a column names, or null.
59
+ ***************************************************************************/
60
+ function yui_asset_id(ref)
61
+ {
62
+ const ids = yui_asset_ids(ref);
63
+ return ids.length? ids[0]: null;
64
+ }
65
+
66
+ /***************************************************************************
67
+ * Turn a `get-asset` answer into something an element can load.
68
+ *
69
+ * Returns null when the answer carries neither -- which the caller must
70
+ * treat as "missing", never as an empty src: an <img src=""> reloads the
71
+ * PAGE in some browsers.
72
+ ***************************************************************************/
73
+ function yui_asset_src(answer)
74
+ {
75
+ if(!answer) {
76
+ return null;
77
+ }
78
+ if(answer.mode === "url" && answer.url) {
79
+ return answer.url;
80
+ }
81
+ if(answer.mode === "inline" && answer.content64) {
82
+ const ct = answer.content_type || "application/octet-stream";
83
+ return `data:${ct};base64,${answer.content64}`;
84
+ }
85
+ return null;
86
+ }
87
+
88
+ /***************************************************************************
89
+ * The marker for an asset that is not there.
90
+ *
91
+ * A missing image used to leave a broken box and no word about it, which
92
+ * is indistinguishable from a slow one and from a bug. This says so, and
93
+ * it says WHICH: `detail` is data (a name, a path) and is never
94
+ * translated, while the label carries its i18n key so a language change
95
+ * re-translates it.
96
+ ***************************************************************************/
97
+ function yui_asset_missing(detail, opts = {})
98
+ {
99
+ const key = opts.key || "asset not available";
100
+ const children = [
101
+ ["span", {class: "ASSET_MISSING_ICON yi-triangle-exclamation"}],
102
+ /*
103
+ * createElement2() translates the content itself when it is given
104
+ * `i18n`, so the KEY is what goes in -- passing t(key) would
105
+ * translate it twice and, worse, freeze it: refresh_language()
106
+ * only re-translates a node that carries its key.
107
+ */
108
+ ["span", {class: "ASSET_MISSING_LABEL", i18n: key}, key]
109
+ ];
110
+ if(detail) {
111
+ children.push(["span", {class: "ASSET_MISSING_DETAIL"}, String(detail)]);
112
+ }
113
+ /*
114
+ * The tooltip is the DETAIL when there is one -- a name or a path,
115
+ * which is data and must never be translated -- and the label
116
+ * otherwise, through `data-i18n-title` so it follows the language.
117
+ */
118
+ const attrs = {class: `ASSET_MISSING ${opts.class || ""}`.trim()};
119
+ if(detail) {
120
+ attrs.title = String(detail);
121
+ } else {
122
+ attrs["data-i18n-title"] = key;
123
+ }
124
+ return createElement2(["div", attrs, children]);
125
+ }
126
+
127
+ /***************************************************************************
128
+ * The element for one asset.
129
+ *
130
+ * The KIND comes from the content type the backend stored, not from the
131
+ * name a person typed: video and audio are assets too, and an <img> whose
132
+ * src is a film shows the broken box this module exists to remove.
133
+ *
134
+ * `detail` is what the missing marker shows -- pass the original name or
135
+ * the source path, the thing a person can act on.
136
+ ***************************************************************************/
137
+ function yui_asset_element(answer, opts = {})
138
+ {
139
+ const src = yui_asset_src(answer);
140
+ if(!src) {
141
+ return yui_asset_missing(opts.detail, opts);
142
+ }
143
+
144
+ const ct = (answer.content_type || "").toLowerCase();
145
+ const cls = `ASSET_MEDIA ${opts.class || ""}`.trim();
146
+
147
+ let el;
148
+ if(ct.startsWith("video/")) {
149
+ el = createElement2(["video", {class: cls, controls: "", preload: "metadata"}]);
150
+ } else if(ct.startsWith("audio/")) {
151
+ el = createElement2(["audio", {class: cls, controls: "", preload: "metadata"}]);
152
+ } else {
153
+ el = createElement2(["img", {class: cls, loading: "lazy"}]);
154
+ el.alt = opts.alt || opts.detail || "";
155
+ }
156
+ el.src = src;
157
+
158
+ /*
159
+ * A url can 403 (expired signature), a blob can be gone from the
160
+ * store, a codec can be unsupported. Whatever the reason, the element
161
+ * is REPLACED by the marker: leaving the dead one in place is the
162
+ * silent gap this module was written to stop.
163
+ */
164
+ el.onerror = () => {
165
+ const $marker = yui_asset_missing(opts.detail, opts);
166
+ if(el.parentNode) {
167
+ el.parentNode.replaceChild($marker, el);
168
+ }
169
+ };
170
+
171
+ return el;
172
+ }
173
+
174
+ export {
175
+ yui_asset_id,
176
+ yui_asset_ids,
177
+ yui_asset_src,
178
+ yui_asset_element,
179
+ yui_asset_missing
180
+ };
@@ -0,0 +1,79 @@
1
+ /***********************************************************************
2
+ * yui_asset.test.js
3
+ *
4
+ * Unit tests for the pure logic of yui_asset.
5
+ * Run with: npm test
6
+ ***********************************************************************/
7
+ import { test, expect } from "vitest";
8
+ import {
9
+ yui_asset_id,
10
+ yui_asset_ids,
11
+ yui_asset_src,
12
+ } from "./yui_asset.js";
13
+
14
+ /*
15
+ * A treedb fkey is `topic^id^hook`.
16
+ */
17
+ test("reads the id out of a single-valued fkey", () => {
18
+ expect(yui_asset_id("assets^abc123^as_foto")).toBe("abc123");
19
+ expect(yui_asset_ids("assets^abc123^as_foto")).toEqual(["abc123"]);
20
+ });
21
+
22
+ test("reads every id out of an array fkey, in order", () => {
23
+ expect(yui_asset_ids([
24
+ "assets^p1^as_plano",
25
+ "assets^p2^as_plano",
26
+ ])).toEqual(["p1", "p2"]);
27
+ expect(yui_asset_id(["assets^p1^as_plano", "assets^p2^as_plano"])).toBe("p1");
28
+ });
29
+
30
+ test("reads an EXPANDED ref, which is what the refs options give back", () => {
31
+ expect(yui_asset_id({id: "abc123", content_type: "image/jpeg"})).toBe("abc123");
32
+ });
33
+
34
+ /*
35
+ * An empty column is the normal state of a device nobody photographed. It
36
+ * must answer "nothing", never throw and never a half-parsed id.
37
+ */
38
+ test("an empty or malformed column yields nothing, and does not throw", () => {
39
+ expect(yui_asset_ids(null)).toEqual([]);
40
+ expect(yui_asset_ids(undefined)).toEqual([]);
41
+ expect(yui_asset_ids("")).toEqual([]);
42
+ expect(yui_asset_ids([])).toEqual([]);
43
+ expect(yui_asset_id(null)).toBe(null);
44
+ expect(yui_asset_id("not-an-fkey")).toBe(null);
45
+ expect(yui_asset_id({})).toBe(null);
46
+ expect(yui_asset_ids(["assets^ok^as_foto", "rubbish", null])).toEqual(["ok"]);
47
+ });
48
+
49
+ /*
50
+ * The two shapes the backend answers with, and the ONE code path they
51
+ * exist to give the caller.
52
+ */
53
+ test("a signed url is used as it comes", () => {
54
+ expect(yui_asset_src({
55
+ mode: "url",
56
+ url: "/assets/ab/cd/abcd.jpg?e=1&s=tok",
57
+ })).toBe("/assets/ab/cd/abcd.jpg?e=1&s=tok");
58
+ });
59
+
60
+ test("inline bytes become a data url carrying their own type", () => {
61
+ expect(yui_asset_src({
62
+ mode: "inline",
63
+ content_type: "image/jpeg",
64
+ content64: "AAAA",
65
+ })).toBe("data:image/jpeg;base64,AAAA");
66
+ });
67
+
68
+ /*
69
+ * Anything else is MISSING, and must come back as null rather than as an
70
+ * empty string: <img src=""> reloads the page in some browsers, which is a
71
+ * worse failure than the one being reported.
72
+ */
73
+ test("an unusable answer is null, never an empty src", () => {
74
+ expect(yui_asset_src(null)).toBe(null);
75
+ expect(yui_asset_src({})).toBe(null);
76
+ expect(yui_asset_src({mode: "url"})).toBe(null);
77
+ expect(yui_asset_src({mode: "inline", content_type: "image/png"})).toBe(null);
78
+ expect(yui_asset_src({mode: "url", url: ""})).toBe(null);
79
+ });