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,738 @@
1
+ /* Bismillah */
2
+
3
+ /*
4
+
5
+ SparkLineBox - v26.09
6
+
7
+ UI COMPONENT TEMPLATE
8
+ - An alternative to MiniGraphBox: a small KPI box with a title, a value text, an icon and a line graph (sparkline)
9
+ instead of bars. Same API: addValue, setValues, setTitleText, setValueText, setIcon...
10
+ - The line is drawn with SVG (one path for the line, one for the area under it). Objects are created one time,
11
+ only the path data changes. (Fast for live data)
12
+ - Trend badge: "▲ 12%" or "▼ 3%", the last value compared with the first visible value. (showTrend, or your own trendText)
13
+ - Last value dot, optional min / max dots, and marked values: addValue(95, "red") draws a dot in that color.
14
+ - Mouse over: a tooltip shows the value of the nearest point. (showTooltip: 1)
15
+ - maxPoints: How many values are kept. New values are added to the right, the first value is removed when it is full.
16
+ - minValue / maxValue: 0 or null means auto (the smallest / biggest visible value). minValue: 0 keeps the base line at zero.
17
+ - curve: "smooth" or "linear".
18
+
19
+ VALUE: A number, or { value, color }
20
+
21
+ USAGE:
22
+ const graph = SparkLineBox({ title: "Sold Items", valueText: "1.3K", iconFile: "carrot.png", lineColor: "tomato" });
23
+ graph.addValue(24);
24
+ graph.addValue(95, "red"); // Marked value: a red dot on the line
25
+ graph.setValues([10, 20, 15]);
26
+ SparkLineBox({ title: "CPU", maxValue: 100, minValue: 0, valueFormat: (value) => value + "%" });
27
+
28
+ Started Date: September 2026
29
+ Webpage: https://bug7a.github.io/js-components/
30
+
31
+ Copyright 2020-2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
32
+ Licensed under the Apache License, Version 2.0
33
+ SPDX-License-Identifier: Apache-2.0
34
+
35
+ */
36
+
37
+ "use strict";
38
+
39
+ // Default values:
40
+ const SparkLineBoxDefaults = {
41
+ key: "0",
42
+ width: 200,
43
+ height: 80,
44
+ titleText: "Sold Items",
45
+ valueText: "1.3K",
46
+ allowHtml: 0, // 1: titleText, valueText, trendText and the text of valueFormat are HTML (only for your own texts)
47
+ iconFile: "", // "": No icon
48
+ lineColor: "tomato",
49
+ values: [], // [10, 20, { value: 15, color: "orange" }]
50
+ maxPoints: 30, // How many values are shown
51
+ minValue: null, // null: Auto. 0: The base line is always zero.
52
+ maxValue: 0, // 0: Auto (the biggest visible value is the top of the graph)
53
+ curve: "smooth", // "smooth", "linear"
54
+ showArea: 1, // 1: Light fill under the line
55
+ showLastDot: 1, // 1: A dot on the last value
56
+ showMinMax: 0, // 1: Dots on the smallest and the biggest value
57
+ showTrend: 1, // 1: Trend badge (last value compared with the first visible value)
58
+ trendText: "", // Your own badge text. "": Calculated. (Ex: "▲ 12%")
59
+ showTooltip: 1, // 1: Tooltip with the value on mouse over
60
+ animated: 1, // 1: The line moves with a short motion when a value is added.
61
+ valueFormat: function (value) { return String(value); }, // Tooltip text
62
+ onClick: function (self) { },
63
+ style: {
64
+ box: {
65
+ color: "whitesmoke",
66
+ border: 1,
67
+ borderColor: Black(0.6),
68
+ round: 4,
69
+ },
70
+ text: {
71
+ padding: 8,
72
+ },
73
+ title: {
74
+ fontSize: 12,
75
+ textColor: Black(0.5),
76
+ },
77
+ valueText: {
78
+ fontSize: 20,
79
+ textColor: Black(0.75),
80
+ },
81
+ icon: {
82
+ width: 24,
83
+ height: 24,
84
+ },
85
+ trend: {
86
+ fontSize: 11,
87
+ upColor: "#2E9E5B",
88
+ downColor: "#D64545",
89
+ flatColor: Black(0.45),
90
+ round: 4,
91
+ padding: [5, 1],
92
+ backgroundOpacity: 0.12,
93
+ },
94
+ graph: {
95
+ height: 30, // Height of the graph area at the bottom
96
+ padding: 4, // Left and right space
97
+ lineWidth: 2,
98
+ areaOpacity: 0.15,
99
+ dotRadius: 3,
100
+ minMaxDotRadius: 2.5,
101
+ minMaxColor: Black(0.45),
102
+ dotRingColor: White(1), // The ring around the dots: the color of the box (a dark box: its dark color)
103
+ motion: 0.2, // Seconds. 0: No animation
104
+ },
105
+ tooltip: {
106
+ color: "#373836",
107
+ textColor: White(1),
108
+ fontSize: 11,
109
+ round: 4,
110
+ padding: [6, 2],
111
+ lineColor: Black(0.25),
112
+ },
113
+ }
114
+ };
115
+
116
+ const SparkLineBox = function (params = {}) {
117
+
118
+ // Merge params:
119
+ mergeIntoIfMissing(params, SparkLineBoxDefaults);
120
+
121
+ // Edit params, if needed:
122
+ const _bs = params.style.box;
123
+ params.color = _bs.color;
124
+ params.border = _bs.border;
125
+ params.borderColor = _bs.borderColor;
126
+ params.round = _bs.round;
127
+
128
+ // BOX: Component container
129
+ let box = startObject(params);
130
+
131
+ // *** PRIVATE VARIABLES:
132
+ const _s = box.style;
133
+ const SVG_NS = "http://www.w3.org/2000/svg";
134
+ let svg, areaPath, linePath, lastDot, minDot, maxDot, hoverLine, hoverDot;
135
+ let markDots = []; // Dots of the marked values (with a color)
136
+ let points = []; // [{ x, y, item }] of the last draw
137
+ let cancelResize = null; // (box.after: stopped by remove())
138
+ let graphWidth = 0;
139
+ let hoverIndex = -1;
140
+ const RESIZE_WAIT = 30; // ms
141
+ const TOOLTIP_EDGE_SPACE = 2; // Between the tooltip and the box sides
142
+ const TOOLTIP_GAP = 8; // Between the tooltip and the point
143
+
144
+ // *** PUBLIC VARIABLES:
145
+ // [var] Visible values, from left to right: [{ value, color }]
146
+ box.values = [];
147
+
148
+ // *** PRIVATE FUNCTIONS:
149
+
150
+ const setText = function (label, text) {
151
+ const value = (text === undefined || text === null) ? "" : String(text);
152
+ if (box.allowHtml == 1) label.text = value;
153
+ else label.plainText = value;
154
+ };
155
+
156
+ const normalizeValue = function (item) {
157
+ if (item !== null && typeof item == "object") {
158
+ return { value: basic.num(item.value) || 0, color: item.color || "" };
159
+ }
160
+ return { value: basic.num(item) || 0, color: "" };
161
+ };
162
+
163
+ // How many points the graph has room for (maxPoints, at least 2).
164
+ const getSlotCount = function () {
165
+ return Math.max(2, basic.num(box.maxPoints) || 2);
166
+ };
167
+
168
+ // Keep only the last values that fit in the graph.
169
+ const trimValues = function () {
170
+ const max = getSlotCount();
171
+ if (box.values.length > max) {
172
+ box.values = box.values.slice(box.values.length - max);
173
+ }
174
+ };
175
+
176
+ // { min, max } of the graph scale
177
+ const getRange = function () {
178
+ let min = Infinity;
179
+ let max = -Infinity;
180
+ box.values.forEach(function (item) {
181
+ if (item.value < min) min = item.value;
182
+ if (item.value > max) max = item.value;
183
+ });
184
+ if (box.values.length == 0) {
185
+ min = 0;
186
+ max = 0;
187
+ }
188
+ const hasMinValue = (box.minValue !== null && box.minValue !== undefined && box.minValue !== "");
189
+ if (hasMinValue) min = Math.min(basic.num(box.minValue) || 0, min);
190
+ if (box.maxValue > 0) max = Math.max(box.maxValue, min);
191
+ return { min: min, max: max };
192
+ };
193
+
194
+ const createSvgElement = function (name, attributes) {
195
+ const element = document.createElementNS(SVG_NS, name);
196
+ Object.keys(attributes).forEach(function (key) { element.setAttribute(key, attributes[key]); });
197
+ return element;
198
+ };
199
+
200
+ // SVG elements are not basic.js objects: they are shown with style.display.
201
+ const setShown = function (element, isShown) {
202
+ element.style.display = (isShown) ? "" : "none";
203
+ };
204
+
205
+ const placeDot = function (dot, point) {
206
+ dot.setAttribute("cx", point.x);
207
+ dot.setAttribute("cy", point.y);
208
+ };
209
+
210
+ // Path data of the line: "M x y C ..." (smooth) or "M x y L x y ..." (linear)
211
+ const getLinePathData = function (list) {
212
+
213
+ if (list.length == 0) return "";
214
+ if (list.length == 1) return "M " + list[0].x + " " + list[0].y;
215
+
216
+ let d = "M " + list[0].x + " " + list[0].y;
217
+
218
+ if (box.curve === "linear") {
219
+ for (let i = 1; i < list.length; i++) d += " L " + list[i].x + " " + list[i].y;
220
+ return d;
221
+ }
222
+
223
+ // Smooth: Catmull-Rom to cubic bezier
224
+ for (let i = 0; i < list.length - 1; i++) {
225
+ const p0 = list[Math.max(i - 1, 0)];
226
+ const p1 = list[i];
227
+ const p2 = list[i + 1];
228
+ const p3 = list[Math.min(i + 2, list.length - 1)];
229
+ const c1x = p1.x + (p2.x - p0.x) / 6;
230
+ const c1y = p1.y + (p2.y - p0.y) / 6;
231
+ const c2x = p2.x - (p3.x - p1.x) / 6;
232
+ const c2y = p2.y - (p3.y - p1.y) / 6;
233
+ d += " C " + r1(c1x) + " " + r1(c1y) + " " + r1(c2x) + " " + r1(c2y) + " " + p2.x + " " + p2.y;
234
+ }
235
+ return d;
236
+
237
+ };
238
+
239
+ const r1 = function (value) {
240
+ return Math.round(value * 10) / 10;
241
+ };
242
+
243
+ // WHY: The "d" transition only works on the CSS property (Chrome). The attribute is set for the other browsers.
244
+ const setPathData = function (path, data) {
245
+ path.setAttribute("d", data);
246
+ if (SparkLineBox.supportsCssPath) path.style.d = (data) ? 'path("' + data + '")' : "";
247
+ };
248
+
249
+ const setLineMotion = function (enabled) {
250
+ const seconds = (enabled && box.animated == 1) ? _s.graph.motion : 0;
251
+ const motion = (seconds > 0) ? "d " + seconds + "s" : "none";
252
+ const dotMotion = (seconds > 0) ? "cx " + seconds + "s, cy " + seconds + "s" : "none";
253
+ linePath.style.transition = motion;
254
+ areaPath.style.transition = motion;
255
+ lastDot.style.transition = dotMotion; // Only the last dot moves. The min / max and marked dots belong to old points, they jump.
256
+ };
257
+
258
+ // Path data of the line and the area for a point list
259
+ const getPathDataSet = function (list) {
260
+ const lineData = getLinePathData(list);
261
+ let areaData = "";
262
+ if (box.showArea == 1 && list.length > 1) {
263
+ const first = list[0];
264
+ const last = list[list.length - 1];
265
+ areaData = lineData + " L " + last.x + " " + _s.graph.height + " L " + first.x + " " + _s.graph.height + " Z";
266
+ }
267
+ return { line: lineData, area: areaData };
268
+ };
269
+
270
+ // { x, y, item } of every value. range: { min, max } of the scale
271
+ const getPoints = function (range) {
272
+
273
+ const padding = _s.graph.padding;
274
+ const lineWidth = _s.graph.lineWidth;
275
+ const innerWidth = Math.max(0, graphWidth - padding * 2);
276
+ const top = lineWidth + _s.graph.dotRadius; // Space for the line width and the dots
277
+ const bottom = _s.graph.height - lineWidth - _s.graph.dotRadius;
278
+ const span = range.max - range.min;
279
+
280
+ // WHY: The x positions come from maxPoints, not from the value count. So the line grows from the left and does not stretch.
281
+ const slots = getSlotCount();
282
+ const step = (slots > 1) ? innerWidth / (slots - 1) : 0;
283
+
284
+ return box.values.map(function (item, index) {
285
+ const x = r1(padding + index * step);
286
+ let y = r1((top + bottom) / 2);
287
+ if (span > 0) y = r1(bottom - (item.value - range.min) / span * (bottom - top));
288
+ return { x: x, y: y, item: item };
289
+ });
290
+
291
+ };
292
+
293
+ // START SHAPE: The old points at their new places (no motion) and the new point on the previous point.
294
+ // Same number of segments as the final shape, so the "d" transition moves only the last segment.
295
+ const drawStartShape = function () {
296
+ const last = points[points.length - 1];
297
+ const previous = points[points.length - 2];
298
+ const startPoints = points.slice(0, -1).concat([{ x: previous.x, y: previous.y, item: last.item }]);
299
+ const startData = getPathDataSet(startPoints);
300
+ setLineMotion(0);
301
+ setPathData(linePath, startData.line);
302
+ if (startData.area) setPathData(areaPath, startData.area);
303
+ placeDot(lastDot, previous);
304
+ void svg.getBoundingClientRect(); // WHY: Apply the start shape now, before the motion is turned on.
305
+ setLineMotion(1);
306
+ };
307
+
308
+ const drawMinMaxDots = function (span) {
309
+
310
+ const isShown = (box.showMinMax == 1 && points.length > 1 && span > 0);
311
+ setShown(minDot, isShown);
312
+ setShown(maxDot, isShown);
313
+ if (!isShown) return;
314
+
315
+ let minPoint = points[0];
316
+ let maxPoint = points[0];
317
+ points.forEach(function (p) {
318
+ if (p.item.value < minPoint.item.value) minPoint = p;
319
+ if (p.item.value > maxPoint.item.value) maxPoint = p;
320
+ });
321
+ placeDot(minDot, minPoint);
322
+ placeDot(maxDot, maxPoint);
323
+
324
+ };
325
+
326
+ // Marked values (own color)
327
+ const drawMarkDots = function () {
328
+
329
+ const marked = points.filter(function (p) { return p.item.color; });
330
+
331
+ while (markDots.length < marked.length) {
332
+ const dot = createSvgElement("circle", { r: _s.graph.dotRadius, stroke: _s.graph.dotRingColor, "stroke-width": 1 });
333
+ svg.appendChild(dot);
334
+ markDots.push(dot);
335
+ }
336
+
337
+ markDots.forEach(function (dot, index) {
338
+ const p = marked[index];
339
+ setShown(dot, !!p);
340
+ if (!p) return;
341
+ placeDot(dot, p);
342
+ dot.setAttribute("fill", p.item.color);
343
+ });
344
+
345
+ };
346
+
347
+ // Draw the line for the current values.
348
+ // animated: 1 -> Only the new (last) point moves: it starts at the previous point and moves to its place.
349
+ // 0 -> The line jumps to the new shape. (setValues, resize)
350
+ // WHY: When the graph is full, every point gets its right neighbour's value. If the whole path is animated, the
351
+ // old points move up and down like a wave. The old points must jump, only the new point is animated.
352
+ const draw = function (animated = 1) {
353
+
354
+ if (!box || !svg) return;
355
+
356
+ const range = getRange();
357
+ points = getPoints(range);
358
+
359
+ const last = points[points.length - 1];
360
+ const canAnimate = (animated && box.animated == 1 && _s.graph.motion > 0 && points.length > 1);
361
+
362
+ if (canAnimate) {
363
+ drawStartShape();
364
+ } else {
365
+ setLineMotion(0);
366
+ }
367
+
368
+ // FINAL SHAPE:
369
+ const data = getPathDataSet(points);
370
+ setPathData(linePath, data.line);
371
+
372
+ if (data.area) setPathData(areaPath, data.area);
373
+ setShown(areaPath, !!data.area);
374
+
375
+ const isLastDotShown = (box.showLastDot == 1 && !!last);
376
+ if (isLastDotShown) placeDot(lastDot, last);
377
+ setShown(lastDot, isLastDotShown);
378
+
379
+ drawMinMaxDots(range.max - range.min);
380
+ drawMarkDots();
381
+
382
+ updateTrend();
383
+ if (hoverIndex >= 0) showHover(hoverIndex);
384
+
385
+ };
386
+
387
+ // Trend badge: last value compared with the first visible value
388
+ const updateTrend = function () {
389
+
390
+ if (box.trendText) {
391
+ setText(box.lblTrend, box.trendText);
392
+ paintTrend(0);
393
+ box.lblTrend.visible = 1;
394
+ return;
395
+ }
396
+
397
+ if (box.showTrend != 1 || box.values.length < 2) {
398
+ box.lblTrend.visible = 0;
399
+ return;
400
+ }
401
+
402
+ const first = box.values[0].value;
403
+ const last = box.values[box.values.length - 1].value;
404
+ const diff = last - first;
405
+ const direction = Math.sign(diff);
406
+ let text;
407
+
408
+ if (first == 0) {
409
+ text = (diff > 0 ? "+" : "") + r1(diff);
410
+ } else {
411
+ const percent = Math.round(Math.abs(diff) / Math.abs(first) * 100);
412
+ text = getTrendArrow(direction) + percent + "%";
413
+ }
414
+
415
+ setText(box.lblTrend, text);
416
+ paintTrend(direction);
417
+ box.lblTrend.visible = 1;
418
+
419
+ };
420
+
421
+ // direction: 1 up, -1 down, 0 flat
422
+ const getTrendArrow = function (direction) {
423
+ if (direction > 0) return "▲ ";
424
+ if (direction < 0) return "▼ ";
425
+ return "";
426
+ };
427
+
428
+ const getTrendColor = function (direction) {
429
+ if (direction > 0) return _s.trend.upColor;
430
+ if (direction < 0) return _s.trend.downColor;
431
+ return _s.trend.flatColor;
432
+ };
433
+
434
+ const paintTrend = function (direction) {
435
+ const color = getTrendColor(direction);
436
+ box.lblTrend.textColor = color;
437
+ box.lblTrend.css.backgroundColor = SparkLineBox.withAlpha(color, _s.trend.backgroundOpacity);
438
+ };
439
+
440
+ // *** HOVER (tooltip):
441
+
442
+ const showHover = function (index) {
443
+
444
+ const p = points[index];
445
+ if (!p) return hideHover();
446
+ hoverIndex = index;
447
+
448
+ hoverLine.setAttribute("x1", p.x);
449
+ hoverLine.setAttribute("x2", p.x);
450
+ hoverLine.setAttribute("y1", 0);
451
+ hoverLine.setAttribute("y2", _s.graph.height);
452
+ setShown(hoverLine, 1);
453
+ placeDot(hoverDot, p);
454
+ hoverDot.setAttribute("fill", p.item.color || box.lineColor);
455
+ setShown(hoverDot, 1);
456
+
457
+ setText(box.lblTooltip, box.valueFormat(p.item.value, p.item));
458
+ box.lblTooltip.visible = 1;
459
+ // Above the point, inside the box
460
+ const tipWidth = box.lblTooltip.elem.offsetWidth;
461
+ const graphTop = box.elem.clientHeight - _s.graph.height;
462
+ let left = p.x - tipWidth / 2;
463
+ left = Math.max(TOOLTIP_EDGE_SPACE, Math.min(left, box.elem.clientWidth - tipWidth - TOOLTIP_EDGE_SPACE));
464
+ box.lblTooltip.left = Math.round(left);
465
+ const top = Math.round(graphTop + p.y - box.lblTooltip.elem.offsetHeight - TOOLTIP_GAP);
466
+ box.lblTooltip.top = Math.max(TOOLTIP_EDGE_SPACE, top);
467
+
468
+ };
469
+
470
+ const hideHover = function () {
471
+ hoverIndex = -1;
472
+ setShown(hoverLine, 0);
473
+ setShown(hoverDot, 0);
474
+ box.lblTooltip.visible = 0;
475
+ };
476
+
477
+ const onMouseMove = function (self, event) {
478
+ if (box.showTooltip != 1 || points.length == 0) return;
479
+ const rect = box.elem.getBoundingClientRect();
480
+ const x = withPageZoom(event.clientX - rect.left) - _s.graph.padding;
481
+ const slots = getSlotCount();
482
+ const step = Math.max(1, (graphWidth - _s.graph.padding * 2) / (slots - 1));
483
+ const index = Math.max(0, Math.min(points.length - 1, Math.round(x / step)));
484
+ if (index !== hoverIndex) showHover(index);
485
+ };
486
+
487
+ // The box size can change: width "100%"
488
+ const refreshSize = function () {
489
+ if (!box) return;
490
+ graphWidth = box.elem.clientWidth;
491
+ svg.setAttribute("viewBox", "0 0 " + Math.max(1, graphWidth) + " " + _s.graph.height);
492
+ draw(0);
493
+ };
494
+
495
+ // *** PUBLIC FUNCTIONS:
496
+
497
+ // Add a value to the right. If the graph is full, the first value is removed.
498
+ box.addValue = function (value, color) {
499
+ const item = normalizeValue(value);
500
+ if (color) item.color = color;
501
+ box.values.push(item);
502
+ trimValues();
503
+ draw(1);
504
+ };
505
+ // USAGE: graph.addValue(24) or graph.addValue(95, "red") (marked value: a red dot)
506
+
507
+ box.setValues = function (values) {
508
+ box.values = (values || []).map(normalizeValue);
509
+ trimValues();
510
+ hideHover();
511
+ draw(0);
512
+ };
513
+ // USAGE: graph.setValues([10, 20, { value: 15, color: "orange" }])
514
+
515
+ box.getValues = function () {
516
+ return box.values.map(function (item) { return item.value; });
517
+ };
518
+
519
+ box.getLastValue = function () {
520
+ const item = box.values[box.values.length - 1];
521
+ return (item) ? item.value : null;
522
+ };
523
+
524
+ box.clear = function () {
525
+ box.values = [];
526
+ hideHover();
527
+ draw(0);
528
+ };
529
+
530
+ box.setTitleText = function (text) {
531
+ box.titleText = text;
532
+ setText(box.lblTitle, text);
533
+ };
534
+
535
+ box.setValueText = function (text) {
536
+ box.valueText = text;
537
+ setText(box.lblValueText, text);
538
+ };
539
+
540
+ box.setIcon = function (iconFile) {
541
+ box.iconFile = iconFile || "";
542
+ box.icon.visible = (box.iconFile) ? 1 : 0;
543
+ if (box.iconFile) box.icon.load(box.iconFile);
544
+ };
545
+ // USAGE: graph.setIcon("assets/carrot.png") or graph.setIcon("") to hide it
546
+
547
+ box.setLineColor = function (color) {
548
+ box.lineColor = color;
549
+ linePath.setAttribute("stroke", color);
550
+ areaPath.setAttribute("fill", color);
551
+ lastDot.setAttribute("fill", color);
552
+ hoverDot.setAttribute("fill", color);
553
+ };
554
+
555
+ // 0: Auto
556
+ box.setMaxValue = function (maxValue) {
557
+ box.maxValue = basic.num(maxValue) || 0;
558
+ draw(0);
559
+ };
560
+
561
+ // null: Auto
562
+ box.setMinValue = function (minValue) {
563
+ const isAuto = (minValue === null || minValue === undefined || minValue === "");
564
+ box.minValue = (isAuto) ? null : basic.num(minValue) || 0;
565
+ draw(0);
566
+ };
567
+
568
+ // "": Calculated from the values
569
+ box.setTrendText = function (text) {
570
+ box.trendText = text || "";
571
+ updateTrend();
572
+ };
573
+
574
+ box.setMaxPoints = function (maxPoints) {
575
+ box.maxPoints = Math.max(2, basic.num(maxPoints) || 2);
576
+ trimValues();
577
+ draw(0);
578
+ };
579
+
580
+ box.refresh = function () {
581
+ refreshSize();
582
+ };
583
+
584
+
585
+ // *** OBJECT VIEW:
586
+
587
+ // BOX: Graph (at the bottom)
588
+ box.graphBox = Box({
589
+ left: 0,
590
+ top: 0,
591
+ width: "100%",
592
+ height: _s.graph.height,
593
+ color: "transparent",
594
+ });
595
+ box.graphBox.css = { top: "auto", bottom: "0px", lineHeight: "0" };
596
+
597
+ // SVG: line, area, dots
598
+ svg = createSvgElement("svg", {
599
+ width: "100%",
600
+ height: _s.graph.height,
601
+ viewBox: "0 0 200 " + _s.graph.height,
602
+ preserveAspectRatio: "none",
603
+ });
604
+ svg.style.display = "block";
605
+ svg.style.overflow = "visible";
606
+ svg.setAttribute("aria-hidden", "true");
607
+
608
+ areaPath = createSvgElement("path", { fill: box.lineColor, "fill-opacity": _s.graph.areaOpacity, stroke: "none" });
609
+ linePath = createSvgElement("path", {
610
+ fill: "none",
611
+ stroke: box.lineColor,
612
+ "stroke-width": _s.graph.lineWidth,
613
+ "stroke-linecap": "round",
614
+ "stroke-linejoin": "round",
615
+ });
616
+ linePath.setAttribute("vector-effect", "non-scaling-stroke");
617
+ hoverLine = createSvgElement("line", { stroke: _s.tooltip.lineColor, "stroke-width": 1, "stroke-dasharray": "2 2" });
618
+ hoverLine.style.display = "none";
619
+ minDot = createSvgElement("circle", { r: _s.graph.minMaxDotRadius, fill: _s.graph.minMaxColor, stroke: _s.graph.dotRingColor, "stroke-width": 1 });
620
+ maxDot = createSvgElement("circle", { r: _s.graph.minMaxDotRadius, fill: _s.graph.minMaxColor, stroke: _s.graph.dotRingColor, "stroke-width": 1 });
621
+ lastDot = createSvgElement("circle", { r: _s.graph.dotRadius, fill: box.lineColor, stroke: _s.graph.dotRingColor, "stroke-width": 1.5 });
622
+ hoverDot = createSvgElement("circle", { r: _s.graph.dotRadius + 1, fill: box.lineColor, stroke: _s.graph.dotRingColor, "stroke-width": 1.5 });
623
+ hoverDot.style.display = "none";
624
+
625
+ [areaPath, linePath, hoverLine, minDot, maxDot, lastDot, hoverDot].forEach(function (element) { svg.appendChild(element); });
626
+ box.graphBox.elem.appendChild(svg);
627
+
628
+ // GROUP: Title and value text (left top)
629
+ VGroup({
630
+ align: "left top",
631
+ padding: _s.text.padding,
632
+ gap: 0,
633
+ });
634
+
635
+ box.lblTitle = Label({
636
+ fontSize: _s.title.fontSize,
637
+ textColor: _s.title.textColor,
638
+ });
639
+ setText(box.lblTitle, box.titleText);
640
+ // WHY: marginBottom: Line heights of the two texts leave a big space between them.
641
+ box.lblTitle.css = { whiteSpace: "nowrap", marginBottom: "-6px" };
642
+
643
+ // GROUP: Value text and trend badge
644
+ HGroup({ width: "auto", height: "auto", align: "left center", gap: 6 });
645
+
646
+ box.lblValueText = Label({
647
+ fontSize: _s.valueText.fontSize,
648
+ textColor: _s.valueText.textColor,
649
+ });
650
+ setText(box.lblValueText, box.valueText);
651
+ box.lblValueText.css.whiteSpace = "nowrap";
652
+
653
+ box.lblTrend = Label({
654
+ text: "",
655
+ fontSize: _s.trend.fontSize,
656
+ textColor: _s.trend.flatColor,
657
+ round: _s.trend.round,
658
+ padding: _s.trend.padding,
659
+ });
660
+ // WHY: marginTop: Lines up with the middle of the big value text.
661
+ box.lblTrend.css = { whiteSpace: "nowrap", lineHeight: "1.5", marginTop: "4px" };
662
+
663
+ endGroup();
664
+
665
+ endGroup();
666
+
667
+ // GROUP: Icon (right top)
668
+ HGroup({
669
+ align: "right top",
670
+ padding: _s.text.padding,
671
+ gap: 0,
672
+ });
673
+
674
+ box.icon = Icon({
675
+ width: _s.icon.width,
676
+ height: _s.icon.height,
677
+ });
678
+ box.icon.elem.alt = "";
679
+
680
+ endGroup();
681
+
682
+ // LABEL: Tooltip (over the graph)
683
+ box.lblTooltip = Label({
684
+ text: "",
685
+ fontSize: _s.tooltip.fontSize,
686
+ textColor: _s.tooltip.textColor,
687
+ color: _s.tooltip.color,
688
+ round: _s.tooltip.round,
689
+ padding: _s.tooltip.padding,
690
+ });
691
+ box.lblTooltip.css = { whiteSpace: "nowrap", lineHeight: "1.4", pointerEvents: "none" };
692
+ box.lblTooltip.visible = 0;
693
+
694
+ // *** OBJECT INIT CODE:
695
+
696
+ box.on("click", function () { box.onClick(box); });
697
+ box.on("mousemove", onMouseMove);
698
+ box.on("mouseleave", function () { hideHover(); });
699
+ box.cursor = "default";
700
+ box.clickable = 1; // WHY: mousemove needs pointer events.
701
+
702
+ box.setIcon(box.iconFile);
703
+
704
+ box.values = (params.values || []).map(normalizeValue); // WHY: Copy. The default [] must not be shared between components.
705
+ trimValues();
706
+ refreshSize();
707
+
708
+ box.onResize(function () {
709
+ if (cancelResize) cancelResize();
710
+ cancelResize = box.after(RESIZE_WAIT, function () {
711
+ cancelResize = null;
712
+ refreshSize();
713
+ });
714
+ });
715
+
716
+ return endObject(box);
717
+
718
+ };
719
+
720
+ // *** STATIC FUNCTIONS:
721
+
722
+ // "#2E9E5B" or "rgb(…)" with an alpha. (For the trend badge background)
723
+ SparkLineBox._alphaCache = {};
724
+ SparkLineBox.withAlpha = function (color, alpha) {
725
+ const key = color + "|" + alpha;
726
+ if (SparkLineBox._alphaCache[key]) return SparkLineBox._alphaCache[key];
727
+ const probe = document.createElement("div");
728
+ probe.style.color = color;
729
+ document.body.appendChild(probe);
730
+ const rgb = getComputedStyle(probe).color; // "rgb(r, g, b)" or "rgba(r, g, b, a)"
731
+ probe.remove();
732
+ const parts = rgb.match(/[\d.]+/g) || [0, 0, 0];
733
+ SparkLineBox._alphaCache[key] = "rgba(" + parts[0] + ", " + parts[1] + ", " + parts[2] + ", " + alpha + ")";
734
+ return SparkLineBox._alphaCache[key];
735
+ };
736
+
737
+ // Chrome and Edge can animate the path with the CSS "d" property.
738
+ SparkLineBox.supportsCssPath = (typeof CSS !== "undefined" && CSS.supports && CSS.supports("d", 'path("M 0 0 L 1 1")'));