@n1kname/components 0.17.2 → 0.19.0
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/package.json +8 -3
- package/src/FloatPanel.astro +150 -0
- package/src/FloatToggle.astro +60 -0
- package/src/MapStage.astro +284 -0
- package/src/PageHeader.astro +33 -2
- package/src/PeriodPicker.astro +13 -9
- package/src/RampLegend.astro +87 -0
- package/src/YearCalendar.astro +207 -0
- package/src/mapstage.js +100 -0
- package/src/period.js +36 -12
- package/src/yearcalendar.js +268 -0
- package/src/yearcalendar.test.mjs +77 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* What a colour scale means, low to high: a title, an optional note (how many), and the ramp
|
|
4
|
+
* between its two ends. The heat ramp by default; pass `colors` for another, such as the
|
|
5
|
+
* sequential tokens. On a map it goes in a FloatPanel; under a chart it stands alone.
|
|
6
|
+
*
|
|
7
|
+
* `steps` draws the colours as blocks, for a scale that is binned; the default is a smooth
|
|
8
|
+
* gradient, for one that isn't. Change the note from script through its data-ramp-note element.
|
|
9
|
+
*/
|
|
10
|
+
interface Props {
|
|
11
|
+
/** What the colour counts: "GPS points". */
|
|
12
|
+
label: string;
|
|
13
|
+
/** After the title, such as how many: "1,284 cells". */
|
|
14
|
+
note?: string;
|
|
15
|
+
low?: string;
|
|
16
|
+
high?: string;
|
|
17
|
+
/** Low to high. Any CSS colours, best tokens. */
|
|
18
|
+
colors?: string[];
|
|
19
|
+
steps?: boolean;
|
|
20
|
+
id?: string;
|
|
21
|
+
[key: string]: unknown;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const HEAT = [1, 2, 3, 4, 5, 6].map((i) => `var(--c-heat-${i})`);
|
|
25
|
+
const { label, note, low = 'Low', high = 'High', colors = HEAT, steps = false, id, class: className, ...rest } = Astro.props;
|
|
26
|
+
const stops = steps
|
|
27
|
+
? colors.map((c, i) => `${c} ${(i / colors.length) * 100}% ${((i + 1) / colors.length) * 100}%`).join(', ')
|
|
28
|
+
: colors.join(', ');
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
<div class:list={['ramp-legend', className]} id={id} {...rest}>
|
|
32
|
+
<p class="what"><strong>{label}</strong><span data-ramp-note>{note ? ` · ${note}` : ''}</span></p>
|
|
33
|
+
<p class="scale">
|
|
34
|
+
<span>{low}</span><span class="sr"> to </span><i aria-hidden="true" style={`--ramp: linear-gradient(to right, ${stops})`}></i><span>{high}</span>
|
|
35
|
+
</p>
|
|
36
|
+
</div>
|
|
37
|
+
|
|
38
|
+
<style>
|
|
39
|
+
.ramp-legend {
|
|
40
|
+
display: grid;
|
|
41
|
+
gap: var(--sp-2);
|
|
42
|
+
color: var(--c-text-secondary);
|
|
43
|
+
font-size: var(--text-caption);
|
|
44
|
+
line-height: var(--leading-caption);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
p {
|
|
48
|
+
margin: 0;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
strong {
|
|
52
|
+
color: var(--c-text);
|
|
53
|
+
font-weight: 600;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
.scale {
|
|
57
|
+
display: flex;
|
|
58
|
+
align-items: center;
|
|
59
|
+
gap: var(--sp-2);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/* A hairline edge, so the yellow keeps its shape on a light surface. */
|
|
63
|
+
i {
|
|
64
|
+
flex: none;
|
|
65
|
+
width: 120px;
|
|
66
|
+
height: 10px;
|
|
67
|
+
border-radius: 3px;
|
|
68
|
+
background: var(--ramp);
|
|
69
|
+
box-shadow: inset 0 0 0 1px color-mix(in oklab, var(--c-text) 14%, transparent);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
.sr {
|
|
73
|
+
position: absolute;
|
|
74
|
+
width: 1px;
|
|
75
|
+
height: 1px;
|
|
76
|
+
overflow: hidden;
|
|
77
|
+
clip-path: inset(50%);
|
|
78
|
+
white-space: nowrap;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
@media (forced-colors: active) {
|
|
82
|
+
i {
|
|
83
|
+
forced-color-adjust: none;
|
|
84
|
+
outline: 1px solid CanvasText;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
</style>
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* One year of days at a glance, a week to a column: which days had something, and how much, in
|
|
4
|
+
* steps of colour. The days are squares that share the width there is, so the year fits a phone
|
|
5
|
+
* and fills a card.
|
|
6
|
+
*
|
|
7
|
+
* It draws the empty year at build time, so the page keeps its shape; fill it with
|
|
8
|
+
* setYearCalendar from `@n1kname/components/yearcalendar.js` when the days arrive, or pass `days`
|
|
9
|
+
* to draw them now (the card and keys still need setYearCalendar). The heat ramp by default; `tone`
|
|
10
|
+
* for one colour in `levels` strengths, or `colors` for any ramp.
|
|
11
|
+
*/
|
|
12
|
+
import '@n1kname/charts/pointer.css';
|
|
13
|
+
import { calendarHTML } from './yearcalendar.js';
|
|
14
|
+
|
|
15
|
+
interface Props {
|
|
16
|
+
/** Unique on the page; setYearCalendar finds the calendar by it. */
|
|
17
|
+
id: string;
|
|
18
|
+
year: number;
|
|
19
|
+
/** ISO date → number, drawn at build time. */
|
|
20
|
+
days?: Record<string, number>;
|
|
21
|
+
/** One colour, in `levels` strengths: var(--c-chart-6). */
|
|
22
|
+
tone?: string;
|
|
23
|
+
levels?: number;
|
|
24
|
+
/** Low to high. Any CSS colours, best tokens. */
|
|
25
|
+
colors?: string[];
|
|
26
|
+
/** Where each colour after the first starts. */
|
|
27
|
+
steps?: number[];
|
|
28
|
+
/** 1 for Monday, 0 for Sunday. */
|
|
29
|
+
weekStart?: 0 | 1;
|
|
30
|
+
/** What the grid says to a screen reader: "Running in 2026: 84 days". */
|
|
31
|
+
label?: string;
|
|
32
|
+
[key: string]: unknown;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const { id, year, days, tone, levels, colors, steps, weekStart = 1, label, class: className, ...rest } = Astro.props;
|
|
36
|
+
const html = calendarHTML({ year, days, tone, levels, colors, steps, weekStart, label });
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
<div id={id} class:list={['year-calendar', className]} {...rest}><Fragment set:html={html} /></div>
|
|
40
|
+
|
|
41
|
+
<style>
|
|
42
|
+
.year-calendar {
|
|
43
|
+
container-type: inline-size;
|
|
44
|
+
position: relative;
|
|
45
|
+
/* Wide, the days stop growing at --year-calendar-cell; the frame is as wide as they need. */
|
|
46
|
+
--yc-gap: 2px;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
.year-calendar :global(.yc-frame) {
|
|
50
|
+
position: relative;
|
|
51
|
+
display: grid;
|
|
52
|
+
grid-template-columns: 1rem minmax(0, 1fr);
|
|
53
|
+
grid-template-areas: '. months' 'wd grid';
|
|
54
|
+
gap: 2px 4px;
|
|
55
|
+
max-width: calc(1rem + 4px + var(--weeks) * (var(--year-calendar-cell, 16px) + var(--yc-gap)));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/* Each month's name over the week holding its first day. */
|
|
59
|
+
.year-calendar :global(.yc-months) {
|
|
60
|
+
grid-area: months;
|
|
61
|
+
position: relative;
|
|
62
|
+
height: 0.875rem;
|
|
63
|
+
font-size: 0.625rem;
|
|
64
|
+
line-height: 1;
|
|
65
|
+
color: var(--c-text-muted);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
.year-calendar :global(.yc-months span) {
|
|
69
|
+
position: absolute;
|
|
70
|
+
left: var(--at);
|
|
71
|
+
white-space: nowrap;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/* On a phone the gaps would take a third of the width: a pixel each gives it to the days. */
|
|
75
|
+
@container (max-width: 480px) {
|
|
76
|
+
.year-calendar :global(.yc-frame) {
|
|
77
|
+
--yc-gap: 1px;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
.year-calendar :global(.yc-grid i) {
|
|
81
|
+
border-radius: 1px;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/* Narrow, a letter each. */
|
|
86
|
+
@container (max-width: 320px) {
|
|
87
|
+
.year-calendar :global(.yc-months span) {
|
|
88
|
+
font-size: 0;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
.year-calendar :global(.yc-months span)::before {
|
|
92
|
+
content: attr(data-short);
|
|
93
|
+
font-size: 0.625rem;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/* The letters take the grid's height and never set it (contain: size): when the days are
|
|
98
|
+
smaller than a letter, the rows stay as tall as the days. */
|
|
99
|
+
.year-calendar :global(.yc-wd) {
|
|
100
|
+
grid-area: wd;
|
|
101
|
+
contain: size;
|
|
102
|
+
display: grid;
|
|
103
|
+
grid-template-rows: repeat(7, minmax(0, 1fr));
|
|
104
|
+
gap: var(--yc-gap);
|
|
105
|
+
align-items: center;
|
|
106
|
+
font-size: 0.5625rem;
|
|
107
|
+
line-height: 1;
|
|
108
|
+
color: var(--c-text-muted);
|
|
109
|
+
text-align: right;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/* Each day a square: the weeks share the width and the rows follow from it. */
|
|
113
|
+
.year-calendar :global(.yc-grid) {
|
|
114
|
+
grid-area: grid;
|
|
115
|
+
min-width: 0;
|
|
116
|
+
display: grid;
|
|
117
|
+
grid-template-rows: repeat(7, auto);
|
|
118
|
+
grid-auto-columns: minmax(0, 1fr);
|
|
119
|
+
grid-auto-flow: column;
|
|
120
|
+
gap: var(--yc-gap);
|
|
121
|
+
align-content: start;
|
|
122
|
+
border-radius: 2px;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
.year-calendar :global(.yc-grid:focus-visible) {
|
|
126
|
+
outline: 2px solid var(--c-focus);
|
|
127
|
+
outline-offset: 3px;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
.year-calendar :global(.yc-grid i) {
|
|
131
|
+
aspect-ratio: 1;
|
|
132
|
+
border-radius: 2px;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
.year-calendar :global(.yc-grid .off) {
|
|
136
|
+
background: color-mix(in oklab, var(--c-text) 9%, var(--c-surface));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/* Days still to come: paler, so the year so far reads as one shape. */
|
|
140
|
+
.year-calendar :global(.yc-grid .later) {
|
|
141
|
+
background: color-mix(in oklab, var(--c-text) 4%, var(--c-surface));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
.year-calendar :global(.yc-grid .out) {
|
|
145
|
+
visibility: hidden;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/* A hairline edge, so a pale step (the heat ramp's yellow) keeps its shape on a light surface. */
|
|
149
|
+
.year-calendar :global(.yc-grid .on) {
|
|
150
|
+
box-shadow: inset 0 0 0 1px color-mix(in oklab, var(--c-text) 10%, transparent);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
.year-calendar :global([data-step='1']) { background: var(--yc-1); }
|
|
154
|
+
.year-calendar :global([data-step='2']) { background: var(--yc-2); }
|
|
155
|
+
.year-calendar :global([data-step='3']) { background: var(--yc-3); }
|
|
156
|
+
.year-calendar :global([data-step='4']) { background: var(--yc-4); }
|
|
157
|
+
.year-calendar :global([data-step='5']) { background: var(--yc-5); }
|
|
158
|
+
.year-calendar :global([data-step='6']) { background: var(--yc-6); }
|
|
159
|
+
.year-calendar :global([data-step='7']) { background: var(--yc-7); }
|
|
160
|
+
.year-calendar :global([data-step='8']) { background: var(--yc-8); }
|
|
161
|
+
.year-calendar :global([data-step='9']) { background: var(--yc-9); }
|
|
162
|
+
|
|
163
|
+
/* The card's figure beside its colour, and the frame round the day a little rounder. */
|
|
164
|
+
.year-calendar :global(.chart-pointer-one) {
|
|
165
|
+
display: flex;
|
|
166
|
+
align-items: center;
|
|
167
|
+
gap: var(--sp-2);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
.year-calendar :global(.chart-pointer-one i) {
|
|
171
|
+
box-shadow: inset 0 0 0 1px color-mix(in oklab, var(--c-text) 14%, transparent);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
.year-calendar :global(.chart-pointer-cell) {
|
|
175
|
+
border-width: 1.5px;
|
|
176
|
+
border-radius: 3px;
|
|
177
|
+
margin: -1.5px 0 0 -1.5px;
|
|
178
|
+
padding: 1.5px;
|
|
179
|
+
box-sizing: content-box;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
.year-calendar :global(.chart-pointer-card) {
|
|
183
|
+
z-index: 5;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
.year-calendar :global(.yc-sr) {
|
|
187
|
+
position: absolute;
|
|
188
|
+
width: 1px;
|
|
189
|
+
height: 1px;
|
|
190
|
+
overflow: hidden;
|
|
191
|
+
clip-path: inset(50%);
|
|
192
|
+
white-space: nowrap;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
@media (forced-colors: active) {
|
|
196
|
+
.year-calendar :global(.yc-grid i) {
|
|
197
|
+
forced-color-adjust: none;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
.year-calendar :global(.yc-grid .off),
|
|
201
|
+
.year-calendar :global(.yc-grid .later) {
|
|
202
|
+
background: Canvas;
|
|
203
|
+
outline: 1px solid GrayText;
|
|
204
|
+
outline-offset: -1px;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
</style>
|
package/src/mapstage.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// What a map in a MapStage needs from script: which message covers it, the room its floating panels
|
|
2
|
+
// leave, side panels put away and brought back, and tokens resolved for a canvas. None of it
|
|
3
|
+
// imports a map library: it hands back plain values MapLibre takes as they are.
|
|
4
|
+
|
|
5
|
+
/** The heat ramp, cold to warm, as tokens. The same in both themes. */
|
|
6
|
+
export const HEAT = [1, 2, 3, 4, 5, 6].map((i) => `var(--c-heat-${i})`);
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A canvas reads colours, not tokens: a token (or any CSS colour), resolved now, as rgb(). Call it
|
|
10
|
+
* again when the theme changes (onTheme).
|
|
11
|
+
*/
|
|
12
|
+
export const resolve = (value) => {
|
|
13
|
+
const probe = document.createElement('i');
|
|
14
|
+
probe.style.color = value;
|
|
15
|
+
probe.hidden = true;
|
|
16
|
+
document.body.append(probe);
|
|
17
|
+
const colour = getComputedStyle(probe).color;
|
|
18
|
+
probe.remove();
|
|
19
|
+
return colour;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/** Calls fn when the theme changes, by the switch (data-theme) or by the system. */
|
|
23
|
+
export const onTheme = (fn) => {
|
|
24
|
+
new MutationObserver(fn).observe(document.documentElement, { attributes: true, attributeFilter: ['data-theme'] });
|
|
25
|
+
matchMedia('(prefers-color-scheme: dark)').addEventListener('change', fn);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A MapLibre expression that colours a feature by a number from 0 to 1 in `property`, along the
|
|
30
|
+
* heat ramp or any list of colours: ['interpolate', ['linear'], ['get', property], 0, …, 1, …].
|
|
31
|
+
* The colours are resolved now; build it again when the theme changes.
|
|
32
|
+
*/
|
|
33
|
+
export const ramp = (property, colours = HEAT) => [
|
|
34
|
+
'interpolate',
|
|
35
|
+
['linear'],
|
|
36
|
+
['get', property],
|
|
37
|
+
...colours.flatMap((c, i) => [i / (colours.length - 1), resolve(c)]),
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Which message covers the map: 'loading' (and the map dims), 'empty', 'failed', or 'ready' for
|
|
42
|
+
* none. A message the MapStage wasn't given is skipped.
|
|
43
|
+
*/
|
|
44
|
+
export const setStage = (stage, state) => {
|
|
45
|
+
const root = stage.closest('[data-map-stage]') ?? stage;
|
|
46
|
+
root.toggleAttribute('data-loading', state === 'loading');
|
|
47
|
+
root.querySelectorAll('[data-over]').forEach((p) => (p.hidden = p.dataset.over !== state));
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Padding for fitBounds and easeTo, so what the map shows stays clear of the panels floating over
|
|
52
|
+
* it: under a floating title (MapStage fill mode) and a top panel, above a bottom one, left of a
|
|
53
|
+
* side panel while it floats. `edge` is the
|
|
54
|
+
* least on every side; `gap` the room kept from a panel. Each axis keeps at least a fifth of the map.
|
|
55
|
+
*/
|
|
56
|
+
export const room = (stage, { edge = 48, gap = 16 } = {}) => {
|
|
57
|
+
const root = stage.closest('[data-map-stage]') ?? stage;
|
|
58
|
+
const frame = root.querySelector('.frame').getBoundingClientRect();
|
|
59
|
+
const pad = { top: edge, right: edge, bottom: edge, left: edge };
|
|
60
|
+
const head = root.querySelector(':scope > .head');
|
|
61
|
+
if (head && getComputedStyle(head).position === 'absolute') pad.top = Math.max(pad.top, head.getBoundingClientRect().bottom - frame.top + gap);
|
|
62
|
+
for (const panel of root.querySelectorAll('[data-float]')) {
|
|
63
|
+
const style = getComputedStyle(panel);
|
|
64
|
+
if (style.position !== 'absolute' || style.display === 'none') continue;
|
|
65
|
+
const r = panel.getBoundingClientRect();
|
|
66
|
+
const place = panel.dataset.float;
|
|
67
|
+
if (place === 'side') pad.right = Math.max(pad.right, frame.right - r.left + gap);
|
|
68
|
+
else if (place.startsWith('top') || place === 'head') pad.top = Math.max(pad.top, r.bottom - frame.top + gap);
|
|
69
|
+
else pad.bottom = Math.max(pad.bottom, frame.bottom - r.top + gap);
|
|
70
|
+
}
|
|
71
|
+
const fit = (a, b, size) => {
|
|
72
|
+
const most = size * 0.8;
|
|
73
|
+
if (pad[a] + pad[b] <= most) return;
|
|
74
|
+
const k = most / (pad[a] + pad[b]);
|
|
75
|
+
pad[a] = Math.floor(pad[a] * k);
|
|
76
|
+
pad[b] = Math.floor(pad[b] * k);
|
|
77
|
+
};
|
|
78
|
+
fit('top', 'bottom', frame.height);
|
|
79
|
+
fit('left', 'right', frame.width);
|
|
80
|
+
return pad;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Wires every FloatToggle on the page once: it opens and closes its side panel, and the panel fires
|
|
85
|
+
* `float-toggle` with { open }. FloatPanel calls it; call it again after adding toggles from script.
|
|
86
|
+
*/
|
|
87
|
+
export const wireFloatToggles = (root = document) => {
|
|
88
|
+
root.querySelectorAll('[data-float-toggle]:not([data-wired])').forEach((button) => {
|
|
89
|
+
button.dataset.wired = '';
|
|
90
|
+
const panel = document.getElementById(button.dataset.floatToggle);
|
|
91
|
+
if (!panel) return;
|
|
92
|
+
panel.toggleAttribute('data-closed', button.getAttribute('aria-expanded') !== 'true');
|
|
93
|
+
button.addEventListener('click', () => {
|
|
94
|
+
const open = button.getAttribute('aria-expanded') !== 'true';
|
|
95
|
+
button.setAttribute('aria-expanded', String(open));
|
|
96
|
+
panel.toggleAttribute('data-closed', !open);
|
|
97
|
+
panel.dispatchEvent(new CustomEvent('float-toggle', { bubbles: true, detail: { open } }));
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
};
|
package/src/period.js
CHANGED
|
@@ -5,6 +5,9 @@
|
|
|
5
5
|
// import { setPeriods } from '@n1kname/components/period.js';
|
|
6
6
|
// picker.addEventListener('period-change', (e) => show(e.detail.value));
|
|
7
7
|
// setPeriods(picker, ['2011', '2012', ..., '2026']); // oldest first
|
|
8
|
+
//
|
|
9
|
+
// A period is a string, shown and kept as it is, or { value, label }: the label on the button and
|
|
10
|
+
// in the list ("Nov 2026"), the value in the address and in `period-change` ("2026-11").
|
|
8
11
|
|
|
9
12
|
const reduced = () => matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
10
13
|
const token = (name) => getComputedStyle(document.documentElement).getPropertyValue(name).trim();
|
|
@@ -16,14 +19,26 @@ const ms = (name, fallback) => {
|
|
|
16
19
|
};
|
|
17
20
|
|
|
18
21
|
// Oldest first.
|
|
19
|
-
const
|
|
20
|
-
const
|
|
22
|
+
const optionsOf = (root) => [...root.querySelectorAll('.periods .option')].reverse();
|
|
23
|
+
const periodsOf = (root) => optionsOf(root).map((o) => o.dataset.value);
|
|
24
|
+
// What a value says: its option's label, the words for all, or nothing before the periods are known.
|
|
25
|
+
const nameOf = (root, v) => {
|
|
26
|
+
if (v === undefined) return '';
|
|
27
|
+
if (v === 'all') return root.dataset.all ?? '';
|
|
28
|
+
return optionsOf(root).find((o) => o.dataset.value === v)?.textContent ?? v;
|
|
29
|
+
};
|
|
30
|
+
// A period as a string or { value, label }, made { value, label }.
|
|
31
|
+
const asPeriod = (p) => (p !== null && typeof p === 'object' ? { value: String(p.value), label: String(p.label ?? p.value) } : { value: String(p), label: String(p) });
|
|
21
32
|
|
|
22
|
-
// The choice: the address's, when the picker keeps it there and it names a period or all.
|
|
33
|
+
// The choice: the address's, when the picker keeps it there and it names a period or all. Before
|
|
34
|
+
// the periods are known, a picker with no initial period has no choice (undefined), and a kept
|
|
35
|
+
// choice that is no longer one of the periods falls back to all or the newest.
|
|
23
36
|
export const periodOf = (root) => {
|
|
24
37
|
const periods = periodsOf(root);
|
|
25
38
|
const all = root.dataset.all !== undefined;
|
|
26
|
-
const
|
|
39
|
+
const kept = root.dataset.value;
|
|
40
|
+
const valid = kept !== undefined && (kept === 'all' ? all : !periods.length || periods.includes(kept));
|
|
41
|
+
const fallback = valid ? kept : all ? 'all' : periods.at(-1);
|
|
27
42
|
if (!root.dataset.param) return fallback;
|
|
28
43
|
const asked = new URL(location.href).searchParams.get(root.dataset.param);
|
|
29
44
|
if (asked === null) return all ? 'all' : fallback;
|
|
@@ -51,7 +66,9 @@ const sync = (root, dir = 0) => {
|
|
|
51
66
|
const v = periodOf(root);
|
|
52
67
|
const periods = periodsOf(root);
|
|
53
68
|
const all = root.dataset.all !== undefined;
|
|
54
|
-
|
|
69
|
+
// Never the word "undefined": a picker built with no periods has no choice until they come.
|
|
70
|
+
if (v === undefined) delete root.dataset.value;
|
|
71
|
+
else root.dataset.value = v;
|
|
55
72
|
roll(root, v, dir);
|
|
56
73
|
const [prev, next] = root.querySelectorAll('.step');
|
|
57
74
|
const at = periods.indexOf(v);
|
|
@@ -59,8 +76,8 @@ const sync = (root, dir = 0) => {
|
|
|
59
76
|
next.disabled = !periods.length || v === 'all' || (!all && at === periods.length - 1);
|
|
60
77
|
root.querySelectorAll('.option').forEach((o) => o.setAttribute('aria-pressed', String(o.dataset.value === v)));
|
|
61
78
|
// The sizer holds the longest words the label may show, so the control keeps its width.
|
|
62
|
-
const longest = [root.dataset.all ?? '', ...
|
|
63
|
-
root.querySelector('.sizer').textContent = longest || v;
|
|
79
|
+
const longest = [root.dataset.all ?? '', ...optionsOf(root).map((o) => o.textContent)].reduce((a, b) => (b.length > a.length ? b : a), '');
|
|
80
|
+
root.querySelector('.sizer').textContent = longest || nameOf(root, v);
|
|
64
81
|
};
|
|
65
82
|
|
|
66
83
|
const go = (root, v, dir) => {
|
|
@@ -72,18 +89,25 @@ const go = (root, v, dir) => {
|
|
|
72
89
|
}
|
|
73
90
|
root.dataset.value = v;
|
|
74
91
|
sync(root, dir);
|
|
75
|
-
root
|
|
92
|
+
changed(root);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// `period-change`: the value, and the words the button shows for it.
|
|
96
|
+
const changed = (root) => {
|
|
97
|
+
const value = root.dataset.value;
|
|
98
|
+
root.dispatchEvent(new CustomEvent('period-change', { bubbles: true, detail: { value, label: nameOf(root, value) } }));
|
|
76
99
|
};
|
|
77
100
|
|
|
78
|
-
// Fills a picker with its periods, oldest first, and shows it.
|
|
101
|
+
// Fills a picker with its periods, oldest first, and shows it. Each a string, or { value, label }.
|
|
79
102
|
export const setPeriods = (root, periods) => {
|
|
80
103
|
root.querySelector('.periods').replaceChildren(
|
|
81
104
|
...[...periods].reverse().map((p) => {
|
|
105
|
+
const { value, label } = asPeriod(p);
|
|
82
106
|
const b = document.createElement('button');
|
|
83
107
|
b.type = 'button';
|
|
84
108
|
b.className = 'option';
|
|
85
|
-
b.dataset.value =
|
|
86
|
-
b.textContent =
|
|
109
|
+
b.dataset.value = value;
|
|
110
|
+
b.textContent = label;
|
|
87
111
|
return b;
|
|
88
112
|
}),
|
|
89
113
|
);
|
|
@@ -153,7 +177,7 @@ export const wirePeriodPicker = (root) => {
|
|
|
153
177
|
addEventListener('popstate', () => {
|
|
154
178
|
const was = root.dataset.value;
|
|
155
179
|
sync(root);
|
|
156
|
-
if (root.dataset.value !== was)
|
|
180
|
+
if (root.dataset.value !== was) changed(root);
|
|
157
181
|
});
|
|
158
182
|
}
|
|
159
183
|
sync(root);
|