@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.
Files changed (68) hide show
  1. package/bundle/openbridge-webcomponents.bundle.js +442 -87
  2. package/bundle/openbridge-webcomponents.bundle.js.map +1 -1
  3. package/custom-elements.json +1970 -28
  4. package/dist/automation/analog-threeway-valve/analog-threeway-valve.d.ts +4 -4
  5. package/dist/automation/analog-threeway-valve/analog-threeway-valve.js.map +1 -1
  6. package/dist/automation/automation-button/abstract-automation-button-storybook-helpers.d.ts +20 -0
  7. package/dist/automation/automation-button/abstract-automation-button-storybook-helpers.d.ts.map +1 -1
  8. package/dist/automation/automation-button/abstract-automation-button-storybook-helpers.js +1 -0
  9. package/dist/automation/automation-button/abstract-automation-button-storybook-helpers.js.map +1 -1
  10. package/dist/automation/automation-button/abstract-automation-button.d.ts +11 -2
  11. package/dist/automation/automation-button/abstract-automation-button.d.ts.map +1 -1
  12. package/dist/automation/automation-button/abstract-automation-button.js +40 -20
  13. package/dist/automation/automation-button/abstract-automation-button.js.map +1 -1
  14. package/dist/automation/automation-button/badge-layout.d.ts +34 -0
  15. package/dist/automation/automation-button/badge-layout.d.ts.map +1 -0
  16. package/dist/automation/automation-button/badge-layout.js +64 -0
  17. package/dist/automation/automation-button/badge-layout.js.map +1 -0
  18. package/dist/automation/digital-threeway-valve/digital-threeway-valve.d.ts +4 -4
  19. package/dist/automation/digital-threeway-valve/digital-threeway-valve.js.map +1 -1
  20. package/dist/automation/electricity-battery/electricity-battery.d.ts +4 -4
  21. package/dist/automation/electricity-battery/electricity-battery.js.map +1 -1
  22. package/dist/building-blocks/bar-horizontal/bar-horizontal.d.ts +4 -0
  23. package/dist/building-blocks/bar-horizontal/bar-horizontal.d.ts.map +1 -1
  24. package/dist/building-blocks/bar-horizontal/bar-horizontal.js +26 -23
  25. package/dist/building-blocks/bar-horizontal/bar-horizontal.js.map +1 -1
  26. package/dist/building-blocks/bar-vertical/bar-vertical.d.ts +4 -0
  27. package/dist/building-blocks/bar-vertical/bar-vertical.d.ts.map +1 -1
  28. package/dist/building-blocks/bar-vertical/bar-vertical.js +26 -23
  29. package/dist/building-blocks/bar-vertical/bar-vertical.js.map +1 -1
  30. package/dist/building-blocks/external-scale/external-scale.d.ts +28 -1
  31. package/dist/building-blocks/external-scale/external-scale.d.ts.map +1 -1
  32. package/dist/building-blocks/external-scale/external-scale.js +30 -4
  33. package/dist/building-blocks/external-scale/external-scale.js.map +1 -1
  34. package/dist/internal/global-setting.d.ts +55 -0
  35. package/dist/internal/global-setting.d.ts.map +1 -0
  36. package/dist/internal/global-setting.js +86 -0
  37. package/dist/internal/global-setting.js.map +1 -0
  38. package/dist/navigation-instruments/gauge-horizontal/gauge-horizontal.d.ts +28 -17
  39. package/dist/navigation-instruments/gauge-horizontal/gauge-horizontal.d.ts.map +1 -1
  40. package/dist/navigation-instruments/gauge-horizontal/gauge-horizontal.js +91 -11
  41. package/dist/navigation-instruments/gauge-horizontal/gauge-horizontal.js.map +1 -1
  42. package/dist/navigation-instruments/gauge-vertical/gauge-vertical.d.ts +28 -17
  43. package/dist/navigation-instruments/gauge-vertical/gauge-vertical.d.ts.map +1 -1
  44. package/dist/navigation-instruments/gauge-vertical/gauge-vertical.js +91 -11
  45. package/dist/navigation-instruments/gauge-vertical/gauge-vertical.js.map +1 -1
  46. package/package.json +1 -1
  47. package/src/automation/analog-threeway-valve/analog-threeway-valve.ts +4 -4
  48. package/src/automation/automation-button/abstract-automation-button-storybook-helpers.ts +3 -0
  49. package/src/automation/automation-button/abstract-automation-button.ts +57 -23
  50. package/src/automation/automation-button/badge-layout.spec.ts +138 -0
  51. package/src/automation/automation-button/badge-layout.ts +120 -0
  52. package/src/automation/digital-threeway-valve/digital-threeway-valve.ts +4 -4
  53. package/src/automation/electricity-battery/electricity-battery.ts +4 -4
  54. package/src/automation/pump/pump.stories.ts +59 -0
  55. package/src/building-blocks/bar-horizontal/bar-horizontal-fixed-aspect.spec.ts +102 -0
  56. package/src/building-blocks/bar-horizontal/bar-horizontal.ts +28 -23
  57. package/src/building-blocks/bar-vertical/bar-vertical-fixed-aspect.spec.ts +101 -0
  58. package/src/building-blocks/bar-vertical/bar-vertical.ts +28 -23
  59. package/src/building-blocks/external-scale/external-scale.ts +72 -3
  60. package/src/internal/global-setting.spec.ts +119 -0
  61. package/src/internal/global-setting.ts +105 -0
  62. package/src/navigation-instruments/gauge-horizontal/gauge-horizontal.spec.ts +288 -0
  63. package/src/navigation-instruments/gauge-horizontal/gauge-horizontal.stories.ts +114 -4
  64. package/src/navigation-instruments/gauge-horizontal/gauge-horizontal.ts +129 -41
  65. package/src/navigation-instruments/gauge-trend/gauge-trend.stories.ts +8 -0
  66. package/src/navigation-instruments/gauge-vertical/gauge-vertical.spec.ts +284 -0
  67. package/src/navigation-instruments/gauge-vertical/gauge-vertical.stories.ts +117 -4
  68. package/src/navigation-instruments/gauge-vertical/gauge-vertical.ts +129 -41
@@ -0,0 +1,138 @@
1
+ import {describe, it, expect, afterEach} from 'vitest';
2
+ import '../pump/pump.js';
3
+ import type {ObcPump} from '../pump/pump.js';
4
+ import {
5
+ AutomationButtonBadgeAlert,
6
+ AutomationButtonBadgeCommandLocked,
7
+ AutomationButtonBadgeControl,
8
+ AutomationButtonBadgeInterlock,
9
+ } from './abstract-automation-button.js';
10
+ import {ObcAutomationBadgeType} from '../automation-badge/automation-badge.js';
11
+ import {
12
+ AutomationButtonBadgeKind as Kind,
13
+ defaultAutomationButtonBadgeLayout,
14
+ getAutomationBadgeLayout,
15
+ resolveBadgeLayout,
16
+ setAutomationBadgeLayout,
17
+ } from './badge-layout.js';
18
+
19
+ describe('resolveBadgeLayout', () => {
20
+ const base = defaultAutomationButtonBadgeLayout;
21
+
22
+ it('keeps the base when nothing overrides it', () => {
23
+ expect(resolveBadgeLayout(base, undefined)).toEqual(base);
24
+ expect(resolveBadgeLayout(base, {})).toEqual(base);
25
+ });
26
+
27
+ it('empties a corner set to null', () => {
28
+ expect(resolveBadgeLayout(base, {bottomRight: null})).toEqual({
29
+ ...base,
30
+ bottomRight: null,
31
+ });
32
+ });
33
+
34
+ it('moves a kind out of the corner it came from', () => {
35
+ expect(resolveBadgeLayout(base, {topLeft: Kind.Alert})).toEqual({
36
+ topLeft: Kind.Alert,
37
+ topRight: null,
38
+ bottomLeft: Kind.Interlock,
39
+ bottomRight: Kind.CommandLocked,
40
+ });
41
+ });
42
+
43
+ it('keeps a kind named twice in the first corner only', () => {
44
+ expect(
45
+ resolveBadgeLayout(base, {topRight: Kind.Alert, bottomLeft: Kind.Alert})
46
+ ).toEqual({...base, bottomLeft: null});
47
+ });
48
+
49
+ it('swaps two kinds when both corners are named', () => {
50
+ expect(
51
+ resolveBadgeLayout(base, {topLeft: Kind.Alert, topRight: Kind.Control})
52
+ ).toEqual({...base, topLeft: Kind.Alert, topRight: Kind.Control});
53
+ });
54
+ });
55
+
56
+ const mounted: HTMLElement[] = [];
57
+ afterEach(() => {
58
+ mounted.splice(0).forEach((el) => el.remove());
59
+ setAutomationBadgeLayout(undefined);
60
+ });
61
+
62
+ async function mountPump() {
63
+ const el = document.createElement('obc-pump') as ObcPump;
64
+ el.badgeControl = AutomationButtonBadgeControl.Auto;
65
+ el.badgeAlert = AutomationButtonBadgeAlert.Alarm;
66
+ el.badgeInterlock = AutomationButtonBadgeInterlock.Interlock;
67
+ el.badgeCommandLocked = AutomationButtonBadgeCommandLocked.CommandLocked;
68
+ document.body.appendChild(el);
69
+ mounted.push(el);
70
+ await el.updateComplete;
71
+ return el;
72
+ }
73
+
74
+ function badgeIn(el: ObcPump, corner: string): string | null {
75
+ const badge = el.shadowRoot!.querySelector(
76
+ `slot[name="${corner}"] obc-automation-badge`
77
+ ) as unknown as {type: ObcAutomationBadgeType} | null;
78
+ return badge?.type ?? null;
79
+ }
80
+
81
+ describe('badge layout on a device', () => {
82
+ it('renders the default layout', async () => {
83
+ const el = await mountPump();
84
+ expect(badgeIn(el, 'badge-top-left')).toBe(ObcAutomationBadgeType.Auto);
85
+ expect(badgeIn(el, 'badge-top-right')).toBe(ObcAutomationBadgeType.Alarm);
86
+ expect(badgeIn(el, 'badge-bottom-left')).toBe(
87
+ ObcAutomationBadgeType.Interlock
88
+ );
89
+ expect(badgeIn(el, 'badge-bottom-right')).toBe(
90
+ ObcAutomationBadgeType.CommandLocked
91
+ );
92
+ });
93
+
94
+ it('re-renders a connected device when the global layout changes', async () => {
95
+ const el = await mountPump();
96
+ setAutomationBadgeLayout({topLeft: Kind.Alert, topRight: Kind.Control});
97
+ await el.updateComplete;
98
+ expect(badgeIn(el, 'badge-top-left')).toBe(ObcAutomationBadgeType.Alarm);
99
+ expect(badgeIn(el, 'badge-top-right')).toBe(ObcAutomationBadgeType.Auto);
100
+ });
101
+
102
+ it('lets the local layout override corners of the global one', async () => {
103
+ setAutomationBadgeLayout({bottomLeft: Kind.CommandLocked});
104
+ expect(getAutomationBadgeLayout().bottomRight).toBeNull();
105
+ const el = await mountPump();
106
+ el.badgeLayout = {bottomRight: Kind.Interlock};
107
+ await el.updateComplete;
108
+ expect(badgeIn(el, 'badge-bottom-left')).toBe(
109
+ ObcAutomationBadgeType.CommandLocked
110
+ );
111
+ expect(badgeIn(el, 'badge-bottom-right')).toBe(
112
+ ObcAutomationBadgeType.Interlock
113
+ );
114
+ });
115
+
116
+ it('stops updating a disconnected device', async () => {
117
+ const el = await mountPump();
118
+ el.remove();
119
+ setAutomationBadgeLayout({topLeft: null});
120
+ expect(el.isUpdatePending).toBe(false);
121
+ });
122
+
123
+ it('picks up a layout set while it was detached', async () => {
124
+ const el = await mountPump();
125
+ el.remove();
126
+ setAutomationBadgeLayout({topLeft: null});
127
+ document.body.appendChild(el);
128
+ await el.updateComplete;
129
+ expect(badgeIn(el, 'badge-top-left')).toBeNull();
130
+ });
131
+
132
+ it('does not re-render on reconnect when nothing changed', async () => {
133
+ const el = await mountPump();
134
+ el.remove();
135
+ document.body.appendChild(el);
136
+ expect(el.isUpdatePending).toBe(false);
137
+ });
138
+ });
@@ -0,0 +1,120 @@
1
+ /**
2
+ * Badge layout of the automation buttons.
3
+ *
4
+ * Says which badge kind each corner of an automation button shows. The app
5
+ * sets one layout for every button with `setAutomationBadgeLayout()`; a
6
+ * button's own `badgeLayout` property changes single corners on top of it.
7
+ *
8
+ * Merge rules, for both the global and the local layout:
9
+ * - a corner left out keeps the layout underneath it,
10
+ * - `null` leaves the corner empty,
11
+ * - a kind moved to a new corner leaves the corner it came from, so it is
12
+ * never shown twice.
13
+ *
14
+ * ```ts
15
+ * setAutomationBadgeLayout({
16
+ * topLeft: AutomationButtonBadgeKind.Alert,
17
+ * topRight: AutomationButtonBadgeKind.Control,
18
+ * });
19
+ * ```
20
+ *
21
+ * @module
22
+ */
23
+ import type {ReactiveControllerHost} from 'lit';
24
+ import {
25
+ GlobalSetting,
26
+ GlobalSettingController,
27
+ } from '../../internal/global-setting.js';
28
+
29
+ export enum AutomationButtonBadgeKind {
30
+ Control = 'control',
31
+ Alert = 'alert',
32
+ Interlock = 'interlock',
33
+ CommandLocked = 'command-locked',
34
+ }
35
+
36
+ /** Which badge kind each corner shows. `null` leaves the corner empty. */
37
+ export interface AutomationButtonBadgeLayout {
38
+ topLeft?: AutomationButtonBadgeKind | null;
39
+ topRight?: AutomationButtonBadgeKind | null;
40
+ bottomLeft?: AutomationButtonBadgeKind | null;
41
+ bottomRight?: AutomationButtonBadgeKind | null;
42
+ }
43
+
44
+ export type ResolvedAutomationButtonBadgeLayout =
45
+ Required<AutomationButtonBadgeLayout>;
46
+
47
+ const corners = ['topLeft', 'topRight', 'bottomLeft', 'bottomRight'] as const;
48
+
49
+ export const defaultAutomationButtonBadgeLayout: Readonly<ResolvedAutomationButtonBadgeLayout> =
50
+ Object.freeze({
51
+ topLeft: AutomationButtonBadgeKind.Control,
52
+ topRight: AutomationButtonBadgeKind.Alert,
53
+ bottomLeft: AutomationButtonBadgeKind.Interlock,
54
+ bottomRight: AutomationButtonBadgeKind.CommandLocked,
55
+ });
56
+
57
+ /**
58
+ * The `base` layout with the corners `override` names put on top of it.
59
+ * A kind `override` names twice stays in the first corner, in reading order.
60
+ */
61
+ export function resolveBadgeLayout(
62
+ base: ResolvedAutomationButtonBadgeLayout,
63
+ override: AutomationButtonBadgeLayout | undefined
64
+ ): ResolvedAutomationButtonBadgeLayout {
65
+ const result = {...base};
66
+ if (!override) {
67
+ return result;
68
+ }
69
+ const placed = new Set<AutomationButtonBadgeKind>();
70
+ for (const corner of corners) {
71
+ const kind = override[corner];
72
+ if (kind === undefined) {
73
+ continue;
74
+ }
75
+ result[corner] = kind !== null && placed.has(kind) ? null : kind;
76
+ if (kind !== null) {
77
+ placed.add(kind);
78
+ }
79
+ }
80
+ for (const corner of corners) {
81
+ const kind = result[corner];
82
+ if (override[corner] === undefined && kind !== null && placed.has(kind)) {
83
+ result[corner] = null;
84
+ }
85
+ }
86
+ return result;
87
+ }
88
+
89
+ const globalLayout = new GlobalSetting<ResolvedAutomationButtonBadgeLayout>({
90
+ ...defaultAutomationButtonBadgeLayout,
91
+ });
92
+
93
+ /**
94
+ * Sets the badge layout of every automation button on the page, and
95
+ * re-renders the ones already connected. `undefined` restores the default.
96
+ */
97
+ export function setAutomationBadgeLayout(
98
+ layout: AutomationButtonBadgeLayout | undefined
99
+ ): void {
100
+ globalLayout.set(
101
+ resolveBadgeLayout(defaultAutomationButtonBadgeLayout, layout)
102
+ );
103
+ }
104
+
105
+ export function getAutomationBadgeLayout(): ResolvedAutomationButtonBadgeLayout {
106
+ return {...globalLayout.get()};
107
+ }
108
+
109
+ /** Re-renders its host when the global badge layout changes, and puts the host's own corners on top of it. */
110
+ export class BadgeLayoutController extends GlobalSettingController<ResolvedAutomationButtonBadgeLayout> {
111
+ constructor(host: ReactiveControllerHost) {
112
+ super(host, globalLayout);
113
+ }
114
+
115
+ resolve(
116
+ local: AutomationButtonBadgeLayout | undefined
117
+ ): ResolvedAutomationButtonBadgeLayout {
118
+ return resolveBadgeLayout(this.value, local);
119
+ }
120
+ }
@@ -60,10 +60,10 @@ export enum DigitalThreewayValveVariant {
60
60
  * @property orientation - Side the stem points to.
61
61
  * @property flows - Readout rows, one per flow: an optional arrow direction with either a value and optional unit (default `%`) or `open` for an `Open`/`Closed` state.
62
62
  * @property variant - Visual style of the button surround.
63
- * @slot badge-top-right - Content projected into the top-right badge position; overrides `badgeAlert`.
64
- * @slot badge-top-left - Content projected into the top-left badge position; overrides `badgeControl`.
65
- * @slot badge-bottom-left - Content projected into the bottom-left badge position; overrides `badgeInterlock`.
66
- * @slot badge-bottom-right - Content projected into the bottom-right badge position; overrides `badgeCommandLocked`.
63
+ * @slot badge-top-right - Content projected into the top-right badge position; overrides the badge `badgeLayout` puts in that corner.
64
+ * @slot badge-top-left - Content projected into the top-left badge position; overrides the badge `badgeLayout` puts in that corner.
65
+ * @slot badge-bottom-left - Content projected into the bottom-left badge position; overrides the badge `badgeLayout` puts in that corner.
66
+ * @slot badge-bottom-right - Content projected into the bottom-right badge position; overrides the badge `badgeLayout` puts in that corner.
67
67
  * @fires click - Fired when the valve is clicked.
68
68
  * @experimental
69
69
  */
@@ -21,10 +21,10 @@ import {ObcAbstractAutomationButtonSquared} from '../automation-button/abstract-
21
21
  * Without a visible tag the button is named `Battery`, followed by the state
22
22
  * (`Battery On`) when the state row is shown.
23
23
  *
24
- * @slot badge-top-right - Content projected into the top-right badge position; overrides `badgeAlert`.
25
- * @slot badge-top-left - Content projected into the top-left badge position; overrides `badgeControl`.
26
- * @slot badge-bottom-left - Content projected into the bottom-left badge position; overrides `badgeInterlock`.
27
- * @slot badge-bottom-right - Content projected into the bottom-right badge position; overrides `badgeCommandLocked`.
24
+ * @slot badge-top-right - Content projected into the top-right badge position; overrides the badge `badgeLayout` puts in that corner.
25
+ * @slot badge-top-left - Content projected into the top-left badge position; overrides the badge `badgeLayout` puts in that corner.
26
+ * @slot badge-bottom-left - Content projected into the bottom-left badge position; overrides the badge `badgeLayout` puts in that corner.
27
+ * @slot badge-bottom-right - Content projected into the bottom-right badge position; overrides the badge `badgeLayout` puts in that corner.
28
28
  * @fires click - Fired when the battery is clicked.
29
29
  * @experimental
30
30
  */
@@ -10,6 +10,16 @@ import {
10
10
  } from '../automation-button/automation-button.js';
11
11
  import {AutomationButtonReadoutStackSize} from '../../components/automation-button-readout-stack/automation-button-readout-stack.js';
12
12
  import {argTypesAbstractAutomationButtonMotorized} from '../automation-button/abstract-automation-button-storybook-helpers.js';
13
+ import {
14
+ AutomationButtonBadgeAlert,
15
+ AutomationButtonBadgeCommandLocked,
16
+ AutomationButtonBadgeControl,
17
+ AutomationButtonBadgeInterlock,
18
+ } from '../automation-button/abstract-automation-button.js';
19
+ import {
20
+ AutomationButtonBadgeKind,
21
+ setAutomationBadgeLayout,
22
+ } from '../automation-button/badge-layout.js';
13
23
 
14
24
  const meta: Meta<typeof ObcPump> = {
15
25
  title: 'Automation/Automation Devices/Pump',
@@ -79,3 +89,52 @@ export const VariantDoubleSizeLarge: Story = {
79
89
  componentSize: 'obc-component-size-large',
80
90
  },
81
91
  };
92
+
93
+ const allBadges = {
94
+ turnedOn: true,
95
+ vertical: false,
96
+ direction: AutomationButtonDirection.forward,
97
+ labelDirection: AutomationButtonLabelDirection.left,
98
+ badgeControl: AutomationButtonBadgeControl.Auto,
99
+ badgeAlert: AutomationButtonBadgeAlert.Alarm,
100
+ badgeInterlock: AutomationButtonBadgeInterlock.Interlock,
101
+ badgeCommandLocked: AutomationButtonBadgeCommandLocked.CommandLocked,
102
+ };
103
+
104
+ export const BadgeLayoutLocal: Story = {
105
+ args: {
106
+ ...allBadges,
107
+ badgeLayout: {
108
+ topLeft: AutomationButtonBadgeKind.Alert,
109
+ topRight: AutomationButtonBadgeKind.Control,
110
+ bottomRight: null,
111
+ },
112
+ },
113
+ parameters: {
114
+ docs: {
115
+ description: {
116
+ story:
117
+ 'Alert and control swap corners on this pump only, and command-locked is hidden.',
118
+ },
119
+ },
120
+ },
121
+ };
122
+
123
+ export const BadgeLayoutGlobal: Story = {
124
+ args: allBadges,
125
+ beforeEach: () => {
126
+ setAutomationBadgeLayout({
127
+ topLeft: AutomationButtonBadgeKind.Interlock,
128
+ bottomLeft: AutomationButtonBadgeKind.Control,
129
+ });
130
+ return () => setAutomationBadgeLayout(undefined);
131
+ },
132
+ parameters: {
133
+ docs: {
134
+ description: {
135
+ story:
136
+ 'The app-wide layout from `setAutomationBadgeLayout()` puts interlock top-left and control bottom-left on every device.',
137
+ },
138
+ },
139
+ },
140
+ };
@@ -0,0 +1,102 @@
1
+ import {describe, expect, it} from 'vitest';
2
+ import {render} from 'vitest-browser-lit';
3
+ import {html} from 'lit';
4
+ import '../../main.css';
5
+ import './bar-horizontal.js';
6
+ import type {ObcBarHorizontal} from './bar-horizontal.js';
7
+
8
+ const frame = () => new Promise((resolve) => requestAnimationFrame(resolve));
9
+
10
+ /** The scale factor settles a few frames after a size change. */
11
+ async function until(check: () => boolean, frames = 40): Promise<void> {
12
+ for (let i = 0; i < frames; i++) {
13
+ if (check()) return;
14
+ await frame();
15
+ }
16
+ throw new Error('the condition never held');
17
+ }
18
+
19
+ const svgOf = (el: Element) => el.querySelector('svg')!;
20
+ const scaleOf = (el: Element) =>
21
+ parseFloat(svgOf(el).style.getPropertyValue('--scale'));
22
+
23
+ /** Bar, ticks and labels with no setpoint: 48 + 24 + 60 = 132 thick. */
24
+ async function mount(boxStyle: string): Promise<ObcBarHorizontal> {
25
+ const screen = render(
26
+ html`<div style=${boxStyle}>
27
+ <obc-bar-horizontal
28
+ style="flex: 1"
29
+ .minValue=${0}
30
+ .maxValue=${100}
31
+ .value=${40}
32
+ .primaryTickmarkInterval=${20}
33
+ .hasBar=${true}
34
+ .hasScale=${true}
35
+ .barThickness=${48}
36
+ .scaleBackground=${true}
37
+ .fixedAspectRatio=${true}
38
+ ></obc-bar-horizontal>
39
+ </div>`
40
+ );
41
+ const el = screen.container.querySelector(
42
+ 'obc-bar-horizontal'
43
+ ) as ObcBarHorizontal;
44
+ await el.updateComplete;
45
+ return el;
46
+ }
47
+
48
+ describe('obc-bar-horizontal fixed aspect ratio', () => {
49
+ it('scales by the host width when the height has room', async () => {
50
+ const el = await mount('display: block; width: 600px; height: 300px');
51
+ await until(() => Math.abs(scaleOf(el) - 600 / 384) < 1e-3);
52
+ expect(svgOf(el).getScreenCTM()!.a).toBeCloseTo(600 / 384, 3);
53
+ });
54
+
55
+ it('follows the painted scale when the host is shorter than the drawing', async () => {
56
+ const el = await mount('display: flex; width: 600px; height: 100px');
57
+ const reports: number[] = [];
58
+ el.addEventListener('scale-dimensions-changed', (e) =>
59
+ reports.push((e as CustomEvent<{thickness: number}>).detail.thickness)
60
+ );
61
+ // meet: the 132-tall drawing fits 100 px before the 384-long one fits 600
62
+ await until(() => Math.abs(scaleOf(el) - 100 / 132) < 1e-3);
63
+ const svg = svgOf(el);
64
+ const text = svg.querySelector('text')!;
65
+ const painted = svg.getScreenCTM()!.a;
66
+ expect(painted).toBeCloseTo(100 / 132, 3);
67
+ expect(parseFloat(getComputedStyle(text).fontSize) * painted).toBeCloseTo(
68
+ 12,
69
+ 1
70
+ );
71
+ await until(() => reports[reports.length - 1] === 100);
72
+ });
73
+
74
+ it('re-measures the scale when the drawing thickness changes under the same box', async () => {
75
+ const el = await mount('display: flex; width: 600px; height: 100px');
76
+ const reports: number[] = [];
77
+ el.addEventListener('scale-dimensions-changed', (e) =>
78
+ reports.push((e as CustomEvent<{thickness: number}>).detail.thickness)
79
+ );
80
+ await until(() => Math.abs(scaleOf(el) - 100 / 132) < 1e-3);
81
+ // 180 + 24 + 60 = 264 tall: the same 100 px now holds less than half
82
+ reports.length = 0;
83
+ el.barThickness = 180;
84
+ await until(() => Math.abs(scaleOf(el) - 100 / 264) < 1e-3);
85
+ await until(() => reports[reports.length - 1] === 100);
86
+ });
87
+
88
+ it('measures the content box of a padded host', async () => {
89
+ const el = await mount('display: flex; width: 600px; height: 100px');
90
+ el.style.padding = '20px';
91
+ const reports: number[] = [];
92
+ el.addEventListener('scale-dimensions-changed', (e) =>
93
+ reports.push((e as CustomEvent<{thickness: number}>).detail.thickness)
94
+ );
95
+ // 60 px of content height for the 132-tall drawing
96
+ await until(() => Math.abs(scaleOf(el) - 60 / 132) < 1e-3);
97
+ reports.length = 0;
98
+ el.barThickness = 180;
99
+ await until(() => Math.abs(scaleOf(el) - 60 / 264) < 1e-3);
100
+ await until(() => reports[reports.length - 1] === 60);
101
+ });
102
+ });
@@ -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,
@@ -166,6 +167,9 @@ export class ObcBarHorizontal extends AdviceMixin(
166
167
  @state()
167
168
  private _scale = 1;
168
169
 
170
+ /** The drawing's height in viewBox units, kept from the last render for the scale measurement. */
171
+ private _viewBoxThickness = 0;
172
+
169
173
  // ResizeController automatically subscribes/unsubscribes based on component lifecycle
170
174
  // @ts-expect-error - Controller is used for side effects, not accessed directly
171
175
  private _resizeController = new ResizeController(this, {
@@ -175,14 +179,12 @@ export class ObcBarHorizontal extends AdviceMixin(
175
179
  const entry = entries[0];
176
180
  if (!entry) return;
177
181
 
178
- // Use the centralized function that computes scale based on reference size
179
- // For horizontal scales, compare container width to reference size
180
- const containerMainAxisSize = entry.contentRect.width;
181
-
182
182
  this._scale = computeFixedAspectRatioScale({
183
183
  orientation: ExternalScaleOrientation.horizontal,
184
- containerMainAxisSize,
184
+ containerMainAxisSize: entry.contentRect.width,
185
+ containerCrossAxisSize: entry.contentRect.height,
185
186
  scaleReferenceSize: this.scaleReferenceSize,
187
+ viewBoxThickness: this._viewBoxThickness,
186
188
  });
187
189
 
188
190
  // Report scaled dimensions to parent chart
@@ -320,6 +322,7 @@ export class ObcBarHorizontal extends AdviceMixin(
320
322
  const layout = computeExternalScaleLayout(
321
323
  toExternalScaleLayoutConfig(config)
322
324
  );
325
+ this._viewBoxThickness = layout.thickness;
323
326
 
324
327
  const parts = renderExternalScale(config);
325
328
 
@@ -393,6 +396,9 @@ export class ObcBarHorizontal extends AdviceMixin(
393
396
  this.setpointPresenceChanged(changed);
394
397
 
395
398
  if (!this.fixedAspectRatio || layoutChanged) {
399
+ // A thinner or thicker drawing under the same box changes the painted
400
+ // scale without a resize event.
401
+ if (this.fixedAspectRatio) this._measureScale();
396
402
  this.reportDimensions();
397
403
  }
398
404
  }
@@ -509,6 +515,20 @@ export class ObcBarHorizontal extends AdviceMixin(
509
515
  this.style.height = '';
510
516
  }
511
517
 
518
+ /** Reads the host's content box into the scale; false while the host has no size yet. */
519
+ private _measureScale(): boolean {
520
+ const {width, height} = readContentBox(this);
521
+ if (width <= 0) return false;
522
+ this._scale = computeFixedAspectRatioScale({
523
+ orientation: ExternalScaleOrientation.horizontal,
524
+ containerMainAxisSize: width,
525
+ containerCrossAxisSize: height,
526
+ scaleReferenceSize: this.scaleReferenceSize,
527
+ viewBoxThickness: this._viewBoxThickness,
528
+ });
529
+ return true;
530
+ }
531
+
512
532
  /**
513
533
  * Calculate scale based on current element size.
514
534
  * Called when fixedAspectRatio is enabled or scaleReferenceSize changes.
@@ -516,13 +536,7 @@ export class ObcBarHorizontal extends AdviceMixin(
516
536
  private _updateScaleFromCurrentSize(): void {
517
537
  // Use requestAnimationFrame to ensure layout is complete
518
538
  requestAnimationFrame(() => {
519
- const containerWidth = this.clientWidth;
520
- if (containerWidth > 0) {
521
- this._scale = computeFixedAspectRatioScale({
522
- orientation: ExternalScaleOrientation.horizontal,
523
- containerMainAxisSize: containerWidth,
524
- scaleReferenceSize: this.scaleReferenceSize,
525
- });
539
+ if (this._measureScale()) {
526
540
  this.requestUpdate();
527
541
  // Report updated dimensions to parent (matches pattern in ResizeController and _refreshBorderRadiusFromCssVar)
528
542
  this.reportDimensions();
@@ -542,17 +556,8 @@ export class ObcBarHorizontal extends AdviceMixin(
542
556
 
543
557
  // In fixed-aspect-ratio mode, recompute scale when border radius changes
544
558
  // (border radius affects effective bar thickness and therefore viewBox)
545
- if (this.fixedAspectRatio) {
546
- const rect = this.getBoundingClientRect();
547
- if (rect.width > 0) {
548
- this._scale = computeFixedAspectRatioScale({
549
- orientation: ExternalScaleOrientation.horizontal,
550
- containerMainAxisSize: rect.width,
551
- scaleReferenceSize: this.scaleReferenceSize,
552
- });
553
- // Report updated dimensions
554
- this.reportDimensions();
555
- }
559
+ if (this.fixedAspectRatio && this._measureScale()) {
560
+ this.reportDimensions();
556
561
  }
557
562
  }
558
563
 
@@ -0,0 +1,101 @@
1
+ import {describe, expect, it} from 'vitest';
2
+ import {render} from 'vitest-browser-lit';
3
+ import {html} from 'lit';
4
+ import '../../main.css';
5
+ import './bar-vertical.js';
6
+ import type {ObcBarVertical} from './bar-vertical.js';
7
+
8
+ const frame = () => new Promise((resolve) => requestAnimationFrame(resolve));
9
+
10
+ /** The scale factor settles a few frames after a size change. */
11
+ async function until(check: () => boolean, frames = 40): Promise<void> {
12
+ for (let i = 0; i < frames; i++) {
13
+ if (check()) return;
14
+ await frame();
15
+ }
16
+ throw new Error('the condition never held');
17
+ }
18
+
19
+ const svgOf = (el: Element) => el.querySelector('svg')!;
20
+ const scaleOf = (el: Element) =>
21
+ parseFloat(svgOf(el).style.getPropertyValue('--scale'));
22
+
23
+ /** Bar, ticks and labels with no setpoint: 48 + 24 + 60 = 132 thick. */
24
+ async function mount(boxStyle: string): Promise<ObcBarVertical> {
25
+ const screen = render(
26
+ html`<div style=${boxStyle}>
27
+ <obc-bar-vertical
28
+ .minValue=${0}
29
+ .maxValue=${100}
30
+ .value=${40}
31
+ .primaryTickmarkInterval=${20}
32
+ .hasBar=${true}
33
+ .hasScale=${true}
34
+ .barThickness=${48}
35
+ .scaleBackground=${true}
36
+ .fixedAspectRatio=${true}
37
+ ></obc-bar-vertical>
38
+ </div>`
39
+ );
40
+ const el = screen.container.querySelector(
41
+ 'obc-bar-vertical'
42
+ ) as ObcBarVertical;
43
+ await el.updateComplete;
44
+ return el;
45
+ }
46
+
47
+ describe('obc-bar-vertical fixed aspect ratio', () => {
48
+ it('scales by the host height when the width has room', async () => {
49
+ const el = await mount('display: block; width: 400px; height: 600px');
50
+ await until(() => Math.abs(scaleOf(el) - 600 / 384) < 1e-3);
51
+ expect(svgOf(el).getScreenCTM()!.a).toBeCloseTo(600 / 384, 3);
52
+ });
53
+
54
+ it('follows the painted scale when the host is narrower than the drawing', async () => {
55
+ const el = await mount('display: block; width: 120px; height: 600px');
56
+ const reports: number[] = [];
57
+ el.addEventListener('scale-dimensions-changed', (e) =>
58
+ reports.push((e as CustomEvent<{thickness: number}>).detail.thickness)
59
+ );
60
+ // meet: the 132-wide drawing fits 120 px before the 384-long one fits 600
61
+ await until(() => Math.abs(scaleOf(el) - 120 / 132) < 1e-3);
62
+ const svg = svgOf(el);
63
+ const text = svg.querySelector('text')!;
64
+ const painted = svg.getScreenCTM()!.a;
65
+ expect(painted).toBeCloseTo(120 / 132, 3);
66
+ expect(parseFloat(getComputedStyle(text).fontSize) * painted).toBeCloseTo(
67
+ 12,
68
+ 1
69
+ );
70
+ await until(() => reports[reports.length - 1] === 120);
71
+ });
72
+
73
+ it('re-measures the scale when the drawing thickness changes under the same box', async () => {
74
+ const el = await mount('display: block; width: 120px; height: 600px');
75
+ const reports: number[] = [];
76
+ el.addEventListener('scale-dimensions-changed', (e) =>
77
+ reports.push((e as CustomEvent<{thickness: number}>).detail.thickness)
78
+ );
79
+ await until(() => Math.abs(scaleOf(el) - 120 / 132) < 1e-3);
80
+ // 180 + 24 + 60 = 264 wide: the same 120 px now holds less than half
81
+ reports.length = 0;
82
+ el.barThickness = 180;
83
+ await until(() => Math.abs(scaleOf(el) - 120 / 264) < 1e-3);
84
+ await until(() => reports[reports.length - 1] === 120);
85
+ });
86
+
87
+ it('measures the content box of a padded host', async () => {
88
+ const el = await mount('display: block; width: 120px; height: 600px');
89
+ el.style.padding = '20px';
90
+ const reports: number[] = [];
91
+ el.addEventListener('scale-dimensions-changed', (e) =>
92
+ reports.push((e as CustomEvent<{thickness: number}>).detail.thickness)
93
+ );
94
+ // 80 px of content width for the 132-wide drawing
95
+ await until(() => Math.abs(scaleOf(el) - 80 / 132) < 1e-3);
96
+ reports.length = 0;
97
+ el.barThickness = 180;
98
+ await until(() => Math.abs(scaleOf(el) - 80 / 264) < 1e-3);
99
+ await until(() => reports[reports.length - 1] === 80);
100
+ });
101
+ });