@yuneta/gobj-ui 7.32.9 → 7.32.11

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,331 @@
1
+ /***********************************************************************
2
+ * yui_preferences.js
3
+ *
4
+ * The Preferences page of an app: what the READER chose for this
5
+ * browser. Every app shows at least the same three rows, in one
6
+ * box headed "Appearance":
7
+ *
8
+ * Theme light theme | dark theme
9
+ * Language the app's languages, each in its own words
10
+ * Navigation stacked strips | back to parent | breadcrumb
11
+ *
12
+ * Two ways to use it:
13
+ *
14
+ * - C_YUI_PREFERENCES, a whole page: a view the shell mounts at
15
+ * /preferences. It draws the three rows, then the boxes the app
16
+ * adds (the `sections` of the setup).
17
+ * - The parts, for an app whose page has fields of its own with
18
+ * actions of its own: yui_pref_header(), yui_pref_field(),
19
+ * yui_pref_segment() and yui_pref_appearance_fields(). The host
20
+ * declares EV_SET_THEME, EV_SET_LANGUAGE and EV_SET_NAV_MODE
21
+ * ({value}) and hands them to yui_pref_apply().
22
+ *
23
+ * The theme and the language are the APP's: it says once, at start
24
+ * up, how to read them and how to change them, with
25
+ * yui_preferences_setup(). The navigation is the shell's
26
+ * (yui_shell_nav_mode / yui_shell_set_nav_mode): every C_YUI_NODE
27
+ * tree with no mode of its own follows it, and an app with no tree
28
+ * keeps the choice for the day it has one.
29
+ *
30
+ * A language is SHOWN and NAMED in its own words ("Español", with
31
+ * its `lang`): a reader looking for their language finds it in the
32
+ * language they read, and an app of ten languages needs no key per
33
+ * language name.
34
+ *
35
+ * Copyright (c) 2026, ArtGins.
36
+ * All Rights Reserved.
37
+ ***********************************************************************/
38
+ import {
39
+ log_error,
40
+ gobj_send_event,
41
+ refresh_language,
42
+ } from "@yuneta/gobj-js";
43
+
44
+ import {t} from "i18next";
45
+
46
+ import {yui_toolbar_icon} from "./yui_toolbar_icon.js";
47
+ import {text_node} from "./yui_text.js";
48
+ import {
49
+ yui_shell_of,
50
+ yui_shell_language_changed,
51
+ yui_shell_nav_mode,
52
+ yui_shell_set_nav_mode,
53
+ } from "./c_yui_shell.js";
54
+
55
+ /***************************************************************
56
+ * Constants
57
+ ***************************************************************/
58
+ /* The keys this module hands to t() as variables (the options of a
59
+ * segment): published for validate-locales. */
60
+ export const YUI_PREFERENCES_I18N_KEYS = [
61
+ "light theme", "dark theme",
62
+ "stacked strips", "back to parent", "breadcrumb"
63
+ ];
64
+
65
+ const THEMES = [
66
+ {value: "light", key: "light theme", icon: "yi-sun"},
67
+ {value: "dark", key: "dark theme", icon: "yi-moon"}
68
+ ];
69
+
70
+ const NAV_MODE_OPTIONS = [
71
+ {value: "stack", key: "stacked strips", icon: "yi-layer-group"},
72
+ {value: "back", key: "back to parent", icon: "yi-arrow-left"},
73
+ {value: "path", key: "breadcrumb", icon: "yi-route"}
74
+ ];
75
+
76
+ /* What the app said at start up (yui_preferences_setup). */
77
+ let __setup__ = null;
78
+
79
+
80
+
81
+
82
+ /***************************
83
+ * Setup
84
+ ***************************/
85
+
86
+
87
+
88
+
89
+ /***************************************************************
90
+ * How this app reads and changes its theme and its language, and
91
+ * the boxes it adds to the page. Called once, at start up, before
92
+ * the page is shown:
93
+ *
94
+ * yui_preferences_setup({
95
+ * current_theme: () => "dark",
96
+ * set_theme: (value) => {...},
97
+ * languages: [{value: "en", text: "English"},
98
+ * {value: "es", text: "Español"}],
99
+ * current_language: () => "es",
100
+ * set_language: (value) => {...}, // the app's i18next
101
+ * sections: (gobj) => [...], // optional: boxes
102
+ * });
103
+ *
104
+ * set_language() switches the app's i18next; the shell is told after
105
+ * it (yui_shell_language_changed), so the app does not do it twice.
106
+ ***************************************************************/
107
+ function yui_preferences_setup(opts)
108
+ {
109
+ let o = opts || {};
110
+ let needed = ["current_theme", "set_theme", "current_language", "set_language"];
111
+ for(let name of needed) {
112
+ if(typeof o[name] !== "function") {
113
+ log_error(`yui_preferences_setup(): '${name}' must be a function`);
114
+ return -1;
115
+ }
116
+ }
117
+ if(!Array.isArray(o.languages) || o.languages.length === 0) {
118
+ log_error(`yui_preferences_setup(): 'languages' must list the app's languages`);
119
+ return -1;
120
+ }
121
+ if(o.sections !== undefined && typeof o.sections !== "function") {
122
+ log_error(`yui_preferences_setup(): 'sections' must be a function`);
123
+ return -1;
124
+ }
125
+ __setup__ = o;
126
+ return 0;
127
+ }
128
+
129
+ /* What the app said, or null (and an error) when it said nothing. */
130
+ function setup_of(who)
131
+ {
132
+ if(!__setup__) {
133
+ log_error(`${who}: no yui_preferences_setup() — the app must call it at start up`);
134
+ }
135
+ return __setup__;
136
+ }
137
+
138
+
139
+
140
+
141
+ /***************************
142
+ * Parts of the page
143
+ ***************************/
144
+
145
+
146
+
147
+
148
+ /***************************************************************
149
+ * The title of the page and the name of its first box.
150
+ ***************************************************************/
151
+ function yui_pref_header()
152
+ {
153
+ return ["div", {class: "PREF_HEADER", style: "margin-bottom:0.5rem;"}, [
154
+ ["h1", {class: "PREF_TITLE title is-3 has-text-link", style: "margin-bottom:0.25rem;",
155
+ i18n: "preferences"}, "preferences"],
156
+ ["p", {class: "PREF_SUBTITLE subtitle is-6 has-text-grey", style: "margin-bottom:1rem;",
157
+ i18n: "appearance"}, "appearance"]
158
+ ]];
159
+ }
160
+
161
+ /***************************************************************
162
+ * A box of fields, as wide as the page reads well.
163
+ ***************************************************************/
164
+ function yui_pref_box(fields, extra_class)
165
+ {
166
+ return ["div", {class: `PREF_BOX box ${extra_class || ""}`, style: "max-width:540px;"},
167
+ fields];
168
+ }
169
+
170
+ /***************************************************************
171
+ * One field: its label over its control.
172
+ ***************************************************************/
173
+ function yui_pref_field(label_key, control, extra_class)
174
+ {
175
+ return ["div", {class: `PREF_FIELD field ${extra_class || ""}`,
176
+ style: "margin-bottom:1.25rem;"}, [
177
+ ["label", {class: "PREF_LABEL label", i18n: label_key}, label_key],
178
+ ["div", {class: "PREF_CONTROL control"}, [control]]
179
+ ]];
180
+ }
181
+
182
+ /***************************************************************
183
+ * A segmented choice: one button per option, the current one
184
+ * marked (`is-primary is-selected`). An option is
185
+ *
186
+ * {value, key, icon} the label and the name are the key
187
+ * {value, text, lang} the label and the name are the text,
188
+ * which is data (a language's own name)
189
+ *
190
+ * A click sends `event_name` {value} to `gobj`: the click is an OS
191
+ * notification, the change is an action of the host.
192
+ ***************************************************************/
193
+ function yui_pref_segment(gobj, event_name, options, current, extra_class)
194
+ {
195
+ let buttons = options.map(function(opt) {
196
+ let active = (opt.value === current);
197
+ let cls = "PREF_SEGMENT_ITEM button" + (active ? " is-primary is-selected" : "");
198
+ let kids = [];
199
+ if(opt.icon) {
200
+ kids.push(["span", {class: "icon"}, [yui_toolbar_icon(opt.icon)]]);
201
+ }
202
+ let attrs = {type: "button", class: cls, "aria-pressed": active ? "true" : "false"};
203
+ if(opt.key) {
204
+ kids.push(["span", {class: "PREF_SEGMENT_TEXT",
205
+ i18n: /* i18n: YUI_PREFERENCES_I18N_KEYS */ opt.key}, opt.key]);
206
+ attrs.title = t(/* i18n: YUI_PREFERENCES_I18N_KEYS */ opt.key);
207
+ attrs["data-i18n-title"] = opt.key;
208
+ attrs["aria-label"] = t(/* i18n: YUI_PREFERENCES_I18N_KEYS */ opt.key);
209
+ attrs["data-i18n-aria-label"] = opt.key;
210
+ } else {
211
+ kids.push(["span", {class: "PREF_SEGMENT_TEXT", lang: opt.lang || opt.value},
212
+ text_node(opt.text)]);
213
+ attrs.title = opt.text;
214
+ attrs["aria-label"] = opt.text;
215
+ attrs.lang = opt.lang || opt.value;
216
+ }
217
+ return ["button", attrs, kids, {
218
+ click: function() {
219
+ gobj_send_event(gobj, event_name, {value: opt.value}, gobj);
220
+ }
221
+ }];
222
+ });
223
+ return ["div", {class: `PREF_SEGMENT buttons has-addons ${extra_class || ""}`}, buttons];
224
+ }
225
+
226
+ /***************************************************************
227
+ * The three rows every app shows: theme, language, navigation.
228
+ * Their clicks send EV_SET_THEME / EV_SET_LANGUAGE /
229
+ * EV_SET_NAV_MODE {value} to `gobj`.
230
+ ***************************************************************/
231
+ function yui_pref_appearance_fields(gobj)
232
+ {
233
+ let setup = setup_of("yui_pref_appearance_fields()");
234
+ if(!setup) {
235
+ return []; // Error already logged
236
+ }
237
+ let shell = yui_shell_of(gobj);
238
+ let languages = setup.languages.map((l) => ({value: l.value, text: l.text, lang: l.value}));
239
+
240
+ return [
241
+ yui_pref_field("theme",
242
+ yui_pref_segment(gobj, "EV_SET_THEME", THEMES, setup.current_theme(), "PREF_THEME"),
243
+ "PREF_THEME_FIELD"),
244
+ yui_pref_field("language",
245
+ /* The languages are the app's, each named in its own words: data */
246
+ yui_pref_segment(gobj, "EV_SET_LANGUAGE", /* i18n: data */ languages,
247
+ setup.current_language(),
248
+ "PREF_LANGUAGE"),
249
+ "PREF_LANGUAGE_FIELD"),
250
+ yui_pref_field("navigation",
251
+ yui_pref_segment(gobj, "EV_SET_NAV_MODE", NAV_MODE_OPTIONS,
252
+ shell ? yui_shell_nav_mode(shell) : "stack", "PREF_NAVIGATION"),
253
+ "PREF_NAVIGATION_FIELD")
254
+ ];
255
+ }
256
+
257
+ /***************************************************************
258
+ * The extra boxes of the app (its `sections`), or none.
259
+ ***************************************************************/
260
+ function yui_pref_sections(gobj)
261
+ {
262
+ let setup = setup_of("yui_pref_sections()");
263
+ if(!setup || !setup.sections) {
264
+ return [];
265
+ }
266
+ let boxes = setup.sections(gobj);
267
+ return Array.isArray(boxes) ? boxes : [];
268
+ }
269
+
270
+
271
+
272
+
273
+ /***************************
274
+ * Applying a choice
275
+ ***************************/
276
+
277
+
278
+
279
+
280
+ /***************************************************************
281
+ * Apply one of the three choices: EV_SET_THEME, EV_SET_LANGUAGE
282
+ * or EV_SET_NAV_MODE, with its {value}. The host calls it from
283
+ * its action and draws its page again: which option is marked is
284
+ * markup, which no refresh_language() moves.
285
+ ***************************************************************/
286
+ function yui_pref_apply(gobj, event, kw)
287
+ {
288
+ let setup = setup_of("yui_pref_apply()");
289
+ if(!setup) {
290
+ return -1; // Error already logged
291
+ }
292
+ let value = (kw && kw.value) || "";
293
+ let shell = yui_shell_of(gobj);
294
+
295
+ switch(event) {
296
+ case "EV_SET_THEME":
297
+ setup.set_theme(value);
298
+ return 0;
299
+
300
+ case "EV_SET_LANGUAGE":
301
+ setup.set_language(value);
302
+ if(shell) {
303
+ yui_shell_language_changed(shell);
304
+ } else {
305
+ refresh_language(document.body, t);
306
+ }
307
+ return 0;
308
+
309
+ case "EV_SET_NAV_MODE":
310
+ if(!shell) {
311
+ log_error(`yui_pref_apply(): no shell, the navigation cannot change`);
312
+ return -1;
313
+ }
314
+ return yui_shell_set_nav_mode(shell, value);
315
+
316
+ default:
317
+ log_error(`yui_pref_apply(): '${event}' is not a preference`);
318
+ return -1;
319
+ }
320
+ }
321
+
322
+ export {
323
+ yui_preferences_setup,
324
+ yui_pref_header,
325
+ yui_pref_box,
326
+ yui_pref_field,
327
+ yui_pref_segment,
328
+ yui_pref_appearance_fields,
329
+ yui_pref_sections,
330
+ yui_pref_apply,
331
+ };