@oicl/openbridge-webcomponents-full-bundle 2.2.0-next.16 → 2.2.0-next.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bundle/openbridge-webcomponents.bundle.js +442 -87
- package/bundle/openbridge-webcomponents.bundle.js.map +1 -1
- package/custom-elements.json +1970 -28
- package/dist/automation/analog-threeway-valve/analog-threeway-valve.d.ts +4 -4
- package/dist/automation/analog-threeway-valve/analog-threeway-valve.js.map +1 -1
- package/dist/automation/automation-button/abstract-automation-button-storybook-helpers.d.ts +20 -0
- package/dist/automation/automation-button/abstract-automation-button-storybook-helpers.d.ts.map +1 -1
- package/dist/automation/automation-button/abstract-automation-button-storybook-helpers.js +1 -0
- package/dist/automation/automation-button/abstract-automation-button-storybook-helpers.js.map +1 -1
- package/dist/automation/automation-button/abstract-automation-button.d.ts +11 -2
- package/dist/automation/automation-button/abstract-automation-button.d.ts.map +1 -1
- package/dist/automation/automation-button/abstract-automation-button.js +40 -20
- package/dist/automation/automation-button/abstract-automation-button.js.map +1 -1
- package/dist/automation/automation-button/badge-layout.d.ts +34 -0
- package/dist/automation/automation-button/badge-layout.d.ts.map +1 -0
- package/dist/automation/automation-button/badge-layout.js +64 -0
- package/dist/automation/automation-button/badge-layout.js.map +1 -0
- package/dist/automation/digital-threeway-valve/digital-threeway-valve.d.ts +4 -4
- package/dist/automation/digital-threeway-valve/digital-threeway-valve.js.map +1 -1
- package/dist/automation/electricity-battery/electricity-battery.d.ts +4 -4
- package/dist/automation/electricity-battery/electricity-battery.js.map +1 -1
- package/dist/building-blocks/bar-horizontal/bar-horizontal.d.ts +4 -0
- package/dist/building-blocks/bar-horizontal/bar-horizontal.d.ts.map +1 -1
- package/dist/building-blocks/bar-horizontal/bar-horizontal.js +26 -23
- package/dist/building-blocks/bar-horizontal/bar-horizontal.js.map +1 -1
- package/dist/building-blocks/bar-vertical/bar-vertical.d.ts +4 -0
- package/dist/building-blocks/bar-vertical/bar-vertical.d.ts.map +1 -1
- package/dist/building-blocks/bar-vertical/bar-vertical.js +26 -23
- package/dist/building-blocks/bar-vertical/bar-vertical.js.map +1 -1
- package/dist/building-blocks/external-scale/external-scale.d.ts +28 -1
- package/dist/building-blocks/external-scale/external-scale.d.ts.map +1 -1
- package/dist/building-blocks/external-scale/external-scale.js +30 -4
- package/dist/building-blocks/external-scale/external-scale.js.map +1 -1
- package/dist/internal/global-setting.d.ts +55 -0
- package/dist/internal/global-setting.d.ts.map +1 -0
- package/dist/internal/global-setting.js +86 -0
- package/dist/internal/global-setting.js.map +1 -0
- package/dist/navigation-instruments/gauge-horizontal/gauge-horizontal.d.ts +28 -17
- package/dist/navigation-instruments/gauge-horizontal/gauge-horizontal.d.ts.map +1 -1
- package/dist/navigation-instruments/gauge-horizontal/gauge-horizontal.js +91 -11
- package/dist/navigation-instruments/gauge-horizontal/gauge-horizontal.js.map +1 -1
- package/dist/navigation-instruments/gauge-vertical/gauge-vertical.d.ts +28 -17
- package/dist/navigation-instruments/gauge-vertical/gauge-vertical.d.ts.map +1 -1
- package/dist/navigation-instruments/gauge-vertical/gauge-vertical.js +91 -11
- package/dist/navigation-instruments/gauge-vertical/gauge-vertical.js.map +1 -1
- package/package.json +1 -1
- package/src/automation/analog-threeway-valve/analog-threeway-valve.ts +4 -4
- package/src/automation/automation-button/abstract-automation-button-storybook-helpers.ts +3 -0
- package/src/automation/automation-button/abstract-automation-button.ts +57 -23
- package/src/automation/automation-button/badge-layout.spec.ts +138 -0
- package/src/automation/automation-button/badge-layout.ts +120 -0
- package/src/automation/digital-threeway-valve/digital-threeway-valve.ts +4 -4
- package/src/automation/electricity-battery/electricity-battery.ts +4 -4
- package/src/automation/pump/pump.stories.ts +59 -0
- package/src/building-blocks/bar-horizontal/bar-horizontal-fixed-aspect.spec.ts +102 -0
- package/src/building-blocks/bar-horizontal/bar-horizontal.ts +28 -23
- package/src/building-blocks/bar-vertical/bar-vertical-fixed-aspect.spec.ts +101 -0
- package/src/building-blocks/bar-vertical/bar-vertical.ts +28 -23
- package/src/building-blocks/external-scale/external-scale.ts +72 -3
- package/src/internal/global-setting.spec.ts +119 -0
- package/src/internal/global-setting.ts +105 -0
- package/src/navigation-instruments/gauge-horizontal/gauge-horizontal.spec.ts +288 -0
- package/src/navigation-instruments/gauge-horizontal/gauge-horizontal.stories.ts +114 -4
- package/src/navigation-instruments/gauge-horizontal/gauge-horizontal.ts +129 -41
- package/src/navigation-instruments/gauge-trend/gauge-trend.stories.ts +8 -0
- package/src/navigation-instruments/gauge-vertical/gauge-vertical.spec.ts +284 -0
- package/src/navigation-instruments/gauge-vertical/gauge-vertical.stories.ts +117 -4
- package/src/navigation-instruments/gauge-vertical/gauge-vertical.ts +129 -41
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
computeExternalScaleEffectiveBarThickness,
|
|
24
24
|
readExternalScaleBorderRadiusPx,
|
|
25
25
|
startExternalScaleBorderRadiusObserver,
|
|
26
|
+
readContentBox,
|
|
26
27
|
ScaleType,
|
|
27
28
|
FillMode,
|
|
28
29
|
AdvicePosition,
|
|
@@ -155,6 +156,9 @@ export class ObcBarVertical extends AdviceMixin(
|
|
|
155
156
|
@state()
|
|
156
157
|
private _scale = 1;
|
|
157
158
|
|
|
159
|
+
/** The drawing's width in viewBox units, kept from the last render for the scale measurement. */
|
|
160
|
+
private _viewBoxThickness = 0;
|
|
161
|
+
|
|
158
162
|
// ResizeController automatically subscribes/unsubscribes based on component lifecycle
|
|
159
163
|
// @ts-expect-error - Controller is used for side effects, not accessed directly
|
|
160
164
|
private _resizeController = new ResizeController(this, {
|
|
@@ -164,14 +168,12 @@ export class ObcBarVertical extends AdviceMixin(
|
|
|
164
168
|
const entry = entries[0];
|
|
165
169
|
if (!entry) return;
|
|
166
170
|
|
|
167
|
-
// Use the centralized function that computes scale based on reference size
|
|
168
|
-
// For vertical scales, compare container height to reference size
|
|
169
|
-
const containerMainAxisSize = entry.contentRect.height;
|
|
170
|
-
|
|
171
171
|
this._scale = computeFixedAspectRatioScale({
|
|
172
172
|
orientation: ExternalScaleOrientation.vertical,
|
|
173
|
-
containerMainAxisSize,
|
|
173
|
+
containerMainAxisSize: entry.contentRect.height,
|
|
174
|
+
containerCrossAxisSize: entry.contentRect.width,
|
|
174
175
|
scaleReferenceSize: this.scaleReferenceSize,
|
|
176
|
+
viewBoxThickness: this._viewBoxThickness,
|
|
175
177
|
});
|
|
176
178
|
|
|
177
179
|
// Report scaled dimensions to parent chart
|
|
@@ -309,6 +311,7 @@ export class ObcBarVertical extends AdviceMixin(
|
|
|
309
311
|
const layout = computeExternalScaleLayout(
|
|
310
312
|
toExternalScaleLayoutConfig(config)
|
|
311
313
|
);
|
|
314
|
+
this._viewBoxThickness = layout.thickness;
|
|
312
315
|
|
|
313
316
|
const parts = renderExternalScale(config);
|
|
314
317
|
|
|
@@ -382,6 +385,9 @@ export class ObcBarVertical extends AdviceMixin(
|
|
|
382
385
|
this.setpointPresenceChanged(changed);
|
|
383
386
|
|
|
384
387
|
if (!this.fixedAspectRatio || layoutChanged) {
|
|
388
|
+
// A thinner or thicker drawing under the same box changes the painted
|
|
389
|
+
// scale without a resize event.
|
|
390
|
+
if (this.fixedAspectRatio) this._measureScale();
|
|
385
391
|
this.reportDimensions();
|
|
386
392
|
}
|
|
387
393
|
}
|
|
@@ -498,6 +504,20 @@ export class ObcBarVertical extends AdviceMixin(
|
|
|
498
504
|
this.style.width = '';
|
|
499
505
|
}
|
|
500
506
|
|
|
507
|
+
/** Reads the host's content box into the scale; false while the host has no size yet. */
|
|
508
|
+
private _measureScale(): boolean {
|
|
509
|
+
const {width, height} = readContentBox(this);
|
|
510
|
+
if (height <= 0) return false;
|
|
511
|
+
this._scale = computeFixedAspectRatioScale({
|
|
512
|
+
orientation: ExternalScaleOrientation.vertical,
|
|
513
|
+
containerMainAxisSize: height,
|
|
514
|
+
containerCrossAxisSize: width,
|
|
515
|
+
scaleReferenceSize: this.scaleReferenceSize,
|
|
516
|
+
viewBoxThickness: this._viewBoxThickness,
|
|
517
|
+
});
|
|
518
|
+
return true;
|
|
519
|
+
}
|
|
520
|
+
|
|
501
521
|
/**
|
|
502
522
|
* Calculate scale based on current element size.
|
|
503
523
|
* Called when fixedAspectRatio is enabled or scaleReferenceSize changes.
|
|
@@ -505,13 +525,7 @@ export class ObcBarVertical extends AdviceMixin(
|
|
|
505
525
|
private _updateScaleFromCurrentSize(): void {
|
|
506
526
|
// Use requestAnimationFrame to ensure layout is complete
|
|
507
527
|
requestAnimationFrame(() => {
|
|
508
|
-
|
|
509
|
-
if (containerHeight > 0) {
|
|
510
|
-
this._scale = computeFixedAspectRatioScale({
|
|
511
|
-
orientation: ExternalScaleOrientation.vertical,
|
|
512
|
-
containerMainAxisSize: containerHeight,
|
|
513
|
-
scaleReferenceSize: this.scaleReferenceSize,
|
|
514
|
-
});
|
|
528
|
+
if (this._measureScale()) {
|
|
515
529
|
this.requestUpdate();
|
|
516
530
|
// Report updated dimensions to parent (matches pattern in ResizeController and _refreshBorderRadiusFromCssVar)
|
|
517
531
|
this.reportDimensions();
|
|
@@ -531,17 +545,8 @@ export class ObcBarVertical extends AdviceMixin(
|
|
|
531
545
|
|
|
532
546
|
// In fixed-aspect-ratio mode, recompute scale when border radius changes
|
|
533
547
|
// (border radius affects effective bar thickness and therefore viewBox)
|
|
534
|
-
if (this.fixedAspectRatio) {
|
|
535
|
-
|
|
536
|
-
if (rect.height > 0) {
|
|
537
|
-
this._scale = computeFixedAspectRatioScale({
|
|
538
|
-
orientation: ExternalScaleOrientation.vertical,
|
|
539
|
-
containerMainAxisSize: rect.height,
|
|
540
|
-
scaleReferenceSize: this.scaleReferenceSize,
|
|
541
|
-
});
|
|
542
|
-
// Report updated dimensions
|
|
543
|
-
this.reportDimensions();
|
|
544
|
-
}
|
|
548
|
+
if (this.fixedAspectRatio && this._measureScale()) {
|
|
549
|
+
this.reportDimensions();
|
|
545
550
|
}
|
|
546
551
|
}
|
|
547
552
|
|
|
@@ -793,6 +793,14 @@ export interface FixedAspectRatioScaleConfig {
|
|
|
793
793
|
containerMainAxisSize: number;
|
|
794
794
|
/** Reference size at which the scale renders at 1:1 (default: 384) */
|
|
795
795
|
scaleReferenceSize: number;
|
|
796
|
+
/**
|
|
797
|
+
* Cross-axis size of the container (width for vertical, height for
|
|
798
|
+
* horizontal). With `viewBoxThickness` it caps the scale the way
|
|
799
|
+
* `xMidYMid meet` does once the container is narrower than the drawing.
|
|
800
|
+
*/
|
|
801
|
+
containerCrossAxisSize?: number;
|
|
802
|
+
/** The drawing's cross-axis size in viewBox units (`computeExternalScaleLayout().thickness`). */
|
|
803
|
+
viewBoxThickness?: number;
|
|
796
804
|
}
|
|
797
805
|
|
|
798
806
|
/**
|
|
@@ -807,17 +815,28 @@ export interface FixedAspectRatioScaleConfig {
|
|
|
807
815
|
* This scale factor is applied to SVG rendering while keeping text labels at constant size
|
|
808
816
|
* by using the CSS variable `--scale` to counter-scale font sizes.
|
|
809
817
|
*
|
|
818
|
+
* The SVG is painted with `xMidYMid meet`, which takes the smaller of the two
|
|
819
|
+
* axes' ratios. Pass the container's cross-axis size and the drawing's
|
|
820
|
+
* thickness so the factor follows the paint when the cross axis is the limit;
|
|
821
|
+
* without them the main axis alone decides, as it does whenever the cross axis
|
|
822
|
+
* has room.
|
|
823
|
+
*
|
|
810
824
|
* Works for all orientations and sides:
|
|
811
825
|
* - Vertical scales (left/right): compare container height vs reference
|
|
812
826
|
* - Horizontal scales (top/bottom): compare container width vs reference
|
|
813
827
|
*
|
|
814
|
-
* @param config Configuration with orientation, container
|
|
828
|
+
* @param config Configuration with orientation, container sizes, reference size and drawing thickness
|
|
815
829
|
* @returns Scale factor (> 0), defaults to 1 if container size is invalid
|
|
816
830
|
*/
|
|
817
831
|
export function computeFixedAspectRatioScale(
|
|
818
832
|
config: FixedAspectRatioScaleConfig
|
|
819
833
|
): number {
|
|
820
|
-
const {
|
|
834
|
+
const {
|
|
835
|
+
containerMainAxisSize,
|
|
836
|
+
scaleReferenceSize,
|
|
837
|
+
containerCrossAxisSize,
|
|
838
|
+
viewBoxThickness,
|
|
839
|
+
} = config;
|
|
821
840
|
|
|
822
841
|
// Guard against zero or negative sizes
|
|
823
842
|
if (containerMainAxisSize <= 0 || scaleReferenceSize <= 0) {
|
|
@@ -827,7 +846,57 @@ export function computeFixedAspectRatioScale(
|
|
|
827
846
|
// Scale = container size / reference size
|
|
828
847
|
// e.g., container=480px, reference=384px => scale=1.25 (zoom in)
|
|
829
848
|
// e.g., container=288px, reference=384px => scale=0.75 (zoom out)
|
|
830
|
-
|
|
849
|
+
const mainScale = containerMainAxisSize / scaleReferenceSize;
|
|
850
|
+
|
|
851
|
+
if (
|
|
852
|
+
containerCrossAxisSize === undefined ||
|
|
853
|
+
containerCrossAxisSize <= 0 ||
|
|
854
|
+
viewBoxThickness === undefined ||
|
|
855
|
+
viewBoxThickness <= 0
|
|
856
|
+
) {
|
|
857
|
+
return mainScale;
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
// Integer client sizes round an unconstrained box down by under a pixel;
|
|
861
|
+
// that is not a constraint.
|
|
862
|
+
if (containerCrossAxisSize + 1 >= viewBoxThickness * mainScale) {
|
|
863
|
+
return mainScale;
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
return config.orientation === ExternalScaleOrientation.vertical
|
|
867
|
+
? computeMeetScale(
|
|
868
|
+
viewBoxThickness,
|
|
869
|
+
scaleReferenceSize,
|
|
870
|
+
containerCrossAxisSize,
|
|
871
|
+
containerMainAxisSize
|
|
872
|
+
)
|
|
873
|
+
: computeMeetScale(
|
|
874
|
+
scaleReferenceSize,
|
|
875
|
+
viewBoxThickness,
|
|
876
|
+
containerMainAxisSize,
|
|
877
|
+
containerCrossAxisSize
|
|
878
|
+
);
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
/**
|
|
882
|
+
* The element's content box in CSS px: the box a `100%` × `100%` SVG child
|
|
883
|
+
* fills, and what a ResizeObserver's `contentRect` reports. `clientWidth` /
|
|
884
|
+
* `clientHeight` include the padding, and `getBoundingClientRect()` is scaled
|
|
885
|
+
* by any ancestor CSS `zoom`, so neither is the box the drawing is painted in.
|
|
886
|
+
*
|
|
887
|
+
* @param el The host element
|
|
888
|
+
* @returns Content width and height in CSS px, 0 × 0 while the element has no layout box
|
|
889
|
+
*/
|
|
890
|
+
export function readContentBox(el: HTMLElement): {
|
|
891
|
+
width: number;
|
|
892
|
+
height: number;
|
|
893
|
+
} {
|
|
894
|
+
const style = getComputedStyle(el);
|
|
895
|
+
const px = (value: string): number => parseFloat(value) || 0;
|
|
896
|
+
return {
|
|
897
|
+
width: el.clientWidth - px(style.paddingLeft) - px(style.paddingRight),
|
|
898
|
+
height: el.clientHeight - px(style.paddingTop) - px(style.paddingBottom),
|
|
899
|
+
};
|
|
831
900
|
}
|
|
832
901
|
|
|
833
902
|
/**
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import {beforeEach, describe, expect, it} from 'vitest';
|
|
2
|
+
import {LitElement, html, type PropertyValues} from 'lit';
|
|
3
|
+
import {GlobalSetting} from './global-setting.js';
|
|
4
|
+
|
|
5
|
+
const label = new GlobalSetting('one');
|
|
6
|
+
|
|
7
|
+
type SettingHost = LitElement & {renders: number};
|
|
8
|
+
|
|
9
|
+
// Registered on first use rather than at module scope: the wrapper
|
|
10
|
+
// generators read every module-level element definition, specs included.
|
|
11
|
+
function settingHost(leavesDuringUpdate = false): SettingHost {
|
|
12
|
+
const tag = leavesDuringUpdate
|
|
13
|
+
? 'test-global-setting-leaving-host'
|
|
14
|
+
: 'test-global-setting-host';
|
|
15
|
+
if (!customElements.get(tag)) {
|
|
16
|
+
const Host = class extends LitElement {
|
|
17
|
+
private readonly label = label.controller(this);
|
|
18
|
+
renders = 0;
|
|
19
|
+
|
|
20
|
+
override render() {
|
|
21
|
+
this.renders++;
|
|
22
|
+
return html`${this.label.value}`;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
customElements.define(
|
|
26
|
+
tag,
|
|
27
|
+
leavesDuringUpdate
|
|
28
|
+
? class extends Host {
|
|
29
|
+
private left = false;
|
|
30
|
+
|
|
31
|
+
/** Leaves the page from inside its own update, and changes the setting before the update completes. */
|
|
32
|
+
override update(changed: PropertyValues) {
|
|
33
|
+
super.update(changed);
|
|
34
|
+
if (!this.left) {
|
|
35
|
+
this.left = true;
|
|
36
|
+
this.remove();
|
|
37
|
+
label.set('two');
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
: Host
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
const host = document.createElement(tag) as SettingHost;
|
|
45
|
+
document.body.appendChild(host);
|
|
46
|
+
return host;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
describe('GlobalSetting', () => {
|
|
50
|
+
beforeEach(() => {
|
|
51
|
+
label.set('one');
|
|
52
|
+
document.body.replaceChildren();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('renders the value, and set() re-renders every connected host', async () => {
|
|
56
|
+
const first = settingHost();
|
|
57
|
+
const second = settingHost();
|
|
58
|
+
await first.updateComplete;
|
|
59
|
+
await second.updateComplete;
|
|
60
|
+
expect(first.shadowRoot!.textContent).toBe('one');
|
|
61
|
+
|
|
62
|
+
label.set('two');
|
|
63
|
+
await first.updateComplete;
|
|
64
|
+
await second.updateComplete;
|
|
65
|
+
|
|
66
|
+
expect(first.shadowRoot!.textContent).toBe('two');
|
|
67
|
+
expect(second.shadowRoot!.textContent).toBe('two');
|
|
68
|
+
expect(first.renders).toBe(2);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('does nothing when the same value is set again', async () => {
|
|
72
|
+
const host = settingHost();
|
|
73
|
+
await host.updateComplete;
|
|
74
|
+
|
|
75
|
+
label.set('one');
|
|
76
|
+
await host.updateComplete;
|
|
77
|
+
|
|
78
|
+
expect(host.renders).toBe(1);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('leaves a detached host alone and lets it catch up on reconnect', async () => {
|
|
82
|
+
const host = settingHost();
|
|
83
|
+
await host.updateComplete;
|
|
84
|
+
|
|
85
|
+
host.remove();
|
|
86
|
+
label.set('two');
|
|
87
|
+
await host.updateComplete;
|
|
88
|
+
expect(host.renders).toBe(1);
|
|
89
|
+
|
|
90
|
+
document.body.appendChild(host);
|
|
91
|
+
await host.updateComplete;
|
|
92
|
+
expect(host.renders).toBe(2);
|
|
93
|
+
expect(host.shadowRoot!.textContent).toBe('two');
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('counts a change during a render that left the page as not rendered', async () => {
|
|
97
|
+
const host = settingHost(true);
|
|
98
|
+
await host.updateComplete;
|
|
99
|
+
expect(host.isConnected).toBe(false);
|
|
100
|
+
expect(host.shadowRoot!.textContent).toBe('one');
|
|
101
|
+
|
|
102
|
+
document.body.appendChild(host);
|
|
103
|
+
await host.updateComplete;
|
|
104
|
+
|
|
105
|
+
expect(host.shadowRoot!.textContent).toBe('two');
|
|
106
|
+
expect(host.renders).toBe(2);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('does not re-render a host reattached while nothing changed', async () => {
|
|
110
|
+
const host = settingHost();
|
|
111
|
+
await host.updateComplete;
|
|
112
|
+
|
|
113
|
+
host.remove();
|
|
114
|
+
document.body.appendChild(host);
|
|
115
|
+
await host.updateComplete;
|
|
116
|
+
|
|
117
|
+
expect(host.renders).toBe(1);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type {ReactiveController, ReactiveControllerHost} from 'lit';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* One app-wide setting the components render from.
|
|
5
|
+
*
|
|
6
|
+
* The application sets it once through a module setter such as
|
|
7
|
+
* `setAutomationBadgeLayout()` or `setAlertStandard()`. Every connected host
|
|
8
|
+
* that holds a controller for it re-renders on each change; a host detached
|
|
9
|
+
* while the value changed catches up on its next connect, and one reattached
|
|
10
|
+
* unchanged renders nothing extra. Setting the same value again is a no-op.
|
|
11
|
+
*
|
|
12
|
+
* It is a plain subscriber set rather than a signal: the value rarely changes
|
|
13
|
+
* and derives nothing, and the library stays off a pre-standard polyfill. A
|
|
14
|
+
* setter built on it can move to a signal later without changing its signature.
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* const alertStandard = new GlobalSetting<AlertStandard | undefined>(undefined);
|
|
18
|
+
* export const setAlertStandard = (s?: AlertStandard) => alertStandard.set(s);
|
|
19
|
+
*
|
|
20
|
+
* class ObcAlertIcon extends LitElement {
|
|
21
|
+
* private readonly standard = alertStandard.controller(this);
|
|
22
|
+
* render() { return draw(this.standard.value); }
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export class GlobalSetting<T> {
|
|
27
|
+
private currentVersion = 0;
|
|
28
|
+
private readonly hosts = new Set<ReactiveControllerHost>();
|
|
29
|
+
|
|
30
|
+
constructor(private value: T) {}
|
|
31
|
+
|
|
32
|
+
get(): T {
|
|
33
|
+
return this.value;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Stores the value and re-renders every connected host. */
|
|
37
|
+
set(value: T): void {
|
|
38
|
+
if (Object.is(value, this.value)) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
this.value = value;
|
|
42
|
+
this.currentVersion++;
|
|
43
|
+
for (const host of this.hosts) {
|
|
44
|
+
host.requestUpdate();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Re-renders `host` whenever the value changes; create it once, in the host's constructor. */
|
|
49
|
+
controller(host: ReactiveControllerHost): GlobalSettingController<T> {
|
|
50
|
+
return new GlobalSettingController(host, this);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Counts the changes, so a controller can tell whether its host rendered the current value. */
|
|
54
|
+
get version(): number {
|
|
55
|
+
return this.currentVersion;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
attach(host: ReactiveControllerHost): void {
|
|
59
|
+
this.hosts.add(host);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
detach(host: ReactiveControllerHost): void {
|
|
63
|
+
this.hosts.delete(host);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Keeps one host rendered with the current value of a `GlobalSetting`. */
|
|
68
|
+
export class GlobalSettingController<T> implements ReactiveController {
|
|
69
|
+
private renderedVersion: number;
|
|
70
|
+
private renderingVersion: number;
|
|
71
|
+
|
|
72
|
+
constructor(
|
|
73
|
+
private readonly host: ReactiveControllerHost,
|
|
74
|
+
private readonly setting: GlobalSetting<T>
|
|
75
|
+
) {
|
|
76
|
+
this.renderedVersion = setting.version;
|
|
77
|
+
this.renderingVersion = setting.version;
|
|
78
|
+
host.addController(this);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
get value(): T {
|
|
82
|
+
return this.setting.get();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** A value set while the host was detached never reached it. */
|
|
86
|
+
hostConnected(): void {
|
|
87
|
+
this.setting.attach(this.host);
|
|
88
|
+
if (this.renderedVersion !== this.setting.version) {
|
|
89
|
+
this.host.requestUpdate();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
hostDisconnected(): void {
|
|
94
|
+
this.setting.detach(this.host);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The version a render starts from; a change while it runs detached must not count as rendered. */
|
|
98
|
+
hostUpdate(): void {
|
|
99
|
+
this.renderingVersion = this.setting.version;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
hostUpdated(): void {
|
|
103
|
+
this.renderedVersion = this.renderingVersion;
|
|
104
|
+
}
|
|
105
|
+
}
|