x4js 2.0.12 → 2.0.14
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/.vscode/launch.json +14 -0
- package/README.md +20 -15
- package/{lib/src/demo → demo}/assets/radio.svg +3 -3
- package/{lib/src/demo → demo}/index.html +11 -11
- package/{lib/src/demo → demo}/main.scss +23 -21
- package/{lib/src/demo/main.tsx → demo/main.ts} +324 -323
- package/demo/package.json +26 -0
- package/demo/scss.d.ts +4 -0
- package/demo/svg.d.ts +1 -0
- package/demo/tsconfig.json +14 -0
- package/lib/README.txt +20 -15
- package/lib/cjs/x4.css +1 -1
- package/lib/cjs/x4.js +2 -1
- package/lib/esm/x4.css +1 -1
- package/lib/esm/x4.mjs +2 -1
- package/lib/src/components/base.scss +25 -26
- package/lib/src/components/boxes/boxes.module.scss +54 -37
- package/lib/src/components/boxes/boxes.ts +278 -125
- package/lib/src/components/breadcrumb/breadcrumb.scss +56 -0
- package/lib/src/components/breadcrumb/breadcrumb.ts +93 -0
- package/lib/src/components/breadcrumb/chevron-right.svg +1 -0
- package/lib/src/components/btngroup/btngroup.module.scss +40 -28
- package/lib/src/components/btngroup/btngroup.ts +152 -101
- package/lib/src/components/button/button.module.scss +172 -153
- package/lib/src/components/button/button.ts +185 -117
- package/lib/src/components/calendar/calendar.module.scss +162 -162
- package/lib/src/components/calendar/calendar.ts +326 -325
- package/lib/src/components/canvas/canvas.module.scss +25 -0
- package/lib/src/components/canvas/canvas.ts +189 -0
- package/lib/src/components/canvas/canvas_ex.ts +269 -0
- package/lib/src/components/checkbox/check.svg +3 -3
- package/lib/src/components/checkbox/checkbox.module.scss +141 -141
- package/lib/src/components/checkbox/checkbox.ts +139 -124
- package/lib/src/components/colorinput/colorinput.module.scss +64 -64
- package/lib/src/components/colorinput/colorinput.ts +90 -87
- package/lib/src/components/colorpicker/colorpicker.module.scss +132 -132
- package/lib/src/components/colorpicker/colorpicker.ts +481 -476
- package/lib/src/components/combobox/combobox.module.scss +132 -120
- package/lib/src/components/combobox/combobox.ts +275 -190
- package/lib/src/components/combobox/updown.svg +3 -3
- package/lib/src/components/components.ts +41 -0
- package/lib/src/components/dialog/dialog.module.scss +105 -71
- package/lib/src/components/dialog/dialog.ts +212 -92
- package/lib/src/components/filedrop/cloud-arrow-up.svg +1 -0
- package/lib/src/components/filedrop/filedrop.module.scss +70 -0
- package/lib/src/components/filedrop/filedrop.ts +131 -0
- package/lib/src/components/form/form.module.scss +38 -34
- package/lib/src/components/form/form.ts +172 -36
- package/lib/src/components/gridview/arrow-down-light.svg +1 -0
- package/lib/src/components/gridview/arrow-up-light.svg +1 -0
- package/lib/src/components/gridview/gridview.module.scss +324 -0
- package/lib/src/components/gridview/gridview.ts +1175 -0
- package/lib/src/components/header/header.module.scss +39 -39
- package/lib/src/components/header/header.ts +129 -123
- package/lib/src/components/icon/icon.module.scss +30 -30
- package/lib/src/components/icon/icon.ts +139 -134
- package/lib/src/components/image/image.module.scss +27 -20
- package/lib/src/components/image/image.ts +168 -67
- package/lib/src/components/input/input.module.scss +74 -69
- package/lib/src/components/input/input.ts +398 -274
- package/lib/src/components/keyboard/arrow-up.svg +1 -0
- package/lib/src/components/keyboard/delete-left.svg +1 -0
- package/lib/src/components/keyboard/eye-slash.svg +1 -0
- package/lib/src/components/keyboard/keyboard.module.scss +134 -0
- package/lib/src/components/keyboard/keyboard.ts +525 -0
- package/lib/src/components/label/label.module.scss +76 -52
- package/lib/src/components/label/label.ts +97 -55
- package/lib/src/components/link/link.ts +81 -0
- package/lib/src/components/listbox/listbox.module.scss +161 -103
- package/lib/src/components/listbox/listbox.ts +539 -427
- package/lib/src/components/menu/menu.module.scss +116 -107
- package/lib/src/components/menu/menu.ts +174 -168
- package/lib/src/components/messages/messages.module.scss +92 -47
- package/lib/src/components/messages/messages.ts +215 -64
- package/lib/src/components/messages/pen-field.svg +1 -0
- package/lib/src/components/normalize.scss +391 -386
- package/lib/src/components/notification/notification.module.scss +83 -81
- package/lib/src/components/notification/notification.ts +107 -108
- package/lib/src/components/panel/panel.module.scss +59 -47
- package/lib/src/components/panel/panel.ts +57 -56
- package/lib/src/components/popup/popup.module.scss +45 -43
- package/lib/src/components/popup/popup.ts +440 -395
- package/lib/src/components/progress/progress.module.scss +56 -56
- package/lib/src/components/progress/progress.ts +43 -42
- package/lib/src/components/propgrid/folder-closed.svg +1 -0
- package/lib/src/components/propgrid/folder-open.svg +1 -0
- package/lib/src/components/propgrid/progrid.module.scss +108 -0
- package/lib/src/components/propgrid/propgrid.ts +271 -0
- package/lib/src/components/propgrid/updown.svg +4 -0
- package/lib/src/components/radio/radio.module.scss +147 -0
- package/lib/src/components/radio/radio.svg +4 -0
- package/lib/src/components/radio/radio.ts +142 -0
- package/lib/src/components/rating/rating.module.scss +22 -22
- package/lib/src/components/rating/rating.ts +131 -125
- package/lib/src/components/select/select.module.scss +9 -0
- package/lib/src/components/select/select.ts +134 -0
- package/lib/src/components/shared.scss +137 -76
- package/lib/src/components/sizers/sizer.module.scss +89 -89
- package/lib/src/components/sizers/sizer.ts +130 -119
- package/lib/src/components/slider/slider.module.scss +117 -70
- package/lib/src/components/slider/slider.ts +197 -142
- package/lib/src/components/switch/switch.module.scss +126 -126
- package/lib/src/components/switch/switch.ts +61 -55
- package/lib/src/components/tabs/tabs.module.scss +45 -46
- package/lib/src/components/tabs/tabs.ts +199 -157
- package/lib/src/components/textarea/textarea.module.scss +63 -59
- package/lib/src/components/textarea/textarea.ts +125 -54
- package/lib/src/components/textedit/textedit.module.scss +115 -113
- package/lib/src/components/textedit/textedit.ts +110 -82
- package/lib/src/components/themes.scss +88 -77
- package/lib/src/components/tickline/tickline.module.scss +26 -0
- package/lib/src/components/tickline/tickline.ts +82 -0
- package/lib/src/components/tooltips/comments-question.svg +1 -0
- package/lib/src/components/tooltips/tooltips.scss +71 -50
- package/lib/src/components/tooltips/tooltips.ts +108 -102
- package/lib/src/components/treeview/treeview.module.scss +184 -115
- package/lib/src/components/treeview/treeview.ts +445 -403
- package/lib/src/components/viewport/viewport.module.scss +31 -24
- package/lib/src/components/viewport/viewport.ts +41 -38
- package/lib/src/core/component.ts +1072 -979
- package/lib/src/core/core_application.ts +264 -0
- package/lib/src/core/core_colors.ts +249 -249
- package/lib/src/core/core_data.ts +1309 -0
- package/lib/src/core/core_dom.ts +471 -471
- package/lib/src/core/core_dragdrop.ts +200 -200
- package/lib/src/core/core_element.ts +109 -97
- package/lib/src/core/core_events.ts +177 -149
- package/lib/src/core/core_i18n.ts +393 -377
- package/lib/src/core/core_react.ts +79 -0
- package/lib/src/core/core_router.ts +237 -221
- package/lib/src/core/core_styles.ts +214 -214
- package/lib/src/core/core_svg.ts +711 -550
- package/lib/src/core/core_tools.ts +906 -673
- package/lib/src/types/scss.d.ts +4 -4
- package/lib/src/types/x4react.d.ts +8 -8
- package/lib/src/x4.scss +18 -18
- package/lib/src/x4.ts +31 -62
- package/lib/src/x4tsx.d.ts +25 -0
- package/lib/styles/x4.css +1 -1
- package/lib/types/x4js.d.ts +853 -127
- package/package.json +5 -6
- package/scripts/build.mjs +378 -0
- package/scripts/prepack.mjs +346 -0
- package/src/components/base.scss +25 -0
- package/src/components/boxes/boxes.module.scss +54 -0
- package/src/components/boxes/boxes.ts +278 -0
- package/src/components/breadcrumb/breadcrumb.scss +56 -0
- package/src/components/breadcrumb/breadcrumb.ts +93 -0
- package/src/components/breadcrumb/chevron-right.svg +1 -0
- package/src/components/btngroup/btngroup.module.scss +41 -0
- package/src/components/btngroup/btngroup.ts +153 -0
- package/src/components/button/button.module.scss +173 -0
- package/src/components/button/button.ts +185 -0
- package/src/components/calendar/calendar-check-sharp-light.svg +1 -0
- package/src/components/calendar/calendar.module.scss +163 -0
- package/src/components/calendar/calendar.ts +327 -0
- package/src/components/calendar/chevron-left-sharp-light.svg +1 -0
- package/src/components/calendar/chevron-right-sharp-light.svg +1 -0
- package/src/components/canvas/canvas.module.scss +25 -0
- package/src/components/canvas/canvas.ts +189 -0
- package/src/components/canvas/canvas_ex.ts +269 -0
- package/src/components/checkbox/check.svg +4 -0
- package/src/components/checkbox/checkbox.module.scss +142 -0
- package/src/components/checkbox/checkbox.ts +140 -0
- package/src/components/colorinput/colorinput.module.scss +65 -0
- package/src/components/colorinput/colorinput.ts +91 -0
- package/src/components/colorinput/crosshairs-simple-sharp-light.svg +1 -0
- package/src/components/colorpicker/colorpicker.module.scss +133 -0
- package/src/components/colorpicker/colorpicker.ts +482 -0
- package/src/components/combobox/combobox.module.scss +133 -0
- package/src/components/combobox/combobox.ts +275 -0
- package/src/components/combobox/updown.svg +4 -0
- package/src/components/components.ts +41 -0
- package/src/components/dialog/dialog.module.scss +105 -0
- package/src/components/dialog/dialog.ts +212 -0
- package/src/components/dialog/xmark-sharp-light.svg +1 -0
- package/src/components/filedrop/cloud-arrow-up.svg +1 -0
- package/src/components/filedrop/filedrop.module.scss +70 -0
- package/src/components/filedrop/filedrop.ts +131 -0
- package/src/components/form/form.module.scss +38 -0
- package/src/components/form/form.ts +172 -0
- package/src/components/gridview/arrow-down-light.svg +1 -0
- package/src/components/gridview/arrow-up-light.svg +1 -0
- package/src/components/gridview/gridview.module.scss +324 -0
- package/src/components/gridview/gridview.ts +1175 -0
- package/src/components/header/header.module.scss +40 -0
- package/src/components/header/header.ts +130 -0
- package/src/components/icon/icon.module.scss +30 -0
- package/src/components/icon/icon.ts +139 -0
- package/src/components/image/image.module.scss +28 -0
- package/src/components/image/image.ts +168 -0
- package/src/components/input/input.module.scss +74 -0
- package/src/components/input/input.ts +398 -0
- package/src/components/keyboard/arrow-up.svg +1 -0
- package/src/components/keyboard/delete-left.svg +1 -0
- package/src/components/keyboard/eye-slash.svg +1 -0
- package/src/components/keyboard/keyboard.module.scss +134 -0
- package/src/components/keyboard/keyboard.ts +525 -0
- package/src/components/label/label.module.scss +76 -0
- package/src/components/label/label.ts +97 -0
- package/src/components/link/link.ts +81 -0
- package/src/components/listbox/listbox.module.scss +161 -0
- package/src/components/listbox/listbox.ts +539 -0
- package/src/components/menu/caret-right-solid.svg +1 -0
- package/src/components/menu/menu.module.scss +117 -0
- package/src/components/menu/menu.ts +174 -0
- package/src/components/messages/circle-exclamation.svg +1 -0
- package/src/components/messages/messages.module.scss +92 -0
- package/src/components/messages/messages.ts +215 -0
- package/src/components/messages/pen-field.svg +1 -0
- package/src/components/normalize.scss +391 -0
- package/src/components/notification/circle-check-solid.svg +1 -0
- package/src/components/notification/circle-exclamation-solid.svg +1 -0
- package/src/components/notification/circle-notch-light.svg +1 -0
- package/src/components/notification/notification.module.scss +84 -0
- package/src/components/notification/notification.ts +107 -0
- package/src/components/notification/xmark-sharp-light.svg +1 -0
- package/src/components/panel/panel.module.scss +60 -0
- package/src/components/panel/panel.ts +58 -0
- package/src/components/popup/popup.module.scss +45 -0
- package/src/components/popup/popup.ts +440 -0
- package/src/components/progress/progress.module.scss +57 -0
- package/src/components/progress/progress.ts +44 -0
- package/src/components/propgrid/folder-closed.svg +1 -0
- package/src/components/propgrid/folder-open.svg +1 -0
- package/src/components/propgrid/progrid.module.scss +108 -0
- package/src/components/propgrid/propgrid.ts +271 -0
- package/src/components/propgrid/updown.svg +4 -0
- package/src/components/radio/radio.module.scss +147 -0
- package/src/components/radio/radio.svg +4 -0
- package/src/components/radio/radio.ts +142 -0
- package/src/components/rating/rating.module.scss +23 -0
- package/src/components/rating/rating.ts +131 -0
- package/src/components/rating/star-sharp-light.svg +1 -0
- package/src/components/rating/star-sharp-solid.svg +1 -0
- package/src/components/select/select.module.scss +9 -0
- package/src/components/select/select.ts +134 -0
- package/src/components/shared.scss +137 -0
- package/src/components/sizers/sizer.module.scss +90 -0
- package/src/components/sizers/sizer.ts +131 -0
- package/src/components/slider/slider.module.scss +118 -0
- package/src/components/slider/slider.ts +198 -0
- package/src/components/switch/switch.module.scss +127 -0
- package/src/components/switch/switch.ts +62 -0
- package/src/components/tabs/tabs.module.scss +45 -0
- package/src/components/tabs/tabs.ts +199 -0
- package/src/components/textarea/textarea.module.scss +63 -0
- package/src/components/textarea/textarea.ts +125 -0
- package/src/components/textedit/textedit.module.scss +116 -0
- package/src/components/textedit/textedit.ts +110 -0
- package/src/components/themes.scss +88 -0
- package/src/components/tickline/tickline.module.scss +26 -0
- package/src/components/tickline/tickline.ts +82 -0
- package/src/components/tooltips/circle-info-sharp-light.svg +1 -0
- package/src/components/tooltips/comments-question.svg +1 -0
- package/src/components/tooltips/tooltips.scss +72 -0
- package/src/components/tooltips/tooltips.ts +109 -0
- package/src/components/treeview/chevron-down-light.svg +1 -0
- package/src/components/treeview/treeview.module.scss +185 -0
- package/src/components/treeview/treeview.ts +445 -0
- package/src/components/viewport/viewport.module.scss +32 -0
- package/src/components/viewport/viewport.ts +41 -0
- package/src/core/component.ts +1072 -0
- package/src/core/core_application.ts +264 -0
- package/src/core/core_colors.ts +250 -0
- package/src/core/core_data.ts +1309 -0
- package/src/core/core_dom.ts +471 -0
- package/src/core/core_dragdrop.ts +201 -0
- package/src/core/core_element.ts +110 -0
- package/src/core/core_events.ts +177 -0
- package/src/core/core_i18n.ts +393 -0
- package/src/core/core_react.ts +79 -0
- package/src/core/core_router.ts +237 -0
- package/src/core/core_styles.ts +214 -0
- package/src/core/core_svg.ts +711 -0
- package/src/core/core_tools.ts +906 -0
- package/src/types/scss.d.ts +4 -0
- package/src/types/svg.d.ts +1 -0
- package/src/types/x4react.d.ts +9 -0
- package/src/x4.scss +19 -0
- package/src/x4.ts +31 -62
- package/src/x4tsx.d.ts +25 -0
- package/tsconfig.json +14 -0
- /package/{lib/src/demo → demo}/assets/house-light.svg +0 -0
|
@@ -0,0 +1,711 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ___ ___ __
|
|
3
|
+
* \ \/ / / _
|
|
4
|
+
* \ / /_| |_
|
|
5
|
+
* / \____ _|
|
|
6
|
+
* /__/\__\ |_|
|
|
7
|
+
*
|
|
8
|
+
* @file core_svg.ts
|
|
9
|
+
* @author Etienne Cochard
|
|
10
|
+
*
|
|
11
|
+
* @copyright (c) 2024 R-libre ingenierie
|
|
12
|
+
*
|
|
13
|
+
* Use of this source code is governed by an MIT-style license
|
|
14
|
+
* that can be found in the LICENSE file or at https://opensource.org/licenses/MIT.
|
|
15
|
+
**/
|
|
16
|
+
|
|
17
|
+
import { Component, ComponentProps } from './component';
|
|
18
|
+
import { isUnitLess } from "./core_styles";
|
|
19
|
+
import { DOMEventHandler, GlobalDOMEvents, addEvent } from './core_dom';
|
|
20
|
+
import { isNumber, isString } from './core_tools';
|
|
21
|
+
|
|
22
|
+
const SVG_NS = "http://www.w3.org/2000/svg";
|
|
23
|
+
|
|
24
|
+
// degrees to radian
|
|
25
|
+
function d2r( d: number ): number {
|
|
26
|
+
return d * Math.PI / 180.0;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// polar to cartesian
|
|
30
|
+
function p2c( x: number, y: number, r: number, deg: number ): {x: number,y: number} {
|
|
31
|
+
const rad = d2r( deg );
|
|
32
|
+
return {
|
|
33
|
+
x: x + r * Math.cos( rad ),
|
|
34
|
+
y: y + r * Math.sin( rad )
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// fix prec for numbers
|
|
39
|
+
function num( x: number ): number {
|
|
40
|
+
return Math.round( x * 1000 ) / 1000;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// clean values
|
|
44
|
+
function clean( a: any, ...b: any ) {
|
|
45
|
+
// just round number values to 3 digits
|
|
46
|
+
b = b.map( ( v: any ) => {
|
|
47
|
+
if( typeof v === 'number' && isFinite(v) ) {
|
|
48
|
+
return num(v);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return v;
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
return String.raw( a, ...b );
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class SvgItem {
|
|
66
|
+
protected _dom : SVGElement;
|
|
67
|
+
|
|
68
|
+
constructor( tag: string ) {
|
|
69
|
+
this._dom = document.createElementNS("http://www.w3.org/2000/svg", tag );
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @returns the svh element dom
|
|
74
|
+
*/
|
|
75
|
+
|
|
76
|
+
getDom( ) {
|
|
77
|
+
return this._dom;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
*
|
|
82
|
+
*/
|
|
83
|
+
|
|
84
|
+
reset( ) {
|
|
85
|
+
const attrs = this._dom.attributes;
|
|
86
|
+
for (let i = attrs.length - 1; i >= 0; i--) {
|
|
87
|
+
this._dom.removeAttribute(attrs[i].name);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* change the stroke color
|
|
96
|
+
* @param color
|
|
97
|
+
*/
|
|
98
|
+
|
|
99
|
+
stroke( color: string, width?: number ): this {
|
|
100
|
+
this.setAttr( 'stroke', color );
|
|
101
|
+
if( width!==undefined ) {
|
|
102
|
+
this.setAttr( 'stroke-width', width+'px' );
|
|
103
|
+
}
|
|
104
|
+
return this;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* change the stroke width
|
|
109
|
+
* @param width
|
|
110
|
+
*/
|
|
111
|
+
strokeWidth( width: number ): this {
|
|
112
|
+
this.setAttr( 'stroke-width', width+'px' );
|
|
113
|
+
return this;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
strokeCap( cap: "butt" | "round" | "sqaure" ) {
|
|
117
|
+
return this.setAttr( "stroke-linecap", cap );
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
strokeOpacity( opacity: number ) {
|
|
121
|
+
return this.setAttr( "stroke-opacity", opacity+"" );
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
*
|
|
126
|
+
*/
|
|
127
|
+
|
|
128
|
+
antiAlias( set: boolean ) {
|
|
129
|
+
return this.setAttr( "shape-rendering", set ? "auto" : "crispEdges" );
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* change the fill color
|
|
134
|
+
* @param color
|
|
135
|
+
*/
|
|
136
|
+
|
|
137
|
+
fill( color: string ): this {
|
|
138
|
+
this.setAttr( 'fill', color );
|
|
139
|
+
return this;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
no_fill( ): this {
|
|
143
|
+
this.setAttr( 'fill', "transparent" );
|
|
144
|
+
return this;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* return the given attribute if any
|
|
149
|
+
*/
|
|
150
|
+
|
|
151
|
+
getAttr( name: string ) : string {
|
|
152
|
+
return this._dom.getAttribute( name );
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
getNumAttr( name: string ) {
|
|
156
|
+
return parseInt( this._dom.getAttribute( name ) );
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* define a new attribute
|
|
161
|
+
* @param name attibute name
|
|
162
|
+
* @param value attribute value
|
|
163
|
+
* @returns this
|
|
164
|
+
*/
|
|
165
|
+
|
|
166
|
+
setAttr( name: string, value: string ) : this {
|
|
167
|
+
this._dom.setAttribute( name, value );
|
|
168
|
+
return this;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
*
|
|
173
|
+
*/
|
|
174
|
+
|
|
175
|
+
setStyle<K extends keyof CSSStyleDeclaration>( name: K, value: string | number ) : this {
|
|
176
|
+
const _style = this._dom.style;
|
|
177
|
+
|
|
178
|
+
if( isNumber(value) ) {
|
|
179
|
+
let v = value+"";
|
|
180
|
+
if( !isUnitLess(name as string) ) {
|
|
181
|
+
v += "px";
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
(_style as any)[name] = v;
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
(_style as any)[name] = value;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return this;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* add a class
|
|
195
|
+
* @param name class name to add
|
|
196
|
+
*/
|
|
197
|
+
|
|
198
|
+
addClass( cls: string ): this {
|
|
199
|
+
if( !cls ) return;
|
|
200
|
+
|
|
201
|
+
if( cls.indexOf(' ')>=0 ) {
|
|
202
|
+
const ccs = cls.split( " " );
|
|
203
|
+
this._dom.classList.add(...ccs);
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
this._dom.classList.add(cls);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return this;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* remove a class
|
|
214
|
+
* @param name class name to remove
|
|
215
|
+
*/
|
|
216
|
+
|
|
217
|
+
removeClass( cls: string ): this {
|
|
218
|
+
if( !cls ) return;
|
|
219
|
+
|
|
220
|
+
if( cls.indexOf(' ')>=0 ) {
|
|
221
|
+
const ccs = cls.split( " " );
|
|
222
|
+
this._dom.classList.remove(...ccs);
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
this._dom.classList.remove(cls);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return this;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
*
|
|
233
|
+
*/
|
|
234
|
+
|
|
235
|
+
clip( id: string ): this {
|
|
236
|
+
this.setAttr( "clip-path", `url(#${id})` );
|
|
237
|
+
return this;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
*
|
|
242
|
+
*/
|
|
243
|
+
|
|
244
|
+
transform( tr: string ): this {
|
|
245
|
+
const t = this.getAttr( "transform" ) ?? "";
|
|
246
|
+
this.setAttr( "transform", t+' '+tr );
|
|
247
|
+
return this;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
clear_transform( ) {
|
|
251
|
+
this.setAttr( "transform", null );
|
|
252
|
+
return this;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
*
|
|
257
|
+
*/
|
|
258
|
+
|
|
259
|
+
rotate( deg: number, cx: number, cy: number ): this {
|
|
260
|
+
this.transform( `rotate( ${deg} ${cx} ${cy} )` );
|
|
261
|
+
return this;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
translate( dx: number, dy: number ): this {
|
|
265
|
+
this.transform( `translate( ${dx} ${dy} )` );
|
|
266
|
+
return this;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
scale( x: number ): this {
|
|
270
|
+
this.transform( `scale( ${x} )` );
|
|
271
|
+
return this;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
*
|
|
276
|
+
*/
|
|
277
|
+
|
|
278
|
+
addDOMEvent<K extends keyof GlobalDOMEvents>( name: K, listener: GlobalDOMEvents[K], prepend = false ) {
|
|
279
|
+
addEvent( this._dom, name, listener as DOMEventHandler, prepend );
|
|
280
|
+
return this;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
*
|
|
288
|
+
*/
|
|
289
|
+
|
|
290
|
+
export class SvgPath extends SvgItem {
|
|
291
|
+
private _path: string;
|
|
292
|
+
|
|
293
|
+
constructor( ) {
|
|
294
|
+
super( 'path' );
|
|
295
|
+
this._path = '';
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
private _update( ): this {
|
|
299
|
+
this.setAttr( 'd', this._path );
|
|
300
|
+
return this;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
reset( ) {
|
|
304
|
+
this._path = "";
|
|
305
|
+
super.reset( );
|
|
306
|
+
return this;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* move the current pos
|
|
311
|
+
* @param x new pos x
|
|
312
|
+
* @param y new pos y
|
|
313
|
+
* @returns this
|
|
314
|
+
*/
|
|
315
|
+
|
|
316
|
+
moveTo( x: number, y: number ) : this {
|
|
317
|
+
this._path += clean`M${x},${y}`;
|
|
318
|
+
return this._update( );
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* draw aline to the given point
|
|
323
|
+
* @param x end x
|
|
324
|
+
* @param y end y
|
|
325
|
+
* @returns this
|
|
326
|
+
*/
|
|
327
|
+
|
|
328
|
+
lineTo( x: number, y: number ): this {
|
|
329
|
+
this._path += clean`L${x},${y}`;
|
|
330
|
+
return this._update( );
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* draw a curve
|
|
335
|
+
*/
|
|
336
|
+
|
|
337
|
+
curveTo( x1: number, y1: number, x2: number, y2: number, x3: number, y3: number ) {
|
|
338
|
+
this._path += clean`C${x1},${y1} ${x2},${y2} ${x3},${y3}`;
|
|
339
|
+
return this._update( );
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* close the currentPath
|
|
345
|
+
*/
|
|
346
|
+
|
|
347
|
+
closePath( ): this {
|
|
348
|
+
this._path += 'Z';
|
|
349
|
+
return this._update( );
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* draw an arc
|
|
354
|
+
* @param x center x
|
|
355
|
+
* @param y center y
|
|
356
|
+
* @param r radius
|
|
357
|
+
* @param start angle start in degrees
|
|
358
|
+
* @param end angle end in degrees
|
|
359
|
+
* @returns this
|
|
360
|
+
*/
|
|
361
|
+
|
|
362
|
+
arc( x: number, y: number, r: number, start: number, end: number, clockwise= true ): this {
|
|
363
|
+
|
|
364
|
+
const st = p2c( x, y, r, start-90 );
|
|
365
|
+
const en = p2c( x, y, r, end-90 );
|
|
366
|
+
|
|
367
|
+
const flag = ((end-start) <= 180 ? "0" : "1");
|
|
368
|
+
this._path += clean`M${st.x},${st.y}A${r},${r} 0 ${flag} ${(clockwise ? '1' : '0')} ${en.x},${en.y}`;
|
|
369
|
+
|
|
370
|
+
return this._update( );
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
*
|
|
376
|
+
*/
|
|
377
|
+
|
|
378
|
+
export class SvgText extends SvgItem {
|
|
379
|
+
|
|
380
|
+
constructor( x: number, y: number, txt: string ) {
|
|
381
|
+
super( 'text' );
|
|
382
|
+
|
|
383
|
+
this.setAttr( 'x', num(x)+'' );
|
|
384
|
+
this.setAttr( 'y', num(y)+'' );
|
|
385
|
+
|
|
386
|
+
this._dom.innerHTML = txt;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
font( font: string ): this {
|
|
390
|
+
return this.setAttr( 'font-family', font );
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
fontSize( size: number | string ): this {
|
|
394
|
+
return this.setAttr( 'font-size', size+'' );
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
fontWeight( weight: 'light' | 'normal' | 'bold' ): this {
|
|
398
|
+
return this.setAttr( 'font-weight', weight );
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
textAlign( align: 'left' | 'center' | 'right' ): this {
|
|
402
|
+
|
|
403
|
+
let al;
|
|
404
|
+
switch( align ) {
|
|
405
|
+
case 'left': al = 'start'; break;
|
|
406
|
+
case 'center': al = 'middle'; break;
|
|
407
|
+
case 'right': al = 'end'; break;
|
|
408
|
+
default: return this;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
return this.setAttr( 'text-anchor', al );
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
verticalAlign( align: 'top' | 'center' | 'bottom' | 'baseline' ): this {
|
|
415
|
+
|
|
416
|
+
let al;
|
|
417
|
+
switch( align ) {
|
|
418
|
+
case 'top': al = 'hanging'; break;
|
|
419
|
+
case 'center': al = 'middle'; break;
|
|
420
|
+
case 'bottom': al = 'baseline'; break;
|
|
421
|
+
case 'baseline': al = 'mathematical'; break;
|
|
422
|
+
default: return;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
return this.setAttr( 'alignment-baseline', al );
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
*
|
|
431
|
+
*/
|
|
432
|
+
|
|
433
|
+
export class SvgIcon extends SvgItem {
|
|
434
|
+
constructor( svg: string ) {
|
|
435
|
+
super( "svg" );
|
|
436
|
+
|
|
437
|
+
if( svg.startsWith("data:image/svg+xml,") ) {
|
|
438
|
+
svg = svg.substring( 19 );
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
const parser = new DOMParser();
|
|
442
|
+
const doc = parser.parseFromString( decodeURIComponent(svg), "image/svg+xml");
|
|
443
|
+
|
|
444
|
+
const parserErrorElement = doc.querySelector("parsererror");
|
|
445
|
+
if( parserErrorElement ) {
|
|
446
|
+
console.error( "error while parsing svg:\n"+ parserErrorElement.textContent );
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
const svgRoot = doc.documentElement; // The <svg> element from the string
|
|
450
|
+
for( let i=0; i<svgRoot.attributes.length; i++) {
|
|
451
|
+
this._dom.setAttribute( svgRoot.attributes[i].name, svgRoot.attributes[i].value );
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
for( let i=0; i<svgRoot.childNodes.length; i++) {
|
|
455
|
+
const child = svgRoot.childNodes[i];
|
|
456
|
+
if (child.nodeType === 1) {
|
|
457
|
+
this._dom.appendChild(child);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
*
|
|
467
|
+
*/
|
|
468
|
+
|
|
469
|
+
export class SvgShape extends SvgItem {
|
|
470
|
+
constructor( tag: string ) {
|
|
471
|
+
super( tag );
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
*
|
|
477
|
+
*/
|
|
478
|
+
|
|
479
|
+
type number_or_perc = number | `${string}%`
|
|
480
|
+
|
|
481
|
+
export class SvgGradient extends SvgItem {
|
|
482
|
+
|
|
483
|
+
private static g_id = 1;
|
|
484
|
+
|
|
485
|
+
private _id: string;
|
|
486
|
+
private _stops: { offset: number_or_perc, color: string } [];
|
|
487
|
+
|
|
488
|
+
constructor( x1: number_or_perc, y1: number_or_perc, x2: number_or_perc, y2: number_or_perc ) {
|
|
489
|
+
super( 'linearGradient')
|
|
490
|
+
|
|
491
|
+
this._id = 'gx-'+SvgGradient.g_id;
|
|
492
|
+
SvgGradient.g_id++;
|
|
493
|
+
|
|
494
|
+
this.setAttr( 'id', this._id );
|
|
495
|
+
this.setAttr( 'x1', isString(x1) ? x1 : num(x1)+'' );
|
|
496
|
+
this.setAttr( 'x2', isString(x2) ? x2 : num(x2)+'' );
|
|
497
|
+
this.setAttr( 'y1', isString(y1) ? y1 : num(y1)+'' );
|
|
498
|
+
this.setAttr( 'y2', isString(y2) ? y2 : num(y2)+'' );
|
|
499
|
+
|
|
500
|
+
this._stops = [];
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
get id( ) {
|
|
504
|
+
return 'url(#'+this._id+')';
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
addStop( offset: number_or_perc, color: string ): this {
|
|
508
|
+
this._dom.insertAdjacentHTML( "beforeend", `<stop offset="${offset}%" stop-color="${color}"></stop>`);
|
|
509
|
+
return this;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
*
|
|
515
|
+
*/
|
|
516
|
+
|
|
517
|
+
export class SvgGroup extends SvgItem {
|
|
518
|
+
|
|
519
|
+
constructor( tag = "g" ) {
|
|
520
|
+
super( tag )
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
*
|
|
525
|
+
*/
|
|
526
|
+
|
|
527
|
+
append<K extends SvgItem>( item: K ): K {
|
|
528
|
+
this._dom.appendChild( item.getDom() );
|
|
529
|
+
return item;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
appendItems<K extends SvgItem>( items: K[] ) {
|
|
533
|
+
items.forEach( item => {
|
|
534
|
+
this._dom.appendChild( item.getDom() );
|
|
535
|
+
} );
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
*
|
|
540
|
+
*/
|
|
541
|
+
|
|
542
|
+
path( ): SvgPath {
|
|
543
|
+
const path = new SvgPath( );
|
|
544
|
+
return this.append( path );
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
text( x: number, y: number, txt: string ) {
|
|
548
|
+
const text = new SvgText( x, y, txt );
|
|
549
|
+
return this.append( text );
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
ellipse( x: number, y: number, r1: number, r2: number ): SvgShape {
|
|
553
|
+
const shape = new SvgShape( 'ellipse' );
|
|
554
|
+
shape.setAttr( 'cx', num(x)+'' );
|
|
555
|
+
shape.setAttr( 'cy', num(y)+'' );
|
|
556
|
+
shape.setAttr( 'rx', num(r1)+'' );
|
|
557
|
+
shape.setAttr( 'ry', num(r2)+'' );
|
|
558
|
+
return this.append( shape );
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
circle( x: number, y: number, r1: number ): SvgShape {
|
|
562
|
+
const shape = new SvgShape( 'ellipse' );
|
|
563
|
+
shape.setAttr( 'cx', num(x)+'' );
|
|
564
|
+
shape.setAttr( 'cy', num(y)+'' );
|
|
565
|
+
shape.setAttr( 'rx', num(r1)+'' );
|
|
566
|
+
shape.setAttr( 'ry', num(r1)+'' );
|
|
567
|
+
return this.append( shape );
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
icon( svg: string, x: number, y: number, w: number, h: number ): SvgIcon {
|
|
571
|
+
const icon = new SvgIcon( svg );
|
|
572
|
+
icon.setAttr( 'x', num(x)+'' );
|
|
573
|
+
icon.setAttr( 'y', num(y)+'' );
|
|
574
|
+
icon.setAttr( 'width', num(w)+'' );
|
|
575
|
+
icon.setAttr( 'height', num(h)+'' );
|
|
576
|
+
return this.append( icon );
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
rect( x: number, y: number, w: number, h: number ): SvgShape {
|
|
580
|
+
|
|
581
|
+
if( h<0 ) {
|
|
582
|
+
y = y+h;
|
|
583
|
+
h = -h;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
const shape = new SvgShape( 'rect' );
|
|
587
|
+
shape.setAttr( 'x', num(x)+'' );
|
|
588
|
+
shape.setAttr( 'y', num(y)+'' );
|
|
589
|
+
shape.setAttr( 'width', num(w)+'' );
|
|
590
|
+
shape.setAttr( 'height', num(h)+'' );
|
|
591
|
+
return this.append( shape );
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
group( id?: string ) {
|
|
595
|
+
const group = new SvgGroup( );
|
|
596
|
+
if( id ) {
|
|
597
|
+
group.setAttr( 'id', id );
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
return this.append( group );
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
*
|
|
605
|
+
* example
|
|
606
|
+
* ```ts
|
|
607
|
+
* const g = c.linear_gradient( '0%', '0%', '0%', '100%' )
|
|
608
|
+
* .addStop( 0, 'red' )
|
|
609
|
+
* .addStop( 100, 'green' );
|
|
610
|
+
*
|
|
611
|
+
* p.rect( 0, 0, 100, 100 )
|
|
612
|
+
* .stroke( g.id );
|
|
613
|
+
*
|
|
614
|
+
* ```
|
|
615
|
+
*/
|
|
616
|
+
|
|
617
|
+
linear_gradient( x1: number_or_perc, y1: number_or_perc, x2: number_or_perc, y2: number_or_perc ) {
|
|
618
|
+
const grad = new SvgGradient( x1, y1, x2, y2 );
|
|
619
|
+
return this.append( grad );
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/**
|
|
623
|
+
* clear
|
|
624
|
+
*/
|
|
625
|
+
|
|
626
|
+
clear( ) {
|
|
627
|
+
const dom = this._dom;
|
|
628
|
+
while( dom.firstChild ) {
|
|
629
|
+
dom.removeChild( dom.firstChild );
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
export class SvgBuilder extends SvgGroup {
|
|
639
|
+
private static g_clip_id = 1;
|
|
640
|
+
private static g_pat_id = 1;
|
|
641
|
+
|
|
642
|
+
constructor( ) {
|
|
643
|
+
super( );
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
addClip( x: number, y: number, w: number, h: number ) {
|
|
647
|
+
const id = 'clip-'+SvgBuilder.g_clip_id++;
|
|
648
|
+
const clip = new SvgGroup( 'clipPath' );
|
|
649
|
+
clip.setAttr('id', id );
|
|
650
|
+
clip.rect( x, y, w, h );
|
|
651
|
+
|
|
652
|
+
this.append(clip);
|
|
653
|
+
return {id,clip};
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
addPattern( x: number, y: number, w: number, h: number ) {
|
|
657
|
+
const id = 'pat-'+SvgBuilder.g_pat_id++;
|
|
658
|
+
|
|
659
|
+
const pat = new SvgGroup( 'pattern' );
|
|
660
|
+
pat.setAttr( 'id', id );
|
|
661
|
+
pat.setAttr( 'x', num(x)+'' );
|
|
662
|
+
pat.setAttr( 'y', num(y)+'' );
|
|
663
|
+
pat.setAttr( 'width', num(w)+'' );
|
|
664
|
+
pat.setAttr( 'height', num(h)+'' );
|
|
665
|
+
pat.setAttr( 'patternUnits', "userSpaceOnUse" );
|
|
666
|
+
|
|
667
|
+
this.append(pat);
|
|
668
|
+
return {id,pat};
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+
/**
|
|
675
|
+
*
|
|
676
|
+
*/
|
|
677
|
+
|
|
678
|
+
interface SvgProps extends ComponentProps {
|
|
679
|
+
viewbox?: string;
|
|
680
|
+
svg: SvgBuilder;
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/**
|
|
684
|
+
*
|
|
685
|
+
*/
|
|
686
|
+
|
|
687
|
+
export class SvgComponent<P extends SvgProps = SvgProps> extends Component<P> {
|
|
688
|
+
|
|
689
|
+
constructor( props: P ) {
|
|
690
|
+
super( { ...props, tag: "svg", ns: SVG_NS } );
|
|
691
|
+
|
|
692
|
+
this.setAttribute( 'xmlns', SVG_NS );
|
|
693
|
+
|
|
694
|
+
if( props.viewbox ) {
|
|
695
|
+
this.setAttribute( "viewBox", props.viewbox );
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
if( props.svg ) {
|
|
699
|
+
this.dom.appendChild( props.svg.getDom() );
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
setSvg( bld: SvgBuilder ) {
|
|
704
|
+
this.clearContent( );
|
|
705
|
+
this.dom.appendChild( bld.getDom() );
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
addItems( ...items: SvgItem[] ) {
|
|
709
|
+
items.forEach( item => this.dom.appendChild( item.getDom() ) );
|
|
710
|
+
}
|
|
711
|
+
}
|