flinker-dom 0.0.1
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/LICENSE.md +19 -0
- package/README.md +7 -0
- package/dist/esm/components.js +675 -0
- package/dist/esm/core.js +190 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/processor.js +175 -0
- package/dist/types/components.d.ts +155 -0
- package/dist/types/core.d.ts +91 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/processor.d.ts +22 -0
- package/package.json +49 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,675 @@
|
|
|
1
|
+
import { RXObservableValue } from 'flinker';
|
|
2
|
+
import { buildClassName } from './core';
|
|
3
|
+
export class UIComponent {
|
|
4
|
+
constructor(tag) {
|
|
5
|
+
this.unsubscribeColl = [];
|
|
6
|
+
this.reactions = [];
|
|
7
|
+
/*
|
|
8
|
+
*
|
|
9
|
+
* Rendering
|
|
10
|
+
*
|
|
11
|
+
*/
|
|
12
|
+
this.willDomUpdate = false;
|
|
13
|
+
this.affectSet = new Set();
|
|
14
|
+
this.props = {};
|
|
15
|
+
/*
|
|
16
|
+
*
|
|
17
|
+
* Destroing
|
|
18
|
+
*
|
|
19
|
+
*/
|
|
20
|
+
this.isDestroyed = false;
|
|
21
|
+
this.tag = tag;
|
|
22
|
+
this.dom = document.createElement(tag);
|
|
23
|
+
UIComponent.activeParent?.addChild(this);
|
|
24
|
+
}
|
|
25
|
+
observe(rx, affect1 = 'affectsProps', affect2) {
|
|
26
|
+
this.unsubscribeColl.push(rx.pipe()
|
|
27
|
+
.onReceive(() => this.render(affect1, affect2))
|
|
28
|
+
.subscribe());
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
propsDidChange(fn) {
|
|
32
|
+
if (this.onPropsChangeSubscribers)
|
|
33
|
+
this.onPropsChangeSubscribers.push(fn);
|
|
34
|
+
else
|
|
35
|
+
this.onPropsChangeSubscribers = [fn];
|
|
36
|
+
!this.willDomUpdate && fn(this.props);
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
map(fn) {
|
|
40
|
+
if (this.mapStateFn !== fn) {
|
|
41
|
+
this.mapStateFn = fn;
|
|
42
|
+
this.render('affectsProps');
|
|
43
|
+
}
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
react(fn) {
|
|
47
|
+
this.reactions.push(fn);
|
|
48
|
+
this.render('affectsProps');
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
whenHovered(fn) {
|
|
52
|
+
this.whenHoveredFn = fn;
|
|
53
|
+
this.render('affectsProps');
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
children(fn) {
|
|
57
|
+
if (this.instantiateChildrenFn !== fn) {
|
|
58
|
+
this.instantiateChildrenFn = fn;
|
|
59
|
+
this.render('recreateChildren');
|
|
60
|
+
}
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
addChild(c) {
|
|
64
|
+
if (!this.childrenColl)
|
|
65
|
+
this.childrenColl = [];
|
|
66
|
+
this.childrenColl.push(c);
|
|
67
|
+
this.dom.appendChild(c.dom);
|
|
68
|
+
}
|
|
69
|
+
render(affect1, affect2) {
|
|
70
|
+
this.affectSet.add(affect1);
|
|
71
|
+
affect2 && this.affectSet.add(affect2);
|
|
72
|
+
if (!this.willDomUpdate) {
|
|
73
|
+
this.willDomUpdate = true;
|
|
74
|
+
setTimeout(() => this.updateDom());
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
updateDom() {
|
|
78
|
+
if (this.isDestroyed)
|
|
79
|
+
return;
|
|
80
|
+
if (!this.willDomUpdate)
|
|
81
|
+
return;
|
|
82
|
+
this.willDomUpdate = false;
|
|
83
|
+
if (this.affectSet.has('affectsProps')) {
|
|
84
|
+
this.updateProps();
|
|
85
|
+
if (this.props.visible === false) {
|
|
86
|
+
this.dom.hidden = true;
|
|
87
|
+
this.dom.className = '';
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
const cn = buildClassName(this.props, 'none');
|
|
91
|
+
this.dom.className = cn;
|
|
92
|
+
this.dom.hidden = false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (this.affectSet.has('recreateChildren')) {
|
|
96
|
+
if (this.childrenColl) {
|
|
97
|
+
this.childrenColl?.forEach(c => c.destroy());
|
|
98
|
+
this.childrenColl = undefined;
|
|
99
|
+
}
|
|
100
|
+
if (this.instantiateChildrenFn !== undefined) {
|
|
101
|
+
const p = UIComponent.activeParent;
|
|
102
|
+
UIComponent.activeParent = this;
|
|
103
|
+
this.instantiateChildrenFn();
|
|
104
|
+
UIComponent.activeParent = p;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (this.affectSet.has('affectsChildrenProps')) {
|
|
108
|
+
this.childrenColl?.forEach(c => c.render('affectsChildrenProps'));
|
|
109
|
+
}
|
|
110
|
+
if (this.affectSet.size > 0) {
|
|
111
|
+
this.didDomUpdate();
|
|
112
|
+
this.affectSet.clear();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
updateProps() {
|
|
116
|
+
const res = {};
|
|
117
|
+
this.reactions.forEach(fn => fn(res));
|
|
118
|
+
if (this.whenHoveredFn) {
|
|
119
|
+
if (!res.pseudo)
|
|
120
|
+
res.pseudo = {};
|
|
121
|
+
const state = {};
|
|
122
|
+
this.whenHoveredFn(state);
|
|
123
|
+
res.pseudo['hover'] = state;
|
|
124
|
+
}
|
|
125
|
+
if (this.mapStateFn)
|
|
126
|
+
this.mapStateFn(res);
|
|
127
|
+
if (this.props.visible === false)
|
|
128
|
+
this.props = {};
|
|
129
|
+
this.props = res;
|
|
130
|
+
if (this.onPropsChangeSubscribers)
|
|
131
|
+
this.onPropsChangeSubscribers.forEach(fn => fn(this.props));
|
|
132
|
+
}
|
|
133
|
+
didDomUpdate() {
|
|
134
|
+
if (this.props.id)
|
|
135
|
+
this.dom.id = this.props.id;
|
|
136
|
+
if (this.props.popUp)
|
|
137
|
+
this.dom.title = this.props.popUp;
|
|
138
|
+
}
|
|
139
|
+
destroy() {
|
|
140
|
+
if (!this.isDestroyed) {
|
|
141
|
+
this.isDestroyed = true;
|
|
142
|
+
this.unsubscribeColl.forEach(f => f());
|
|
143
|
+
this.unsubscribeColl.length = 0;
|
|
144
|
+
if (this.childrenColl) {
|
|
145
|
+
this.childrenColl.forEach(c => c.destroy());
|
|
146
|
+
this.childrenColl = undefined;
|
|
147
|
+
}
|
|
148
|
+
this.dom.remove();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/*
|
|
152
|
+
*
|
|
153
|
+
* Event handlers
|
|
154
|
+
*
|
|
155
|
+
*/
|
|
156
|
+
onClick(callback) {
|
|
157
|
+
this.dom.addEventListener('click', callback);
|
|
158
|
+
return this;
|
|
159
|
+
}
|
|
160
|
+
onDoubleClick(callback) {
|
|
161
|
+
this.dom.addEventListener('dblclick', callback);
|
|
162
|
+
return this;
|
|
163
|
+
}
|
|
164
|
+
onMouseDown(callback) {
|
|
165
|
+
this.dom.addEventListener('mousedown', callback);
|
|
166
|
+
return this;
|
|
167
|
+
}
|
|
168
|
+
onKeyUp(callback) {
|
|
169
|
+
this.dom.addEventListener('keyup', callback);
|
|
170
|
+
return this;
|
|
171
|
+
}
|
|
172
|
+
onKeyDown(callback) {
|
|
173
|
+
this.dom.addEventListener('keydown', callback);
|
|
174
|
+
return this;
|
|
175
|
+
}
|
|
176
|
+
onBlur(callback) {
|
|
177
|
+
this.dom.addEventListener('blur', callback);
|
|
178
|
+
return this;
|
|
179
|
+
}
|
|
180
|
+
onFocus(callback) {
|
|
181
|
+
this.dom.addEventListener('focus', callback);
|
|
182
|
+
return this;
|
|
183
|
+
}
|
|
184
|
+
onInput(callback) {
|
|
185
|
+
this.dom.addEventListener('input', callback);
|
|
186
|
+
return this;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
export const vstackMapper = (s) => {
|
|
190
|
+
const halign = s.halign ?? 'left';
|
|
191
|
+
const valign = s.valign ?? 'top';
|
|
192
|
+
switch (halign) {
|
|
193
|
+
case 'left':
|
|
194
|
+
s.alignItems = 'flex-start';
|
|
195
|
+
break;
|
|
196
|
+
case 'center':
|
|
197
|
+
s.alignItems = 'center';
|
|
198
|
+
break;
|
|
199
|
+
case 'right':
|
|
200
|
+
s.alignItems = 'flex-end';
|
|
201
|
+
break;
|
|
202
|
+
case 'stretch':
|
|
203
|
+
s.alignItems = 'stretch';
|
|
204
|
+
break;
|
|
205
|
+
default:
|
|
206
|
+
s.alignItems = 'flex-start';
|
|
207
|
+
}
|
|
208
|
+
switch (valign) {
|
|
209
|
+
case 'top':
|
|
210
|
+
s.justifyContent = 'flex-start';
|
|
211
|
+
break;
|
|
212
|
+
case 'center':
|
|
213
|
+
s.justifyContent = 'center';
|
|
214
|
+
break;
|
|
215
|
+
case 'base':
|
|
216
|
+
s.alignItems = 'baseline';
|
|
217
|
+
break;
|
|
218
|
+
case 'bottom':
|
|
219
|
+
s.justifyContent = 'flex-end';
|
|
220
|
+
break;
|
|
221
|
+
case 'stretch':
|
|
222
|
+
s.justifyContent = 'space-between';
|
|
223
|
+
break;
|
|
224
|
+
default:
|
|
225
|
+
s.alignItems = 'flex-start';
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
export const vstack = () => {
|
|
229
|
+
return new UIComponent('div')
|
|
230
|
+
.react(s => {
|
|
231
|
+
s.display = 'flex';
|
|
232
|
+
s.flexDirection = 'column';
|
|
233
|
+
s.alignItems = 'flex-start';
|
|
234
|
+
s.justifyContent = 'center';
|
|
235
|
+
s.width = '100%';
|
|
236
|
+
s.gap = '10px';
|
|
237
|
+
s.boxSizing = 'border-box';
|
|
238
|
+
})
|
|
239
|
+
.map(vstackMapper);
|
|
240
|
+
};
|
|
241
|
+
export const hstackMapper = (s) => {
|
|
242
|
+
const halign = s.halign ?? 'left';
|
|
243
|
+
const valign = s.valign ?? 'top';
|
|
244
|
+
switch (halign) {
|
|
245
|
+
case 'left':
|
|
246
|
+
s.justifyContent = 'flex-start';
|
|
247
|
+
break;
|
|
248
|
+
case 'center':
|
|
249
|
+
s.justifyContent = 'center';
|
|
250
|
+
break;
|
|
251
|
+
case 'right':
|
|
252
|
+
s.justifyContent = 'flex-end';
|
|
253
|
+
break;
|
|
254
|
+
case 'stretch':
|
|
255
|
+
s.justifyContent = 'space-between';
|
|
256
|
+
break;
|
|
257
|
+
default:
|
|
258
|
+
s.alignItems = 'flex-start';
|
|
259
|
+
}
|
|
260
|
+
switch (valign) {
|
|
261
|
+
case 'top':
|
|
262
|
+
s.alignItems = 'flex-start';
|
|
263
|
+
break;
|
|
264
|
+
case 'center':
|
|
265
|
+
s.alignItems = 'center';
|
|
266
|
+
break;
|
|
267
|
+
case 'base':
|
|
268
|
+
s.alignItems = 'baseline';
|
|
269
|
+
break;
|
|
270
|
+
case 'bottom':
|
|
271
|
+
s.alignItems = 'flex-end';
|
|
272
|
+
break;
|
|
273
|
+
case 'stretch':
|
|
274
|
+
s.alignItems = 'stretch';
|
|
275
|
+
break;
|
|
276
|
+
default:
|
|
277
|
+
s.alignItems = 'flex-start';
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
export const hstack = () => {
|
|
281
|
+
return new UIComponent('div')
|
|
282
|
+
.react(s => {
|
|
283
|
+
s.display = 'flex';
|
|
284
|
+
s.flexDirection = 'row';
|
|
285
|
+
s.alignItems = 'flex-start';
|
|
286
|
+
s.justifyContent = 'center';
|
|
287
|
+
s.wrap = false;
|
|
288
|
+
s.boxSizing = 'border-box';
|
|
289
|
+
})
|
|
290
|
+
.map(hstackMapper);
|
|
291
|
+
};
|
|
292
|
+
export class Text extends UIComponent {
|
|
293
|
+
didDomUpdate() {
|
|
294
|
+
super.didDomUpdate();
|
|
295
|
+
if (!this.childrenColl)
|
|
296
|
+
this.dom.textContent = this.props.text ?? null;
|
|
297
|
+
if (this.props.htmlText !== undefined)
|
|
298
|
+
this.dom.setHTMLUnsafe(this.props.htmlText);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
export const div = () => {
|
|
302
|
+
return new Text('div');
|
|
303
|
+
};
|
|
304
|
+
export const p = () => {
|
|
305
|
+
return new Text('p');
|
|
306
|
+
};
|
|
307
|
+
export const span = () => {
|
|
308
|
+
return new Text('span');
|
|
309
|
+
};
|
|
310
|
+
export const h1 = () => {
|
|
311
|
+
return new Text('h1');
|
|
312
|
+
};
|
|
313
|
+
export const h2 = () => {
|
|
314
|
+
return new Text('h2');
|
|
315
|
+
};
|
|
316
|
+
export const h3 = () => {
|
|
317
|
+
return new Text('h3');
|
|
318
|
+
};
|
|
319
|
+
export const h4 = () => {
|
|
320
|
+
return new Text('h4');
|
|
321
|
+
};
|
|
322
|
+
export const h5 = () => {
|
|
323
|
+
return new Text('h5');
|
|
324
|
+
};
|
|
325
|
+
export const h6 = () => {
|
|
326
|
+
return new Text('h6');
|
|
327
|
+
};
|
|
328
|
+
export class Button extends Text {
|
|
329
|
+
whenSelected(fn) {
|
|
330
|
+
this.whenSelectedFn = fn;
|
|
331
|
+
this.render('affectsProps');
|
|
332
|
+
return this;
|
|
333
|
+
}
|
|
334
|
+
whenDisabled(fn) {
|
|
335
|
+
this.whenDisabledFn = fn;
|
|
336
|
+
this.render('affectsProps');
|
|
337
|
+
return this;
|
|
338
|
+
}
|
|
339
|
+
updateProps() {
|
|
340
|
+
const res = {};
|
|
341
|
+
this.reactions.forEach(fn => fn(res));
|
|
342
|
+
const isSelected = res.isSelected;
|
|
343
|
+
const isDisabled = res.isDisabled;
|
|
344
|
+
if (this.whenSelectedFn && isSelected) {
|
|
345
|
+
this.whenSelectedFn(res);
|
|
346
|
+
}
|
|
347
|
+
if (this.whenDisabledFn && isDisabled) {
|
|
348
|
+
this.whenDisabledFn(res);
|
|
349
|
+
}
|
|
350
|
+
if (!isSelected && !isDisabled && this.whenHoveredFn) {
|
|
351
|
+
if (!res.pseudo)
|
|
352
|
+
res.pseudo = {};
|
|
353
|
+
const state = {};
|
|
354
|
+
this.whenHoveredFn(state);
|
|
355
|
+
res.pseudo['hover'] = state;
|
|
356
|
+
}
|
|
357
|
+
if (!this.childrenColl)
|
|
358
|
+
this.dom.textContent = this.props.text ?? null;
|
|
359
|
+
if (this.mapStateFn)
|
|
360
|
+
this.mapStateFn(res);
|
|
361
|
+
this.props = res;
|
|
362
|
+
if (this.onPropsChangeSubscribers)
|
|
363
|
+
this.onPropsChangeSubscribers.forEach(fn => fn(this.props));
|
|
364
|
+
}
|
|
365
|
+
didDomUpdate() {
|
|
366
|
+
super.didDomUpdate();
|
|
367
|
+
this.props.href && this.dom.setAttribute('href', this.props.href);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
export const btn = () => {
|
|
371
|
+
return new Button('button')
|
|
372
|
+
.react(s => {
|
|
373
|
+
s.textSelectable = false;
|
|
374
|
+
})
|
|
375
|
+
.whenDisabled(s => {
|
|
376
|
+
s.opacity = '0.5';
|
|
377
|
+
s.cursor = 'not-allowed';
|
|
378
|
+
s.mouseEnabled = false;
|
|
379
|
+
});
|
|
380
|
+
};
|
|
381
|
+
export class LinkBtn extends Button {
|
|
382
|
+
didDomUpdate() {
|
|
383
|
+
super.didDomUpdate();
|
|
384
|
+
this.props.href && this.dom.setAttribute('href', this.props.href);
|
|
385
|
+
this.props.target && this.dom.setAttribute('target', this.props.target);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
export const link = () => {
|
|
389
|
+
return new LinkBtn('a')
|
|
390
|
+
.react(s => {
|
|
391
|
+
s.cursor = 'pointer';
|
|
392
|
+
});
|
|
393
|
+
};
|
|
394
|
+
export const switcher = () => {
|
|
395
|
+
const $sharedState = new RXObservableValue({});
|
|
396
|
+
return btn()
|
|
397
|
+
.react(s => {
|
|
398
|
+
s.trackWidth = 34;
|
|
399
|
+
s.trackHeight = 22;
|
|
400
|
+
s.trackColor = '#ffFFff';
|
|
401
|
+
s.thumbColor = '#000000';
|
|
402
|
+
s.thumbDiameter = 16;
|
|
403
|
+
s.minHeight = '0px';
|
|
404
|
+
s.textSelectable = false;
|
|
405
|
+
s.cornerRadius = s.trackHeight + 'px';
|
|
406
|
+
s.animate = 'background-color 300ms';
|
|
407
|
+
})
|
|
408
|
+
.whenSelected(s => {
|
|
409
|
+
s.trackColor = '#aa0000';
|
|
410
|
+
s.thumbColor = '#ffFFff';
|
|
411
|
+
})
|
|
412
|
+
.map(s => {
|
|
413
|
+
s.bgColor = s.trackColor ?? '#ff0000';
|
|
414
|
+
s.width = s.trackWidth + 'px';
|
|
415
|
+
s.height = s.trackHeight + 'px';
|
|
416
|
+
})
|
|
417
|
+
.propsDidChange(props => $sharedState.value = props)
|
|
418
|
+
.children(() => {
|
|
419
|
+
//thumb
|
|
420
|
+
div()
|
|
421
|
+
.observe($sharedState)
|
|
422
|
+
.react(s => {
|
|
423
|
+
const ss = $sharedState.value;
|
|
424
|
+
const thumbDiameter = ss.thumbDiameter ?? 0;
|
|
425
|
+
const trackWidth = ss.trackWidth ?? 0;
|
|
426
|
+
const trackHeight = ss.trackHeight ?? 0;
|
|
427
|
+
const edgeOffset = (trackHeight - thumbDiameter) / 2;
|
|
428
|
+
s.bgColor = ss.thumbColor;
|
|
429
|
+
s.width = ss.thumbDiameter + 'px';
|
|
430
|
+
s.height = ss.thumbDiameter + 'px';
|
|
431
|
+
s.cornerRadius = ss.thumbDiameter + 'px';
|
|
432
|
+
s.animateAll = '300ms';
|
|
433
|
+
s.position = 'relative';
|
|
434
|
+
s.top = '0';
|
|
435
|
+
s.left = (ss.isSelected ? trackWidth - thumbDiameter - edgeOffset : edgeOffset) + 'px';
|
|
436
|
+
});
|
|
437
|
+
});
|
|
438
|
+
};
|
|
439
|
+
/*
|
|
440
|
+
*
|
|
441
|
+
* Observer
|
|
442
|
+
*
|
|
443
|
+
**/
|
|
444
|
+
class Observer extends UIComponent {
|
|
445
|
+
constructor() {
|
|
446
|
+
super('p');
|
|
447
|
+
this.hiddenElement = this.dom;
|
|
448
|
+
this.hiddenElement.hidden = true;
|
|
449
|
+
}
|
|
450
|
+
observe(rx) {
|
|
451
|
+
this.unsubscribeColl.push(rx.pipe()
|
|
452
|
+
.debounce(0)
|
|
453
|
+
.onReceive((v) => this.replaceDom(v))
|
|
454
|
+
.subscribe());
|
|
455
|
+
return this;
|
|
456
|
+
}
|
|
457
|
+
onReceive(fn) {
|
|
458
|
+
this.onReceiveFn = fn;
|
|
459
|
+
}
|
|
460
|
+
replaceDom(value) {
|
|
461
|
+
if (this.isDestroyed)
|
|
462
|
+
return;
|
|
463
|
+
const p = UIComponent.activeParent;
|
|
464
|
+
UIComponent.activeParent = undefined;
|
|
465
|
+
const u = this.onReceiveFn?.(value);
|
|
466
|
+
UIComponent.activeParent = p;
|
|
467
|
+
if (typeof u === 'object' && 'dom' in u) {
|
|
468
|
+
this.dom.replaceWith(u.dom);
|
|
469
|
+
this.instance?.destroy();
|
|
470
|
+
this.instance = u;
|
|
471
|
+
this.dom = u.dom;
|
|
472
|
+
}
|
|
473
|
+
else if (this.dom !== this.hiddenElement) {
|
|
474
|
+
this.dom.replaceWith(this.hiddenElement);
|
|
475
|
+
this.instance?.destroy();
|
|
476
|
+
this.dom = this.hiddenElement;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
didDomUpdate() {
|
|
480
|
+
super.didDomUpdate();
|
|
481
|
+
if (this.affectSet.has('affectsChildrenProps')) {
|
|
482
|
+
if (this.instance && !this.instance.isDestroyed)
|
|
483
|
+
this.instance.render('affectsProps', 'affectsChildrenProps');
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
destroy() {
|
|
487
|
+
super.destroy();
|
|
488
|
+
this.instance?.destroy();
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
export const observer = (rx) => {
|
|
492
|
+
const res = new Observer();
|
|
493
|
+
res.observe(rx);
|
|
494
|
+
return res;
|
|
495
|
+
};
|
|
496
|
+
/*
|
|
497
|
+
*
|
|
498
|
+
* List: div
|
|
499
|
+
*
|
|
500
|
+
**/
|
|
501
|
+
export class List extends UIComponent {
|
|
502
|
+
constructor(tag) {
|
|
503
|
+
super(tag);
|
|
504
|
+
this.itemsFn = undefined;
|
|
505
|
+
this._itemRenderer = (item) => p().react(state => state.text = item);
|
|
506
|
+
this.equalsFn = (a, b) => a === b;
|
|
507
|
+
this._items = [];
|
|
508
|
+
this.childrenColl = [];
|
|
509
|
+
}
|
|
510
|
+
items(fn) {
|
|
511
|
+
this.itemsFn = fn;
|
|
512
|
+
return this;
|
|
513
|
+
}
|
|
514
|
+
itemRenderer(fn) {
|
|
515
|
+
this._itemRenderer = fn;
|
|
516
|
+
return this;
|
|
517
|
+
}
|
|
518
|
+
equals(fn) {
|
|
519
|
+
this.equalsFn = fn;
|
|
520
|
+
return this;
|
|
521
|
+
}
|
|
522
|
+
didDomUpdate() {
|
|
523
|
+
super.didDomUpdate();
|
|
524
|
+
const actualItems = this.itemsFn ? [...this.itemsFn()] : [];
|
|
525
|
+
const actualChildren = [];
|
|
526
|
+
let index = 0;
|
|
527
|
+
if (!this.childrenColl)
|
|
528
|
+
this.childrenColl = [];
|
|
529
|
+
while (index < actualItems.length) {
|
|
530
|
+
const actualItem = actualItems[index];
|
|
531
|
+
const oldVN = this.childrenColl[index];
|
|
532
|
+
if (index < this._items.length) {
|
|
533
|
+
if (this.equalsFn(this._items[index], actualItem)) {
|
|
534
|
+
actualChildren.push(oldVN);
|
|
535
|
+
}
|
|
536
|
+
else {
|
|
537
|
+
const newVN = this._itemRenderer(actualItem, index);
|
|
538
|
+
oldVN.dom.replaceWith(newVN.dom);
|
|
539
|
+
oldVN.destroy();
|
|
540
|
+
actualChildren.push(newVN);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
else {
|
|
544
|
+
const newVN = this._itemRenderer(actualItem, index);
|
|
545
|
+
this.dom.appendChild(newVN.dom);
|
|
546
|
+
actualChildren.push(newVN);
|
|
547
|
+
}
|
|
548
|
+
index++;
|
|
549
|
+
}
|
|
550
|
+
if (index < this.childrenColl.length) {
|
|
551
|
+
this.childrenColl.slice(index).forEach(n => n.destroy());
|
|
552
|
+
}
|
|
553
|
+
this.childrenColl = actualChildren;
|
|
554
|
+
this._items = actualItems;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
export const vlist = () => {
|
|
558
|
+
return new List('div')
|
|
559
|
+
.react(s => {
|
|
560
|
+
s.display = 'flex';
|
|
561
|
+
s.flexDirection = 'column';
|
|
562
|
+
s.alignItems = 'flex-start';
|
|
563
|
+
s.justifyContent = 'center';
|
|
564
|
+
s.width = '100%';
|
|
565
|
+
s.gap = '10px';
|
|
566
|
+
s.boxSizing = 'border-box';
|
|
567
|
+
})
|
|
568
|
+
.map(vstackMapper);
|
|
569
|
+
};
|
|
570
|
+
export const hlist = () => {
|
|
571
|
+
return new List('div')
|
|
572
|
+
.react(s => {
|
|
573
|
+
s.display = 'flex';
|
|
574
|
+
s.flexDirection = 'row';
|
|
575
|
+
s.alignItems = 'flex-start';
|
|
576
|
+
s.justifyContent = 'center';
|
|
577
|
+
s.wrap = false;
|
|
578
|
+
s.boxSizing = 'border-box';
|
|
579
|
+
})
|
|
580
|
+
.map(hstackMapper);
|
|
581
|
+
};
|
|
582
|
+
export const spacer = () => {
|
|
583
|
+
return new UIComponent('div')
|
|
584
|
+
.react(s => {
|
|
585
|
+
s.flexGrow = 1;
|
|
586
|
+
})
|
|
587
|
+
.map(s => {
|
|
588
|
+
if (s.width?.includes('px')) {
|
|
589
|
+
s.minWidth = s.width;
|
|
590
|
+
s.maxWidth = s.width;
|
|
591
|
+
}
|
|
592
|
+
if (s.height?.includes('px')) {
|
|
593
|
+
s.minHeight = s.height;
|
|
594
|
+
s.maxHeight = s.height;
|
|
595
|
+
}
|
|
596
|
+
});
|
|
597
|
+
};
|
|
598
|
+
export class Image extends Button {
|
|
599
|
+
didDomUpdate() {
|
|
600
|
+
super.didDomUpdate();
|
|
601
|
+
this.props.src && this.dom.setAttribute('src', this.props.src);
|
|
602
|
+
this.props.alt && this.dom.setAttribute('alt', this.props.alt);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
export const image = () => {
|
|
606
|
+
return new Image('img');
|
|
607
|
+
};
|
|
608
|
+
export class Input extends UIComponent {
|
|
609
|
+
bind(rx) {
|
|
610
|
+
this.unsubscribeColl.push(rx.pipe()
|
|
611
|
+
.onReceive(v => this.dom.value = v)
|
|
612
|
+
.subscribe());
|
|
613
|
+
this.onInput((e) => rx.value = e.target.value);
|
|
614
|
+
return this;
|
|
615
|
+
}
|
|
616
|
+
whenFocused(fn) {
|
|
617
|
+
this.whenFocusedFn = fn;
|
|
618
|
+
this.render('affectsProps');
|
|
619
|
+
return this;
|
|
620
|
+
}
|
|
621
|
+
whenPlaceholderShown(fn) {
|
|
622
|
+
this.whenPlaceholderShownFn = fn;
|
|
623
|
+
this.render('affectsProps');
|
|
624
|
+
return this;
|
|
625
|
+
}
|
|
626
|
+
updateProps() {
|
|
627
|
+
const res = {};
|
|
628
|
+
this.reactions.forEach(fn => fn(res));
|
|
629
|
+
if (this.whenHoveredFn) {
|
|
630
|
+
if (!res.pseudo)
|
|
631
|
+
res.pseudo = {};
|
|
632
|
+
const state = {};
|
|
633
|
+
this.whenHoveredFn(state);
|
|
634
|
+
res.pseudo['hover'] = state;
|
|
635
|
+
}
|
|
636
|
+
if (this.whenFocusedFn) {
|
|
637
|
+
if (!res.pseudo)
|
|
638
|
+
res.pseudo = {};
|
|
639
|
+
const state = {};
|
|
640
|
+
this.whenFocusedFn(state);
|
|
641
|
+
res.pseudo['focus'] = state;
|
|
642
|
+
}
|
|
643
|
+
if (this.whenPlaceholderShownFn) {
|
|
644
|
+
if (!res.pseudo)
|
|
645
|
+
res.pseudo = {};
|
|
646
|
+
const state = {};
|
|
647
|
+
this.whenPlaceholderShownFn(state);
|
|
648
|
+
res.pseudo['placeholder'] = state;
|
|
649
|
+
}
|
|
650
|
+
if (this.mapStateFn)
|
|
651
|
+
this.mapStateFn(res);
|
|
652
|
+
this.props = res;
|
|
653
|
+
if (this.onPropsChangeSubscribers)
|
|
654
|
+
this.onPropsChangeSubscribers.forEach(fn => fn(this.props));
|
|
655
|
+
}
|
|
656
|
+
didDomUpdate() {
|
|
657
|
+
super.didDomUpdate();
|
|
658
|
+
this.props.placeholder && this.dom.setAttribute('placeholder', this.props.placeholder);
|
|
659
|
+
this.props.autoCorrect && this.dom.setAttribute('autoCorrect', this.props.autoCorrect);
|
|
660
|
+
this.props.autoComplete && this.dom.setAttribute('autoComplete', this.props.autoComplete);
|
|
661
|
+
this.props.type && this.dom.setAttribute('type', this.props.type);
|
|
662
|
+
this.props.inputMode && this.dom.setAttribute('inputMode', this.props.inputMode);
|
|
663
|
+
this.props.maxLength && this.dom.setAttribute('maxLength', this.props.maxLength);
|
|
664
|
+
this.props.caretColor && this.dom.setAttribute('caretColor', this.props.caretColor);
|
|
665
|
+
this.dom.spellcheck = this.props.spellCheck ?? false;
|
|
666
|
+
if (this.props.autoFocus)
|
|
667
|
+
this.dom.focus();
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
export const input = (type = 'text') => {
|
|
671
|
+
return new Input('input').react(s => s.type = type);
|
|
672
|
+
};
|
|
673
|
+
export const textarea = () => {
|
|
674
|
+
return new Input('textarea').react(s => s.type = 'text');
|
|
675
|
+
};
|