x4js 1.5.16 → 1.5.18

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 (75) hide show
  1. package/lib/cjs/app_sockets.js +74 -0
  2. package/lib/cjs/index.js +1 -0
  3. package/lib/cjs/treeview.js +27 -12
  4. package/lib/cjs/version.js +1 -1
  5. package/lib/esm/app_sockets.js +70 -0
  6. package/lib/esm/index.mjs +1 -0
  7. package/lib/esm/treeview.js +26 -12
  8. package/lib/esm/version.js +1 -1
  9. package/lib/{src/version.ts → types/app_sockets.d.ts} +29 -30
  10. package/lib/types/index.d.ts +1 -0
  11. package/lib/types/treeview.d.ts +5 -2
  12. package/lib/types/version.d.ts +1 -1
  13. package/package.json +1 -4
  14. package/lib/src/MIT-license.md +0 -14
  15. package/lib/src/action.ts +0 -88
  16. package/lib/src/alpha.jpg +0 -0
  17. package/lib/src/application.ts +0 -251
  18. package/lib/src/autocomplete.ts +0 -197
  19. package/lib/src/base64.ts +0 -166
  20. package/lib/src/base_component.ts +0 -152
  21. package/lib/src/button.ts +0 -355
  22. package/lib/src/calendar.ts +0 -328
  23. package/lib/src/canvas.ts +0 -505
  24. package/lib/src/cardview.ts +0 -227
  25. package/lib/src/checkbox.ts +0 -188
  26. package/lib/src/color.ts +0 -752
  27. package/lib/src/colorpicker.ts +0 -1639
  28. package/lib/src/combobox.ts +0 -465
  29. package/lib/src/component.ts +0 -2329
  30. package/lib/src/datastore.ts +0 -1322
  31. package/lib/src/dialog.ts +0 -656
  32. package/lib/src/dom_events.ts +0 -315
  33. package/lib/src/drag_manager.ts +0 -199
  34. package/lib/src/drawtext.ts +0 -355
  35. package/lib/src/fileupload.ts +0 -213
  36. package/lib/src/form.ts +0 -375
  37. package/lib/src/formatters.ts +0 -105
  38. package/lib/src/gridview.ts +0 -1185
  39. package/lib/src/i18n.ts +0 -346
  40. package/lib/src/icon.ts +0 -335
  41. package/lib/src/image.ts +0 -204
  42. package/lib/src/index.ts +0 -88
  43. package/lib/src/input.ts +0 -249
  44. package/lib/src/label.ts +0 -128
  45. package/lib/src/layout.ts +0 -430
  46. package/lib/src/link.ts +0 -86
  47. package/lib/src/listview.ts +0 -765
  48. package/lib/src/md5.ts +0 -438
  49. package/lib/src/menu.ts +0 -425
  50. package/lib/src/messagebox.ts +0 -224
  51. package/lib/src/panel.ts +0 -86
  52. package/lib/src/popup.ts +0 -494
  53. package/lib/src/property_editor.ts +0 -337
  54. package/lib/src/radiobtn.ts +0 -197
  55. package/lib/src/rating.ts +0 -135
  56. package/lib/src/request.ts +0 -300
  57. package/lib/src/router.ts +0 -185
  58. package/lib/src/settings.ts +0 -77
  59. package/lib/src/sidebarview.ts +0 -103
  60. package/lib/src/spreadsheet.ts +0 -1449
  61. package/lib/src/styles.ts +0 -343
  62. package/lib/src/svgcomponent.ts +0 -577
  63. package/lib/src/tabbar.ts +0 -151
  64. package/lib/src/tabview.ts +0 -110
  65. package/lib/src/textarea.ts +0 -235
  66. package/lib/src/textedit.ts +0 -544
  67. package/lib/src/toaster.ts +0 -80
  68. package/lib/src/tools.ts +0 -1473
  69. package/lib/src/tooltips.ts +0 -191
  70. package/lib/src/treeview.ts +0 -696
  71. package/lib/src/x4.less +0 -2243
  72. package/lib/src/x4dom.ts +0 -57
  73. package/lib/src/x4events.ts +0 -585
  74. package/lib/src/x4react.ts +0 -90
  75. package/lib/x4.css +0 -1780
package/lib/src/canvas.ts DELETED
@@ -1,505 +0,0 @@
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
- }
@@ -1,227 +0,0 @@
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
- }