x4js 1.5.18 → 1.5.20

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 (242) hide show
  1. package/lib/{README.md → README.txt} +0 -0
  2. package/lib/cjs/index.js +39 -103
  3. package/lib/cjs/index.js.map +7 -0
  4. package/lib/esm/index.mjs +39 -87
  5. package/lib/esm/index.mjs.map +7 -0
  6. package/lib/{license.md → licence.md} +0 -0
  7. package/lib/src/MIT-license.md +14 -0
  8. package/lib/{types/action.d.ts → src/action.ts} +64 -33
  9. package/lib/src/alpha.jpg +0 -0
  10. package/lib/src/app_sockets.ts +81 -0
  11. package/lib/src/application.ts +251 -0
  12. package/lib/src/autocomplete.ts +197 -0
  13. package/lib/src/base64.ts +166 -0
  14. package/lib/src/base_component.ts +152 -0
  15. package/lib/src/button.ts +355 -0
  16. package/lib/src/calendar.ts +328 -0
  17. package/lib/src/canvas.ts +505 -0
  18. package/lib/src/cardview.ts +227 -0
  19. package/lib/src/checkbox.ts +188 -0
  20. package/lib/src/color.ts +752 -0
  21. package/lib/src/colorpicker.ts +1639 -0
  22. package/lib/src/combobox.ts +465 -0
  23. package/lib/src/component.ts +2329 -0
  24. package/lib/{esm/dom_events.js → src/copyright.txt} +27 -29
  25. package/lib/src/datastore.ts +1322 -0
  26. package/lib/src/dialog.ts +656 -0
  27. package/lib/src/dom_events.ts +315 -0
  28. package/lib/src/drag_manager.ts +199 -0
  29. package/lib/src/drawtext.ts +355 -0
  30. package/lib/src/fileupload.ts +213 -0
  31. package/lib/src/form.ts +375 -0
  32. package/lib/src/formatters.ts +105 -0
  33. package/lib/src/gridview.ts +1185 -0
  34. package/lib/src/i18n.ts +346 -0
  35. package/lib/src/icon.ts +335 -0
  36. package/lib/src/image.ts +204 -0
  37. package/lib/src/index.ts +89 -0
  38. package/lib/src/input.ts +249 -0
  39. package/lib/src/label.ts +128 -0
  40. package/lib/src/layout.ts +430 -0
  41. package/lib/src/link.ts +86 -0
  42. package/lib/src/listview.ts +765 -0
  43. package/lib/{esm/md5.js → src/md5.ts} +438 -403
  44. package/lib/src/menu.ts +425 -0
  45. package/lib/src/messagebox.ts +224 -0
  46. package/lib/src/panel.ts +86 -0
  47. package/lib/src/popup.ts +494 -0
  48. package/lib/src/property_editor.ts +337 -0
  49. package/lib/src/radiobtn.ts +197 -0
  50. package/lib/src/rating.ts +135 -0
  51. package/lib/src/request.ts +300 -0
  52. package/lib/src/router.ts +185 -0
  53. package/lib/src/settings.ts +77 -0
  54. package/lib/src/sidebarview.ts +103 -0
  55. package/lib/src/spreadsheet.ts +1449 -0
  56. package/lib/src/styles.ts +343 -0
  57. package/lib/src/svgcomponent.ts +577 -0
  58. package/lib/src/tabbar.ts +151 -0
  59. package/lib/src/tabview.ts +110 -0
  60. package/lib/src/textarea.ts +235 -0
  61. package/lib/src/textedit.ts +544 -0
  62. package/lib/src/toaster.ts +80 -0
  63. package/lib/src/tools.ts +1473 -0
  64. package/lib/src/tooltips.ts +191 -0
  65. package/lib/src/treeview.ts +716 -0
  66. package/lib/{esm/version.js → src/version.ts} +30 -29
  67. package/lib/{styles → src}/x4.less +0 -0
  68. package/lib/src/x4dom.ts +57 -0
  69. package/lib/src/x4events.ts +585 -0
  70. package/lib/src/x4react.ts +90 -0
  71. package/package.json +52 -46
  72. package/lib/cjs/action.js +0 -58
  73. package/lib/cjs/app_sockets.js +0 -74
  74. package/lib/cjs/application.js +0 -182
  75. package/lib/cjs/autocomplete.js +0 -155
  76. package/lib/cjs/base_component.js +0 -124
  77. package/lib/cjs/button.js +0 -263
  78. package/lib/cjs/calendar.js +0 -254
  79. package/lib/cjs/canvas.js +0 -361
  80. package/lib/cjs/cardview.js +0 -161
  81. package/lib/cjs/checkbox.js +0 -139
  82. package/lib/cjs/color.js +0 -591
  83. package/lib/cjs/colorpicker.js +0 -1468
  84. package/lib/cjs/combobox.js +0 -349
  85. package/lib/cjs/component.js +0 -1719
  86. package/lib/cjs/datastore.js +0 -985
  87. package/lib/cjs/dialog.js +0 -478
  88. package/lib/cjs/dom_events.js +0 -30
  89. package/lib/cjs/drag_manager.js +0 -145
  90. package/lib/cjs/drawtext.js +0 -276
  91. package/lib/cjs/fileupload.js +0 -167
  92. package/lib/cjs/form.js +0 -304
  93. package/lib/cjs/formatters.js +0 -90
  94. package/lib/cjs/gridview.js +0 -881
  95. package/lib/cjs/i18n.js +0 -266
  96. package/lib/cjs/icon.js +0 -272
  97. package/lib/cjs/image.js +0 -157
  98. package/lib/cjs/input.js +0 -174
  99. package/lib/cjs/label.js +0 -95
  100. package/lib/cjs/layout.js +0 -322
  101. package/lib/cjs/link.js +0 -64
  102. package/lib/cjs/listview.js +0 -546
  103. package/lib/cjs/md5.js +0 -403
  104. package/lib/cjs/menu.js +0 -301
  105. package/lib/cjs/messagebox.js +0 -177
  106. package/lib/cjs/panel.js +0 -68
  107. package/lib/cjs/popup.js +0 -383
  108. package/lib/cjs/property_editor.js +0 -252
  109. package/lib/cjs/radiobtn.js +0 -140
  110. package/lib/cjs/rating.js +0 -102
  111. package/lib/cjs/request.js +0 -246
  112. package/lib/cjs/router.js +0 -139
  113. package/lib/cjs/settings.js +0 -69
  114. package/lib/cjs/sidebarview.js +0 -82
  115. package/lib/cjs/spreadsheet.js +0 -1084
  116. package/lib/cjs/styles.js +0 -275
  117. package/lib/cjs/svgcomponent.js +0 -447
  118. package/lib/cjs/tabbar.js +0 -111
  119. package/lib/cjs/tabview.js +0 -85
  120. package/lib/cjs/textarea.js +0 -165
  121. package/lib/cjs/textedit.js +0 -415
  122. package/lib/cjs/toaster.js +0 -64
  123. package/lib/cjs/tools.js +0 -1197
  124. package/lib/cjs/tooltips.js +0 -157
  125. package/lib/cjs/treeview.js +0 -535
  126. package/lib/cjs/version.js +0 -32
  127. package/lib/cjs/x4dom.js +0 -32
  128. package/lib/cjs/x4events.js +0 -384
  129. package/lib/cjs/x4react.js +0 -72
  130. package/lib/esm/action.js +0 -53
  131. package/lib/esm/app_sockets.js +0 -70
  132. package/lib/esm/application.js +0 -184
  133. package/lib/esm/autocomplete.js +0 -155
  134. package/lib/esm/base_component.js +0 -107
  135. package/lib/esm/button.js +0 -257
  136. package/lib/esm/calendar.js +0 -249
  137. package/lib/esm/canvas.js +0 -358
  138. package/lib/esm/cardview.js +0 -159
  139. package/lib/esm/checkbox.js +0 -134
  140. package/lib/esm/color.js +0 -588
  141. package/lib/esm/colorpicker.js +0 -1475
  142. package/lib/esm/combobox.js +0 -351
  143. package/lib/esm/component.js +0 -1711
  144. package/lib/esm/datastore.js +0 -990
  145. package/lib/esm/dialog.js +0 -490
  146. package/lib/esm/drag_manager.js +0 -147
  147. package/lib/esm/drawtext.js +0 -271
  148. package/lib/esm/fileupload.js +0 -163
  149. package/lib/esm/form.js +0 -304
  150. package/lib/esm/formatters.js +0 -81
  151. package/lib/esm/gridview.js +0 -889
  152. package/lib/esm/i18n.js +0 -257
  153. package/lib/esm/icon.js +0 -261
  154. package/lib/esm/image.js +0 -154
  155. package/lib/esm/input.js +0 -182
  156. package/lib/esm/label.js +0 -90
  157. package/lib/esm/layout.js +0 -310
  158. package/lib/esm/link.js +0 -59
  159. package/lib/esm/listview.js +0 -534
  160. package/lib/esm/menu.js +0 -300
  161. package/lib/esm/messagebox.js +0 -161
  162. package/lib/esm/panel.js +0 -65
  163. package/lib/esm/popup.js +0 -379
  164. package/lib/esm/property_editor.js +0 -251
  165. package/lib/esm/radiobtn.js +0 -136
  166. package/lib/esm/rating.js +0 -97
  167. package/lib/esm/request.js +0 -224
  168. package/lib/esm/router.js +0 -136
  169. package/lib/esm/settings.js +0 -66
  170. package/lib/esm/sidebarview.js +0 -79
  171. package/lib/esm/spreadsheet.js +0 -1097
  172. package/lib/esm/styles.js +0 -270
  173. package/lib/esm/svgcomponent.js +0 -450
  174. package/lib/esm/tabbar.js +0 -107
  175. package/lib/esm/tabview.js +0 -83
  176. package/lib/esm/textarea.js +0 -160
  177. package/lib/esm/textedit.js +0 -412
  178. package/lib/esm/toaster.js +0 -62
  179. package/lib/esm/tools.js +0 -1158
  180. package/lib/esm/tooltips.js +0 -153
  181. package/lib/esm/treeview.js +0 -527
  182. package/lib/esm/x4dom.js +0 -29
  183. package/lib/esm/x4events.js +0 -376
  184. package/lib/esm/x4react.js +0 -71
  185. package/lib/types/app_sockets.d.ts +0 -29
  186. package/lib/types/application.d.ts +0 -104
  187. package/lib/types/autocomplete.d.ts +0 -58
  188. package/lib/types/base_component.d.ts +0 -88
  189. package/lib/types/button.d.ts +0 -151
  190. package/lib/types/calendar.d.ts +0 -82
  191. package/lib/types/canvas.d.ts +0 -92
  192. package/lib/types/cardview.d.ts +0 -87
  193. package/lib/types/checkbox.d.ts +0 -77
  194. package/lib/types/color.d.ts +0 -148
  195. package/lib/types/colorpicker.d.ts +0 -107
  196. package/lib/types/combobox.d.ts +0 -107
  197. package/lib/types/component.d.ts +0 -601
  198. package/lib/types/datastore.d.ts +0 -396
  199. package/lib/types/dialog.d.ts +0 -175
  200. package/lib/types/dom_events.d.ts +0 -302
  201. package/lib/types/drag_manager.d.ts +0 -54
  202. package/lib/types/drawtext.d.ts +0 -48
  203. package/lib/types/fileupload.d.ts +0 -64
  204. package/lib/types/form.d.ts +0 -133
  205. package/lib/types/formatters.d.ts +0 -35
  206. package/lib/types/gridview.d.ts +0 -175
  207. package/lib/types/i18n.d.ts +0 -73
  208. package/lib/types/icon.d.ts +0 -64
  209. package/lib/types/image.d.ts +0 -55
  210. package/lib/types/index.d.ts +0 -84
  211. package/lib/types/input.d.ts +0 -91
  212. package/lib/types/label.d.ts +0 -58
  213. package/lib/types/layout.d.ts +0 -87
  214. package/lib/types/link.d.ts +0 -50
  215. package/lib/types/listview.d.ts +0 -178
  216. package/lib/types/md5.d.ts +0 -61
  217. package/lib/types/menu.d.ts +0 -130
  218. package/lib/types/messagebox.d.ts +0 -69
  219. package/lib/types/panel.d.ts +0 -47
  220. package/lib/types/popup.d.ts +0 -75
  221. package/lib/types/property_editor.d.ts +0 -71
  222. package/lib/types/radiobtn.d.ts +0 -73
  223. package/lib/types/rating.d.ts +0 -53
  224. package/lib/types/request.d.ts +0 -52
  225. package/lib/types/router.d.ts +0 -42
  226. package/lib/types/settings.d.ts +0 -37
  227. package/lib/types/sidebarview.d.ts +0 -49
  228. package/lib/types/spreadsheet.d.ts +0 -222
  229. package/lib/types/styles.d.ts +0 -85
  230. package/lib/types/svgcomponent.d.ts +0 -215
  231. package/lib/types/tabbar.d.ts +0 -58
  232. package/lib/types/tabview.d.ts +0 -49
  233. package/lib/types/textarea.d.ts +0 -77
  234. package/lib/types/textedit.d.ts +0 -123
  235. package/lib/types/toaster.d.ts +0 -42
  236. package/lib/types/tools.d.ts +0 -394
  237. package/lib/types/tooltips.d.ts +0 -46
  238. package/lib/types/treeview.d.ts +0 -144
  239. package/lib/types/version.d.ts +0 -29
  240. package/lib/types/x4dom.d.ts +0 -49
  241. package/lib/types/x4events.d.ts +0 -269
  242. package/lib/types/x4react.d.ts +0 -41
@@ -0,0 +1,505 @@
1
+ /**
2
+ * ___ ___ __
3
+ * \ \/ / / _
4
+ * \ / /_| |_
5
+ * / \____ _|
6
+ * /__/\__\ |_|
7
+ *
8
+ * @file canvas.ts
9
+ * @author Etienne Cochard
10
+ *
11
+ * Copyright (c) 2019-2022 R-libre ingenierie
12
+ *
13
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
14
+ * of this software and associated documentation files (the "Software"), to deal
15
+ * in the Software without restriction, including without limitation the rights
16
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
17
+ * of the Software, and to permit persons to whom the Software is furnished to do so,
18
+ * subject to the following conditions:
19
+ * The above copyright notice and this permission notice shall be included in all copies
20
+ * or substantial portions of the Software.
21
+ *
22
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
23
+ * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
24
+ * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
+ **/
29
+
30
+ import { Component, CProps, CEventMap, html } from './component'
31
+ import { BasicEvent, EventCallback } from './x4events'
32
+
33
+ interface EvPaint extends BasicEvent {
34
+ ctx: CanvasPainter;
35
+ }
36
+
37
+ function EvPaint(ctx: CanvasPainter) {
38
+ return BasicEvent<EvPaint>({ ctx });
39
+ }
40
+
41
+ interface CanvasEventMap extends CEventMap {
42
+ paint: EvPaint;
43
+ }
44
+
45
+ interface CanvasProps extends CProps<CanvasEventMap> {
46
+ autoClear?: boolean;
47
+ painter?: PaintHandler;
48
+ paint: EventCallback<EvPaint>
49
+ }
50
+
51
+
52
+ // ============================================================================
53
+ // [CANVAS]
54
+ // ============================================================================
55
+
56
+
57
+
58
+
59
+ export interface CanvasPainter extends CanvasRenderingContext2D {
60
+ width: number;
61
+ height: number;
62
+
63
+ smoothLine(points: any[], path: CanvasPath, move: boolean): void;
64
+ smoothLineEx(_points: any[], tension: number, numOfSeg: number, path: CanvasPath, move?: boolean, close?: boolean): void;
65
+ line(x1: number, y1: number, x2: number, y2: number, color: string, lineWidth: number): void;
66
+ roundRect(x: number, y: number, width: number, height: number, radius: number): void;
67
+ calcTextSize(text: string, rounded: boolean): { width: number, height: number };
68
+ setFontSize(fs: number): void;
69
+ circle(x: number, y: number, radius: number): void;
70
+ }
71
+
72
+ function mkPainter(c2d: CanvasRenderingContext2D, w: number, h: number): CanvasPainter {
73
+
74
+ let cp = c2d as CanvasPainter;
75
+
76
+ cp.width = w;
77
+ cp.height = h;
78
+
79
+ cp.smoothLine = smoothLine;
80
+ cp.smoothLineEx = smoothLineEx;
81
+ cp.line = line;
82
+ cp.roundRect = roundRect;
83
+ cp.calcTextSize = calcTextSize;
84
+ cp.setFontSize = setFontSize;
85
+ cp.circle = circle;
86
+
87
+ return cp;
88
+ }
89
+
90
+ function smoothLine( this: CanvasRenderingContext2D, points: any[], path: CanvasPath = null, move = true) {
91
+ if (points.length < 2) {
92
+ return;
93
+ }
94
+
95
+ if (!path) {
96
+ path = this;
97
+ }
98
+
99
+ if (points.length == 2) {
100
+ if (move !== false) {
101
+ path.moveTo(points[0].x, points[0].y);
102
+ }
103
+ else {
104
+ path.lineTo(points[0].x, points[0].y);
105
+ }
106
+
107
+ path.lineTo(points[1].x, points[1].y);
108
+ return;
109
+ }
110
+
111
+ function midPointBtw(p1, p2) {
112
+ return {
113
+ x: p1.x + (p2.x - p1.x) / 2,
114
+ y: p1.y + (p2.y - p1.y) / 2
115
+ };
116
+ }
117
+
118
+ function getQuadraticXY(t, sx, sy, cp1x, cp1y, ex, ey) {
119
+ return {
120
+ x: (1 - t) * (1 - t) * sx + 2 * (1 - t) * t * cp1x + t * t * ex,
121
+ y: (1 - t) * (1 - t) * sy + 2 * (1 - t) * t * cp1y + t * t * ey
122
+ };
123
+ }
124
+
125
+ let p1 = points[0],
126
+ p2 = points[1],
127
+ p3 = p1;
128
+
129
+ path.moveTo(p1.x, p1.y);
130
+
131
+ for (let i = 1, len = points.length; i < len; i++) {
132
+ // we pick the point between pi+1 & pi+2 as the
133
+ // end point and p1 as our control point
134
+ let midPoint = midPointBtw(p1, p2);
135
+ //this.quadraticCurveTo(p1.x, p1.y, midPoint.x, midPoint.y);
136
+ for (let i = 0; i < 8; i++) {
137
+ let { x, y } = getQuadraticXY(i / 8, p3.x, p3.y, p1.x, p1.y, midPoint.x, midPoint.y);
138
+ path.lineTo(x, y);
139
+ }
140
+
141
+ p1 = points[i];
142
+ p2 = points[i + 1];
143
+ p3 = midPoint;
144
+ }
145
+
146
+ // Draw last line as a straight line while
147
+ // we wait for the next point to be able to calculate
148
+ // the bezier control point
149
+
150
+ path.lineTo(p1.x, p1.y);
151
+ }
152
+
153
+ function smoothLineEx(this: CanvasRenderingContext2D, _points: any[], tension: number = 0.5, numOfSeg: number = 10, path: CanvasPath = null, move = true, close = false) {
154
+
155
+ let points = [];
156
+
157
+ //pts = points.slice(0);
158
+ for (let p = 0, pc = _points.length; p < pc; p++) {
159
+ points.push(_points[p].x);
160
+ points.push(_points[p].y);
161
+ }
162
+
163
+ let pts,
164
+ i = 1,
165
+ l = points.length,
166
+ rPos = 0,
167
+ rLen = (l - 2) * numOfSeg + 2 + (close ? 2 * numOfSeg : 0),
168
+ res = new Float32Array(rLen),
169
+ cache = new Float32Array((numOfSeg + 2) * 4),
170
+ cachePtr = 4;
171
+
172
+ pts = points.slice(0);
173
+
174
+ if (close) {
175
+ pts.unshift(points[l - 1]); // insert end point as first point
176
+ pts.unshift(points[l - 2]);
177
+ pts.push(points[0], points[1]); // first point as last point
178
+ }
179
+ else {
180
+ pts.unshift(points[1]); // copy 1. point and insert at beginning
181
+ pts.unshift(points[0]);
182
+ pts.push(points[l - 2], points[l - 1]); // duplicate end-points
183
+ }
184
+
185
+ // cache inner-loop calculations as they are based on t alone
186
+ cache[0] = 1; // 1,0,0,0
187
+
188
+ for (; i < numOfSeg; i++) {
189
+
190
+ var st = i / numOfSeg,
191
+ st2 = st * st,
192
+ st3 = st2 * st,
193
+ st23 = st3 * 2,
194
+ st32 = st2 * 3;
195
+
196
+ cache[cachePtr++] = st23 - st32 + 1; // c1
197
+ cache[cachePtr++] = st32 - st23; // c2
198
+ cache[cachePtr++] = st3 - 2 * st2 + st; // c3
199
+ cache[cachePtr++] = st3 - st2; // c4
200
+ }
201
+
202
+ cache[cachePtr] = 1; // 0,1,0,0
203
+
204
+ // calc. points
205
+ parse(pts, cache, l);
206
+
207
+ if (close) {
208
+ //l = points.length;
209
+ pts = [];
210
+ pts.push(points[l - 4], points[l - 3], points[l - 2], points[l - 1]); // second last and last
211
+ pts.push(points[0], points[1], points[2], points[3]); // first and second
212
+ parse(pts, cache, 4);
213
+ }
214
+
215
+ function parse(pts, cache, l) {
216
+
217
+ for (var i = 2, t; i < l; i += 2) {
218
+
219
+ var pt1 = pts[i],
220
+ pt2 = pts[i + 1],
221
+ pt3 = pts[i + 2],
222
+ pt4 = pts[i + 3],
223
+
224
+ t1x = (pt3 - pts[i - 2]) * tension,
225
+ t1y = (pt4 - pts[i - 1]) * tension,
226
+ t2x = (pts[i + 4] - pt1) * tension,
227
+ t2y = (pts[i + 5] - pt2) * tension;
228
+
229
+ for (t = 0; t < numOfSeg; t++) {
230
+
231
+ var c = t << 2, //t * 4;
232
+
233
+ c1 = cache[c],
234
+ c2 = cache[c + 1],
235
+ c3 = cache[c + 2],
236
+ c4 = cache[c + 3];
237
+
238
+ res[rPos++] = c1 * pt1 + c2 * pt3 + c3 * t1x + c4 * t2x;
239
+ res[rPos++] = c1 * pt2 + c2 * pt4 + c3 * t1y + c4 * t2y;
240
+ }
241
+ }
242
+ }
243
+
244
+ // add last point
245
+ l = close ? 0 : points.length - 2;
246
+ res[rPos++] = points[l];
247
+ res[rPos] = points[l + 1];
248
+
249
+ if (!path) {
250
+ path = this;
251
+ }
252
+
253
+ // add lines to path
254
+ for (let i = 0, l = res.length; i < l; i += 2) {
255
+ if (i == 0 && move !== false) {
256
+ path.moveTo(res[i], res[i + 1]);
257
+ }
258
+ else {
259
+ path.lineTo(res[i], res[i + 1]);
260
+ }
261
+ }
262
+ }
263
+
264
+ function line(this: CanvasRenderingContext2D, x1: number, y1: number, x2: number, y2: number, color: string, lineWidth: number = 1) {
265
+ this.save();
266
+ this.beginPath();
267
+ this.moveTo(x1, y1);
268
+ this.lineTo(x2, y2);
269
+ this.lineWidth = lineWidth;
270
+ this.strokeStyle = color;
271
+ this.stroke();
272
+ this.restore();
273
+ }
274
+
275
+ function roundRect(this: CanvasRenderingContext2D, x: number, y: number, width: number, height: number, radius: number) {
276
+ //this.beginPath( );
277
+ this.moveTo(x + radius, y);
278
+ this.lineTo(x + width - radius, y);
279
+ this.quadraticCurveTo(x + width, y, x + width, y + radius);
280
+ this.lineTo(x + width, y + height - radius);
281
+ this.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
282
+ this.lineTo(x + radius, y + height);
283
+ this.quadraticCurveTo(x, y + height, x, y + height - radius);
284
+ this.lineTo(x, y + radius);
285
+ this.quadraticCurveTo(x, y, x + radius, y);
286
+ this.closePath();
287
+ }
288
+
289
+ function calcTextSize( this: CanvasRenderingContext2D, text: string, rounded = false): { width: number, height: number } {
290
+
291
+ let fh = this.measureText(text);
292
+ let lh = fh.fontBoundingBoxAscent + fh.fontBoundingBoxDescent;
293
+
294
+ if (rounded) {
295
+ return { width: Math.round(fh.width), height: Math.round(lh) };
296
+ }
297
+ else {
298
+ return { width: fh.width, height: lh };
299
+ }
300
+ }
301
+
302
+ function setFontSize( this: CanvasRenderingContext2D, fs: number) {
303
+ let fsize = Math.round(fs) + 'px';
304
+ this.font = this.font.replace(/\d+px/, fsize);
305
+ }
306
+
307
+ function circle( this: CanvasRenderingContext2D, x: number, y: number, radius: number) {
308
+ this.moveTo(x + radius, y);
309
+ this.arc(x, y, radius, 0, Math.PI * 2);
310
+ }
311
+
312
+ type PaintHandler = (ctx: CanvasPainter) => any;
313
+
314
+ /**
315
+ *
316
+ */
317
+
318
+ //export class CanvasProps extends CProps
319
+ //{
320
+ // low level handlers
321
+ //mousedown?: (ev: MouseEvent) => any;
322
+ //mousemove?: (ev: MouseEvent) => any;
323
+ //mouseup?: (ev: MouseEvent) => any;
324
+ //mouseleave?: (ev: MouseEvent) => any;
325
+ //mousewheel?: (ev: WheelEvent) => any;
326
+ //click?: (ev: MouseEvent) => any;
327
+ //dblclick?: (ev: MouseEvent) => any;
328
+ //touchstart?: (ev: TouchEvent) => any;
329
+ //touchmove?: (ev: TouchEvent) => any;
330
+ //touchend?: (ev: TouchEvent) => any;
331
+ //keydown?: (ev: KeyboardEvent) => any;
332
+ //keyup?: (ev: KeyboardEvent) => any;
333
+ //}
334
+
335
+
336
+ /**
337
+ * Standard Canvas
338
+ */
339
+
340
+ export class Canvas extends Component<CanvasProps, CanvasEventMap> {
341
+
342
+ private m_iwidth: number = -1;
343
+ private m_iheight: number = -1;
344
+ private m_scale = 1.0;
345
+ private m_canvas: Component;
346
+
347
+ constructor(props: CanvasProps) {
348
+ super(props);
349
+
350
+ //if( props.mousedown ) { this.setDomEvent( 'mousedown', props.mousedown ); }
351
+ //if( props.mousemove ) { this.setDomEvent( 'mousemove', props.mousemove ); }
352
+ //if( props.mouseup ) { this.setDomEvent( 'mouseup', props.mouseup ); }
353
+ //if( props.mousewheel ) { this.setDomEvent( 'wheel', props.mousewheel ); }
354
+ //if( props.mouseleave ) { this.setDomEvent( 'mouseleave', props.mouseleave ); }
355
+ //if( props.click ) { this.setDomEvent( 'click', props.click ); }
356
+ //if( props.dblclick ) { this.setDomEvent( 'dblclick', props.dblclick ); }
357
+ //if( props.touchstart ) { this.setDomEvent( 'touchstart', props.touchstart ); }
358
+ //if( props.touchmove ) { this.setDomEvent( 'touchmove', props.touchmove ); }
359
+ //if( props.touchend ) { this.setDomEvent( 'touchend', props.touchend ); }
360
+ //if( props.keydown ) { this.setDomEvent( 'keydown', props.keydown ); this.setAttribute( 'tabindex', 0 ); }
361
+ //if( props.keyup ) { this.setDomEvent( 'keyup', props.keyup ); this.setAttribute( 'tabindex', 0 ); }
362
+ //if( props.paint ) { this.onPaint( props.paint ); }
363
+
364
+ this.setDomEvent('sizechange', () => { this._paint(); })
365
+ this.mapPropEvents( props, 'paint' );
366
+ }
367
+
368
+ /** @ignore */
369
+ render() {
370
+ this.m_iwidth = -1;
371
+ this.m_iheight = -1;
372
+ this.m_canvas = new Component({
373
+ tag: 'canvas'
374
+ });
375
+
376
+ this.setContent( this.m_canvas );
377
+ // this.redraw(10);
378
+ }
379
+
380
+ update(delay=0 ) {
381
+ this.m_iheight = this.m_iwidth = -1;
382
+ super.update( delay );
383
+ }
384
+
385
+ /**
386
+ * scale the whole canvas
387
+ */
388
+
389
+ scale(scale: number) {
390
+ this.m_scale = scale;
391
+ this.m_iwidth = -1; // force recalc
392
+ this.redraw();
393
+ }
394
+
395
+ /**
396
+ * return the internal canvas
397
+ */
398
+ get canvas(): Component {
399
+ return this.m_canvas;
400
+ }
401
+
402
+ /**
403
+ * redraw the canvas (force a paint)
404
+ */
405
+
406
+ private $update_rep = 0;
407
+ public redraw(wait?: number) {
408
+
409
+ if (wait !== undefined) {
410
+
411
+ if( ++this.$update_rep>=20 ) {
412
+ this.stopTimer( 'update' );
413
+ this._paint( );
414
+ }
415
+ else {
416
+ this.startTimer( 'update', wait, false, () => this._paint() );
417
+ }
418
+ }
419
+ else {
420
+ this.stopTimer( 'update' );
421
+ this._paint();
422
+ }
423
+ }
424
+
425
+ /**
426
+ *
427
+ */
428
+ private _paint() {
429
+ this.$update_rep = 0;
430
+
431
+ let dom = this.dom;
432
+ if (!this.isUserVisible() ) {
433
+ return;
434
+ }
435
+
436
+ let canvas = this.m_canvas.dom as HTMLCanvasElement,
437
+ w = dom.clientWidth,
438
+ h = dom.clientHeight;
439
+
440
+ let ctx = <CanvasPainter>canvas.getContext('2d');
441
+ if (w != this.m_iwidth || h != this.m_iheight) {
442
+ // adjustment for HDPI
443
+ let devicePixelRatio = window.devicePixelRatio || 1;
444
+ let backingStoreRatio = (<any>ctx).webkitBackingStorePixelRatio ||
445
+ (<any>ctx).mozBackingStorePixelRatio ||
446
+ (<any>ctx).msBackingStorePixelRatio ||
447
+ (<any>ctx).oBackingStorePixelRatio ||
448
+ (<any>ctx).backingStorePixelRatio || 1;
449
+
450
+ let canvas = this.canvas;
451
+
452
+ if (devicePixelRatio !== backingStoreRatio || this.m_scale != 1.0) {
453
+ let ratio = devicePixelRatio / backingStoreRatio,
454
+ rw = w * ratio,
455
+ rh = h * ratio;
456
+
457
+ canvas.setAttribute('width', '' + rw);
458
+ canvas.setAttribute('height', '' + rh);
459
+ canvas.setStyleValue('width', w);
460
+ canvas.setStyleValue('height', h);
461
+
462
+ ratio *= this.m_scale;
463
+ ctx.scale(ratio, ratio);
464
+ }
465
+ else {
466
+ canvas.setAttribute('width', '' + w);
467
+ canvas.setAttribute('height', '' + h);
468
+ canvas.setStyleValue('width', w);
469
+ canvas.setStyleValue('height', h);
470
+ ctx.scale(1, 1);
471
+ }
472
+
473
+ this.m_iwidth = w;
474
+ this.m_iheight = h;
475
+ }
476
+
477
+ if (w && h) {
478
+
479
+ let cc = mkPainter(ctx, w, h);
480
+
481
+ if (this.m_props.autoClear) {
482
+ cc.clearRect(0, 0, w, h);
483
+ }
484
+
485
+ cc.save();
486
+ cc.translate(-0.5, -0.5);
487
+ this.paint(cc);
488
+ cc.restore();
489
+ }
490
+ }
491
+
492
+ protected paint(ctx: CanvasPainter) {
493
+ try {
494
+ if (this.m_props.painter) {
495
+ this.m_props.painter(ctx);
496
+ }
497
+ else {
498
+ this.emit('paint', EvPaint(ctx));
499
+ }
500
+ }
501
+ catch (x) {
502
+ console.assert(false, x);
503
+ }
504
+ }
505
+ }
@@ -0,0 +1,227 @@
1
+ /**
2
+ * ___ ___ __
3
+ * \ \/ / / _
4
+ * \ / /_| |_
5
+ * / \____ _|
6
+ * /__/\__\ |_|
7
+ *
8
+ * @file cardview.ts
9
+ * @author Etienne Cochard
10
+ *
11
+ * Copyright (c) 2019-2022 R-libre ingenierie
12
+ *
13
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
14
+ * of this software and associated documentation files (the "Software"), to deal
15
+ * in the Software without restriction, including without limitation the rights
16
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
17
+ * of the Software, and to permit persons to whom the Software is furnished to do so,
18
+ * subject to the following conditions:
19
+ * The above copyright notice and this permission notice shall be included in all copies
20
+ * or substantial portions of the Software.
21
+ *
22
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
23
+ * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
24
+ * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
+ **/
29
+
30
+ import { Component, CProps, CEventMap } from './component'
31
+ import { EvChange } from './x4events'
32
+
33
+ import { IconID } from './icon'
34
+ import { asap, isFunction } from './tools'
35
+
36
+ export type PageOrCallback = Component | (() => Component);
37
+
38
+ export interface ICardViewItem {
39
+ icon?: IconID;
40
+ name: string,
41
+ title: string;
42
+ page: PageOrCallback; // pre builded page
43
+ }
44
+
45
+
46
+ interface CardViewEventMap extends CEventMap {
47
+ change?: EvChange; // value is page name
48
+ }
49
+
50
+ export interface CardViewProps extends CProps<CardViewEventMap> {
51
+ pages: ICardViewItem[];
52
+ active?: string;
53
+ }
54
+
55
+ interface ICardItemEx extends ICardViewItem {
56
+ selector: Component;
57
+ active: boolean;
58
+ }
59
+
60
+ /**
61
+ * Standard CardView class
62
+ * a card view is composed of multiples pages with only one visible at a time.
63
+ * pages can be selected by a component (like tabs ou sidebar).
64
+ * or by code.
65
+ */
66
+
67
+ export class CardView<P extends CardViewProps = CardViewProps, E extends CardViewEventMap = CardViewEventMap> extends Component<P, E>
68
+ {
69
+ protected m_cards: ICardItemEx[];
70
+ protected m_ipage: string; // initialy selected page
71
+ protected m_cpage: ICardItemEx; // currently selected page
72
+
73
+ constructor(props: P) {
74
+ super(props);
75
+
76
+ this.m_cards = [];
77
+ this.m_ipage = props.active;
78
+ this.m_cpage = null;
79
+
80
+ this.singleShot(() => {
81
+ this.setPages(props.pages);
82
+ });
83
+ }
84
+
85
+ /** @ignore */
86
+ render() {
87
+
88
+ let pages = [];
89
+ this.m_cards.forEach((p) => {
90
+ if (p.page) {
91
+ pages.push(p.page);
92
+ }
93
+ });
94
+
95
+ this.setContent(pages);
96
+ }
97
+
98
+ /**
99
+ * switch to a specific card
100
+ * @param name - card name as define in constructor
101
+ */
102
+
103
+ public switchTo(name?: string) {
104
+
105
+ if (this.m_cards.length == 0) {
106
+ return;
107
+ }
108
+
109
+ if (name === undefined) {
110
+ name = this.m_cards[0].name;
111
+ }
112
+
113
+ if (name === this.m_cpage?.name) {
114
+ return;
115
+ }
116
+
117
+ // hide old one
118
+ if (this.m_cpage) {
119
+ if (this.m_cpage.selector) {
120
+ this.m_cpage.selector.removeClass('@active');
121
+ }
122
+
123
+ if (this.m_cpage.page && !(this.m_cpage.page instanceof Function)) {
124
+ let page = <Component>this.m_cpage.page;
125
+ page.removeClass('@active');
126
+ page.addClass('@hidden');
127
+ }
128
+ }
129
+
130
+ this.m_cpage = this.m_cards.find((card) => card.name == name);
131
+
132
+ if (this.m_cpage) {
133
+ if (this.m_cpage.page) {
134
+ if (isFunction(this.m_cpage.page)) {
135
+ this.m_cpage.page = this.m_cpage.page();
136
+ console.assert(this.m_cpage.page != null, 'You must return a valid component');
137
+ }
138
+
139
+ let page = this.m_cpage.page as Component;
140
+ page.addClass('@active');
141
+ page.removeClass('@hidden');
142
+
143
+ if (!page.dom) {
144
+ this._preparePage(page);
145
+ }
146
+
147
+ if (this.m_cpage.selector) {
148
+ this.m_cpage.selector.addClass('@active');
149
+ }
150
+ }
151
+
152
+ this.emit('change', EvChange(this.m_cpage.name));
153
+ }
154
+ }
155
+
156
+ /**
157
+ *
158
+ */
159
+
160
+ setPages(pages: ICardViewItem[]) {
161
+ let active = this._initTabs(pages);
162
+ if (active) {
163
+ asap(() => {
164
+ this.switchTo(active)
165
+ this.update();
166
+ });
167
+ }
168
+ }
169
+
170
+ /**
171
+ *
172
+ */
173
+
174
+ private _initTabs(pages): any {
175
+
176
+ if (!pages) {
177
+ return;
178
+ }
179
+
180
+ let active = this.m_ipage;
181
+
182
+ pages.forEach((p) => {
183
+
184
+ if( !p ) {
185
+ return;
186
+ }
187
+
188
+ let card: ICardItemEx = { ...p };
189
+
190
+ card.selector = this._prepareSelector(p);
191
+ card.active = false;
192
+
193
+ this.m_cards.push(card);
194
+
195
+ if (!active) {
196
+ active = p.name;
197
+ }
198
+
199
+ if (p.active) {
200
+ active = p.name;
201
+ }
202
+ });
203
+
204
+ return active;
205
+ }
206
+
207
+ protected _updateSelector() {
208
+ }
209
+
210
+ /**
211
+ * prepare the cardinfo
212
+ * can be used by derivations to create & set selectors
213
+ */
214
+
215
+ protected _prepareSelector(card: ICardViewItem): Component {
216
+ return null;
217
+ }
218
+
219
+ /**
220
+ *
221
+ */
222
+
223
+ protected _preparePage(page: Component) {
224
+ page.setStyleValue('flex', 1);
225
+ page.addClass('@tab-page');
226
+ }
227
+ }