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.
Files changed (149) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +14 -0
  3. data/NOTICE +5 -0
  4. data/README.md +242 -0
  5. data/exe/ozdenui +17 -0
  6. data/lib/ozdenui/AGENTS.md +210 -0
  7. data/lib/ozdenui/COMPONENTS.md +3017 -0
  8. data/lib/ozdenui/app.rb +749 -0
  9. data/lib/ozdenui/command.rb +107 -0
  10. data/lib/ozdenui/components.rb +368 -0
  11. data/lib/ozdenui/dialogs.rb +212 -0
  12. data/lib/ozdenui/files.rb +124 -0
  13. data/lib/ozdenui/icon.png +0 -0
  14. data/lib/ozdenui/kit.json +1 -0
  15. data/lib/ozdenui/kit.rb +115 -0
  16. data/lib/ozdenui/look.rb +229 -0
  17. data/lib/ozdenui/objects.rb +652 -0
  18. data/lib/ozdenui/server.rb +270 -0
  19. data/lib/ozdenui/values.rb +273 -0
  20. data/lib/ozdenui/version.rb +15 -0
  21. data/lib/ozdenui/web/basic/LICENSE +14 -0
  22. data/lib/ozdenui/web/basic/basic.min.css +1 -0
  23. data/lib/ozdenui/web/basic/basic.min.js +78 -0
  24. data/lib/ozdenui/web/basic/font/open-sans/LICENSE.txt +206 -0
  25. data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Bold.ttf +0 -0
  26. data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Regular.ttf +0 -0
  27. data/lib/ozdenui/web/basic/scroll-bar.min.js +3 -0
  28. data/lib/ozdenui/web/bridge.js +584 -0
  29. data/lib/ozdenui/web/comp/accordion.js +659 -0
  30. data/lib/ozdenui/web/comp/action-button/add.png +0 -0
  31. data/lib/ozdenui/web/comp/action-button.js +521 -0
  32. data/lib/ozdenui/web/comp/action-tab-bar.js +383 -0
  33. data/lib/ozdenui/web/comp/app-bar.js +237 -0
  34. data/lib/ozdenui/web/comp/badge.js +292 -0
  35. data/lib/ozdenui/web/comp/bottom-bar.js +555 -0
  36. data/lib/ozdenui/web/comp/breadcrumbs.js +524 -0
  37. data/lib/ozdenui/web/comp/canvas.js +652 -0
  38. data/lib/ozdenui/web/comp/chat-view.js +827 -0
  39. data/lib/ozdenui/web/comp/check-box.js +490 -0
  40. data/lib/ozdenui/web/comp/code-editor.js +965 -0
  41. data/lib/ozdenui/web/comp/color-picker.js +636 -0
  42. data/lib/ozdenui/web/comp/context-menu.js +951 -0
  43. data/lib/ozdenui/web/comp/currency-field.js +209 -0
  44. data/lib/ozdenui/web/comp/data-table/arrow.svg +2 -0
  45. data/lib/ozdenui/web/comp/data-table.js +937 -0
  46. data/lib/ozdenui/web/comp/date-picker.js +1055 -0
  47. data/lib/ozdenui/web/comp/dialog.js +992 -0
  48. data/lib/ozdenui/web/comp/email-field.js +65 -0
  49. data/lib/ozdenui/web/comp/file-picker.js +920 -0
  50. data/lib/ozdenui/web/comp/filters.js +807 -0
  51. data/lib/ozdenui/web/comp/form.js +602 -0
  52. data/lib/ozdenui/web/comp/full-screen-select/arrow.svg +2 -0
  53. data/lib/ozdenui/web/comp/full-screen-select/cancel.svg +2 -0
  54. data/lib/ozdenui/web/comp/full-screen-select.js +614 -0
  55. data/lib/ozdenui/web/comp/gauge.js +439 -0
  56. data/lib/ozdenui/web/comp/graph-box.js +3911 -0
  57. data/lib/ozdenui/web/comp/grid-group.js +185 -0
  58. data/lib/ozdenui/web/comp/hold-to-confirm-button/trash-red.png +0 -0
  59. data/lib/ozdenui/web/comp/hold-to-confirm-button/trash.png +0 -0
  60. data/lib/ozdenui/web/comp/hold-to-confirm-button.js +289 -0
  61. data/lib/ozdenui/web/comp/image-view.js +598 -0
  62. data/lib/ozdenui/web/comp/item-list.js +1740 -0
  63. data/lib/ozdenui/web/comp/json-editor.js +3221 -0
  64. data/lib/ozdenui/web/comp/json-view.js +597 -0
  65. data/lib/ozdenui/web/comp/label-editor.js +1140 -0
  66. data/lib/ozdenui/web/comp/left-menu/data-table.png +0 -0
  67. data/lib/ozdenui/web/comp/left-menu/home.png +0 -0
  68. data/lib/ozdenui/web/comp/left-menu/reports.png +0 -0
  69. data/lib/ozdenui/web/comp/left-menu/setting.png +0 -0
  70. data/lib/ozdenui/web/comp/left-menu.js +548 -0
  71. data/lib/ozdenui/web/comp/line-progress-bar.js +343 -0
  72. data/lib/ozdenui/web/comp/loading-screen.js +413 -0
  73. data/lib/ozdenui/web/comp/log-view.js +530 -0
  74. data/lib/ozdenui/web/comp/login-page.js +685 -0
  75. data/lib/ozdenui/web/comp/markdown-view.js +1045 -0
  76. data/lib/ozdenui/web/comp/menu-bar.js +624 -0
  77. data/lib/ozdenui/web/comp/mini-graph-box.js +492 -0
  78. data/lib/ozdenui/web/comp/minimal-accent.js +208 -0
  79. data/lib/ozdenui/web/comp/notice.js +229 -0
  80. data/lib/ozdenui/web/comp/number-field.js +128 -0
  81. data/lib/ozdenui/web/comp/numeric-keyboard.js +298 -0
  82. data/lib/ozdenui/web/comp/page-control.js +762 -0
  83. data/lib/ozdenui/web/comp/page-header.js +191 -0
  84. data/lib/ozdenui/web/comp/password-field/hide-btn.png +0 -0
  85. data/lib/ozdenui/web/comp/password-field/hide-btn2.png +0 -0
  86. data/lib/ozdenui/web/comp/password-field/show-btn.png +0 -0
  87. data/lib/ozdenui/web/comp/password-field/show-btn2.png +0 -0
  88. data/lib/ozdenui/web/comp/password-field.js +192 -0
  89. data/lib/ozdenui/web/comp/period-bar.js +436 -0
  90. data/lib/ozdenui/web/comp/phone-field.js +322 -0
  91. data/lib/ozdenui/web/comp/progress-bar.js +992 -0
  92. data/lib/ozdenui/web/comp/property-panel.js +3639 -0
  93. data/lib/ozdenui/web/comp/radio-button.js +527 -0
  94. data/lib/ozdenui/web/comp/range-slider.js +569 -0
  95. data/lib/ozdenui/web/comp/rich-text-editor.js +1064 -0
  96. data/lib/ozdenui/web/comp/scroll-area.js +308 -0
  97. data/lib/ozdenui/web/comp/scrolling-text.js +297 -0
  98. data/lib/ozdenui/web/comp/search-input/clear-round.svg +1 -0
  99. data/lib/ozdenui/web/comp/search-input/clear.svg +1 -0
  100. data/lib/ozdenui/web/comp/search-input/filter.png +0 -0
  101. data/lib/ozdenui/web/comp/search-input/filter.svg +1 -0
  102. data/lib/ozdenui/web/comp/search-input/search.svg +1 -0
  103. data/lib/ozdenui/web/comp/search-input.js +725 -0
  104. data/lib/ozdenui/web/comp/search-results.js +596 -0
  105. data/lib/ozdenui/web/comp/select-box.js +1862 -0
  106. data/lib/ozdenui/web/comp/settings-row.js +299 -0
  107. data/lib/ozdenui/web/comp/side-panel.js +616 -0
  108. data/lib/ozdenui/web/comp/slider-field.js +952 -0
  109. data/lib/ozdenui/web/comp/slider.js +838 -0
  110. data/lib/ozdenui/web/comp/smart-button/alert-black.png +0 -0
  111. data/lib/ozdenui/web/comp/smart-button/clock.png +0 -0
  112. data/lib/ozdenui/web/comp/smart-button.js +981 -0
  113. data/lib/ozdenui/web/comp/smart-table/clock.png +0 -0
  114. data/lib/ozdenui/web/comp/smart-table/down.png +0 -0
  115. data/lib/ozdenui/web/comp/smart-table/scroll.png +0 -0
  116. data/lib/ozdenui/web/comp/smart-table/sort.png +0 -0
  117. data/lib/ozdenui/web/comp/smart-table/up.png +0 -0
  118. data/lib/ozdenui/web/comp/smart-table.js +1883 -0
  119. data/lib/ozdenui/web/comp/sortable-list.js +1181 -0
  120. data/lib/ozdenui/web/comp/spark-line-box.js +738 -0
  121. data/lib/ozdenui/web/comp/splitter.js +411 -0
  122. data/lib/ozdenui/web/comp/status-bar.js +408 -0
  123. data/lib/ozdenui/web/comp/step-bar.js +345 -0
  124. data/lib/ozdenui/web/comp/stepper.js +753 -0
  125. data/lib/ozdenui/web/comp/tabs.js +1366 -0
  126. data/lib/ozdenui/web/comp/tag-input.js +1107 -0
  127. data/lib/ozdenui/web/comp/tap-effect.js +170 -0
  128. data/lib/ozdenui/web/comp/text-area-field.js +233 -0
  129. data/lib/ozdenui/web/comp/text-field.js +1006 -0
  130. data/lib/ozdenui/web/comp/time-line.js +398 -0
  131. data/lib/ozdenui/web/comp/time-picker.js +1032 -0
  132. data/lib/ozdenui/web/comp/tiny-select/arrow.svg +1 -0
  133. data/lib/ozdenui/web/comp/tiny-select.js +322 -0
  134. data/lib/ozdenui/web/comp/tiny-table.js +283 -0
  135. data/lib/ozdenui/web/comp/toast.js +891 -0
  136. data/lib/ozdenui/web/comp/toggle.js +757 -0
  137. data/lib/ozdenui/web/comp/tool-bar.js +760 -0
  138. data/lib/ozdenui/web/comp/tooltip.js +534 -0
  139. data/lib/ozdenui/web/comp/tree-view.js +1179 -0
  140. data/lib/ozdenui/web/comp/ui-effects.js +1097 -0
  141. data/lib/ozdenui/web/comp/ui-standards.js +403 -0
  142. data/lib/ozdenui/web/comp/url-field.js +190 -0
  143. data/lib/ozdenui/web/comp/web-view.js +588 -0
  144. data/lib/ozdenui/web/components.js +123 -0
  145. data/lib/ozdenui/web/index.html +39 -0
  146. data/lib/ozdenui/window.rb +188 -0
  147. data/lib/ozdenui/window_cocoa.rb +221 -0
  148. data/lib/ozdenui.rb +32 -0
  149. metadata +200 -0
@@ -0,0 +1,588 @@
1
+ /* Bismillah */
2
+
3
+ /*
4
+
5
+ WebView - v26.09
6
+
7
+ UI COMPONENT TEMPLATE
8
+ - A box that shows a web page or an HTML text in an iframe.
9
+ - load(url), loadHtml(html) (srcdoc), reload(), clear(). The iframe always fills the box (also with width: "100%").
10
+ - Loading overlay (spinner) until the page is ready, a placeholder text when nothing is loaded, and showError(text)
11
+ for your own error messages. A same origin URL that fails (404, missing file) calls onError. Other origins can not be
12
+ checked by the browser; loadTimeout -> onTimeout helps for pages that never load. Only the page opened by load() is
13
+ checked: a link in the page that opens another site in the iframe (Ex: a payment page) calls onLoad, not onError.
14
+ - Messages: postMessage(data) sends to the page, onMessage(self, data, event) gets the messages of the page.
15
+ Only the messages of this iframe are delivered. (Ex: modules in ozden-panel)
16
+ - Same origin pages: getWindow(), getDocument(), autoHeight: 1 (the box grows with the page).
17
+ - scale: 0.5 -> A scaled preview of the page (Ex: a desktop page in a small card).
18
+ - interactive: 0 -> A transparent cover over the page: the visitor can not click it, the box gets the clicks. (Previews)
19
+ - toolbar: 1 -> A small bar with the URL, reload and "open in a new tab" buttons.
20
+ - sandbox / allow / referrerPolicy / loading attributes of the iframe can be set with params.
21
+ - remove() removes the message listener and unloads the page.
22
+
23
+ USAGE:
24
+ const view = WebView({ width: "100%", height: 400, url: "module1/index.htm", onLoad: (self) => println("ready") });
25
+ view.load("https://example.com");
26
+ view.loadHtml("<h1>Hello</h1>");
27
+ view.postMessage({ type: "theme", value: "dark" });
28
+ WebView({ url: "page.htm", onMessage: (self, data) => println(data.type) });
29
+ WebView({ url: "page.htm", scale: 0.5, interactive: 0, onClick: (self) => basic.go("page.htm") });
30
+
31
+ Started Date: September 2026
32
+ Webpage: https://bug7a.github.io/js-components/
33
+
34
+ Copyright 2020-2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
35
+ Licensed under the Apache License, Version 2.0
36
+ SPDX-License-Identifier: Apache-2.0
37
+
38
+ */
39
+
40
+ "use strict";
41
+
42
+ // Default values:
43
+ const WebViewDefaults = {
44
+ key: "0",
45
+ width: 400,
46
+ height: 300,
47
+ url: "", // "": Nothing is loaded (placeholder is shown)
48
+ htmlText: "", // HTML text instead of a URL (srcdoc). NOTE: Not "html": that is the innerHTML setter of a basic.js Box.
49
+ ariaLabel: "Web view", // the name a screen reader says (the title of the iframe)
50
+ sandbox: null, // null: No sandbox. Ex: "allow-scripts allow-same-origin allow-forms allow-popups"
51
+ allow: "", // Ex: "fullscreen; clipboard-write"
52
+ referrerPolicy: "", // Ex: "no-referrer"
53
+ lazy: 0, // 1: The browser loads the page when the box is near the screen. (loading="lazy")
54
+ scale: 1, // 0.5: The page is shown at half size (scaled preview). NOTE: Not "zoom": basic.js uses it for the page.
55
+ interactive: 1, // 0: The page can not be clicked or scrolled. (Preview)
56
+ autoHeight: 0, // 1: The box height follows the page height. (Same origin pages only)
57
+ toolbar: 0, // 1: A bar with the URL, reload and "open in a new tab" buttons (Only at create time)
58
+ showLoading: 1, // 1: Spinner until the page is loaded
59
+ emptyText: "No page", // Shown when nothing is loaded
60
+ loadTimeout: 0, // Seconds. 0: No timeout. Ex: 15 -> onTimeout is called when the page is not loaded in 15 seconds.
61
+ onLoad: function (self) { },
62
+ onError: function (self) { }, // Same origin URL of load() that could not be loaded (the browser shows its error page)
63
+ onTimeout: function (self) { },
64
+ onMessage: function (self, data, event) { }, // Messages from the page (window.parent.postMessage)
65
+ onClick: function (self) { }, // interactive: 0 -> click on the cover
66
+ style: {
67
+ box: {
68
+ color: White(1),
69
+ border: 1,
70
+ borderColor: Black(0.12),
71
+ round: 8,
72
+ },
73
+ loading: {
74
+ color: White(0.85), // Cover color while loading
75
+ spinnerColor: "#141414",
76
+ trackColor: Black(0.1), // The rest of the spinner ring
77
+ spinnerSize: 28,
78
+ },
79
+ placeholder: {
80
+ color: Black(0.03),
81
+ fontSize: 14,
82
+ textColor: Black(0.4),
83
+ },
84
+ error: {
85
+ color: White(0.95),
86
+ fontSize: 14,
87
+ textColor: "#D64545",
88
+ },
89
+ toolbar: {
90
+ height: 36,
91
+ color: Black(0.04),
92
+ borderColor: Black(0.08),
93
+ fontSize: 12,
94
+ textColor: Black(0.55),
95
+ iconColor: Black(0.55),
96
+ iconHoverColor: "#141414",
97
+ buttonHoverColor: Black(0.06), // The background of a button under the mouse
98
+ },
99
+ }
100
+ };
101
+
102
+ const WebView = function (params = {}) {
103
+
104
+ // Merge params:
105
+ mergeIntoIfMissing(params, WebViewDefaults);
106
+
107
+ // Edit params, if needed:
108
+ const _bs = params.style.box;
109
+ params.color = _bs.color;
110
+ params.border = _bs.border;
111
+ params.borderColor = _bs.borderColor;
112
+ params.round = _bs.round;
113
+ const startUrl = params.url;
114
+ const startHtml = params.htmlText;
115
+ params.url = "";
116
+ params.htmlText = "";
117
+
118
+ // BOX: Component container
119
+ let box = startObject(params);
120
+
121
+ // *** PRIVATE VARIABLES:
122
+ const _s = box.style;
123
+ let iframe = null;
124
+ let isLoading = 0;
125
+ let cancelLoadTimer = null; // (box.after: stopped by remove())
126
+ let heightObserver = null;
127
+ let heightTimer = null;
128
+ let toolbarHeight = 0;
129
+ const HEIGHT_WAIT = 30; // ms: a burst of page resizes is measured once
130
+
131
+ // *** PUBLIC VARIABLES:
132
+ box.isLoaded = 0; // [var] 1 after the current page is loaded
133
+ // NOTE: Default values are also public variables. (box.url, box.htmlText, box.scale, box.interactive)
134
+
135
+ // *** PRIVATE FUNCTIONS:
136
+
137
+ const applyScale = function () {
138
+ const zoom = (box.scale > 0) ? box.scale : 1;
139
+ const percent = Math.round(100 / zoom * 1000) / 1000;
140
+ iframe.style.width = percent + "%";
141
+ iframe.style.height = percent + "%";
142
+ iframe.style.transform = (zoom === 1) ? "" : "scale(" + zoom + ")";
143
+ iframe.style.transformOrigin = "0 0";
144
+ };
145
+
146
+ const applyInteractive = function () {
147
+ const isInteractive = (box.interactive == 1);
148
+ box.cover.visible = (isInteractive) ? 0 : 1;
149
+ iframe.style.pointerEvents = (isInteractive) ? "auto" : "none";
150
+ iframe.setAttribute("tabindex", (isInteractive) ? "0" : "-1");
151
+ };
152
+
153
+ const setLoading = function (loading) {
154
+ isLoading = loading;
155
+ box.loadingBox.visible = (loading && box.showLoading == 1) ? 1 : 0;
156
+ if (cancelLoadTimer) cancelLoadTimer();
157
+ cancelLoadTimer = null;
158
+ if (loading && box.loadTimeout > 0) {
159
+ cancelLoadTimer = box.after(box.loadTimeout * 1000, function () {
160
+ cancelLoadTimer = null;
161
+ if (!isLoading) return;
162
+ setLoading(0);
163
+ box.onTimeout(box);
164
+ });
165
+ }
166
+ };
167
+
168
+ const updatePlaceholder = function () {
169
+ box.placeholderBox.visible = (!box.url && !box.htmlText) ? 1 : 0;
170
+ };
171
+
172
+ const updateToolbar = function () {
173
+ if (!box.toolbarBox) return;
174
+ box.lblUrl.plainText = (box.htmlText) ? "(HTML)" : (box.url || "");
175
+ box.lblUrl.elem.title = box.url || "";
176
+ box.btnOpen.visible = (box.url) ? 1 : 0;
177
+ box.btnReload.visible = (box.url || box.htmlText) ? 1 : 0;
178
+ };
179
+
180
+ // Is the URL on the origin of this page? (Then its document can be read.)
181
+ const isSameOrigin = function (url) {
182
+ try {
183
+ return new URL(url, location.href).origin === location.origin;
184
+ } catch (error) {
185
+ return false;
186
+ }
187
+ };
188
+
189
+ // WHY: a real server (Live Server, nginx, GitHub Pages) answers a missing page with a 404 and its own HTML page: that
190
+ // document can be read, so only file:// called onError. The status of the answer tells it.
191
+ // The status of the page in the iframe. null: the browser does not tell it.
192
+ const getResponseStatus = function () {
193
+ try {
194
+ const entry = iframe.contentWindow.performance.getEntriesByType("navigation")[0];
195
+ return (entry && entry.responseStatus !== undefined) ? entry.responseStatus : null;
196
+ } catch (error) {
197
+ return null;
198
+ }
199
+ };
200
+
201
+ const finishLoad = function () {
202
+ box.isLoaded = 1;
203
+ setLoading(0);
204
+ startHeightObserver();
205
+ box.onLoad(box);
206
+ };
207
+
208
+ const finishWithError = function () {
209
+ setLoading(0);
210
+ box.onError(box);
211
+ };
212
+
213
+ // WHY: Safari does not tell the status of a page (no responseStatus), so a 404 page looked like a page that loaded.
214
+ // The server is asked once more. WHY HEAD: It sends no page, and a URL that does something on a GET runs only once.
215
+ const askResponseStatus = function (url) {
216
+ const finish = function (isError) {
217
+ // WHY: The box can be removed, or another page can be opened, while the answer comes.
218
+ if (!box || box.url !== url || !isLoading) return;
219
+ if (isError) finishWithError();
220
+ else finishLoad();
221
+ };
222
+ fetch(url, { method: "HEAD", cache: "no-cache" }).then(function (response) {
223
+ const isMethodRefused = (response.status === 405 || response.status === 501);
224
+ finish(response.status >= 400 && !isMethodRefused);
225
+ }, function () {
226
+ finish(false);
227
+ });
228
+ };
229
+
230
+ const onIframeLoad = function () {
231
+
232
+ if (!box) return;
233
+ // WHY: Chrome fires "load" for about:blank too. Nothing is loaded then.
234
+ if (!box.url && !box.htmlText) return;
235
+
236
+ // WHY: Only the page opened by load() is checked (isLoading). A link in the page can open another site in the
237
+ // iframe (a payment or 3D Secure page of a shop); its document can not be read either, but it is not an error.
238
+ if (!isLoading || !box.url || !isSameOrigin(box.url)) {
239
+ finishLoad();
240
+ return;
241
+ }
242
+
243
+ // WHY: A failed same origin page (404, missing file) also fires "load", with the error page of the browser. Its document can not be read.
244
+ if (!box.getDocument()) {
245
+ finishWithError();
246
+ return;
247
+ }
248
+
249
+ const status = getResponseStatus();
250
+ if (status === null) {
251
+ askResponseStatus(box.url);
252
+ return;
253
+ }
254
+ if (status >= 400) finishWithError();
255
+ else finishLoad();
256
+
257
+ };
258
+
259
+ // Messages of this iframe only
260
+ const onWindowMessage = function (self, event) {
261
+ if (!box || !iframe || event.source !== iframe.contentWindow) return;
262
+ box.onMessage(box, event.data, event);
263
+ };
264
+
265
+ // *** AUTO HEIGHT (same origin):
266
+
267
+ const updateHeight = function () {
268
+ if (!box || box.autoHeight != 1) return;
269
+ const doc = box.getDocument();
270
+ if (!doc || !doc.documentElement) return;
271
+ const pageHeight = Math.max(doc.documentElement.scrollHeight, (doc.body) ? doc.body.scrollHeight : 0);
272
+ const zoom = (box.scale > 0) ? box.scale : 1;
273
+ const height = Math.ceil(pageHeight * zoom) + toolbarHeight + (_bs.border * 2);
274
+ if (height > 0 && box.height !== height) box.height = height;
275
+ };
276
+
277
+ const startHeightObserver = function () {
278
+ stopHeightObserver();
279
+ if (box.autoHeight != 1) return;
280
+ const doc = box.getDocument();
281
+ if (!doc || !doc.documentElement || typeof ResizeObserver === "undefined") {
282
+ updateHeight();
283
+ return;
284
+ }
285
+ heightObserver = new ResizeObserver(function () {
286
+ heightTimer = waitAndRun(heightTimer, updateHeight, HEIGHT_WAIT);
287
+ });
288
+ heightObserver.observe(doc.documentElement);
289
+ if (doc.body) heightObserver.observe(doc.body);
290
+ updateHeight();
291
+ };
292
+
293
+ const stopHeightObserver = function () {
294
+ if (heightObserver) heightObserver.disconnect();
295
+ heightObserver = null;
296
+ clearTimeout(heightTimer);
297
+ };
298
+
299
+ // *** PUBLIC FUNCTIONS:
300
+
301
+ box.load = function (url) {
302
+ box.url = url || "";
303
+ box.htmlText = "";
304
+ box.isLoaded = 0;
305
+ stopHeightObserver();
306
+ box.hideError();
307
+ iframe.removeAttribute("srcdoc");
308
+ if (box.url) {
309
+ setLoading(1);
310
+ iframe.src = box.url;
311
+ } else {
312
+ setLoading(0);
313
+ iframe.src = "about:blank";
314
+ }
315
+ updatePlaceholder();
316
+ updateToolbar();
317
+ };
318
+ // USAGE: view.load("page.htm") or view.load("") to unload
319
+
320
+ // HTML text as the page (srcdoc). Relative links in it are relative to this page.
321
+ box.loadHtml = function (html) {
322
+ box.htmlText = html || "";
323
+ box.url = "";
324
+ box.isLoaded = 0;
325
+ stopHeightObserver();
326
+ box.hideError();
327
+ iframe.removeAttribute("src");
328
+ if (box.htmlText) {
329
+ setLoading(1);
330
+ iframe.srcdoc = box.htmlText;
331
+ } else {
332
+ iframe.removeAttribute("srcdoc");
333
+ iframe.src = "about:blank";
334
+ setLoading(0);
335
+ }
336
+ updatePlaceholder();
337
+ updateToolbar();
338
+ };
339
+ // USAGE: view.loadHtml("<h1>Hello</h1>")
340
+
341
+ box.reload = function () {
342
+ if (box.htmlText) box.loadHtml(box.htmlText);
343
+ else if (box.url) box.load(box.url);
344
+ };
345
+
346
+ box.clear = function () {
347
+ box.load("");
348
+ };
349
+
350
+ // Window of the page. (Cross origin: only postMessage can be used on it.)
351
+ box.getWindow = function () {
352
+ return iframe.contentWindow;
353
+ };
354
+
355
+ // Document of the page, or null when it is another origin.
356
+ box.getDocument = function () {
357
+ try {
358
+ return iframe.contentDocument || (iframe.contentWindow && iframe.contentWindow.document) || null;
359
+ } catch (error) {
360
+ return null;
361
+ }
362
+ };
363
+
364
+ // Sends a message to the page. In the page: window.addEventListener("message", (event) => event.data)
365
+ box.postMessage = function (data, targetOrigin = "*") {
366
+ if (!iframe.contentWindow) return 0;
367
+ iframe.contentWindow.postMessage(data, targetOrigin);
368
+ return 1;
369
+ };
370
+ // USAGE: view.postMessage({ type: "theme", value: "dark" })
371
+
372
+ box.getIframe = function () {
373
+ return iframe;
374
+ };
375
+
376
+ box.setScale = function (scale) {
377
+ box.scale = (Number(scale) > 0) ? Number(scale) : 1;
378
+ applyScale();
379
+ updateHeight();
380
+ };
381
+ // USAGE: view.setScale(0.5)
382
+
383
+ box.setInteractive = function (interactive) {
384
+ box.interactive = basic.isOn(interactive) ? 1 : 0;
385
+ applyInteractive();
386
+ };
387
+
388
+ box.setAutoHeight = function (autoHeight) {
389
+ box.autoHeight = basic.isOn(autoHeight) ? 1 : 0;
390
+ if (box.autoHeight == 1 && box.isLoaded) startHeightObserver();
391
+ else stopHeightObserver();
392
+ };
393
+
394
+ box.showError = function (text) {
395
+ setLoading(0);
396
+ box.lblError.plainText = text || "The page could not be loaded.";
397
+ box.errorBox.visible = 1;
398
+ };
399
+ // USAGE: view.showError("The module is not available.")
400
+
401
+ box.hideError = function () {
402
+ box.errorBox.visible = 0;
403
+ };
404
+
405
+ box.isLoadingPage = function () {
406
+ return isLoading;
407
+ };
408
+
409
+ // WHY: box.superRemove is overwritten by a component that extends this one, so the local copy is called below.
410
+ const superRemove = box.remove;
411
+ box.superRemove = superRemove;
412
+ box.remove = function () {
413
+ if (!box) return; // WHY: remove() can be called twice (also by the parent's remove()).
414
+ stopHeightObserver(); // (a ResizeObserver of the document in the iframe: basic.js does not know it)
415
+ iframe.removeAttribute("srcdoc");
416
+ iframe.src = "about:blank"; // WHY: Unload the page (stops its timers and sounds).
417
+ superRemove.call(box); // NOTE: basic.js remove(). It cleans all the events and the objects inside.
418
+ box = null;
419
+ };
420
+
421
+ // *** OBJECT VIEW:
422
+
423
+ box.clipContent = 1;
424
+ box.clickable = 1;
425
+
426
+ // GROUP: Toolbar (optional) and page area
427
+ box.contentBox = VGroup({ width: "100%", height: "100%", align: "left top", gap: 0, position: "relative" });
428
+ box.contentBox.css.alignItems = "stretch";
429
+
430
+ if (box.toolbar == 1) {
431
+ toolbarHeight = _s.toolbar.height;
432
+
433
+ // GROUP: Toolbar
434
+ box.toolbarBox = HGroup({
435
+ width: "100%",
436
+ height: toolbarHeight,
437
+ align: "left center",
438
+ gap: 6,
439
+ padding: [10, 0],
440
+ color: _s.toolbar.color,
441
+ });
442
+ box.toolbarBox.shrink = 0;
443
+ box.toolbarBox.css = {
444
+ borderBottom: "1px solid " + _s.toolbar.borderColor,
445
+ boxSizing: "border-box",
446
+ };
447
+
448
+ // LABEL: URL
449
+ box.lblUrl = Label({ text: "", fontSize: _s.toolbar.fontSize, textColor: _s.toolbar.textColor });
450
+ box.lblUrl.css = { flex: "1 1 0", minWidth: "0" };
451
+ box.lblUrl.ellipsis = 1;
452
+ box.lblUrl.css = {
453
+ direction: "rtl", // WHY: The end of a long URL is more useful than its start.
454
+ textAlign: "left",
455
+ };
456
+
457
+ // LABEL: Reload
458
+ box.btnReload = WebView.createIconButton(WebView.getReloadSvg(_s.toolbar.iconColor), "Reload", _s.toolbar);
459
+ // LABEL: Open in a new tab
460
+ box.btnOpen = WebView.createIconButton(WebView.getOpenSvg(_s.toolbar.iconColor), "Open in a new tab", _s.toolbar);
461
+
462
+ endGroup();
463
+ }
464
+
465
+ // BOX: Page area (iframe, cover, overlays)
466
+ box.pageBox = Box({ width: "100%", height: "auto", color: "transparent", position: "relative" });
467
+ box.pageBox.css = { flex: "1 1 0", minHeight: "0", overflow: "hidden" };
468
+
469
+ // IFRAME:
470
+ iframe = document.createElement("iframe");
471
+ iframe.title = box.ariaLabel;
472
+ iframe.style.display = "block";
473
+ iframe.style.border = "none";
474
+ iframe.style.width = "100%";
475
+ iframe.style.height = "100%";
476
+ iframe.style.background = "transparent";
477
+ if (box.sandbox !== null && box.sandbox !== undefined) iframe.setAttribute("sandbox", box.sandbox);
478
+ if (box.allow) iframe.setAttribute("allow", box.allow);
479
+ if (box.referrerPolicy) iframe.setAttribute("referrerpolicy", box.referrerPolicy);
480
+ if (box.lazy == 1) iframe.setAttribute("loading", "lazy");
481
+ box.pageBox.elem.appendChild(iframe);
482
+
483
+ // WHY: The overlays are created in pageBox (a Box, not a group): they are absolute and cover the page.
484
+ // setDefaultContainerBox() is not in the start/end list, so it is set again after every endGroup().
485
+ setDefaultContainerBox(box.pageBox);
486
+
487
+ // BOX: Cover (interactive: 0)
488
+ box.cover = Box(0, 0, "100%", "100%", { color: "transparent", clickable: 1, visible: 0 });
489
+ box.cover.cursor = "pointer";
490
+
491
+ // GROUP: Placeholder
492
+ box.placeholderBox = VGroup({ width: "100%", height: "100%", align: "center center", color: _s.placeholder.color, position: "absolute" });
493
+ box.placeholderBox.css = { left: "0px", top: "0px" };
494
+ box.lblPlaceholder = Label({ text: WebView.escapeHtml(box.emptyText), fontSize: _s.placeholder.fontSize, textColor: _s.placeholder.textColor });
495
+ endGroup();
496
+
497
+ // GROUP: Loading (spinner)
498
+ setDefaultContainerBox(box.pageBox);
499
+ box.loadingBox = VGroup({ width: "100%", height: "100%", align: "center center", color: _s.loading.color, position: "absolute" });
500
+ box.loadingBox.css = { left: "0px", top: "0px", pointerEvents: "none" };
501
+ box.spinner = Box({ width: _s.loading.spinnerSize, height: _s.loading.spinnerSize, color: "transparent", round: 100 });
502
+ box.spinner.css = {
503
+ border: "3px solid " + _s.loading.trackColor,
504
+ borderTopColor: _s.loading.spinnerColor,
505
+ boxSizing: "border-box",
506
+ animation: "webViewSpin 0.8s linear infinite",
507
+ };
508
+ endGroup();
509
+
510
+ // GROUP: Error
511
+ setDefaultContainerBox(box.pageBox);
512
+ box.errorBox = VGroup({ width: "100%", height: "100%", align: "center center", padding: 20, color: _s.error.color, position: "absolute" });
513
+ box.errorBox.css = { left: "0px", top: "0px" };
514
+ box.lblError = Label({ text: "", fontSize: _s.error.fontSize, textColor: _s.error.textColor, textAlign: "center" });
515
+ endGroup();
516
+
517
+ endGroup(); // contentBox
518
+ // NOTE: endGroup() of contentBox returns the default container to the component box.
519
+
520
+ box.loadingBox.visible = 0;
521
+ box.errorBox.visible = 0;
522
+
523
+ // *** OBJECT INIT CODE:
524
+ WebView.injectCss();
525
+
526
+ box.listenTo(iframe, "load", onIframeLoad); // (removed with the box)
527
+ box.listenTo(window, "message", onWindowMessage);
528
+
529
+ box.cover.on("click", function () { box.onClick(box); });
530
+ if (box.toolbarBox) {
531
+ box.btnReload.on("click", function () { box.reload(); });
532
+ box.btnOpen.on("click", function () {
533
+ if (!box.url) return;
534
+ window.open(box.url, "_blank", "noopener");
535
+ });
536
+ }
537
+
538
+ applyScale();
539
+ applyInteractive();
540
+ updatePlaceholder();
541
+ updateToolbar();
542
+
543
+ if (startHtml) box.loadHtml(startHtml);
544
+ else if (startUrl) box.load(startUrl);
545
+
546
+ return endObject(box);
547
+
548
+ };
549
+
550
+ // *** STATIC FUNCTIONS:
551
+
552
+ // WHY: Label.text uses innerHTML. Texts must not be read as HTML.
553
+ WebView.escapeHtml = basic.escapeHtml; // (the name is kept: other code may use it)
554
+
555
+ // LABEL: Small icon button of the toolbar
556
+ WebView.createIconButton = function (svg, ariaLabel, style) {
557
+ const btn = Label({ text: svg, width: 26, height: 26, round: 5, clickable: 1 });
558
+ btn.elem.style.display = "flex";
559
+ btn.css = { alignItems: "center", justifyContent: "center", transition: "background-color 0.15s" };
560
+ btn.lineHeight = "0";
561
+ btn.shrink = 0;
562
+ btn.cursor = "pointer";
563
+ btn.role = "button";
564
+ btn.aria = { label: ariaLabel };
565
+ btn.elem.title = ariaLabel;
566
+ btn.on("mouseenter", function () {
567
+ btn.color = style.buttonHoverColor;
568
+ btn.text = svg.replace(style.iconColor, style.iconHoverColor);
569
+ });
570
+ btn.on("mouseleave", function () {
571
+ btn.color = "transparent";
572
+ btn.text = svg;
573
+ });
574
+ return btn;
575
+ };
576
+
577
+ WebView.getReloadSvg = function (color) {
578
+ return '<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="' + color + '" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12a9 9 0 1 1-2.6-6.4"/><path d="M21 3v6h-6"/></svg>';
579
+ };
580
+
581
+ WebView.getOpenSvg = function (color) {
582
+ return '<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="' + color + '" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"><path d="M14 4h6v6"/><path d="M20 4 10 14"/><path d="M18 13v6H5V6h6"/></svg>';
583
+ };
584
+
585
+ // The spinner animation can not be set with inline styles.
586
+ WebView.injectCss = function () {
587
+ basic.addCss("web-view-css", "@keyframes webViewSpin { to { transform: rotate(360deg); } }");
588
+ };