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