ozdenui 0.1.0
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.
- checksums.yaml +7 -0
- data/LICENSE +14 -0
- data/NOTICE +5 -0
- data/README.md +242 -0
- data/exe/ozdenui +17 -0
- data/lib/ozdenui/AGENTS.md +210 -0
- data/lib/ozdenui/COMPONENTS.md +3017 -0
- data/lib/ozdenui/app.rb +749 -0
- data/lib/ozdenui/command.rb +107 -0
- data/lib/ozdenui/components.rb +368 -0
- data/lib/ozdenui/dialogs.rb +212 -0
- data/lib/ozdenui/files.rb +124 -0
- data/lib/ozdenui/icon.png +0 -0
- data/lib/ozdenui/kit.json +1 -0
- data/lib/ozdenui/kit.rb +115 -0
- data/lib/ozdenui/look.rb +229 -0
- data/lib/ozdenui/objects.rb +652 -0
- data/lib/ozdenui/server.rb +270 -0
- data/lib/ozdenui/values.rb +273 -0
- data/lib/ozdenui/version.rb +15 -0
- data/lib/ozdenui/web/basic/LICENSE +14 -0
- data/lib/ozdenui/web/basic/basic.min.css +1 -0
- data/lib/ozdenui/web/basic/basic.min.js +78 -0
- data/lib/ozdenui/web/basic/font/open-sans/LICENSE.txt +206 -0
- data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Bold.ttf +0 -0
- data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Regular.ttf +0 -0
- data/lib/ozdenui/web/basic/scroll-bar.min.js +3 -0
- data/lib/ozdenui/web/bridge.js +584 -0
- data/lib/ozdenui/web/comp/accordion.js +659 -0
- data/lib/ozdenui/web/comp/action-button/add.png +0 -0
- data/lib/ozdenui/web/comp/action-button.js +521 -0
- data/lib/ozdenui/web/comp/action-tab-bar.js +383 -0
- data/lib/ozdenui/web/comp/app-bar.js +237 -0
- data/lib/ozdenui/web/comp/badge.js +292 -0
- data/lib/ozdenui/web/comp/bottom-bar.js +555 -0
- data/lib/ozdenui/web/comp/breadcrumbs.js +524 -0
- data/lib/ozdenui/web/comp/canvas.js +652 -0
- data/lib/ozdenui/web/comp/chat-view.js +827 -0
- data/lib/ozdenui/web/comp/check-box.js +490 -0
- data/lib/ozdenui/web/comp/code-editor.js +965 -0
- data/lib/ozdenui/web/comp/color-picker.js +636 -0
- data/lib/ozdenui/web/comp/context-menu.js +951 -0
- data/lib/ozdenui/web/comp/currency-field.js +209 -0
- data/lib/ozdenui/web/comp/data-table/arrow.svg +2 -0
- data/lib/ozdenui/web/comp/data-table.js +937 -0
- data/lib/ozdenui/web/comp/date-picker.js +1055 -0
- data/lib/ozdenui/web/comp/dialog.js +992 -0
- data/lib/ozdenui/web/comp/email-field.js +65 -0
- data/lib/ozdenui/web/comp/file-picker.js +920 -0
- data/lib/ozdenui/web/comp/filters.js +807 -0
- data/lib/ozdenui/web/comp/form.js +602 -0
- data/lib/ozdenui/web/comp/full-screen-select/arrow.svg +2 -0
- data/lib/ozdenui/web/comp/full-screen-select/cancel.svg +2 -0
- data/lib/ozdenui/web/comp/full-screen-select.js +614 -0
- data/lib/ozdenui/web/comp/gauge.js +439 -0
- data/lib/ozdenui/web/comp/graph-box.js +3911 -0
- data/lib/ozdenui/web/comp/grid-group.js +185 -0
- data/lib/ozdenui/web/comp/hold-to-confirm-button/trash-red.png +0 -0
- data/lib/ozdenui/web/comp/hold-to-confirm-button/trash.png +0 -0
- data/lib/ozdenui/web/comp/hold-to-confirm-button.js +289 -0
- data/lib/ozdenui/web/comp/image-view.js +598 -0
- data/lib/ozdenui/web/comp/item-list.js +1740 -0
- data/lib/ozdenui/web/comp/json-editor.js +3221 -0
- data/lib/ozdenui/web/comp/json-view.js +597 -0
- data/lib/ozdenui/web/comp/label-editor.js +1140 -0
- data/lib/ozdenui/web/comp/left-menu/data-table.png +0 -0
- data/lib/ozdenui/web/comp/left-menu/home.png +0 -0
- data/lib/ozdenui/web/comp/left-menu/reports.png +0 -0
- data/lib/ozdenui/web/comp/left-menu/setting.png +0 -0
- data/lib/ozdenui/web/comp/left-menu.js +548 -0
- data/lib/ozdenui/web/comp/line-progress-bar.js +343 -0
- data/lib/ozdenui/web/comp/loading-screen.js +413 -0
- data/lib/ozdenui/web/comp/log-view.js +530 -0
- data/lib/ozdenui/web/comp/login-page.js +685 -0
- data/lib/ozdenui/web/comp/markdown-view.js +1045 -0
- data/lib/ozdenui/web/comp/menu-bar.js +624 -0
- data/lib/ozdenui/web/comp/mini-graph-box.js +492 -0
- data/lib/ozdenui/web/comp/minimal-accent.js +208 -0
- data/lib/ozdenui/web/comp/notice.js +229 -0
- data/lib/ozdenui/web/comp/number-field.js +128 -0
- data/lib/ozdenui/web/comp/numeric-keyboard.js +298 -0
- data/lib/ozdenui/web/comp/page-control.js +762 -0
- data/lib/ozdenui/web/comp/page-header.js +191 -0
- data/lib/ozdenui/web/comp/password-field/hide-btn.png +0 -0
- data/lib/ozdenui/web/comp/password-field/hide-btn2.png +0 -0
- data/lib/ozdenui/web/comp/password-field/show-btn.png +0 -0
- data/lib/ozdenui/web/comp/password-field/show-btn2.png +0 -0
- data/lib/ozdenui/web/comp/password-field.js +192 -0
- data/lib/ozdenui/web/comp/period-bar.js +436 -0
- data/lib/ozdenui/web/comp/phone-field.js +322 -0
- data/lib/ozdenui/web/comp/progress-bar.js +992 -0
- data/lib/ozdenui/web/comp/property-panel.js +3639 -0
- data/lib/ozdenui/web/comp/radio-button.js +527 -0
- data/lib/ozdenui/web/comp/range-slider.js +569 -0
- data/lib/ozdenui/web/comp/rich-text-editor.js +1064 -0
- data/lib/ozdenui/web/comp/scroll-area.js +308 -0
- data/lib/ozdenui/web/comp/scrolling-text.js +297 -0
- data/lib/ozdenui/web/comp/search-input/clear-round.svg +1 -0
- data/lib/ozdenui/web/comp/search-input/clear.svg +1 -0
- data/lib/ozdenui/web/comp/search-input/filter.png +0 -0
- data/lib/ozdenui/web/comp/search-input/filter.svg +1 -0
- data/lib/ozdenui/web/comp/search-input/search.svg +1 -0
- data/lib/ozdenui/web/comp/search-input.js +725 -0
- data/lib/ozdenui/web/comp/search-results.js +596 -0
- data/lib/ozdenui/web/comp/select-box.js +1862 -0
- data/lib/ozdenui/web/comp/settings-row.js +299 -0
- data/lib/ozdenui/web/comp/side-panel.js +616 -0
- data/lib/ozdenui/web/comp/slider-field.js +952 -0
- data/lib/ozdenui/web/comp/slider.js +838 -0
- data/lib/ozdenui/web/comp/smart-button/alert-black.png +0 -0
- data/lib/ozdenui/web/comp/smart-button/clock.png +0 -0
- data/lib/ozdenui/web/comp/smart-button.js +981 -0
- data/lib/ozdenui/web/comp/smart-table/clock.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/down.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/scroll.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/sort.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/up.png +0 -0
- data/lib/ozdenui/web/comp/smart-table.js +1883 -0
- data/lib/ozdenui/web/comp/sortable-list.js +1181 -0
- data/lib/ozdenui/web/comp/spark-line-box.js +738 -0
- data/lib/ozdenui/web/comp/splitter.js +411 -0
- data/lib/ozdenui/web/comp/status-bar.js +408 -0
- data/lib/ozdenui/web/comp/step-bar.js +345 -0
- data/lib/ozdenui/web/comp/stepper.js +753 -0
- data/lib/ozdenui/web/comp/tabs.js +1366 -0
- data/lib/ozdenui/web/comp/tag-input.js +1107 -0
- data/lib/ozdenui/web/comp/tap-effect.js +170 -0
- data/lib/ozdenui/web/comp/text-area-field.js +233 -0
- data/lib/ozdenui/web/comp/text-field.js +1006 -0
- data/lib/ozdenui/web/comp/time-line.js +398 -0
- data/lib/ozdenui/web/comp/time-picker.js +1032 -0
- data/lib/ozdenui/web/comp/tiny-select/arrow.svg +1 -0
- data/lib/ozdenui/web/comp/tiny-select.js +322 -0
- data/lib/ozdenui/web/comp/tiny-table.js +283 -0
- data/lib/ozdenui/web/comp/toast.js +891 -0
- data/lib/ozdenui/web/comp/toggle.js +757 -0
- data/lib/ozdenui/web/comp/tool-bar.js +760 -0
- data/lib/ozdenui/web/comp/tooltip.js +534 -0
- data/lib/ozdenui/web/comp/tree-view.js +1179 -0
- data/lib/ozdenui/web/comp/ui-effects.js +1097 -0
- data/lib/ozdenui/web/comp/ui-standards.js +403 -0
- data/lib/ozdenui/web/comp/url-field.js +190 -0
- data/lib/ozdenui/web/comp/web-view.js +588 -0
- data/lib/ozdenui/web/components.js +123 -0
- data/lib/ozdenui/web/index.html +39 -0
- data/lib/ozdenui/window.rb +188 -0
- data/lib/ozdenui/window_cocoa.rb +221 -0
- data/lib/ozdenui.rb +32 -0
- metadata +200 -0
|
@@ -0,0 +1,584 @@
|
|
|
1
|
+
/* Bismillah */
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
|
|
5
|
+
bridge.js - ozdenui v0.1 (OzdenUI: basic.js for Ruby)
|
|
6
|
+
The page side of ozdenui: it runs the commands of the Ruby program on basic.js.
|
|
7
|
+
|
|
8
|
+
The program sends messages over the event stream "commands" (Server-Sent Events); the page answers and tells its
|
|
9
|
+
events with POST "emit", one request at a time, so everything reaches the program in the order it happened.
|
|
10
|
+
|
|
11
|
+
Messages of the program: { kind, n, ... }; the page answers each one with { reply: n, value } or { reply: n, error }.
|
|
12
|
+
{ kind: "apply", commands: [...] } every command makes, changes or removes a basic.js object (below)
|
|
13
|
+
{ kind: "read", id, key } the value of a property as basic.js reads it
|
|
14
|
+
{ kind: "ask", id, method, args } a method of an object and its result
|
|
15
|
+
{ kind: "eval", script } JavaScript of the program
|
|
16
|
+
{ kind: "accent", name, color, colorDark } the packages of the accent color, also for components loaded later
|
|
17
|
+
|
|
18
|
+
Commands:
|
|
19
|
+
{ op: "create", id, type: "Box" | a component ("CheckBox"), args: [left, top, width, height], props: {...},
|
|
20
|
+
parent: id or "page", state: { value: "value" }, src: "app.rb:12" }
|
|
21
|
+
{ op: "set", id, props: {...} }
|
|
22
|
+
{ op: "call", id, method, args: [...], refresh: 1 } refresh: the state goes back to the program after the call
|
|
23
|
+
{ op: "static", target: "Toast", method: "show", args: [...], answer: callback id (for a Promise), keep: id (what it
|
|
24
|
+
returned becomes an object of the program: a bubble) }
|
|
25
|
+
{ op: "part", id, of: id, path: ["panels", 0] } a box inside a component, to put objects into
|
|
26
|
+
{ op: "placeBubble", id, target: id, side: "top" } a popover is placed again when its objects are in it
|
|
27
|
+
{ op: "listen", id, event: "click" }
|
|
28
|
+
{ op: "remove", id }
|
|
29
|
+
|
|
30
|
+
A component that is not on the page yet is loaded with what it needs (components.js) before the commands that name it run.
|
|
31
|
+
|
|
32
|
+
Values from the program: { __bpCallback: id } becomes a function that tells the program (wholeArgs: [1]: the values at
|
|
33
|
+
these places are JSON data that goes whole, as { __bpJson: text }); { __bpObject: id } the object;
|
|
34
|
+
{ __bpNoop: 1 } an empty function; { __bpFormat: { prefix, suffix, thousands, decimals, percent } } a function that gives
|
|
35
|
+
the text of a number at once (the answer of a function of the program comes too late for a page that draws).
|
|
36
|
+
|
|
37
|
+
Started Date: November 2026
|
|
38
|
+
Webpage: https://bug7a.github.io/ozdenui-ruby/
|
|
39
|
+
|
|
40
|
+
Copyright 2020-2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
|
|
41
|
+
Licensed under the Apache License, Version 2.0
|
|
42
|
+
SPDX-License-Identifier: Apache-2.0
|
|
43
|
+
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
"use strict";
|
|
47
|
+
|
|
48
|
+
window.__bp = (function () {
|
|
49
|
+
|
|
50
|
+
// *** PRIVATE VARIABLES:
|
|
51
|
+
const MAKERS = { Box: Box, Label: Label, Button: Button, Input: Input, Icon: Icon, VGroup: VGroup, HGroup: HGroup };
|
|
52
|
+
const GROUP_TYPES = ["VGroup", "HGroup", "GridGroup", "ScrollArea"]; // ScrollArea leaves its content started
|
|
53
|
+
const PAGE_ID = "page";
|
|
54
|
+
const MAX_VALUE_DEPTH = 64; // only a guard: a value that holds itself is cut by toJson() where it comes again
|
|
55
|
+
const MAX_FRACTION_DIGITS = 20;
|
|
56
|
+
const SWEEP_INTERVAL_MS = 1000; // at most this often apply() looks for the objects the page removed itself
|
|
57
|
+
const MAX_FRAME_PX = 120; // a title bar and the edges of a window are smaller than this
|
|
58
|
+
const FIT_WAIT_MS = 300;
|
|
59
|
+
const RETRY_MS = 300;
|
|
60
|
+
// WHY: the program gives its messages to the page that said "ready" last, by this name (a page loaded again is new)
|
|
61
|
+
const PAGE_NAME = String(Date.now()) + "-" + String(Math.random()).slice(2); // a message that could not be sent is tried again after this
|
|
62
|
+
const GLOBAL_NAME = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
63
|
+
const objects = new Map(); // id -> basic.js object
|
|
64
|
+
let lastSweepAt = 0;
|
|
65
|
+
let isProgramTold = false;
|
|
66
|
+
const earlyMessages = []; // the messages of the page before the program was told that it is ready
|
|
67
|
+
const outbox = []; // what waits to go to the program, in order
|
|
68
|
+
let isSending = false;
|
|
69
|
+
let accent = null; // { name, color, colorDark }: the packages of the accent color (run: accent)
|
|
70
|
+
|
|
71
|
+
// *** PRIVATE FUNCTIONS:
|
|
72
|
+
|
|
73
|
+
const objectOf = function (id) {
|
|
74
|
+
if (id === PAGE_ID) return page;
|
|
75
|
+
const obj = objects.get(id);
|
|
76
|
+
if (!obj) throw new Error("There is no object " + id + " (removed?)");
|
|
77
|
+
return obj;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const isBasicObject = function (value) {
|
|
81
|
+
return !!(value && value.elem instanceof Element);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
// A function or object of the page by its name: a basic.js object maker or a component (CheckBox, Toast).
|
|
85
|
+
// WHY Function(): the components are top-level "const" (const CheckBox = function...), so they are not on
|
|
86
|
+
// window; the name is checked first, so only a name can go in.
|
|
87
|
+
const resolveGlobal = function (name) {
|
|
88
|
+
if (MAKERS[name]) return MAKERS[name];
|
|
89
|
+
if (!GLOBAL_NAME.test(name)) return undefined;
|
|
90
|
+
return Function("return (typeof " + name + " !== \"undefined\") ? " + name + " : undefined;")();
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
// A value of the page as it goes to the program (JSON): an object of basic.js becomes its id. ancestors: the objects
|
|
94
|
+
// the value is inside of; one of them again (an object of a component that holds another one that holds it) is null.
|
|
95
|
+
// WHY not a small depth: it cut the data of the program too, without a word: data.owner.address of an item of an
|
|
96
|
+
// ItemList, a node of a TreeView or a row came to the program as nil.
|
|
97
|
+
const toJson = function (value, depth = 0, ancestors = []) {
|
|
98
|
+
if (value === null || value === undefined) return null;
|
|
99
|
+
if (typeof value === "function") return null;
|
|
100
|
+
if (typeof value !== "object") return value;
|
|
101
|
+
if (isBasicObject(value)) return objectToJson(value, depth, ancestors);
|
|
102
|
+
if (value instanceof Date) return value.toISOString();
|
|
103
|
+
if (value instanceof Node || value instanceof Event) return null;
|
|
104
|
+
if (depth >= MAX_VALUE_DEPTH || ancestors.includes(value)) return null;
|
|
105
|
+
const inside = ancestors.concat([value]);
|
|
106
|
+
if (Array.isArray(value)) {
|
|
107
|
+
return value.map(function (item) { return toJson(item, depth + 1, inside); });
|
|
108
|
+
}
|
|
109
|
+
const json = {};
|
|
110
|
+
Object.keys(value).forEach(function (key) {
|
|
111
|
+
json[key] = toJson(value[key], depth + 1, inside);
|
|
112
|
+
});
|
|
113
|
+
return json;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
// A basic.js object: its id. WHY id and data: a part of a component (a row of a list) has no id of
|
|
117
|
+
// the program, but its own id and data say which one it is.
|
|
118
|
+
const objectToJson = function (obj, depth, ancestors) {
|
|
119
|
+
if (obj.__bpId) return { __bpObject: obj.__bpId };
|
|
120
|
+
const json = { __bpObject: null };
|
|
121
|
+
if (obj.id !== undefined && obj.id !== "" && typeof obj.id !== "object") json.id = obj.id;
|
|
122
|
+
if (obj.data !== undefined) json.data = toJson(obj.data, depth + 1, ancestors);
|
|
123
|
+
return json;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
// "checked" reads a property, "getSelectedKeys()" calls a method.
|
|
127
|
+
const readAccessor = function (obj, accessor) {
|
|
128
|
+
if (!accessor.endsWith("()")) return obj[accessor];
|
|
129
|
+
const method = obj[accessor.slice(0, -2)];
|
|
130
|
+
return (typeof method === "function") ? method.call(obj) : undefined;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// The values the program keeps: the state of a component ({ checked: "checked", selectedKeys: "getSelectedKeys()" })
|
|
134
|
+
const stateOf = function (obj) {
|
|
135
|
+
const spec = obj && obj.__bpState;
|
|
136
|
+
if (!spec) return null;
|
|
137
|
+
const keys = Object.keys(spec);
|
|
138
|
+
if (!keys.length) return null;
|
|
139
|
+
const state = {};
|
|
140
|
+
keys.forEach(function (key) {
|
|
141
|
+
state[key] = toJson(readAccessor(obj, spec[key]));
|
|
142
|
+
});
|
|
143
|
+
return state;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// WHY one request at a time: two requests can reach the program in another order; the events of the page and its
|
|
147
|
+
// answers must come as they happened (a Dialog tells its answer, then it is told as removed).
|
|
148
|
+
const pump = function () {
|
|
149
|
+
if (isSending || !outbox.length) return;
|
|
150
|
+
isSending = true;
|
|
151
|
+
const batch = outbox.splice(0, outbox.length);
|
|
152
|
+
const sendBatch = function () {
|
|
153
|
+
fetch("emit", { method: "POST", body: JSON.stringify(batch) }).then(function (response) {
|
|
154
|
+
if (!response.ok) throw new Error("emit " + response.status);
|
|
155
|
+
isSending = false;
|
|
156
|
+
pump();
|
|
157
|
+
}).catch(function () {
|
|
158
|
+
setTimeout(sendBatch, RETRY_MS); // the program is busy or gone: the same batch again, so nothing is lost
|
|
159
|
+
});
|
|
160
|
+
};
|
|
161
|
+
sendBatch();
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
const post = function (message) {
|
|
165
|
+
outbox.push(message);
|
|
166
|
+
pump();
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const send = function (targetId, eventName, data) {
|
|
170
|
+
post({ target: targetId, event: eventName, data: data });
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
// The values of a callback as they go to the program. The places of wholeArgs are JSON data that goes whole, as a JSON text.
|
|
174
|
+
// WHY: the value of a JsonEditor may be any JSON, also deeper than MAX_VALUE_DEPTH, and a big one: JSON.stringify
|
|
175
|
+
// writes it exactly, faster than toJson() walks it.
|
|
176
|
+
const argsToJson = function (args, wholeArgs) {
|
|
177
|
+
return args.map(function (arg, place) {
|
|
178
|
+
const isWhole = (wholeArgs && wholeArgs.includes(place) && arg !== undefined);
|
|
179
|
+
if (!isWhole) return toJson(arg, 1);
|
|
180
|
+
try {
|
|
181
|
+
return { __bpJson: JSON.stringify(arg) };
|
|
182
|
+
} catch (error) {
|
|
183
|
+
return toJson(arg, 1); // WHY: a value that holds itself has no JSON text
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
const makeCallback = function (callbackId, ownerId, wholeArgs) {
|
|
189
|
+
return function (...args) {
|
|
190
|
+
const owner = (ownerId) ? objects.get(ownerId) : null;
|
|
191
|
+
send(callbackId, "callback", { args: argsToJson(args, wholeArgs), state: stateOf(owner) });
|
|
192
|
+
};
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
// The text of a number, as the format of the program writes it ("{:,.1f} TL", as in OzdenUI for Python). WHY en-US: the format writes 1,234.5.
|
|
196
|
+
const makeNumberFormat = function (format) {
|
|
197
|
+
const options = { useGrouping: (format.thousands === 1) };
|
|
198
|
+
if (format.decimals === null) {
|
|
199
|
+
options.maximumFractionDigits = MAX_FRACTION_DIGITS;
|
|
200
|
+
} else {
|
|
201
|
+
options.minimumFractionDigits = format.decimals;
|
|
202
|
+
options.maximumFractionDigits = format.decimals;
|
|
203
|
+
}
|
|
204
|
+
const isPercent = (format.percent === 1);
|
|
205
|
+
const percentSign = isPercent ? "%" : ""; // WHY: {:.1%} of 0.123 is 12.3%, with its sign
|
|
206
|
+
return function (value) {
|
|
207
|
+
if (typeof value !== "number") return String(value);
|
|
208
|
+
const number = isPercent ? value * 100 : value;
|
|
209
|
+
return format.prefix + number.toLocaleString("en-US", options) + percentSign + format.suffix;
|
|
210
|
+
};
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
// A value from the program as the page takes it: the markers become functions and objects.
|
|
214
|
+
const revive = function (value, ownerId) {
|
|
215
|
+
if (value === null || typeof value !== "object") return value;
|
|
216
|
+
if (Array.isArray(value)) {
|
|
217
|
+
return value.map(function (item) { return revive(item, ownerId); });
|
|
218
|
+
}
|
|
219
|
+
if (value.__bpCallback) return makeCallback(value.__bpCallback, ownerId, value.wholeArgs);
|
|
220
|
+
if (value.__bpObject) return objectOf(value.__bpObject);
|
|
221
|
+
if (value.__bpNoop) return function () {};
|
|
222
|
+
if (value.__bpFormat) return makeNumberFormat(value.__bpFormat);
|
|
223
|
+
const revived = {};
|
|
224
|
+
Object.keys(value).forEach(function (key) {
|
|
225
|
+
revived[key] = revive(value[key], ownerId);
|
|
226
|
+
});
|
|
227
|
+
return revived;
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const makeObject = function (command, props) {
|
|
231
|
+
const make = resolveGlobal(command.type);
|
|
232
|
+
if (typeof make !== "function") {
|
|
233
|
+
throw new Error(command.type + " is not a component of the kit");
|
|
234
|
+
}
|
|
235
|
+
const obj = make(...command.args, props);
|
|
236
|
+
// WHY: A group starts as the container of the next objects. The program gives the parent of every object
|
|
237
|
+
// itself, so a group is closed at once (the function of a group in the program only names the parent).
|
|
238
|
+
if (GROUP_TYPES.includes(command.type)) endGroup();
|
|
239
|
+
return obj;
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const create = function (command) {
|
|
243
|
+
const parent = objectOf(command.parent || PAGE_ID);
|
|
244
|
+
const props = revive(command.props, command.id);
|
|
245
|
+
const obj = createIn(parent, function () {
|
|
246
|
+
return makeObject(command, props);
|
|
247
|
+
});
|
|
248
|
+
obj.__bpId = command.id;
|
|
249
|
+
obj.__bpState = command.state || null;
|
|
250
|
+
objects.set(command.id, obj);
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
const set = function (command) {
|
|
254
|
+
const obj = objectOf(command.id);
|
|
255
|
+
const props = revive(command.props, command.id);
|
|
256
|
+
// WHY: page (a MainBox) has no props(): its values go one by one (Object.assign calls the setters).
|
|
257
|
+
if (typeof obj.props !== "function") {
|
|
258
|
+
Object.assign(obj, props);
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
obj.props(props);
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
const call = function (command) {
|
|
265
|
+
const obj = objectOf(command.id);
|
|
266
|
+
if (typeof obj[command.method] !== "function") throw new Error("There is no method " + command.method);
|
|
267
|
+
obj[command.method](...revive(command.args, command.id));
|
|
268
|
+
// WHY: a silent change (setValue(v, 1)) tells nobody, and the component may change the value (a slider keeps
|
|
269
|
+
// it inside min..max): the program gets the state as the page has it.
|
|
270
|
+
if (command.refresh) send(command.id, "__state", { state: stateOf(obj) });
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
const keepObject = function (id, obj) {
|
|
274
|
+
obj.__bpId = id;
|
|
275
|
+
objects.set(id, obj);
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
const callStatic = function (command) {
|
|
279
|
+
const target = resolveGlobal(command.target);
|
|
280
|
+
if (!target || typeof target[command.method] !== "function") {
|
|
281
|
+
throw new Error(command.target + "." + command.method + " is not loaded");
|
|
282
|
+
}
|
|
283
|
+
const result = target[command.method](...revive(command.args, null));
|
|
284
|
+
if (command.keep) keepObject(command.keep, result);
|
|
285
|
+
if (!command.answer) return;
|
|
286
|
+
const tellAnswer = function (value) {
|
|
287
|
+
send(command.answer, "callback", { args: [toJson(value)], state: null });
|
|
288
|
+
};
|
|
289
|
+
Promise.resolve(result).then(tellAnswer, function () { tellAnswer(null); });
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
const stepInto = function (obj, step, ownerId) {
|
|
293
|
+
if (obj === undefined || obj === null) return obj;
|
|
294
|
+
if (step && typeof step === "object" && step.call) {
|
|
295
|
+
const method = obj[step.call];
|
|
296
|
+
return (typeof method === "function") ? method.apply(obj, revive(step.args || [], ownerId)) : undefined;
|
|
297
|
+
}
|
|
298
|
+
return obj[step];
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
const part = function (command) {
|
|
302
|
+
let obj = objectOf(command.of);
|
|
303
|
+
command.path.forEach(function (step) {
|
|
304
|
+
obj = stepInto(obj, step, command.of);
|
|
305
|
+
});
|
|
306
|
+
if (!isBasicObject(obj)) throw new Error("There is no box at " + JSON.stringify(command.path));
|
|
307
|
+
obj.__bpId = obj.__bpId || command.id;
|
|
308
|
+
objects.set(command.id, obj);
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
// WHY: the objects of a popover come after it was placed, and it grew from where it stood.
|
|
312
|
+
const placeBubble = function (command) {
|
|
313
|
+
const bubble = objectOf(command.id);
|
|
314
|
+
Tooltip.place(bubble, Tooltip.toPageRect(objectOf(command.target)), command.side);
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
// What an event tells the program. An Input sends its text with every event: the program keeps it, so reading
|
|
318
|
+
// its text in the program needs no question to the page.
|
|
319
|
+
const dataOf = function (id, obj, event) {
|
|
320
|
+
const data = {};
|
|
321
|
+
if (obj.inputElement) data.text = obj.text;
|
|
322
|
+
if (event && event.key !== undefined) {
|
|
323
|
+
data.key = event.key;
|
|
324
|
+
data.code = event.code;
|
|
325
|
+
data.shift = event.shiftKey;
|
|
326
|
+
data.ctrl = event.ctrlKey;
|
|
327
|
+
data.alt = event.altKey;
|
|
328
|
+
data.meta = event.metaKey;
|
|
329
|
+
}
|
|
330
|
+
if (event && event.clientX !== undefined) {
|
|
331
|
+
data.x = event.clientX;
|
|
332
|
+
data.y = event.clientY;
|
|
333
|
+
}
|
|
334
|
+
if (event && event.detail && typeof event.detail === "object") data.detail = toJson(event.detail); // a CustomEvent
|
|
335
|
+
data.state = (id === PAGE_ID) ? { width: page.width, height: page.height } : stateOf(obj);
|
|
336
|
+
return data;
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
const listen = function (command) {
|
|
340
|
+
const obj = objectOf(command.id);
|
|
341
|
+
obj.on(command.event, function (self, event) {
|
|
342
|
+
send(command.id, command.event, dataOf(command.id, obj, event));
|
|
343
|
+
});
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
// The objects that are not on the page any more leave the list, and the program is told (it forgets their callbacks).
|
|
347
|
+
// WHY not only after remove: a component removes objects itself too (a closed Dialog, the panel of a removed tab).
|
|
348
|
+
const sweepRemoved = function () {
|
|
349
|
+
lastSweepAt = performance.now();
|
|
350
|
+
const removedIds = [];
|
|
351
|
+
objects.forEach(function (obj, id) {
|
|
352
|
+
if (obj.elem.isConnected) return;
|
|
353
|
+
removedIds.push(id);
|
|
354
|
+
objects.delete(id);
|
|
355
|
+
});
|
|
356
|
+
return removedIds;
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
// basic.js removes the objects inside the removed one too.
|
|
360
|
+
const remove = function (command) {
|
|
361
|
+
objectOf(command.id).remove();
|
|
362
|
+
return sweepRemoved();
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
const HANDLERS = { create: create, set: set, call: call, static: callStatic, part: part, placeBubble: placeBubble, listen: listen, remove: remove };
|
|
366
|
+
|
|
367
|
+
// *** COMPONENTS THAT ARE NOT LOADED BY index.html (window.__bpComponents: name -> { file, needs })
|
|
368
|
+
|
|
369
|
+
const loadScript = function (file) {
|
|
370
|
+
return new Promise(function (resolve, reject) {
|
|
371
|
+
const script = document.createElement("script");
|
|
372
|
+
script.src = file;
|
|
373
|
+
script.onload = resolve;
|
|
374
|
+
script.onerror = function () { reject(new Error("could not load " + file)); };
|
|
375
|
+
document.head.appendChild(script);
|
|
376
|
+
});
|
|
377
|
+
};
|
|
378
|
+
|
|
379
|
+
// The files of the components and of what they need, each once, the needed ones first.
|
|
380
|
+
const filesInOrder = function (names, errors) {
|
|
381
|
+
const files = [];
|
|
382
|
+
const seen = new Set(); // WHY: two components may need each other
|
|
383
|
+
const addFile = function (name) {
|
|
384
|
+
if (seen.has(name)) return;
|
|
385
|
+
seen.add(name);
|
|
386
|
+
const entry = (window.__bpComponents || {})[name];
|
|
387
|
+
if (!entry) {
|
|
388
|
+
errors.push("unknown component " + name);
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
(entry.needs || []).forEach(addFile);
|
|
392
|
+
const isLoaded = (resolveGlobal(name) !== undefined);
|
|
393
|
+
if (!isLoaded && !files.includes(entry.file)) files.push(entry.file);
|
|
394
|
+
};
|
|
395
|
+
names.forEach(addFile);
|
|
396
|
+
return files;
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
// *** PUBLIC FUNCTIONS:
|
|
400
|
+
|
|
401
|
+
// Applies the commands in order. A failed command does not stop the others: its error goes back to the program
|
|
402
|
+
// with the line of the program that sent it.
|
|
403
|
+
const apply = function (commands) {
|
|
404
|
+
const startedAt = performance.now();
|
|
405
|
+
const answer = { errors: [], removed: [] };
|
|
406
|
+
commands.forEach(function (command) {
|
|
407
|
+
try {
|
|
408
|
+
const removedIds = HANDLERS[command.op](command);
|
|
409
|
+
if (removedIds) answer.removed.push(...removedIds);
|
|
410
|
+
} catch (error) {
|
|
411
|
+
// WHY String(): a thrown text or object has no message; the program reads the message
|
|
412
|
+
const message = (error && error.message) ? error.message : String(error);
|
|
413
|
+
answer.errors.push({ op: command.op, id: command.id || command.target, src: command.src || "", message: message });
|
|
414
|
+
// WHY: the program waits for the answer of a dialog; a call that failed never gave one, so it waited for ever
|
|
415
|
+
if (command.op === "static" && command.answer) send(command.answer, "callback", { args: [null], state: null });
|
|
416
|
+
}
|
|
417
|
+
});
|
|
418
|
+
if (performance.now() - lastSweepAt > SWEEP_INTERVAL_MS) answer.removed.push(...sweepRemoved());
|
|
419
|
+
answer.ms = performance.now() - startedAt;
|
|
420
|
+
return answer;
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
// The value of a property as basic.js reads it (obj.measure(:width))
|
|
424
|
+
const read = function (id, key) {
|
|
425
|
+
return toJson(objectOf(id)[key]);
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
// A method of an object and its result (obj.ask(:get_value))
|
|
429
|
+
const ask = function (id, method, args) {
|
|
430
|
+
const obj = objectOf(id);
|
|
431
|
+
if (typeof obj[method] !== "function") throw new Error("There is no method " + method);
|
|
432
|
+
return toJson(obj[method](...revive(args, id)));
|
|
433
|
+
};
|
|
434
|
+
|
|
435
|
+
const loadComponents = async function (names) {
|
|
436
|
+
const errors = [];
|
|
437
|
+
for (const file of filesInOrder(names, errors)) {
|
|
438
|
+
try {
|
|
439
|
+
await loadScript(file);
|
|
440
|
+
} catch (error) {
|
|
441
|
+
errors.push(error.message);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
if (accent) addAccentPackages();
|
|
445
|
+
return { errors: errors };
|
|
446
|
+
};
|
|
447
|
+
|
|
448
|
+
// WHY again after a load: MinimalAccent gives its packages to the components that are on the page.
|
|
449
|
+
const addAccentPackages = function () {
|
|
450
|
+
if (typeof MinimalAccent === "undefined") return;
|
|
451
|
+
MinimalAccent.addPackages(accent.name, accent.color, accent.colorDark);
|
|
452
|
+
};
|
|
453
|
+
|
|
454
|
+
// The components the commands name (a type to make, the target of a static call) that are not on the page yet.
|
|
455
|
+
const namesToLoad = function (commands) {
|
|
456
|
+
const names = [];
|
|
457
|
+
commands.forEach(function (command) {
|
|
458
|
+
const name = (command.op === "create") ? command.type : (command.op === "static") ? command.target : "";
|
|
459
|
+
if (!name || names.includes(name) || MAKERS[name]) return;
|
|
460
|
+
if ((window.__bpComponents || {})[name] && resolveGlobal(name) === undefined) names.push(name);
|
|
461
|
+
});
|
|
462
|
+
return names;
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
// *** THE MESSAGES OF THE PROGRAM, one after the other.
|
|
466
|
+
|
|
467
|
+
const KINDS = {
|
|
468
|
+
apply: async function (message) {
|
|
469
|
+
const names = namesToLoad(message.commands);
|
|
470
|
+
const loaded = (names.length) ? await loadComponents(names) : { errors: [] };
|
|
471
|
+
const answer = apply(message.commands);
|
|
472
|
+
loaded.errors.forEach(function (text) { answer.errors.push({ op: "load", id: "", src: "", message: text }); });
|
|
473
|
+
return answer;
|
|
474
|
+
},
|
|
475
|
+
read: async function (message) { return read(message.id, message.key); },
|
|
476
|
+
ask: async function (message) { return ask(message.id, message.method, message.args); },
|
|
477
|
+
eval: async function (message) { return toJson(await (0, eval)(message.script)); },
|
|
478
|
+
accent: async function (message) {
|
|
479
|
+
accent = { name: message.name, color: message.color, colorDark: message.colorDark };
|
|
480
|
+
await loadComponents(["MinimalAccent"]);
|
|
481
|
+
},
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
let lastMessage = Promise.resolve();
|
|
485
|
+
|
|
486
|
+
const takeMessage = function (message) {
|
|
487
|
+
lastMessage = lastMessage.then(async function () {
|
|
488
|
+
try {
|
|
489
|
+
post({ reply: message.n, value: await KINDS[message.kind](message) });
|
|
490
|
+
} catch (error) {
|
|
491
|
+
post({ reply: message.n, error: (error && error.message) ? error.message : String(error) });
|
|
492
|
+
}
|
|
493
|
+
});
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
// *** THE MESSAGES OF THE PAGE go to the terminal of the program (a warning of a component, an error).
|
|
497
|
+
|
|
498
|
+
const tellProgramMessage = function (kind, text) {
|
|
499
|
+
if (!isProgramTold) {
|
|
500
|
+
earlyMessages.push([kind, text]);
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
send(PAGE_ID, "__message", { kind: kind, text: text });
|
|
504
|
+
};
|
|
505
|
+
|
|
506
|
+
["error", "warn"].forEach(function (kind) {
|
|
507
|
+
const original = console[kind].bind(console);
|
|
508
|
+
console[kind] = function (...args) {
|
|
509
|
+
original(...args);
|
|
510
|
+
tellProgramMessage(kind, args.map(String).join(" "));
|
|
511
|
+
};
|
|
512
|
+
});
|
|
513
|
+
window.addEventListener("error", function (event) {
|
|
514
|
+
tellProgramMessage("error", event.message + " (" + event.filename + ":" + event.lineno + ")");
|
|
515
|
+
});
|
|
516
|
+
window.addEventListener("unhandledrejection", function (event) {
|
|
517
|
+
tellProgramMessage("error", "Promise: " + String(event.reason));
|
|
518
|
+
});
|
|
519
|
+
|
|
520
|
+
// WHY: a file dropped on the window opened in it, in the place of the program. (A FilePicker takes its own drop;
|
|
521
|
+
// a text dragged into a field goes on as usual.)
|
|
522
|
+
const keepFileOutOfThePage = function (event) {
|
|
523
|
+
const isFile = event.dataTransfer && Array.from(event.dataTransfer.types).includes("Files");
|
|
524
|
+
if (isFile) event.preventDefault();
|
|
525
|
+
};
|
|
526
|
+
window.addEventListener("dragover", keepFileOutOfThePage);
|
|
527
|
+
window.addEventListener("drop", keepFileOutOfThePage);
|
|
528
|
+
|
|
529
|
+
// *** READY: the program starts when basic.js made the page.
|
|
530
|
+
|
|
531
|
+
const tellProgramWhenReady = function () {
|
|
532
|
+
isProgramTold = true;
|
|
533
|
+
const prefersDark = !!(window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches);
|
|
534
|
+
post({ ready: { page: PAGE_NAME, basicVersion: basic.version, width: page.width, height: page.height, prefersDark: prefersDark } });
|
|
535
|
+
earlyMessages.forEach(function (message) { tellProgramMessage(message[0], message[1]); });
|
|
536
|
+
// WHY after ready: the program sends nothing to a page that did not say it is there
|
|
537
|
+
const stream = new EventSource("commands?page=" + PAGE_NAME);
|
|
538
|
+
stream.onmessage = function (event) { takeMessage(JSON.parse(event.data)); };
|
|
539
|
+
};
|
|
540
|
+
|
|
541
|
+
// The ground of a page loaded again for another theme (set_theme): the program leaves it in window.name, which
|
|
542
|
+
// stays over the load. WHY: the new page was light for a moment before the program drew it dark.
|
|
543
|
+
const EARLY_PAGE_COLOR_PREFIX = "ozdenui-page-color:";
|
|
544
|
+
const takeEarlyPageColor = function () {
|
|
545
|
+
const name = String(window.name || "");
|
|
546
|
+
if (name.indexOf(EARLY_PAGE_COLOR_PREFIX) !== 0) return;
|
|
547
|
+
const color = name.slice(EARLY_PAGE_COLOR_PREFIX.length);
|
|
548
|
+
if (color) page.color = color;
|
|
549
|
+
};
|
|
550
|
+
|
|
551
|
+
// A window of a browser is as large as the program asked with its title bar, so its page is smaller: the window
|
|
552
|
+
// grows by the difference before the program starts (the web view of macOS is fitted by the program itself).
|
|
553
|
+
// WHY only a small difference: a tab of a browser has the size of its window, and can not be resized.
|
|
554
|
+
const fitWindowToPage = function (onDone) {
|
|
555
|
+
const meta = document.querySelector("meta[name=ozdenui-size]");
|
|
556
|
+
const wanted = (meta) ? meta.content.split("x").map(Number) : [];
|
|
557
|
+
const extraWidth = wanted[0] - window.innerWidth;
|
|
558
|
+
const extraHeight = wanted[1] - window.innerHeight;
|
|
559
|
+
const isTitleBar = (extraWidth >= 0 && extraWidth <= MAX_FRAME_PX && extraHeight > 0 && extraHeight <= MAX_FRAME_PX);
|
|
560
|
+
if (!isTitleBar) {
|
|
561
|
+
onDone();
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
let isDone = false;
|
|
565
|
+
const finish = function () {
|
|
566
|
+
if (isDone) return;
|
|
567
|
+
isDone = true;
|
|
568
|
+
window.removeEventListener("resize", finish);
|
|
569
|
+
onDone();
|
|
570
|
+
};
|
|
571
|
+
window.addEventListener("resize", finish);
|
|
572
|
+
setTimeout(finish, FIT_WAIT_MS); // a window that does not let itself be resized
|
|
573
|
+
window.resizeBy(extraWidth, extraHeight);
|
|
574
|
+
};
|
|
575
|
+
|
|
576
|
+
// basic.js calls start() when page is created.
|
|
577
|
+
window.start = function () {
|
|
578
|
+
takeEarlyPageColor();
|
|
579
|
+
fitWindowToPage(tellProgramWhenReady);
|
|
580
|
+
};
|
|
581
|
+
|
|
582
|
+
return { apply: apply, read: read, ask: ask, objectOf: objectOf, loadComponents: loadComponents };
|
|
583
|
+
|
|
584
|
+
})();
|