x4js 2.0.4 → 2.0.6
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/build/x4.css +1 -0
- package/build/x4.js +1 -0
- package/lib/README.txt +15 -0
- package/lib/cjs/x4.css +1 -0
- package/lib/cjs/x4.js +1 -0
- package/lib/esm/x4.css +1 -0
- package/lib/esm/x4.mjs +1 -0
- package/lib/src/components/base.scss +26 -0
- package/lib/src/components/boxes/boxes.module.scss +37 -0
- package/lib/src/components/boxes/boxes.ts +125 -0
- package/lib/src/components/btngroup/btngroup.module.scss +29 -0
- package/lib/src/components/btngroup/btngroup.ts +106 -0
- package/lib/src/components/button/button.module.scss +154 -0
- package/lib/src/components/button/button.ts +117 -0
- package/lib/src/components/calendar/calendar-check-sharp-light.svg +1 -0
- package/lib/src/components/calendar/calendar.module.scss +163 -0
- package/lib/src/components/calendar/calendar.ts +326 -0
- package/lib/src/components/calendar/chevron-left-sharp-light.svg +1 -0
- package/lib/src/components/calendar/chevron-right-sharp-light.svg +1 -0
- package/lib/src/components/checkbox/check.svg +4 -0
- package/lib/src/components/checkbox/checkbox.module.scss +142 -0
- package/lib/src/components/checkbox/checkbox.ts +125 -0
- package/lib/src/components/colorinput/colorinput.module.scss +65 -0
- package/lib/src/components/colorinput/colorinput.ts +88 -0
- package/lib/src/components/colorinput/crosshairs-simple-sharp-light.svg +1 -0
- package/lib/src/components/colorpicker/colorpicker.module.scss +133 -0
- package/lib/src/components/colorpicker/colorpicker.ts +477 -0
- package/lib/src/components/combobox/combobox.module.scss +121 -0
- package/lib/src/components/combobox/combobox.ts +190 -0
- package/lib/src/components/combobox/updown.svg +4 -0
- package/lib/src/components/dialog/dialog.module.scss +71 -0
- package/lib/src/components/dialog/dialog.ts +91 -0
- package/lib/src/components/dialog/xmark-sharp-light.svg +1 -0
- package/lib/src/components/form/form.module.scss +34 -0
- package/lib/src/components/form/form.ts +36 -0
- package/lib/src/components/header/header.module.scss +40 -0
- package/lib/src/components/header/header.ts +124 -0
- package/lib/src/components/icon/icon.module.scss +30 -0
- package/lib/src/components/icon/icon.ts +134 -0
- package/lib/src/components/image/image.module.scss +21 -0
- package/lib/src/components/image/image.ts +67 -0
- package/lib/src/components/input/input.module.scss +69 -0
- package/lib/src/components/input/input.ts +274 -0
- package/lib/src/components/label/label.module.scss +52 -0
- package/lib/src/components/label/label.ts +55 -0
- package/lib/src/components/listbox/listbox.module.scss +103 -0
- package/lib/src/components/listbox/listbox.ts +427 -0
- package/lib/src/components/menu/caret-right-solid.svg +1 -0
- package/lib/src/components/menu/menu.module.scss +108 -0
- package/lib/src/components/menu/menu.ts +168 -0
- package/lib/src/components/messages/circle-exclamation.svg +1 -0
- package/lib/src/components/messages/messages.module.scss +47 -0
- package/lib/src/components/messages/messages.ts +64 -0
- package/lib/src/components/normalize.scss +386 -0
- package/lib/src/components/notification/circle-check-solid.svg +1 -0
- package/lib/src/components/notification/circle-exclamation-solid.svg +1 -0
- package/lib/src/components/notification/circle-notch-light.svg +1 -0
- package/lib/src/components/notification/notification.module.scss +82 -0
- package/lib/src/components/notification/notification.ts +108 -0
- package/lib/src/components/notification/xmark-sharp-light.svg +1 -0
- package/lib/src/components/panel/panel.module.scss +48 -0
- package/lib/src/components/panel/panel.ts +57 -0
- package/lib/src/components/popup/popup.module.scss +43 -0
- package/lib/src/components/popup/popup.ts +395 -0
- package/lib/src/components/progress/progress.module.scss +57 -0
- package/lib/src/components/progress/progress.ts +43 -0
- package/lib/src/components/rating/rating.module.scss +23 -0
- package/lib/src/components/rating/rating.ts +125 -0
- package/lib/src/components/rating/star-sharp-light.svg +1 -0
- package/lib/src/components/rating/star-sharp-solid.svg +1 -0
- package/lib/src/components/shared.scss +76 -0
- package/lib/src/components/sizers/sizer.module.scss +90 -0
- package/lib/src/components/sizers/sizer.ts +120 -0
- package/lib/src/components/slider/slider.module.scss +71 -0
- package/lib/src/components/slider/slider.ts +143 -0
- package/lib/src/components/switch/switch.module.scss +127 -0
- package/lib/src/components/switch/switch.ts +56 -0
- package/lib/src/components/tabs/tabs.module.scss +46 -0
- package/lib/src/components/tabs/tabs.ts +157 -0
- package/lib/src/components/textarea/textarea.module.scss +59 -0
- package/lib/src/components/textarea/textarea.ts +54 -0
- package/lib/src/components/textedit/textedit.module.scss +114 -0
- package/lib/src/components/textedit/textedit.ts +82 -0
- package/lib/src/components/themes.scss +77 -0
- package/lib/src/components/tooltips/circle-info-sharp-light.svg +1 -0
- package/lib/src/components/tooltips/tooltips.scss +51 -0
- package/lib/src/components/tooltips/tooltips.ts +103 -0
- package/lib/src/components/treeview/chevron-down-light.svg +1 -0
- package/lib/src/components/treeview/treeview.module.scss +116 -0
- package/lib/src/components/treeview/treeview.ts +403 -0
- package/lib/src/components/viewport/viewport.module.scss +25 -0
- package/lib/src/components/viewport/viewport.ts +38 -0
- package/lib/src/core/component.ts +979 -0
- package/lib/src/core/core_colors.ts +250 -0
- package/lib/src/core/core_dom.ts +471 -0
- package/lib/src/core/core_dragdrop.ts +201 -0
- package/lib/src/core/core_element.ts +98 -0
- package/lib/src/core/core_events.ts +149 -0
- package/lib/src/core/core_i18n.ts +377 -0
- package/lib/src/core/core_router.ts +221 -0
- package/lib/src/core/core_styles.ts +215 -0
- package/lib/src/core/core_svg.ts +550 -0
- package/lib/src/core/core_tools.ts +673 -0
- package/lib/src/demo/assets/house-light.svg +1 -0
- package/lib/src/demo/assets/radio.svg +4 -0
- package/lib/src/demo/index.html +12 -0
- package/lib/src/demo/main.scss +21 -0
- package/lib/src/demo/main.tsx +323 -0
- package/lib/src/x4.scss +19 -0
- package/lib/src/x4.ts +60 -0
- package/lib/styles/x4.css +1 -0
- package/lib/types/x4.d.ts +45060 -0
- package/package.json +3 -12
- package/scripts/build.mjs +362 -0
- package/scripts/prepack.mjs +51 -0
- package/src/components/base.scss +26 -0
- package/src/components/boxes/boxes.module.scss +37 -0
- package/src/components/boxes/boxes.ts +125 -0
- package/src/components/btngroup/btngroup.module.scss +29 -0
- package/src/components/btngroup/btngroup.ts +106 -0
- package/src/components/button/button.module.scss +154 -0
- package/src/components/button/button.ts +117 -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 +326 -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/checkbox/check.svg +4 -0
- package/src/components/checkbox/checkbox.module.scss +142 -0
- package/src/components/checkbox/checkbox.ts +125 -0
- package/src/components/colorinput/colorinput.module.scss +65 -0
- package/src/components/colorinput/colorinput.ts +88 -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 +477 -0
- package/src/components/combobox/combobox.module.scss +121 -0
- package/src/components/combobox/combobox.ts +190 -0
- package/src/components/combobox/updown.svg +4 -0
- package/src/components/dialog/dialog.module.scss +71 -0
- package/src/components/dialog/dialog.ts +91 -0
- package/src/components/dialog/xmark-sharp-light.svg +1 -0
- package/src/components/form/form.module.scss +34 -0
- package/src/components/form/form.ts +36 -0
- package/src/components/header/header.module.scss +40 -0
- package/src/components/header/header.ts +124 -0
- package/src/components/icon/icon.module.scss +30 -0
- package/src/components/icon/icon.ts +134 -0
- package/src/components/image/image.module.scss +21 -0
- package/src/components/image/image.ts +67 -0
- package/src/components/input/input.module.scss +69 -0
- package/src/components/input/input.ts +274 -0
- package/src/components/label/label.module.scss +52 -0
- package/src/components/label/label.ts +55 -0
- package/src/components/listbox/listbox.module.scss +103 -0
- package/src/components/listbox/listbox.ts +427 -0
- package/src/components/menu/caret-right-solid.svg +1 -0
- package/src/components/menu/menu.module.scss +108 -0
- package/src/components/menu/menu.ts +168 -0
- package/src/components/messages/circle-exclamation.svg +1 -0
- package/src/components/messages/messages.module.scss +47 -0
- package/src/components/messages/messages.ts +64 -0
- package/src/components/normalize.scss +386 -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 +82 -0
- package/src/components/notification/notification.ts +108 -0
- package/src/components/notification/xmark-sharp-light.svg +1 -0
- package/src/components/panel/panel.module.scss +48 -0
- package/src/components/panel/panel.ts +57 -0
- package/src/components/popup/popup.module.scss +43 -0
- package/src/components/popup/popup.ts +395 -0
- package/src/components/progress/progress.module.scss +57 -0
- package/src/components/progress/progress.ts +43 -0
- package/src/components/rating/rating.module.scss +23 -0
- package/src/components/rating/rating.ts +125 -0
- package/src/components/rating/star-sharp-light.svg +1 -0
- package/src/components/rating/star-sharp-solid.svg +1 -0
- package/src/components/shared.scss +76 -0
- package/src/components/sizers/sizer.module.scss +90 -0
- package/src/components/sizers/sizer.ts +120 -0
- package/src/components/slider/slider.module.scss +71 -0
- package/src/components/slider/slider.ts +143 -0
- package/src/components/switch/switch.module.scss +127 -0
- package/src/components/switch/switch.ts +56 -0
- package/src/components/tabs/tabs.module.scss +46 -0
- package/src/components/tabs/tabs.ts +157 -0
- package/src/components/textarea/textarea.module.scss +59 -0
- package/src/components/textarea/textarea.ts +54 -0
- package/src/components/textedit/textedit.module.scss +114 -0
- package/src/components/textedit/textedit.ts +82 -0
- package/src/components/themes.scss +77 -0
- package/src/components/tooltips/circle-info-sharp-light.svg +1 -0
- package/src/components/tooltips/tooltips.scss +51 -0
- package/src/components/tooltips/tooltips.ts +103 -0
- package/src/components/treeview/chevron-down-light.svg +1 -0
- package/src/components/treeview/treeview.module.scss +116 -0
- package/src/components/treeview/treeview.ts +403 -0
- package/src/components/viewport/viewport.module.scss +25 -0
- package/src/components/viewport/viewport.ts +38 -0
- package/src/core/component.ts +979 -0
- package/src/core/core_colors.ts +250 -0
- package/src/core/core_dom.ts +471 -0
- package/src/core/core_dragdrop.ts +201 -0
- package/src/core/core_element.ts +98 -0
- package/src/core/core_events.ts +149 -0
- package/src/core/core_i18n.ts +377 -0
- package/src/core/core_router.ts +221 -0
- package/src/core/core_styles.ts +215 -0
- package/src/core/core_svg.ts +550 -0
- package/src/core/core_tools.ts +673 -0
- package/src/demo/assets/house-light.svg +1 -0
- package/src/demo/assets/radio.svg +4 -0
- package/src/demo/index.html +12 -0
- package/src/demo/main.scss +21 -0
- package/src/demo/main.tsx +323 -0
- package/src/x4.scss +19 -0
- package/src/x4.ts +60 -0
- package/tsconfig.json +14 -0
- package/types/scss.d.ts +4 -0
- package/types/svg.d.ts +4 -0
- package/types/x4react.d.ts +9 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ___ ___ __
|
|
3
|
+
* \ \/ / / _
|
|
4
|
+
* \ / /_| |_
|
|
5
|
+
* / \____ _|
|
|
6
|
+
* /__/\__\ |_|
|
|
7
|
+
*
|
|
8
|
+
* @file core_colors.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 { clamp, isString } from './core_tools';
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
function hx( v: number ) {
|
|
21
|
+
const hex = v.toString( 16 );
|
|
22
|
+
return hex.padStart( 2, '0' );
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function round( v: number ) {
|
|
26
|
+
return Math.round(v);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
export interface Rgb {
|
|
31
|
+
red: number;
|
|
32
|
+
green: number;
|
|
33
|
+
blue: number;
|
|
34
|
+
alpha: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface Hsv {
|
|
38
|
+
hue: number;
|
|
39
|
+
saturation: number;
|
|
40
|
+
value: number;
|
|
41
|
+
alpha: number;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
export class Color {
|
|
46
|
+
|
|
47
|
+
private rgb: [red:number,green:number,blue:number,alpha:number] = [0,0,0,1];
|
|
48
|
+
private invalid = false;
|
|
49
|
+
|
|
50
|
+
constructor( value: string );
|
|
51
|
+
constructor( r: number, g: number, b: number, a?: number );
|
|
52
|
+
constructor( ...args: any[] ) {
|
|
53
|
+
if( isString(args[0] ) ) {
|
|
54
|
+
this.setValue( args[0] );
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
this.setRgb( args[0], args[1], args[2], args[3] );
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* accepts:
|
|
63
|
+
* #aaa
|
|
64
|
+
* #ababab
|
|
65
|
+
* #ababab55
|
|
66
|
+
* rgb(a,b,c)
|
|
67
|
+
* rgba(a,b,c,d)
|
|
68
|
+
* var( --color-5 )
|
|
69
|
+
*/
|
|
70
|
+
|
|
71
|
+
setValue( value: string ): this {
|
|
72
|
+
|
|
73
|
+
this.invalid = false;
|
|
74
|
+
|
|
75
|
+
if( value.length==4 && /#[0-9a-fA-F]{3}/.test(value) ) {
|
|
76
|
+
const r1 = parseInt( value[1], 16 );
|
|
77
|
+
const g1 = parseInt( value[2], 16 );
|
|
78
|
+
const b1 = parseInt( value[3], 16 );
|
|
79
|
+
return this.setRgb( r1<<4|r1, g1<<4|g1, b1<<4|b1, 1.0 );
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if( value.length==7 && /#[0-9a-fA-F]{6}/.test(value) ) {
|
|
83
|
+
const r1 = parseInt( value[1], 16 );
|
|
84
|
+
const r2 = parseInt( value[2], 16 );
|
|
85
|
+
const g1 = parseInt( value[3], 16 );
|
|
86
|
+
const g2 = parseInt( value[4], 16 );
|
|
87
|
+
const b1 = parseInt( value[5], 16 );
|
|
88
|
+
const b2 = parseInt( value[6], 16 );
|
|
89
|
+
return this.setRgb( r1<<4|r2, g1<<4|g2, b1<<4|b2, 1.0 );
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if( value.length==9 && /#[0-9a-fA-F]{8}/.test(value) ) {
|
|
93
|
+
const r1 = parseInt( value[1], 16 );
|
|
94
|
+
const r2 = parseInt( value[2], 16 );
|
|
95
|
+
const g1 = parseInt( value[3], 16 );
|
|
96
|
+
const g2 = parseInt( value[4], 16 );
|
|
97
|
+
const b1 = parseInt( value[5], 16 );
|
|
98
|
+
const b2 = parseInt( value[6], 16 );
|
|
99
|
+
const a1 = parseInt( value[7], 16 );
|
|
100
|
+
const a2 = parseInt( value[8], 16 );
|
|
101
|
+
return this.setRgb( r1<<4|r2, g1<<4|g2, b1<<4|b2, (a1<<4|a2) / 255.0 );
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if( value.startsWith('rgba') ) {
|
|
105
|
+
const re = /rgba\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*((\d+)|(\d*\.\d+)|(\.\d+))\s*\)/;
|
|
106
|
+
const m = re.exec( value );
|
|
107
|
+
if( m ) {
|
|
108
|
+
return this.setRgb( parseInt(m[1]), parseInt(m[2]), parseInt(m[3]), parseFloat(m[4]) );
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
else if( value.startsWith('rgb') ) {
|
|
112
|
+
const re = /rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/;
|
|
113
|
+
const m = re.exec( value );
|
|
114
|
+
if( m ) {
|
|
115
|
+
return this.setRgb( parseInt(m[1]), parseInt(m[2]), parseInt(m[3]), 1.0 );
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
else if( value.startsWith("var") ) {
|
|
119
|
+
const re = /var\s*\(([^)]*)\)/;
|
|
120
|
+
const m = re.exec( value );
|
|
121
|
+
if( m ) {
|
|
122
|
+
const expr = m[1].trim( );
|
|
123
|
+
const style = getComputedStyle( document.documentElement );
|
|
124
|
+
const value = style.getPropertyValue( expr );
|
|
125
|
+
return this.setValue( value );
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
this.invalid = true;
|
|
130
|
+
return this.setRgb(255,0,0,1);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
setHsv( h: number, s: number, v: number, a = 1.0 ): this {
|
|
134
|
+
|
|
135
|
+
let i = Math.min(5, Math.floor(h * 6)),
|
|
136
|
+
f = h * 6 - i,
|
|
137
|
+
p = v * (1 - s),
|
|
138
|
+
q = v * (1 - f * s),
|
|
139
|
+
t = v * (1 - (1 - f) * s);
|
|
140
|
+
|
|
141
|
+
let R, G, B;
|
|
142
|
+
|
|
143
|
+
switch (i) {
|
|
144
|
+
case 0:
|
|
145
|
+
R = v;
|
|
146
|
+
G = t;
|
|
147
|
+
B = p;
|
|
148
|
+
break;
|
|
149
|
+
case 1:
|
|
150
|
+
R = q;
|
|
151
|
+
G = v;
|
|
152
|
+
B = p;
|
|
153
|
+
break;
|
|
154
|
+
case 2:
|
|
155
|
+
R = p;
|
|
156
|
+
G = v;
|
|
157
|
+
B = t;
|
|
158
|
+
break;
|
|
159
|
+
case 3:
|
|
160
|
+
R = p;
|
|
161
|
+
G = q;
|
|
162
|
+
B = v;
|
|
163
|
+
break;
|
|
164
|
+
case 4:
|
|
165
|
+
R = t;
|
|
166
|
+
G = p;
|
|
167
|
+
B = v;
|
|
168
|
+
break;
|
|
169
|
+
case 5:
|
|
170
|
+
R = v;
|
|
171
|
+
G = p;
|
|
172
|
+
B = q;
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return this.setRgb( R*255, G*255, B*255, a );
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
setRgb( r: number, g: number, b: number, a: number ): this {
|
|
181
|
+
this.rgb = [clamp(r,0,255),clamp(g,0,255),clamp(b,0,255),clamp(a,0,1)];
|
|
182
|
+
return this;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
toRgbString( withAlpha?: boolean ): string {
|
|
186
|
+
const _ = this.rgb;
|
|
187
|
+
return withAlpha===false || _[3]==1 ? `rgb(${round(_[0])},${round(_[1])},${round(_[2])})` : `rgba(${round(_[0])},${round(_[1])},${round(_[2])},${_[3].toFixed(3)})`
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
toHexString( ): string {
|
|
191
|
+
const _ = this.rgb;
|
|
192
|
+
return _[3]==1 ? `#(${hx(_[0])},${hx(_[1])},${hx(_[2])})` : `rgba(${hx(_[0])},${hx(_[1])},${hx(_[2])},${hx(_[3]*255)})`
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
toRgb( ): Rgb {
|
|
196
|
+
const _ = this.rgb;
|
|
197
|
+
return { red: _[0], green: _[1], blue: _[2], alpha: _[3] };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
toHsv( ): Hsv {
|
|
201
|
+
|
|
202
|
+
let el = this.toRgb( );
|
|
203
|
+
|
|
204
|
+
el.red /= 255.0;
|
|
205
|
+
el.green /= 255.0;
|
|
206
|
+
el.blue /= 255.0;
|
|
207
|
+
|
|
208
|
+
const max = Math.max(el.red, el.green, el.blue);
|
|
209
|
+
const min = Math.min(el.red, el.green, el.blue);
|
|
210
|
+
const delta = max - min;
|
|
211
|
+
const saturation = (max === 0) ? 0 : (delta / max);
|
|
212
|
+
const value = max;
|
|
213
|
+
|
|
214
|
+
let hue;
|
|
215
|
+
|
|
216
|
+
if (delta === 0) {
|
|
217
|
+
hue = 0;
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
switch (max) {
|
|
221
|
+
case el.red:
|
|
222
|
+
hue = (el.green - el.blue) / delta / 6 + (el.green < el.blue ? 1 : 0);
|
|
223
|
+
break;
|
|
224
|
+
|
|
225
|
+
case el.green:
|
|
226
|
+
hue = (el.blue - el.red) / delta / 6 + 1 / 3;
|
|
227
|
+
break;
|
|
228
|
+
|
|
229
|
+
case el.blue:
|
|
230
|
+
hue = (el.red - el.green) / delta / 6 + 2 / 3;
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return { hue, saturation, value, alpha: el.alpha };
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
getAlpha( ) {
|
|
239
|
+
return this.rgb[3];
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
setAlpha( a: number ): this {
|
|
243
|
+
this.rgb[3] = clamp( a, 0, 1 );
|
|
244
|
+
return this;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
isInvalid( ) {
|
|
248
|
+
return this.invalid;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ___ ___ __
|
|
3
|
+
* \ \/ / / _
|
|
4
|
+
* \ / /_| |_
|
|
5
|
+
* / \____ _|
|
|
6
|
+
* /__/\__\ |_|
|
|
7
|
+
*
|
|
8
|
+
* @file core_dom.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
|
+
/** @ignore this events must be defined on domNode (do not bubble) */
|
|
18
|
+
export const unbubbleEvents = {
|
|
19
|
+
mouseleave: 1, mouseenter: 1, load: 1, unload: 1, scroll: 1, focus: 1, blur: 1, rowexit: 1, beforeunload: 1, stop: 1,
|
|
20
|
+
dragdrop: 1, dragenter: 1, dragexit: 1, draggesture: 1, dragover: 1, contextmenu: 1, created: 2, removed: 2, sizechange: 2
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type DOMEventHandler = ( ev: Event ) => void;
|
|
24
|
+
type EventEntry = Record<string,DOMEventHandler | DOMEventHandler[]>;
|
|
25
|
+
|
|
26
|
+
const event_handlers = new WeakMap<Node,EventEntry>( );
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* handle dom mutations
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
let mutObserver: MutationObserver = null;
|
|
33
|
+
|
|
34
|
+
const observeMutation = (mutations: MutationRecord[], observer: MutationObserver): void => {
|
|
35
|
+
|
|
36
|
+
const sendEvent = ( node: Node, code: "created" | "removed" ) => {
|
|
37
|
+
// console.log( "notify", node, code );
|
|
38
|
+
|
|
39
|
+
const store = event_handlers.get( node );
|
|
40
|
+
if ( store && store[code] ) {
|
|
41
|
+
node.dispatchEvent( new Event( code, {} ) );
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const notify = ( node: Node, create: boolean ) => {
|
|
46
|
+
|
|
47
|
+
if( create ) {
|
|
48
|
+
sendEvent( node, "created" );
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
for( let c=node.firstChild; c; c=c.nextSibling ) {
|
|
52
|
+
notify( c, create );
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if( !create ) {
|
|
56
|
+
sendEvent( node, "removed" );
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
for (const mutation of mutations) {
|
|
62
|
+
if( mutation.type=="childList" ) {
|
|
63
|
+
if( mutation.addedNodes ) {
|
|
64
|
+
mutation.addedNodes.forEach( node => {
|
|
65
|
+
notify( node, true );
|
|
66
|
+
} );
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if( mutation.removedNodes ) {
|
|
70
|
+
mutation.removedNodes.forEach( node => {
|
|
71
|
+
notify( node, false );
|
|
72
|
+
} );
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
*
|
|
82
|
+
*/
|
|
83
|
+
|
|
84
|
+
let sizeObserver: ResizeObserver = null;
|
|
85
|
+
|
|
86
|
+
function observeSize(entries: ResizeObserverEntry[]) {
|
|
87
|
+
entries.forEach((entry) => {
|
|
88
|
+
let dom = entry.target as HTMLElement;
|
|
89
|
+
if (dom.offsetParent !== null) {
|
|
90
|
+
dom.dispatchEvent( new Event('resized') );
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
*
|
|
97
|
+
*/
|
|
98
|
+
|
|
99
|
+
export function dispatchEvent(ev: Event) {
|
|
100
|
+
|
|
101
|
+
let target = ev.target as Node,
|
|
102
|
+
noup = (unbubbleEvents as any)[ev.type] === 2;
|
|
103
|
+
|
|
104
|
+
while (target) {
|
|
105
|
+
const store = event_handlers.get( target );
|
|
106
|
+
if ( store ) {
|
|
107
|
+
const callback = store[ev.type];
|
|
108
|
+
if( callback ) {
|
|
109
|
+
if( Array.isArray(callback) ) {
|
|
110
|
+
callback.some( c => c( ev ) );
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
callback( ev );
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (ev.stopPropagation || ev.defaultPrevented || noup) {
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
target = target.parentNode;
|
|
123
|
+
|
|
124
|
+
// no need to go above
|
|
125
|
+
if (target == document) {
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
*
|
|
133
|
+
*/
|
|
134
|
+
|
|
135
|
+
export function addEvent( node: Node, name: string, handler: DOMEventHandler, prepend = false ) {
|
|
136
|
+
|
|
137
|
+
if( name=="removed" || name=="created" ) {
|
|
138
|
+
if( !mutObserver ) {
|
|
139
|
+
mutObserver = new MutationObserver( observeMutation )
|
|
140
|
+
mutObserver.observe( document.body, {childList: true,subtree: true} );
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
else if( name=="resized" ) {
|
|
144
|
+
if (!sizeObserver) {
|
|
145
|
+
sizeObserver = new ResizeObserver( observeSize );
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
sizeObserver.observe( node as Element );
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
let store = event_handlers.get( node );
|
|
153
|
+
if( !store ) {
|
|
154
|
+
store = {}
|
|
155
|
+
event_handlers.set( node, store );
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if( !store[name] ) {
|
|
159
|
+
store[name] = handler;
|
|
160
|
+
node.addEventListener( name, dispatchEvent );
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
const entry = store[name];
|
|
164
|
+
if( Array.isArray(entry) ) {
|
|
165
|
+
entry.push( handler );
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
store[name] = [entry,handler];
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
*
|
|
175
|
+
*/
|
|
176
|
+
|
|
177
|
+
export interface GlobalDOMEvents {
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Fires when the user aborts the download.
|
|
181
|
+
* @param ev The event.
|
|
182
|
+
*/
|
|
183
|
+
|
|
184
|
+
abort?: (ev: UIEvent) => any;
|
|
185
|
+
animationcancel?: (ev: AnimationEvent) => any;
|
|
186
|
+
animationend?: (ev: AnimationEvent) => any;
|
|
187
|
+
animationiteration?: (ev: AnimationEvent) => any;
|
|
188
|
+
animationstart?: (ev: AnimationEvent) => any;
|
|
189
|
+
auxclick?: (ev: MouseEvent) => any;
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Fires when the object loses the input focus.
|
|
193
|
+
* @param ev The focus event.
|
|
194
|
+
*/
|
|
195
|
+
blur?: (ev: FocusEvent) => any;
|
|
196
|
+
cancel?: (ev: Event) => any;
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Occurs when playback is possible, but would require further buffering.
|
|
200
|
+
* @param ev The event.
|
|
201
|
+
*/
|
|
202
|
+
canplay?: (ev: Event) => any;
|
|
203
|
+
canplaythrough?: (ev: Event) => any;
|
|
204
|
+
/**
|
|
205
|
+
* Fires when the contents of the object or selection have changed.
|
|
206
|
+
* @param ev The event.
|
|
207
|
+
*/
|
|
208
|
+
change?: (ev: Event) => any;
|
|
209
|
+
/**
|
|
210
|
+
* Fires when the user clicks the left mouse button on the object
|
|
211
|
+
* @param ev The mouse event.
|
|
212
|
+
*/
|
|
213
|
+
click?: (ev: MouseEvent) => any;
|
|
214
|
+
close?: (ev: Event) => any;
|
|
215
|
+
/**
|
|
216
|
+
* Fires when the user clicks the right mouse button in the client area, opening the context menu.
|
|
217
|
+
* @param ev The mouse event.
|
|
218
|
+
*/
|
|
219
|
+
contextmenu?: (ev: MouseEvent) => any;
|
|
220
|
+
cuechange?: (ev: Event) => any;
|
|
221
|
+
/**
|
|
222
|
+
* Fires when the user double-clicks the object.
|
|
223
|
+
* @param ev The mouse event.
|
|
224
|
+
*/
|
|
225
|
+
dblclick?: (ev: MouseEvent) => any;
|
|
226
|
+
/**
|
|
227
|
+
* Fires on the source object continuously during a drag operation.
|
|
228
|
+
* @param ev The event.
|
|
229
|
+
*/
|
|
230
|
+
drag?: (ev: DragEvent) => any;
|
|
231
|
+
/**
|
|
232
|
+
* Fires on the source object when the user releases the mouse at the close of a drag operation.
|
|
233
|
+
* @param ev The event.
|
|
234
|
+
*/
|
|
235
|
+
dragend?: (ev: DragEvent) => any;
|
|
236
|
+
/**
|
|
237
|
+
* Fires on the target element when the user drags the object to a valid drop target.
|
|
238
|
+
* @param ev The drag event.
|
|
239
|
+
*/
|
|
240
|
+
dragenter?: (ev: DragEvent) => any;
|
|
241
|
+
dragexit?: (ev: Event) => any;
|
|
242
|
+
/**
|
|
243
|
+
* Fires on the target object when the user moves the mouse out of a valid drop target during a drag operation.
|
|
244
|
+
* @param ev The drag event.
|
|
245
|
+
*/
|
|
246
|
+
dragleave?: (ev: DragEvent) => any;
|
|
247
|
+
/**
|
|
248
|
+
* Fires on the target element continuously while the user drags the object over a valid drop target.
|
|
249
|
+
* @param ev The event.
|
|
250
|
+
*/
|
|
251
|
+
dragover?: (ev: DragEvent) => any;
|
|
252
|
+
/**
|
|
253
|
+
* Fires on the source object when the user starts to drag a text selection or selected object.
|
|
254
|
+
* @param ev The event.
|
|
255
|
+
*/
|
|
256
|
+
dragstart?: (ev: DragEvent) => any;
|
|
257
|
+
drop?: (ev: DragEvent) => any;
|
|
258
|
+
/**
|
|
259
|
+
* Occurs when the duration attribute is updated.
|
|
260
|
+
* @param ev The event.
|
|
261
|
+
*/
|
|
262
|
+
durationchange?: (ev: Event) => any;
|
|
263
|
+
/**
|
|
264
|
+
* Occurs when the media element is reset to its initial state.
|
|
265
|
+
* @param ev The event.
|
|
266
|
+
*/
|
|
267
|
+
emptied?: (ev: Event) => any;
|
|
268
|
+
/**
|
|
269
|
+
* Occurs when the end of playback is reached.
|
|
270
|
+
* @param ev The event
|
|
271
|
+
*/
|
|
272
|
+
ended?: (ev: Event) => any;
|
|
273
|
+
/**
|
|
274
|
+
* Fires when an error occurs during object loading.
|
|
275
|
+
* @param ev The event.
|
|
276
|
+
*/
|
|
277
|
+
error?: OnErrorEventHandler;
|
|
278
|
+
/**
|
|
279
|
+
* Fires when the object receives focus.
|
|
280
|
+
* @param ev The event.
|
|
281
|
+
*/
|
|
282
|
+
focusin?: (ev: FocusEvent) => any;
|
|
283
|
+
focusout?: (ev: FocusEvent) => any;
|
|
284
|
+
focus?: (ev: FocusEvent) => any;
|
|
285
|
+
gotpointercapture?: (ev: PointerEvent) => any;
|
|
286
|
+
input?: (ev: Event) => any;
|
|
287
|
+
invalid?: (ev: Event) => any;
|
|
288
|
+
/**
|
|
289
|
+
* Fires when the user presses a key.
|
|
290
|
+
* @param ev The keyboard event
|
|
291
|
+
*/
|
|
292
|
+
keydown?: (ev: KeyboardEvent) => any;
|
|
293
|
+
/**
|
|
294
|
+
* Fires when the user presses an alphanumeric key.
|
|
295
|
+
* @param ev The event.
|
|
296
|
+
*/
|
|
297
|
+
keypress?: (ev: KeyboardEvent) => any;
|
|
298
|
+
/**
|
|
299
|
+
* Fires when the user releases a key.
|
|
300
|
+
* @param ev The keyboard event
|
|
301
|
+
*/
|
|
302
|
+
keyup?: (ev: KeyboardEvent) => any;
|
|
303
|
+
/**
|
|
304
|
+
* Fires immediately after the browser loads the object.
|
|
305
|
+
* @param ev The event.
|
|
306
|
+
*/
|
|
307
|
+
load?: (ev: Event) => any;
|
|
308
|
+
/**
|
|
309
|
+
* Occurs when media data is loaded at the current playback position.
|
|
310
|
+
* @param ev The event.
|
|
311
|
+
*/
|
|
312
|
+
loadeddata?: (ev: Event) => any;
|
|
313
|
+
/**
|
|
314
|
+
* Occurs when the duration and dimensions of the media have been determined.
|
|
315
|
+
* @param ev The event.
|
|
316
|
+
*/
|
|
317
|
+
loadedmetadata?: (ev: Event) => any;
|
|
318
|
+
/**
|
|
319
|
+
* Occurs when Internet Explorer begins looking for media data.
|
|
320
|
+
* @param ev The event.
|
|
321
|
+
*/
|
|
322
|
+
loadstart?: (ev: Event) => any;
|
|
323
|
+
lostpointercapture?: (ev: PointerEvent) => any;
|
|
324
|
+
/**
|
|
325
|
+
* Fires when the user clicks the object with either mouse button.
|
|
326
|
+
* @param ev The mouse event.
|
|
327
|
+
*/
|
|
328
|
+
mousedown?: (ev: MouseEvent) => any;
|
|
329
|
+
mouseenter?: (ev: MouseEvent) => any;
|
|
330
|
+
mouseleave?: (ev: MouseEvent) => any;
|
|
331
|
+
/**
|
|
332
|
+
* Fires when the user moves the mouse over the object.
|
|
333
|
+
* @param ev The mouse event.
|
|
334
|
+
*/
|
|
335
|
+
mousemove?: (ev: MouseEvent) => any;
|
|
336
|
+
/**
|
|
337
|
+
* Fires when the user moves the mouse pointer outside the boundaries of the object.
|
|
338
|
+
* @param ev The mouse event.
|
|
339
|
+
*/
|
|
340
|
+
mouseout?: (ev: MouseEvent) => any;
|
|
341
|
+
/**
|
|
342
|
+
* Fires when the user moves the mouse pointer into the object.
|
|
343
|
+
* @param ev The mouse event.
|
|
344
|
+
*/
|
|
345
|
+
mouseover?: (ev: MouseEvent) => any;
|
|
346
|
+
/**
|
|
347
|
+
* Fires when the user releases a mouse button while the mouse is over the object.
|
|
348
|
+
* @param ev The mouse event.
|
|
349
|
+
*/
|
|
350
|
+
mouseup?: (ev: MouseEvent) => any;
|
|
351
|
+
/**
|
|
352
|
+
* Occurs when playback is paused.
|
|
353
|
+
* @param ev The event.
|
|
354
|
+
*/
|
|
355
|
+
pause?: (ev: Event) => any;
|
|
356
|
+
/**
|
|
357
|
+
* Occurs when the play method is requested.
|
|
358
|
+
* @param ev The event.
|
|
359
|
+
*/
|
|
360
|
+
play?: (ev: Event) => any;
|
|
361
|
+
/**
|
|
362
|
+
* Occurs when the audio or video has started playing.
|
|
363
|
+
* @param ev The event.
|
|
364
|
+
*/
|
|
365
|
+
playing?: (ev: Event) => any;
|
|
366
|
+
pointercancel?: (ev: PointerEvent) => any;
|
|
367
|
+
pointerdown?: (ev: PointerEvent) => any;
|
|
368
|
+
pointerenter?: (ev: PointerEvent) => any;
|
|
369
|
+
pointerleave?: (ev: PointerEvent) => any;
|
|
370
|
+
pointermove?: (ev: PointerEvent) => any;
|
|
371
|
+
pointerout?: (ev: PointerEvent) => any;
|
|
372
|
+
pointerover?: (ev: PointerEvent) => any;
|
|
373
|
+
pointerup?: (ev: PointerEvent) => any;
|
|
374
|
+
/**
|
|
375
|
+
* Occurs to indicate progress while downloading media data.
|
|
376
|
+
* @param ev The event.
|
|
377
|
+
*/
|
|
378
|
+
progress?: (ev: ProgressEvent) => any;
|
|
379
|
+
/**
|
|
380
|
+
* Occurs when the playback rate is increased or decreased.
|
|
381
|
+
* @param ev The event.
|
|
382
|
+
*/
|
|
383
|
+
ratechange?: (ev: Event) => any;
|
|
384
|
+
/**
|
|
385
|
+
* Fires when the user resets a form.
|
|
386
|
+
* @param ev The event.
|
|
387
|
+
*/
|
|
388
|
+
reset?: (ev: Event) => any;
|
|
389
|
+
//resize?: (ev: UIEvent) => any; remove to avoid errors with sizechange event
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Fires when the user repositions the scroll box in the scroll bar on the object.
|
|
393
|
+
* @param ev The event.
|
|
394
|
+
*/
|
|
395
|
+
scroll?: (ev: Event) => any;
|
|
396
|
+
securitypolicyviolation?: (ev: SecurityPolicyViolationEvent) => any;
|
|
397
|
+
/**
|
|
398
|
+
* Occurs when the seek operation ends.
|
|
399
|
+
* @param ev The event.
|
|
400
|
+
*/
|
|
401
|
+
seeked?: (ev: Event) => any;
|
|
402
|
+
/**
|
|
403
|
+
* Occurs when the current playback position is moved.
|
|
404
|
+
* @param ev The event.
|
|
405
|
+
*/
|
|
406
|
+
seeking?: (ev: Event) => any;
|
|
407
|
+
/**
|
|
408
|
+
* Fires when the current selection changes.
|
|
409
|
+
* @param ev The event.
|
|
410
|
+
*/
|
|
411
|
+
select?: (ev: Event) => any;
|
|
412
|
+
selectionchange?: (ev: Event) => any;
|
|
413
|
+
selectstart?: (ev: Event) => any;
|
|
414
|
+
/**
|
|
415
|
+
* Occurs when the download has stopped.
|
|
416
|
+
* @param ev The event.
|
|
417
|
+
*/
|
|
418
|
+
stalled?: (ev: Event) => any;
|
|
419
|
+
submit?: (ev: Event) => any;
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Occurs if the load operation has been intentionally halted.
|
|
423
|
+
* @param ev The event.
|
|
424
|
+
*/
|
|
425
|
+
suspend?: (ev: Event) => any;
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Occurs to indicate the current playback position.
|
|
429
|
+
* @param ev The event.
|
|
430
|
+
*/
|
|
431
|
+
timeupdate?: (ev: Event) => any;
|
|
432
|
+
toggle?: (ev: Event) => any;
|
|
433
|
+
touchcancel?: (ev: TouchEvent) => any;
|
|
434
|
+
touchend?: (ev: TouchEvent) => any;
|
|
435
|
+
touchmove?: (ev: TouchEvent) => any;
|
|
436
|
+
touchstart?: (ev: TouchEvent) => any;
|
|
437
|
+
transitioncancel?: (ev: TransitionEvent) => any;
|
|
438
|
+
transitionend?: (ev: TransitionEvent) => any;
|
|
439
|
+
transitionrun?: (ev: TransitionEvent) => any;
|
|
440
|
+
transitionstart?: (ev: TransitionEvent) => any;
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Occurs when the volume is changed, or playback is muted or unmuted.
|
|
444
|
+
* @param ev The event.
|
|
445
|
+
*/
|
|
446
|
+
volumechange?: (ev: Event) => any;
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Occurs when playback stops because the next frame of a video resource is not available.
|
|
450
|
+
* @param ev The event.
|
|
451
|
+
*/
|
|
452
|
+
waiting?: (ev: Event) => any;
|
|
453
|
+
wheel?: (ev: WheelEvent) => any;
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* custom x4 events
|
|
457
|
+
*/
|
|
458
|
+
|
|
459
|
+
resized?: (ev: Event) => void; // occurs when size changed
|
|
460
|
+
created?: ( ev: Event ) => void; // occurs when inserted in the dom
|
|
461
|
+
removed?: ( ev: Event ) => void; // occurs when removed from dom
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
|