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,598 @@
1
+ /* Bismillah */
2
+
3
+ /*
4
+
5
+ ImageView - v26.11
6
+
7
+ UI COMPONENT
8
+ - A place to look at a picture: a photo, or a plot drawn by a program. It opens fitted to the box.
9
+ - Zoom: the mouse wheel and a trackpad pinch (around the mouse), two fingers on a touch screen (around the fingers),
10
+ the buttons of its toolbar. A double click goes to 100% at the mouse, and back to the fitted size.
11
+ - Move: drag it. A picture bigger than the box can not leave a gap at its edges; a smaller one stays in the middle.
12
+ - Keyboard (when it has the focus): + - zoom, 0 fits, 1 is 100%, the arrows move.
13
+ - The fitted size follows the size of the box. (After a zoom the picture keeps its zoom.)
14
+ - A file, an address or a data: address (a picture in the memory, Ex: a plot of Python or a canvas).
15
+ - A plot that is drawn again does not blink: setImage() keeps the old picture until the new one is ready, and a new
16
+ picture of the same size keeps the zoom and the place. (A picture of another size opens fitted.)
17
+ - The pixels stay sharp from 300% up (pixelatedZoom), to look at a picture pixel by pixel.
18
+ - Style packages: "light" (default), "dark". Select with styleName (ImageView.styles), or later with setTheme().
19
+
20
+ USAGE:
21
+ const view = ImageView({ width: 600, height: 400, imageFile: "photo.jpg", altText: "A cat" });
22
+ view.setImage("data:image/png;base64,...");
23
+ view.clearImage();
24
+ view.zoomIn();
25
+ view.zoomOut();
26
+ view.zoomToFit();
27
+ view.zoomToActualSize(); // 100%
28
+ view.setZoomLevel(2); // 200%
29
+ println(view.zoomLevel); // read it
30
+ view.setShowToolbar(0);
31
+ view.setTheme("dark"); // the picture and its zoom stay
32
+ ImageView({ styleName: "dark", enlargeToFit: 1, onZoomChange: function (self) { println(self.zoomLevel); } });
33
+ ImageView({ style: { image: { color: "white" } } }); // A ground behind a transparent picture
34
+
35
+ Started Date: November 2026
36
+ Webpage: https://bug7a.github.io/js-components/
37
+
38
+ Copyright 2020-2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
39
+ Licensed under the Apache License, Version 2.0
40
+ SPDX-License-Identifier: Apache-2.0
41
+
42
+ */
43
+
44
+ "use strict";
45
+
46
+ // Default values:
47
+ const ImageViewDefaults = {
48
+ key: "0", // Your name for this object: read it in a callback (self.key)
49
+ width: "100%",
50
+ height: 360,
51
+ imageFile: "", // A file, an address or a data: address. "": empty
52
+ altText: "", // What the picture shows, for a screen reader
53
+ minZoom: 0.1, // 1 is 100%. (A big picture can always go down to its fitted size.)
54
+ maxZoom: 8,
55
+ zoomStep: 1.25, // A button or a key: multiplies the zoom
56
+ enlargeToFit: 0, // 1: a picture smaller than the box also grows to fit (a vector drawing). 0: it stays at 100%
57
+ wheelZoom: 1, // 0: the wheel is left to the page around it (a viewer in a long page)
58
+ pixelatedZoom: 3, // From this zoom the pixels stay sharp. 0: never
59
+ showToolbar: 1,
60
+ emptyText: "No image",
61
+ errorText: "This image can not be opened.",
62
+ ariaLabel: "Image viewer",
63
+ buttonTexts: { zoomOut: "Zoom out", actualSize: "Actual size", zoomIn: "Zoom in", fit: "Fit to the box" }, // For a screen reader
64
+ onLoad: function (self) { }, // self.naturalWidth, self.naturalHeight
65
+ onError: function (self) { },
66
+ onZoomChange: function (self) { }, // self.zoomLevel
67
+ styleName: "light", // "light", "dark" or a name added to ImageView.styles
68
+ style: { // Light style package (default)
69
+ box: { color: "#EFEFEC", border: 1, borderColor: Black(0.14), round: 8 },
70
+ image: { color: "transparent" }, // The ground of the picture
71
+ toolbar: { color: "white", border: 1, borderColor: Black(0.12), round: 9, padding: 3, gap: 1, bottom: 10, shadow: "0 2px 10px " + Black(0.16) },
72
+ toolButton: { size: 30, iconSize: 16, round: 6, textColor: Black(0.72), hoverColor: Black(0.07), disabledOpacity: 0.32 },
73
+ zoomText: { fontSize: 12, width: 48 },
74
+ empty: { fontSize: 13, textColor: Black(0.5), iconSize: 34, lineColor: Black(0.2), space: 14, round: 6 },
75
+ error: { textColor: "#B93226" },
76
+ focus: { ringColor: "rgba(63, 141, 148, 0.8)" },
77
+ },
78
+ };
79
+
80
+ const ImageView = function (params = {}) {
81
+
82
+ // Merge style package: params.style > ImageView.styles[styleName] > ImageViewDefaults.style (light)
83
+ const pageStyle = params.style; // WHY: setTheme() puts it over another package.
84
+ basic.applyStylePackage(params, ImageView.styles, ImageViewDefaults);
85
+
86
+ // Merge params:
87
+ mergeIntoIfMissing(params, ImageViewDefaults);
88
+
89
+ // BOX: Component container
90
+ const box = startObject(params);
91
+
92
+ // *** PRIVATE VARIABLES:
93
+ let _s = box.style;
94
+ const WHEEL_SPEED = 0.002; // How much a pixel of the wheel zooms
95
+ const PINCH_SPEED = 0.01; // WHY: The pinch of a trackpad comes as a wheel with small numbers.
96
+ const WHEEL_LINE_HEIGHT = 16; // px: a wheel that counts in lines
97
+ const KEY_MOVE = 40; // px: an arrow key
98
+ const SVG_DEFAULT_WIDTH = 300; // WHY: A picture without a size of its own (an SVG with only a viewBox) has none.
99
+ const SVG_DEFAULT_HEIGHT = 150;
100
+ const DOUBLE_CLICK_ZOOM = 2; // WHY: A small picture is already at 100% when it is fitted.
101
+ const ICONS = {
102
+ minus: '<path d="M4 8h8"/>',
103
+ plus: '<path d="M4 8h8M8 4v8"/>',
104
+ fit: '<path d="M2.5 6V2.5H6M10 2.5h3.5V6M13.5 10v3.5H10M6 13.5H2.5V10"/>',
105
+ picture: '<rect x="1.5" y="2.5" width="13" height="11" rx="2"/><circle cx="5.5" cy="6" r="1.2"/><path d="M2 12l3.5-3.5 2.5 2.5 2.5-3 3.5 4"/>',
106
+ };
107
+ let imageLeft = 0;
108
+ let imageTop = 0;
109
+ let isFitted = 1; // 1: the picture follows the size of the box
110
+ let hasImage = 0;
111
+ let noteKind = "empty"; // "empty", "error", "": what the note says ("": no note)
112
+ const pointers = new Map(); // The fingers (or the mouse) on the picture: pointerId -> { x, y }
113
+ let lastPinch = null; // { distance, x, y }
114
+
115
+ // *** PUBLIC VARIABLES:
116
+ box.zoomLevel = 1; // Read only: 1 is 100%. Change it with setZoomLevel().
117
+ box.naturalWidth = 0; // The size of the picture
118
+ box.naturalHeight = 0;
119
+ // NOTE: Default values are also public variables. (box.imageFile, box.minZoom, box.maxZoom...)
120
+
121
+ // *** PRIVATE FUNCTIONS:
122
+
123
+ // The style of a package, with the style of the page over it. (setTheme)
124
+ const styleOfPackage = function (styleName) {
125
+ const fresh = { styleName: styleName, style: pageStyle };
126
+ basic.applyStylePackage(fresh, ImageView.styles, ImageViewDefaults);
127
+ mergeIntoIfMissing(fresh, ImageViewDefaults);
128
+ return fresh.style;
129
+ };
130
+
131
+ // A point of an event in the box. WHY the scale: on a page fitted with page.fit() a screen px is not a px of the box.
132
+ const screenScale = function () {
133
+ const width = box.elem.offsetWidth;
134
+ return (width > 0) ? box.elem.getBoundingClientRect().width / width : 1;
135
+ };
136
+
137
+ const pointInBox = function (event) {
138
+ const rect = box.elem.getBoundingClientRect();
139
+ const scale = screenScale();
140
+ return {
141
+ x: (event.clientX - rect.left) / scale - box.elem.clientLeft,
142
+ y: (event.clientY - rect.top) / scale - box.elem.clientTop,
143
+ };
144
+ };
145
+
146
+ const middleOfBox = function () {
147
+ return { x: box.elem.clientWidth / 2, y: box.elem.clientHeight / 2 };
148
+ };
149
+
150
+ const fittedZoom = function () {
151
+ if (!hasImage || box.elem.clientWidth <= 0 || box.elem.clientHeight <= 0) return 1;
152
+ const zoom = Math.min(box.elem.clientWidth / box.naturalWidth, box.elem.clientHeight / box.naturalHeight);
153
+ return (box.enlargeToFit == 1) ? zoom : Math.min(zoom, 1);
154
+ };
155
+
156
+ const lowestZoom = function () {
157
+ return Math.min(box.minZoom, fittedZoom());
158
+ };
159
+
160
+ const highestZoom = function () {
161
+ return Math.max(box.maxZoom, fittedZoom());
162
+ };
163
+
164
+ // One axis: a picture smaller than the box stays in the middle; a bigger one leaves no gap at its edges.
165
+ const limitPosition = function (position, imageSize, boxSize) {
166
+ if (imageSize <= boxSize) return (boxSize - imageSize) / 2;
167
+ return basic.clamp(position, boxSize - imageSize, 0);
168
+ };
169
+
170
+ // LAYOUT: the picture at its zoom and place
171
+ // WHY its size and not a CSS scale: the browser draws it sharp at every zoom (a vector drawing too).
172
+ const placeImage = function () {
173
+ const width = box.naturalWidth * box.zoomLevel;
174
+ const height = box.naturalHeight * box.zoomLevel;
175
+ imageLeft = limitPosition(imageLeft, width, box.elem.clientWidth);
176
+ imageTop = limitPosition(imageTop, height, box.elem.clientHeight);
177
+ box.image.left = imageLeft;
178
+ box.image.top = imageTop;
179
+ box.image.width = width;
180
+ box.image.height = height;
181
+ };
182
+
183
+ const canMove = function () {
184
+ const isWider = (box.naturalWidth * box.zoomLevel > box.elem.clientWidth);
185
+ const isTaller = (box.naturalHeight * box.zoomLevel > box.elem.clientHeight);
186
+ return (hasImage && (isWider || isTaller));
187
+ };
188
+
189
+ // WHY not button.enabled: a disabled button loses the focus, and the keys of a keyboard user with it.
190
+ // At its limit a click does nothing anyway.
191
+ const paintToolButton = function (button, isUsable) {
192
+ button.isUsable = isUsable;
193
+ button.opacity = (isUsable) ? 1 : _s.toolButton.disabledOpacity;
194
+ button.cursor = (isUsable) ? "pointer" : "default";
195
+ button.aria = { disabled: (isUsable) ? "false" : "true" };
196
+ if (!isUsable) button.color = "transparent";
197
+ };
198
+
199
+ // PAINT: what depends on the zoom
200
+ const paintZoom = function () {
201
+ const isPixelated = (box.pixelatedZoom > 0 && box.zoomLevel >= box.pixelatedZoom);
202
+ box.image.css.imageRendering = (isPixelated) ? "pixelated" : "auto";
203
+ box.btnZoomText.plainText = Math.round(box.zoomLevel * 100) + "%";
204
+ paintToolButton(box.btnZoomOut, box.zoomLevel > lowestZoom());
205
+ paintToolButton(box.btnZoomIn, box.zoomLevel < highestZoom());
206
+ box.cursor = canMove() ? "grab" : "default";
207
+ // WHY: At the fitted size a finger still scrolls the page around the viewer; zoomed in, it moves the picture.
208
+ box.css.touchAction = canMove() ? "none" : "pan-x pan-y";
209
+ };
210
+
211
+ // PAINT: what is shown: the picture, or a note (no note: a picture is loading, or it is there)
212
+ const paintContent = function () {
213
+ const isError = (noteKind === "error");
214
+ const noteColor = (isError) ? _s.error.textColor : _s.empty.textColor;
215
+ box.image.visible = (hasImage) ? 1 : 0;
216
+ box.toolbar.visible = (hasImage && box.showToolbar == 1) ? 1 : 0;
217
+ box.emptyGroup.visible = (noteKind) ? 1 : 0;
218
+ box.lblEmpty.plainText = (isError) ? box.errorText : box.emptyText;
219
+ box.lblEmpty.textColor = noteColor;
220
+ box.icoEmpty.textColor = noteColor;
221
+ };
222
+
223
+ const applyToolButtonLook = function (button) {
224
+ const look = _s.toolButton;
225
+ button.props({ width: look.size, height: look.size, fontSize: _s.zoomText.fontSize, textColor: look.textColor, round: look.round });
226
+ button.setFocusRing(_s.focus.ringColor);
227
+ };
228
+
229
+ // LOOK: everything that comes from the style (the first drawing and setTheme)
230
+ const applyLook = function () {
231
+ const toolbarLook = _s.toolbar;
232
+ const emptySpace = _s.empty.space + "px";
233
+ box.props(_s.box);
234
+ box.setFocusRing(_s.focus.ringColor);
235
+ box.elem.style.setProperty("--image-view-icon", _s.toolButton.iconSize + "px");
236
+ box.image.color = _s.image.color;
237
+ box.emptyGroup.round = _s.empty.round;
238
+ // WHY in css: the bottom and right props of basic.js clear top and left.
239
+ box.emptyGroup.css = { left: emptySpace, top: emptySpace, right: emptySpace, bottom: emptySpace };
240
+ box.emptyGroup.css.border = "1px dashed " + _s.empty.lineColor;
241
+ box.icoEmpty.props({ width: _s.empty.iconSize, height: _s.empty.iconSize });
242
+ box.lblEmpty.fontSize = _s.empty.fontSize;
243
+ box.toolbar.props({
244
+ gap: toolbarLook.gap,
245
+ padding: toolbarLook.padding,
246
+ color: toolbarLook.color,
247
+ border: toolbarLook.border,
248
+ borderColor: toolbarLook.borderColor,
249
+ round: toolbarLook.round,
250
+ boxShadow: toolbarLook.shadow,
251
+ });
252
+ box.toolbar.css.bottom = toolbarLook.bottom + "px";
253
+ [box.btnZoomOut, box.btnZoomText, box.btnZoomIn, box.btnFit].forEach(applyToolButtonLook);
254
+ box.btnZoomText.width = _s.zoomText.width;
255
+ paintContent();
256
+ };
257
+
258
+ // Everything of a zoom change goes through here.
259
+ const zoomAtPoint = function (zoom, point) {
260
+ const oldZoom = box.zoomLevel;
261
+ box.zoomLevel = basic.clamp(zoom, lowestZoom(), highestZoom());
262
+ const ratio = box.zoomLevel / oldZoom;
263
+ imageLeft = point.x - (point.x - imageLeft) * ratio;
264
+ imageTop = point.y - (point.y - imageTop) * ratio;
265
+ placeImage();
266
+ paintZoom();
267
+ if (box.zoomLevel != oldZoom) box.onZoomChange(box);
268
+ };
269
+
270
+ const moveImage = function (deltaX, deltaY) {
271
+ imageLeft += deltaX;
272
+ imageTop += deltaY;
273
+ placeImage();
274
+ };
275
+
276
+ const showFitted = function () {
277
+ isFitted = 1;
278
+ zoomAtPoint(fittedZoom(), middleOfBox());
279
+ };
280
+
281
+ const zoomByUser = function (zoom, point) {
282
+ if (!hasImage) return;
283
+ isFitted = 0;
284
+ zoomAtPoint(zoom, point);
285
+ };
286
+
287
+ const onImageLoad = function () {
288
+ const width = box.image.elem.naturalWidth || SVG_DEFAULT_WIDTH;
289
+ const height = box.image.elem.naturalHeight || SVG_DEFAULT_HEIGHT;
290
+ const isSameSize = (hasImage && width == box.naturalWidth && height == box.naturalHeight);
291
+ hasImage = 1;
292
+ box.naturalWidth = width;
293
+ box.naturalHeight = height;
294
+ if (!isSameSize) {
295
+ box.zoomLevel = fittedZoom(); // WHY: A new picture opens fitted; this is not a zoom of the user.
296
+ isFitted = 1;
297
+ placeImage();
298
+ paintZoom();
299
+ box.onZoomChange(box);
300
+ }
301
+ noteKind = "";
302
+ paintContent();
303
+ box.onLoad(box);
304
+ };
305
+
306
+ const onImageError = function () {
307
+ hasImage = 0;
308
+ noteKind = "error";
309
+ paintContent();
310
+ box.onError(box);
311
+ };
312
+
313
+ const onWheel = function (self, event) {
314
+ if (box.wheelZoom != 1 || !hasImage) return;
315
+ event.preventDefault(); // WHY: The wheel zooms here; the page around it must not scroll too.
316
+ const speed = (event.ctrlKey) ? PINCH_SPEED : WHEEL_SPEED;
317
+ const pixels = (event.deltaMode === 1) ? event.deltaY * WHEEL_LINE_HEIGHT : event.deltaY;
318
+ zoomByUser(box.zoomLevel * Math.exp(-pixels * speed), pointInBox(event));
319
+ };
320
+
321
+ const pinchOfPointers = function () {
322
+ const list = Array.from(pointers.values());
323
+ return {
324
+ distance: Math.hypot(list[0].x - list[1].x, list[0].y - list[1].y),
325
+ x: (list[0].x + list[1].x) / 2,
326
+ y: (list[0].y + list[1].y) / 2,
327
+ };
328
+ };
329
+
330
+ // WHY: The drag goes on when the mouse leaves the box. A pointer that has just ended makes the browser throw.
331
+ const capturePointer = function (pointerId) {
332
+ try {
333
+ box.elem.setPointerCapture(pointerId);
334
+ } catch (error) {
335
+ pointers.delete(pointerId);
336
+ }
337
+ };
338
+
339
+ const onPointerDown = function (self, event) {
340
+ if (!hasImage || event.button !== 0 || box.toolbar.elem.contains(event.target)) return;
341
+ pointers.set(event.pointerId, pointInBox(event));
342
+ capturePointer(event.pointerId);
343
+ if (pointers.size == 2) lastPinch = pinchOfPointers();
344
+ if (canMove()) box.cursor = "grabbing";
345
+ };
346
+
347
+ const onPointerMove = function (self, event) {
348
+ const last = pointers.get(event.pointerId);
349
+ if (!last) return;
350
+ const point = pointInBox(event);
351
+ pointers.set(event.pointerId, point);
352
+
353
+ if (pointers.size == 1) {
354
+ if (!canMove()) return;
355
+ isFitted = 0;
356
+ moveImage(point.x - last.x, point.y - last.y);
357
+ } else if (pointers.size == 2 && lastPinch) {
358
+ const pinch = pinchOfPointers();
359
+ moveImage(pinch.x - lastPinch.x, pinch.y - lastPinch.y);
360
+ if (lastPinch.distance > 0) zoomByUser(box.zoomLevel * pinch.distance / lastPinch.distance, pinch);
361
+ lastPinch = pinch;
362
+ }
363
+ };
364
+
365
+ const onPointerEnd = function (self, event) {
366
+ pointers.delete(event.pointerId);
367
+ if (pointers.size < 2) lastPinch = null;
368
+ if (pointers.size == 0) box.cursor = canMove() ? "grab" : "default";
369
+ };
370
+
371
+ const onDoubleClick = function (self, event) {
372
+ if (!hasImage || box.toolbar.elem.contains(event.target)) return;
373
+ if (!isFitted) {
374
+ box.zoomToFit();
375
+ return;
376
+ }
377
+ const isAtActualSize = (Math.abs(box.zoomLevel - 1) < 0.01);
378
+ zoomByUser((isAtActualSize) ? DOUBLE_CLICK_ZOOM : 1, pointInBox(event));
379
+ };
380
+
381
+ const onKeyDown = function (self, event) {
382
+ if (!hasImage || box.toolbar.elem.contains(event.target)) return;
383
+ if (event.ctrlKey || event.metaKey || event.altKey) return; // WHY: Ctrl + 0 and the others belong to the browser.
384
+ const moves = { ArrowLeft: [KEY_MOVE, 0], ArrowRight: [-KEY_MOVE, 0], ArrowUp: [0, KEY_MOVE], ArrowDown: [0, -KEY_MOVE] };
385
+ const move = moves[event.key];
386
+ if (move && canMove()) {
387
+ isFitted = 0;
388
+ moveImage(move[0], move[1]);
389
+ } else if (event.key === "+" || event.key === "=") {
390
+ box.zoomIn();
391
+ } else if (event.key === "-") {
392
+ box.zoomOut();
393
+ } else if (event.key === "0") {
394
+ box.zoomToFit();
395
+ } else if (event.key === "1") {
396
+ box.zoomToActualSize();
397
+ } else {
398
+ return;
399
+ }
400
+ event.preventDefault();
401
+ };
402
+
403
+ const onBoxResize = function () {
404
+ if (!hasImage) return;
405
+ if (isFitted) showFitted();
406
+ else placeImage();
407
+ };
408
+
409
+ const svgIcon = function (paths) {
410
+ return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="currentColor"' +
411
+ ' stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round">' + paths + "</svg>";
412
+ };
413
+
414
+ // BUTTON: of the toolbar
415
+ const makeToolButton = function (ariaLabel, onClick) {
416
+ const button = Button({
417
+ color: "transparent",
418
+ shrink: 0,
419
+ css: { padding: "0", display: "flex", alignItems: "center", justifyContent: "center" },
420
+ });
421
+ button.minimal = 1;
422
+ button.isUsable = 1;
423
+ button.aria = { label: ariaLabel };
424
+ button.onHoverChange(function (self, isOver) {
425
+ self.color = (isOver && self.isUsable) ? _s.toolButton.hoverColor : "transparent";
426
+ });
427
+ button.on("click", onClick);
428
+ return button;
429
+ };
430
+
431
+ const makeIconButton = function (iconName, ariaLabel, onClick) {
432
+ const button = makeToolButton(ariaLabel, onClick);
433
+ button.text = svgIcon(ICONS[iconName]);
434
+ button.elem.classList.add("image-view-icon");
435
+ return button;
436
+ };
437
+
438
+ // *** PUBLIC FUNCTIONS:
439
+
440
+ // file: a file, an address or a data: address. "": no picture
441
+ box.setImage = function (file, altText = box.altText) {
442
+ box.imageFile = file;
443
+ box.altText = altText;
444
+ box.image.alt = altText;
445
+ // WHY: The old picture stays until the new one is ready (a browser keeps it on the screen meanwhile).
446
+ if (file) {
447
+ noteKind = "";
448
+ paintContent();
449
+ box.image.load(file);
450
+ return;
451
+ }
452
+ hasImage = 0;
453
+ noteKind = "empty";
454
+ pointers.clear();
455
+ lastPinch = null;
456
+ box.image.elem.removeAttribute("src");
457
+ paintContent();
458
+ };
459
+ // USAGE: view.setImage("plot.png", "The sales of the year")
460
+
461
+ box.clearImage = function () {
462
+ box.setImage("");
463
+ };
464
+ // USAGE: view.clearImage()
465
+
466
+ box.zoomIn = function () {
467
+ zoomByUser(box.zoomLevel * box.zoomStep, middleOfBox());
468
+ };
469
+
470
+ box.zoomOut = function () {
471
+ zoomByUser(box.zoomLevel / box.zoomStep, middleOfBox());
472
+ };
473
+
474
+ box.zoomToFit = function () {
475
+ if (hasImage) showFitted();
476
+ };
477
+
478
+ box.zoomToActualSize = function () {
479
+ zoomByUser(1, middleOfBox());
480
+ };
481
+
482
+ box.setZoomLevel = function (zoom) {
483
+ zoomByUser(zoom, middleOfBox());
484
+ };
485
+ // USAGE: get: view.zoomLevel, set: view.setZoomLevel(2) // 200%
486
+
487
+ box.setShowToolbar = function (value) {
488
+ box.showToolbar = basic.isOn(value) ? 1 : 0;
489
+ box.toolbar.visible = (hasImage && box.showToolbar == 1) ? 1 : 0;
490
+ };
491
+ // USAGE: view.setShowToolbar(0)
492
+
493
+ // Another style package while it is on the page. A style given by the page stays over it.
494
+ box.setTheme = function (styleName) {
495
+ box.styleName = styleName;
496
+ _s = styleOfPackage(styleName);
497
+ box.style = _s;
498
+ applyLook();
499
+ };
500
+ // USAGE: view.setTheme("dark")
501
+
502
+ // *** OBJECT VIEW:
503
+
504
+ // An SVG icon fills its place in a button.
505
+ basic.addCss("image-view-icon", ".image-view-icon svg { display: block; width: var(--image-view-icon); height: var(--image-view-icon); }");
506
+
507
+ // BOX: The frame
508
+ box.clickable = 1;
509
+ box.tabIndex = 0;
510
+ box.role = "group";
511
+ box.aria = { label: box.ariaLabel };
512
+
513
+ // ICON: The picture
514
+ box.image = Icon({ left: 0, top: 0, width: 1, height: 1, visible: 0 });
515
+ // WHY: The size comes from zoomAtPoint; the picture is never stretched by its element.
516
+ box.image.imageFit = "fill";
517
+
518
+ // GROUP: The note of an empty view (or of a picture that can not be opened), in the middle at every size
519
+ box.emptyGroup = VGroup({
520
+ align: "center center",
521
+ gap: 8,
522
+ padding: 12,
523
+ css: { position: "absolute", width: "auto", height: "auto" },
524
+ });
525
+
526
+ // LABEL: Its icon (the color of the text)
527
+ box.icoEmpty = Label({});
528
+ box.icoEmpty.text = svgIcon(ICONS.picture);
529
+ box.icoEmpty.elem.classList.add("image-view-note-icon");
530
+
531
+ // LABEL: Its text
532
+ box.lblEmpty = Label({ textAlign: "center" });
533
+
534
+ endGroup();
535
+ basic.addCss("image-view-note-icon", ".image-view-note-icon svg { display: block; width: 100%; height: 100%; }");
536
+
537
+ // GROUP: The toolbar
538
+ box.toolbar = HGroup({
539
+ width: "auto",
540
+ height: "auto",
541
+ align: "center center",
542
+ // WHY: In the middle at every size of the box and of its text.
543
+ css: { position: "absolute", left: "50%", top: "auto", transform: "translateX(-50%)" },
544
+ });
545
+ box.toolbar.clickable = 1; // WHY: A click between its buttons must not fall on the picture.
546
+
547
+ box.btnZoomOut = makeIconButton("minus", box.buttonTexts.zoomOut, function () { box.zoomOut(); });
548
+
549
+ // BUTTON: The zoom. A click goes to 100%.
550
+ box.btnZoomText = makeToolButton(box.buttonTexts.actualSize, function () { box.zoomToActualSize(); });
551
+
552
+ box.btnZoomIn = makeIconButton("plus", box.buttonTexts.zoomIn, function () { box.zoomIn(); });
553
+ box.btnFit = makeIconButton("fit", box.buttonTexts.fit, function () { box.zoomToFit(); });
554
+
555
+ endGroup();
556
+
557
+ // *** OBJECT INIT CODE:
558
+
559
+ box.listenTo(box.image.elem, "load", onImageLoad);
560
+ box.listenTo(box.image.elem, "error", onImageError);
561
+ // WHY passive: false: onWheel stops the page around the viewer from scrolling.
562
+ box.on("wheel", onWheel, { passive: false });
563
+ box.on("pointerdown", onPointerDown);
564
+ box.on("pointermove", onPointerMove);
565
+ box.on("pointerup", onPointerEnd);
566
+ box.on("pointercancel", onPointerEnd);
567
+ box.on("dblclick", onDoubleClick);
568
+ box.on("keydown", onKeyDown);
569
+ box.onResize(onBoxResize);
570
+
571
+ applyLook();
572
+ box.setImage(box.imageFile, box.altText);
573
+
574
+ return endObject(box);
575
+
576
+ };
577
+
578
+ // *** STYLE PACKAGES:
579
+ // USAGE: ImageView({ styleName: "dark" })
580
+ // USAGE: ImageView({ styleName: "dark", style: { box: { round: 0, border: 0 } } }) // Change only some keys.
581
+ // NOTE: A new package needs only the keys that differ from the default (light) style.
582
+ // USAGE: ImageView.styles.myStyle = { box: { color: "#FFFDF5" } };
583
+ ImageView.styles = {
584
+
585
+ // A light gray ground, a white toolbar.
586
+ light: ImageViewDefaults.style,
587
+
588
+ // For a dark window.
589
+ dark: {
590
+ box: { color: "#161615", borderColor: White(0.1) },
591
+ toolbar: { color: "#2B2B2A", borderColor: White(0.12), shadow: "0 2px 12px " + Black(0.5) },
592
+ toolButton: { textColor: White(0.85), hoverColor: White(0.1) },
593
+ empty: { textColor: White(0.52), lineColor: White(0.2) },
594
+ error: { textColor: "#FF9F93" },
595
+ focus: { ringColor: "rgba(111, 198, 205, 0.8)" },
596
+ },
597
+
598
+ };