@yuneta/gobj-ui 7.27.22 → 7.27.23

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.
@@ -0,0 +1,246 @@
1
+ /***********************************************************************
2
+ * yui_gobj_tree_view.js
3
+ *
4
+ * The gobj tree of a yuno (C_YUI_GOBJ_TREE_JS) in a window of
5
+ * its own -- a sheet on a phone -- for a HOST that is not the
6
+ * Frontend view: typically one showing the tree of a BACKEND
7
+ * yuno (`source: "remote"`), which has to answer what the tree
8
+ * asks for (see the header of c_yui_gobj_tree_js.js).
9
+ *
10
+ * The tree is a child of the host and publishes to it, so the
11
+ * host's FSM declares the four remote output events. The window
12
+ * is only its frame.
13
+ *
14
+ * let handle = open_gobj_tree_view(gobj, {
15
+ * name: "gate_energia-2046",
16
+ * source: "remote",
17
+ * yuno_name: "gate_energia^2046",
18
+ * title_prefix: "gate_energia^2046",
19
+ * title: "gobj tree", // an i18n KEY
20
+ * $header: $status, // optional
21
+ * on_close: () => gobj_send_event(gobj, "EV_VIEW_CLOSED", {}, gobj),
22
+ * });
23
+ * ...
24
+ * gobj_send_event(handle.tree, "EV_LOAD_TREE", {tree: answer.data}, gobj);
25
+ * ...
26
+ * close_gobj_tree_view(handle); // in mt_stop
27
+ *
28
+ * Copyright (c) 2026, ArtGins.
29
+ * All Rights Reserved.
30
+ ***********************************************************************/
31
+ import {
32
+ clean_name,
33
+ createElement2,
34
+ gobj_create,
35
+ gobj_create_service,
36
+ gobj_destroy,
37
+ gobj_find_service,
38
+ gobj_is_running,
39
+ gobj_read_pointer_attr,
40
+ gobj_short_name,
41
+ gobj_start,
42
+ gobj_stop,
43
+ is_gobj,
44
+ log_error,
45
+ log_warning,
46
+ } from "@yuneta/gobj-js";
47
+
48
+ import {yui_shell_show_modal} from "./shell_modals.js";
49
+ import {yui_shell_of} from "./c_yui_shell.js";
50
+
51
+ import {t} from "i18next";
52
+
53
+
54
+ /************************************************************
55
+ * Open the tree.
56
+ *
57
+ * opts:
58
+ * name what tells this tree from another one of the
59
+ * same host: it names the window and the tree,
60
+ * and the tree keeps the reader's arrangement
61
+ * (layout, folds, camera) under it.
62
+ * source "local" | "remote" (the tree's attr).
63
+ * yuno_name remote: the name the yuno is shown by.
64
+ * title an i18n KEY, the kind of window.
65
+ * title_prefix the DATA half of the title (the yuno).
66
+ * $header an element shown above the tree (a status
67
+ * line of the host), or nothing.
68
+ * on_close called when the reader dismisses it (✕).
69
+ *
70
+ * Returns {tree, win, modal}, or null (logged).
71
+ ************************************************************/
72
+ function open_gobj_tree_view(host, opts)
73
+ {
74
+ opts = opts || {};
75
+
76
+ if(!host || !opts.name) {
77
+ log_error(`yui_gobj_tree_view: open without a host or a name`);
78
+ return null;
79
+ }
80
+ let name = clean_name(opts.name);
81
+
82
+ let tree = gobj_create(
83
+ `gobj-tree-${name}`,
84
+ "C_YUI_GOBJ_TREE_JS",
85
+ {
86
+ source: opts.source || "local",
87
+ yuno_name: opts.yuno_name || "",
88
+ subscriber: host,
89
+ },
90
+ host
91
+ );
92
+ if(!tree) {
93
+ log_error(`${gobj_short_name(host)}: cannot create the gobj tree '${name}'`);
94
+ return null;
95
+ }
96
+
97
+ /* CREATED, not started: its mt_start builds the graph, which
98
+ * measures a canvas and observes its size -- it has to be in the
99
+ * document, and the window below is what puts it there. */
100
+ let $tree = gobj_read_pointer_attr(tree, "$container");
101
+ let $box = createElement2(
102
+ ["div", {class: "GOBJ_TREE_VIEW_BODY",
103
+ style: "height:100%; display:flex; flex-direction:column; min-height:0;"}, []]
104
+ );
105
+ if(opts.$header) {
106
+ $box.appendChild(opts.$header);
107
+ }
108
+ let $grow = createElement2(
109
+ ["div", {class: "GOBJ_TREE_VIEW_TREE", style: "flex:1 1 auto; min-height:0;"}, []]
110
+ );
111
+ $grow.appendChild($tree);
112
+ $box.appendChild($grow);
113
+
114
+ let handle = {tree: tree, win: null, modal: null};
115
+
116
+ /* The ✕ path. `close_window()` calls this and THEN destroys the
117
+ * window, so the window (or the sheet) is cleared FIRST, and the
118
+ * tree -- which is not the window's child -- is taken down here,
119
+ * or it stays alive and running with nobody to see it. */
120
+ let on_close = () => {
121
+ handle.win = null;
122
+ handle.modal = null;
123
+ close_gobj_tree_view(handle);
124
+
125
+ if(typeof opts.on_close === "function") {
126
+ opts.on_close();
127
+ }
128
+ };
129
+
130
+ if(is_mobile()) {
131
+ let shell = yui_shell_of(host);
132
+ if(!shell) {
133
+ log_error(`${gobj_short_name(host)}: no shell, cannot open the gobj tree sheet`);
134
+ close_gobj_tree_view(handle);
135
+ return null;
136
+ }
137
+ handle.modal = yui_shell_show_modal(shell, /* i18n: data */ $box, {
138
+ dialog: true,
139
+ logical_class: "GOBJ_TREE_VIEW_SHEET",
140
+ title_prefix: opts.title_prefix || "",
141
+ title: opts.title || "",
142
+ t: t,
143
+ on_close: on_close,
144
+ });
145
+ gobj_start(tree); /* mounted: now it can measure itself */
146
+ return handle;
147
+ }
148
+
149
+ handle.win = gobj_create_service(
150
+ `gobj-tree-win-${name}`,
151
+ "C_YUI_WINDOW",
152
+ {
153
+ $parent: (typeof document !== "undefined" &&
154
+ document.getElementById("top-layer")) || null,
155
+ subscriber: null,
156
+ modal: false,
157
+ showMax: true,
158
+ showFooter: false,
159
+ resizable: true,
160
+ center: true,
161
+ auto_save_size_and_position: true,
162
+ width: 960,
163
+ height: 680,
164
+ logical_class: "GOBJ_TREE_VIEW_WINDOW",
165
+ title_prefix: opts.title_prefix || "",
166
+ title: opts.title || "",
167
+ icon: "yi-hexagon-nodes",
168
+ body: $box,
169
+ manager: gobj_find_service("__window_manager__", false) || null,
170
+ on_close: on_close,
171
+ },
172
+ host
173
+ );
174
+ if(!handle.win) {
175
+ log_error(`${gobj_short_name(host)}: cannot create the gobj tree window '${name}'`);
176
+ close_gobj_tree_view(handle);
177
+ return null;
178
+ }
179
+ gobj_start(handle.win);
180
+ gobj_start(tree); /* mounted: now it can measure itself */
181
+
182
+ return handle;
183
+ }
184
+
185
+ /************************************************************
186
+ * Close whatever open_gobj_tree_view() opened. Safe on null
187
+ * and on a handle whose window the reader already dismissed.
188
+ ************************************************************/
189
+ function close_gobj_tree_view(handle)
190
+ {
191
+ if(!handle) {
192
+ return;
193
+ }
194
+
195
+ let tree = handle.tree;
196
+ let win = handle.win;
197
+ let modal = handle.modal;
198
+
199
+ handle.tree = null;
200
+ handle.win = null;
201
+ handle.modal = null;
202
+
203
+ if(win && is_gobj(win)) {
204
+ try {
205
+ /* STOP, then destroy: gobj_destroy() of a running gobj
206
+ * logs and skips mt_stop. The ✕ path already stopped it. */
207
+ if(gobj_is_running(win)) {
208
+ gobj_stop(win);
209
+ }
210
+ gobj_destroy(win);
211
+ } catch(e) {
212
+ log_warning(`yui_gobj_tree_view: window already gone: ${e}`);
213
+ }
214
+ }
215
+ if(modal && typeof modal.close === "function") {
216
+ try {
217
+ modal.close();
218
+ } catch(e) {
219
+ log_warning(`yui_gobj_tree_view: sheet already gone: ${e}`);
220
+ }
221
+ }
222
+ if(tree && is_gobj(tree)) {
223
+ try {
224
+ if(gobj_is_running(tree)) {
225
+ gobj_stop(tree);
226
+ }
227
+ gobj_destroy(tree);
228
+ } catch(e) {
229
+ log_warning(`yui_gobj_tree_view: tree already gone: ${e}`);
230
+ }
231
+ }
232
+ }
233
+
234
+ /************************************************************
235
+ * A window is not a sheet: below this width there is no room
236
+ * for a title bar the reader can drag.
237
+ ************************************************************/
238
+ function is_mobile()
239
+ {
240
+ return typeof window !== "undefined" && window.innerWidth <= 768;
241
+ }
242
+
243
+ export {
244
+ open_gobj_tree_view,
245
+ close_gobj_tree_view,
246
+ };