mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
feat(ui): render rail cards and card blocks as components behind their builders
The rail cards, card blocks, settings, chips and row lists are Vue components in src/ui/rail/. createRailCards() and createRailCardBlocks() keep their update and destroy API and patch the components synchronously with render(), so Weather and Recent imagery are unchanged. Controls of a removed block dispatch nothing, a released range keeps its value until the owner's next update, and range values are written only when they differ. Weather's remount state is keyed by its panel. Tests of rendered parts run on happy-dom.
This commit is contained in:
1 parent
d9fdcfc641
commit
dcecf5c34c
20 files changed
+754
-766
No files matched your search
@@ -0,0 +1,67 @@
|
||||
<script setup>
|
||||
import { computed, onBeforeUnmount } from 'vue';
|
||||
import RailCardBlocks from './RailCardBlocks.vue';
|
||||
|
||||
// One keyed card: a disclosure header and its blocks. The owner decides
|
||||
// which card is open.
|
||||
const props = defineProps({
|
||||
card: { type: Object, required: true },
|
||||
cardClassName: { type: String, default: '' },
|
||||
badgeClassName: { type: String, default: '' },
|
||||
onParams: { type: Function, default: () => {} },
|
||||
onOpen: { type: Function, default: () => {} },
|
||||
});
|
||||
const bodyId = `rail-card-body-${nextBodyId()}`;
|
||||
const open = computed(() => props.card.open !== false);
|
||||
// A removed card's header opens nothing.
|
||||
let live = true;
|
||||
onBeforeUnmount(() => {
|
||||
live = false;
|
||||
});
|
||||
const select = () => live && props.onOpen(props.card.id);
|
||||
</script>
|
||||
|
||||
<script>
|
||||
let bodies = 0;
|
||||
const nextBodyId = () => ++bodies;
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<article
|
||||
:class="`rail-card${cardClassName ? ` ${cardClassName}` : ''}${open ? ' is-open' : ''}`"
|
||||
:data-card-id="card.id"
|
||||
:data-open="String(open)"
|
||||
><button
|
||||
class="rail-card-header"
|
||||
type="button"
|
||||
:aria-controls="bodyId"
|
||||
:aria-expanded="String(open)"
|
||||
@click="select"
|
||||
><span class="rail-card-labels"
|
||||
><span class="rail-card-heading"
|
||||
><span class="data-icon" aria-hidden="true" :hidden="!card.icon">{{
|
||||
card.icon || ''
|
||||
}}</span
|
||||
><span class="rail-card-title rail-card-nowrap" :title="card.title">{{
|
||||
card.title
|
||||
}}</span></span
|
||||
><span
|
||||
:class="`gev-status-badge gev-status-badge--plain rail-card-badge${badgeClassName ? ` ${badgeClassName}` : ''}`"
|
||||
:hidden="!card.badge"
|
||||
>{{ card.badge || '' }}</span
|
||||
></span
|
||||
><span class="rail-card-disclosure" aria-hidden="true">{{
|
||||
open ? '▾' : '▸'
|
||||
}}</span
|
||||
><span
|
||||
:class="`rail-card-compact${card.compactStatus ? ' status' : ''}`"
|
||||
:hidden="open"
|
||||
>{{ card.compact || '' }}</span
|
||||
></button
|
||||
><div :id="bodyId" class="rail-card-body" :hidden="!open"
|
||||
><RailCardBlocks
|
||||
:blocks="card.blocks || []"
|
||||
:card-id="card.id"
|
||||
:on-params="onParams" /></div
|
||||
></article>
|
||||
</template>
|
||||
@@ -0,0 +1,197 @@
|
||||
<script setup>
|
||||
import { onBeforeUnmount } from 'vue';
|
||||
import { t } from '../../i18n/index.js';
|
||||
import { formatScaleLabel, unitLabel } from '../unitLabels.js';
|
||||
import RailCardSetting from './RailCardSetting.vue';
|
||||
import RailChips from './RailChips.vue';
|
||||
import RailRowList from './RailRowList.vue';
|
||||
|
||||
// Optional, ordered blocks without disclosure state. Every control is keyed.
|
||||
const props = defineProps({
|
||||
blocks: { type: Array, default: () => [] },
|
||||
cardId: { type: String, default: undefined },
|
||||
onParams: { type: Function, default: () => {} },
|
||||
});
|
||||
|
||||
// Controls of removed blocks dispatch nothing: a detached element keeps its
|
||||
// last handler, which names a block the owner no longer lists.
|
||||
let live = true;
|
||||
onBeforeUnmount(() => {
|
||||
live = false;
|
||||
});
|
||||
|
||||
function dispatch(descriptor, block) {
|
||||
if (!live || !props.blocks.includes(block)) return;
|
||||
if (!descriptor || descriptor.disabled) return;
|
||||
if (descriptor.params) props.onParams(props.cardId, descriptor.params);
|
||||
else descriptor.onClick?.();
|
||||
}
|
||||
|
||||
const rootTag = (block) =>
|
||||
block.type === 'list' ? 'ol' : block.type === 'actions' ? 'footer' : 'div';
|
||||
const lines = (block) => block.lines || [{ id: block.id, text: block.text }];
|
||||
|
||||
// "960 — 1050+ гПа": the unit stays with the last number.
|
||||
function scaleText(low, high, units) {
|
||||
const unit = unitLabel(units);
|
||||
const range = `${formatScaleLabel(low)} — ${formatScaleLabel(high)}`;
|
||||
return unit ? `${range} ${unit}` : range;
|
||||
}
|
||||
|
||||
function legendView(legend) {
|
||||
const colors = (legend?.colors || []).filter((color) =>
|
||||
/^#(?:[0-9a-f]{6}|[0-9a-f]{8})$/i.test(color),
|
||||
);
|
||||
const visible = colors.length >= 2 && colors.length === legend.colors.length;
|
||||
const zeroVisible =
|
||||
visible &&
|
||||
legend.units === '°C' &&
|
||||
Number.isInteger(legend.zeroIndex) &&
|
||||
legend.zeroIndex >= 0 &&
|
||||
legend.zeroIndex < colors.length;
|
||||
return {
|
||||
colors,
|
||||
visible,
|
||||
categorical: Boolean(legend?.categorical),
|
||||
zeroVisible,
|
||||
// Styles are strings so an unchanged legend writes nothing; browsers
|
||||
// normalize the colors they read back.
|
||||
rampStyle: visible
|
||||
? `background: linear-gradient(to right, ${colors.join(',')})`
|
||||
: undefined,
|
||||
zeroStyle: zeroVisible
|
||||
? `left: ${(legend.zeroIndex / (colors.length - 1)) * 100}%`
|
||||
: undefined,
|
||||
scale: visible
|
||||
? scaleText(legend.labels[0], legend.labels.at(-1), legend.units)
|
||||
: '',
|
||||
labels: legend?.labels || [],
|
||||
};
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<template v-for="block in blocks" :key="`${block.id}:${block.type}`"
|
||||
><RailRowList
|
||||
v-if="block.type === 'list'"
|
||||
:list="block.list"
|
||||
:block-id="block.id"
|
||||
:on-select="(item) => dispatch(item, block)"
|
||||
/><component
|
||||
:is="rootTag(block)"
|
||||
v-else
|
||||
:class="`rail-card-${block.type}`"
|
||||
:data-block-id="block.id"
|
||||
:hidden="
|
||||
block.type === 'legend' ? !legendView(block.legend).visible : undefined
|
||||
"
|
||||
><RailCardBlocks
|
||||
v-if="block.type === 'settings'"
|
||||
:blocks="
|
||||
(block.settings || []).map((setting) => ({
|
||||
...setting,
|
||||
type: 'setting',
|
||||
}))
|
||||
"
|
||||
:card-id="cardId"
|
||||
:on-params="onParams"
|
||||
/><RailCardSetting
|
||||
v-else-if="block.type === 'setting'"
|
||||
:setting="block"
|
||||
:dispatch="(descriptor) => dispatch(descriptor, block)"
|
||||
/><template v-else-if="block.type === 'result'"
|
||||
><div class="rail-card-result-header"
|
||||
><span class="panel-title">{{ block.label }}</span
|
||||
><button
|
||||
class="data-toggle-chip"
|
||||
type="button"
|
||||
data-action-id="clear"
|
||||
:aria-label="t('Clear reading')"
|
||||
:title="t('Clear reading')"
|
||||
:hidden="!block.clear"
|
||||
@click="dispatch(block.clear, block)"
|
||||
>×</button
|
||||
></div
|
||||
><div class="rail-card-result-lines"
|
||||
><RailCardBlocks
|
||||
:blocks="[
|
||||
{ id: 'result-lines', type: 'lines', lines: block.lines },
|
||||
]"
|
||||
:card-id="cardId"
|
||||
:on-params="onParams" /></div></template
|
||||
><template v-else-if="block.type === 'legend'"
|
||||
><div
|
||||
class="rail-card-ramp"
|
||||
:hidden="
|
||||
!legendView(block.legend).visible ||
|
||||
legendView(block.legend).categorical
|
||||
"
|
||||
:style="legendView(block.legend).rampStyle"
|
||||
><span
|
||||
class="rail-card-zero"
|
||||
aria-hidden="true"
|
||||
:hidden="!legendView(block.legend).zeroVisible"
|
||||
:style="legendView(block.legend).zeroStyle"
|
||||
></span></div
|
||||
><div
|
||||
class="rail-card-scale"
|
||||
:hidden="legendView(block.legend).categorical"
|
||||
>{{ legendView(block.legend).scale }}</div
|
||||
><div
|
||||
class="rail-card-legend-entries"
|
||||
:hidden="!legendView(block.legend).categorical"
|
||||
><template v-if="legendView(block.legend).categorical"
|
||||
><div
|
||||
v-for="(color, index) in legendView(block.legend).colors"
|
||||
:key="index"
|
||||
class="rail-card-legend-entry"
|
||||
:data-color="color"
|
||||
><span
|
||||
class="rail-card-legend-swatch"
|
||||
:style="`background: ${color}`"
|
||||
></span
|
||||
><span>{{
|
||||
legendView(block.legend).labels[index] || ''
|
||||
}}</span></div
|
||||
></template
|
||||
></div
|
||||
></template
|
||||
><template v-else-if="block.type === 'actions'"
|
||||
><template
|
||||
v-for="action in block.actions || []"
|
||||
:key="`${action.id}:${action.href ? 'a' : 'button'}`"
|
||||
><a
|
||||
v-if="action.href"
|
||||
class="data-toggle-chip"
|
||||
:data-action-id="action.id"
|
||||
target="_blank"
|
||||
rel="noopener"
|
||||
:href="action.href"
|
||||
:title="action.title || undefined"
|
||||
@click="dispatch(action, block)"
|
||||
>{{ action.label }}</a
|
||||
><button
|
||||
v-else
|
||||
class="data-toggle-chip"
|
||||
type="button"
|
||||
:data-action-id="action.id"
|
||||
:title="action.title || undefined"
|
||||
:disabled="Boolean(action.disabled)"
|
||||
@click="dispatch(action, block)"
|
||||
>{{ action.label }}</button
|
||||
><span class="rail-card-action-hint" :hidden="!action.hint">{{
|
||||
action.hint || ''
|
||||
}}</span></template
|
||||
></template
|
||||
><template v-else
|
||||
><div
|
||||
v-for="line in lines(block)"
|
||||
:key="line.id"
|
||||
:class="`rail-card-line${line.muted ? ' muted' : ''}`"
|
||||
:data-line-id="line.id"
|
||||
>{{ String(line.text ?? '') }}</div
|
||||
></template
|
||||
></component
|
||||
></template
|
||||
>
|
||||
</template>
|
||||
@@ -0,0 +1,104 @@
|
||||
<script setup>
|
||||
import { computed, ref, watch } from 'vue';
|
||||
import { vRangeValue } from './rangeValue.js';
|
||||
import RailChips from './RailChips.vue';
|
||||
|
||||
// One setting: a chip group and an optional range. The handle's value is held
|
||||
// until the owner's next update; a range that commits on change keeps it
|
||||
// across updates until its descriptor changes.
|
||||
const props = defineProps({
|
||||
setting: { type: Object, required: true },
|
||||
dispatch: { type: Function, required: true },
|
||||
});
|
||||
const draft = ref(null);
|
||||
|
||||
watch(
|
||||
() => props.setting,
|
||||
() => {
|
||||
const range = props.setting.range;
|
||||
const held = draft.value;
|
||||
if (
|
||||
held &&
|
||||
(held.committed ||
|
||||
!range ||
|
||||
range.disabled ||
|
||||
!range.commitOnChange ||
|
||||
['id', 'param', 'min', 'max', 'step', 'value'].some(
|
||||
(key) => range[key] !== held.range[key],
|
||||
))
|
||||
)
|
||||
draft.value = null;
|
||||
},
|
||||
{ flush: 'sync' },
|
||||
);
|
||||
|
||||
const range = computed(() => props.setting.range);
|
||||
const value = computed(() => {
|
||||
const { min, max } = range.value;
|
||||
const candidate = Number(draft.value?.value ?? range.value.value);
|
||||
return Math.min(
|
||||
Number(max),
|
||||
Math.max(Number(min), Number.isFinite(candidate) ? candidate : Number(min)),
|
||||
);
|
||||
});
|
||||
const valueText = computed(() => `${value.value}${range.value.unit || ''}`);
|
||||
|
||||
function rangeParams(input) {
|
||||
const current = range.value;
|
||||
const next = Number(input.value);
|
||||
if (
|
||||
!current?.param ||
|
||||
current.disabled ||
|
||||
!Number.isFinite(next) ||
|
||||
next < Number(current.min) ||
|
||||
next > Number(current.max)
|
||||
)
|
||||
return null;
|
||||
return { params: { [current.param]: next } };
|
||||
}
|
||||
|
||||
function onInput(event) {
|
||||
const params = rangeParams(event.target);
|
||||
if (!params) return;
|
||||
// The readout follows the handle at once, whether or not the owner redraws.
|
||||
draft.value = { range: range.value, value: Number(event.target.value) };
|
||||
if (!range.value.commitOnChange) props.dispatch(params);
|
||||
}
|
||||
|
||||
function onChange(event) {
|
||||
if (!range.value?.commitOnChange) return;
|
||||
const params = rangeParams(event.target);
|
||||
// Committed: the next update shows the owner's value, as before.
|
||||
if (draft.value) draft.value = { ...draft.value, committed: true };
|
||||
if (params) props.dispatch(params);
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<span class="rail-card-setting-label panel-title">{{ setting.label }}</span
|
||||
><div class="rail-card-chips" role="group" :aria-label="setting.label"
|
||||
><RailChips :chips="setting.chips" :on-select="dispatch" /></div
|
||||
><div class="rail-card-setting-range-row" :hidden="!range"
|
||||
><template v-if="range"
|
||||
><span class="rail-card-setting-range-label" :hidden="!range.label">{{
|
||||
range.label || ''
|
||||
}}</span
|
||||
><input
|
||||
type="range"
|
||||
class="gev-quantitative-slider rail-card-setting-range"
|
||||
:data-range-id="range.id || setting.id"
|
||||
:min="String(Number(range.min))"
|
||||
:max="String(Number(range.max))"
|
||||
:step="String(Number(range.step))"
|
||||
v-range-value="String(value)"
|
||||
:disabled="Boolean(range.disabled)"
|
||||
:aria-label="range.ariaLabel || setting.label"
|
||||
:aria-valuetext="valueText"
|
||||
@input="onInput"
|
||||
@change="onChange"
|
||||
/><span class="rail-card-setting-range-value">{{
|
||||
valueText
|
||||
}}</span></template
|
||||
></div
|
||||
>
|
||||
</template>
|
||||
@@ -0,0 +1,24 @@
|
||||
<script setup>
|
||||
import RailCard from './RailCard.vue';
|
||||
|
||||
// Keyed cards in the owner's order; the owner owns the open-card choice.
|
||||
defineProps({
|
||||
cards: { type: Array, default: () => [] },
|
||||
cardClassName: { type: String, default: '' },
|
||||
badgeClassName: { type: String, default: '' },
|
||||
onParams: { type: Function, default: () => {} },
|
||||
onOpen: { type: Function, default: () => {} },
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<RailCard
|
||||
v-for="card in cards"
|
||||
:key="card.id"
|
||||
:card="card"
|
||||
:card-class-name="cardClassName"
|
||||
:badge-class-name="badgeClassName"
|
||||
:on-params="onParams"
|
||||
:on-open="onOpen"
|
||||
/>
|
||||
</template>
|
||||
@@ -0,0 +1,28 @@
|
||||
<script setup>
|
||||
// Keyed chips shared by rail cards; the owner decides what a click does.
|
||||
defineProps({
|
||||
chips: { type: Array, default: () => [] },
|
||||
onSelect: { type: Function, default: () => {} },
|
||||
});
|
||||
|
||||
const chipClass = (chip) =>
|
||||
`data-toggle-chip chip-${chip.state || (chip.active ? 'active' : 'idle')}${chip.active ? ' active' : ''}`;
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<button
|
||||
v-for="chip in chips"
|
||||
:key="chip.id"
|
||||
type="button"
|
||||
:data-chip-id="chip.id"
|
||||
:class="chipClass(chip)"
|
||||
:style="chip.color ? `--chip-color: ${chip.color}` : undefined"
|
||||
:data-chip-color="chip.color || undefined"
|
||||
:title="chip.title || undefined"
|
||||
:disabled="Boolean(chip.disabled)"
|
||||
:aria-pressed="String(Boolean(chip.active))"
|
||||
:aria-busy="String(Boolean(chip.busy))"
|
||||
@click="onSelect(chip)"
|
||||
>{{ chip.label }}</button
|
||||
>
|
||||
</template>
|
||||
@@ -0,0 +1,62 @@
|
||||
<script setup>
|
||||
import { onMounted, onUpdated, ref } from 'vue';
|
||||
|
||||
// An ordered list whose rows keep their identity, so a focused row stays.
|
||||
const props = defineProps({
|
||||
list: { type: Object, default: null },
|
||||
blockId: { type: String, default: undefined },
|
||||
onSelect: { type: Function, default: () => {} },
|
||||
});
|
||||
const root = ref(null);
|
||||
|
||||
const rowClass = (item) =>
|
||||
`data-row-list-item${item.disabled ? ' note' : ''}${item.active ? ' active' : ''}${item.current ? ' current' : ''}`;
|
||||
|
||||
// Follow the flight, but never steal a scroll the reader is making
|
||||
// themselves: only when the step actually changed.
|
||||
function followCurrent() {
|
||||
const list = root.value;
|
||||
if (!list) return;
|
||||
const current = props.list?.items?.find((item) => item.current);
|
||||
if (!current) {
|
||||
delete list.dataset.currentId;
|
||||
return;
|
||||
}
|
||||
if (list.dataset.currentId === current.id) return;
|
||||
list.dataset.currentId = current.id;
|
||||
[...list.querySelectorAll('.data-row-list-item')]
|
||||
.find((button) => button.dataset.listItemId === current.id)
|
||||
?.scrollIntoView?.({ block: 'nearest' });
|
||||
}
|
||||
onMounted(followCurrent);
|
||||
onUpdated(followCurrent);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<ol
|
||||
ref="root"
|
||||
class="data-row-list"
|
||||
:data-block-id="blockId"
|
||||
:hidden="!list?.items?.length"
|
||||
:aria-label="list?.ariaLabel || ''"
|
||||
><li
|
||||
v-for="item in list?.items ?? []"
|
||||
:key="item.id"
|
||||
:data-list-item-id="item.id"
|
||||
><button
|
||||
type="button"
|
||||
:class="rowClass(item)"
|
||||
:data-list-item-id="item.id"
|
||||
:disabled="Boolean(item.disabled)"
|
||||
:aria-current="item.current ? 'step' : 'false'"
|
||||
:aria-pressed="item.active ? 'true' : 'false'"
|
||||
:title="String(item.text ?? '')"
|
||||
@click="onSelect(item)"
|
||||
><span class="data-row-list-lead">{{ String(item.lead ?? '') }}</span
|
||||
><span class="data-row-list-text">{{
|
||||
String(item.text ?? '')
|
||||
}}</span></button
|
||||
></li
|
||||
></ol
|
||||
>
|
||||
</template>
|
||||
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* `v-range-value`: a range input's live value, written only when it differs.
|
||||
* A `:value` binding would also rewrite the value attribute on every patch;
|
||||
* the directive runs after the element's min, max and step are patched, so
|
||||
* the value is never clamped to a stale range.
|
||||
*/
|
||||
const apply = (el, { value }) => {
|
||||
const next = String(value);
|
||||
if (el.value !== next) el.value = next;
|
||||
};
|
||||
export const vRangeValue = { mounted: apply, updated: apply };
|
||||
Reference in new issue
Block a user