2026-07-11 02:58:56 +00:00
|
|
|
|
// Compare view: line up several places over one date range and see which best
|
|
|
|
|
|
// matches a comfort temperature. For each location we pull the same daily record
|
|
|
|
|
|
// the Calendar uses (api/v2/calendar) and, per day, take a chosen temperature
|
|
|
|
|
|
// (daytime high / daily mean / overnight low / feels-like) and measure it against
|
|
|
|
|
|
// the comfort target. A day "hits comfort" when it lands within a tolerance band;
|
|
|
|
|
|
// otherwise it's colder or warmer, and we track by how much.
|
|
|
|
|
|
//
|
2026-07-11 03:17:17 +00:00
|
|
|
|
// The whole comparison — locations, comfort target, band, judged temperature and
|
|
|
|
|
|
// date range — lives in the URL hash, so a link reproduces exactly what you see.
|
|
|
|
|
|
// Comfort / band / judged-temperature re-rank instantly (all values are already in
|
|
|
|
|
|
// hand). Editing the date range or the location set doesn't refetch on its own:
|
|
|
|
|
|
// it arms the Refresh button, and the load runs when the user taps it.
|
2026-07-11 02:58:56 +00:00
|
|
|
|
|
Convert the frontend to ES modules; split nav.js by concern (#48)
The frontend was classic scripts sharing one global scope, with a
load-order contract enforced only by comments (leaflet -> nav ->
shared -> mappicker -> page) and hand-rolled window.Thermograph /
window.LocationPicker namespaces. Page scripts now import what they use;
the dependency graph replaces the ordering contract, and no app globals
remain (Leaflet stays a classic script / global L, loaded first).
nav.js had grown four concerns; it's now three single-purpose modules:
- nav.js: last-location memory + header view-links + locHash.
- units.js: the °F/°C toggle and unit-aware formatting. The compare
special case is gone — pages that want the toggle import units.js;
compare simply doesn't.
- cache.js: the IndexedDB response cache, SWR getJSON, bundle-seeded
view prefetch and neighbor warming. prefetchViews(lat, lon, ownViews)
now takes the calling page's own view names instead of a page-identity
map (VIEW_OWN) — adding a page no longer means editing this module.
The slice->URL map stays here as bundle-contract knowledge.
frontend/package.json ({"type": "module"}) makes CI's node --check
parse the files as modules.
Verified: node --check on all files as modules; 108 backend tests;
headless-Chromium smoke across all five pages against live data — zero
console/page errors, all render assertions pass (cards, chart, calendar
grid + metric switch, ladders, compare ranking, legend scales).
2026-07-11 20:28:33 +00:00
|
|
|
|
import { loadLastLocation, saveLastLocation } from "./nav.js";
|
|
|
|
|
|
import { getJSON, TTL } from "./cache.js";
|
|
|
|
|
|
import { initFindButton } from "./mappicker.js";
|
|
|
|
|
|
import { MONTHS, pad, isoOfDate, monthStart, monthEnd, buildChunks,
|
|
|
|
|
|
clickOpensPicker } from "./shared.js";
|
2026-07-11 21:48:21 +00:00
|
|
|
|
import { fmtTemp, fmtDelta, onUnitChange } from "./units.js";
|
Extract shared.js: one home for tier colors, scales, formatters, helpers (#47)
The page scripts each re-declared the shared presentation layer — the tier
color table existed in four places (app.js, calendar.js, day.js, style.css)
and the scale label tables in three (plus legend.html's own drifting copy),
alongside per-page copies of the dryness ramp, formatters, ord, todayISO,
esc, the weather icons/summary, month helpers and the 2-year range chunker.
~240 duplicated lines deleted (net -236 with the new module included).
- frontend/shared.js (IIFE, extends window.Thermograph): tier colors read
from style.css's :root custom properties at load — the CSS is now the
single source of truth; the JS map exists only because inline-SVG work
(chart + PNG export) needs literal values, with hex fallbacks for a
missing stylesheet. Plus SCALE_TEMP/SCALE_RAIN, drynessColor, fmt*, ord,
todayISO, esc, placeLabel, month/chunk date helpers, clickOpensPicker,
and the weatherType summary (dsr-aware; the day page just omits dsr).
- nav.js: wrapped in an IIFE — its ~15 top-level functions were globals in
the shared classic-script scope, and a leaked locHash collided with page
destructuring (caught by the browser smoke, not by node --check).
locHash is now exported and used by all 8 former hand-built hash sites.
- mappicker.js: initFindButton/setFindLabel replace the Find-button block
each page rebuilt.
- legend.html renders its scales from the shared tables, so the guide can
no longer drift from what the app shows.
Verified: node --check on all JS; 108 backend tests; headless-Chromium
smoke over all five pages against live data — no console/page errors,
legend rows 9/9, weekly 7 cards + colored chart/key/table + metric toggle,
calendar grid + key + metric switch, day 7 ladders + weather icon,
compare seeded rank card.
2026-07-11 20:21:48 +00:00
|
|
|
|
|
2026-07-11 02:58:56 +00:00
|
|
|
|
const CMP = {
|
|
|
|
|
|
comfort: "thermograph:cmpComfort",
|
|
|
|
|
|
tol: "thermograph:cmpTol",
|
|
|
|
|
|
basis: "thermograph:cmpBasis",
|
|
|
|
|
|
range: "thermograph:cmpRange",
|
|
|
|
|
|
locs: "thermograph:cmpLocs",
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const lsGet = (k) => { try { return localStorage.getItem(k); } catch (e) { return null; } };
|
|
|
|
|
|
const lsSet = (k, v) => { try { localStorage.setItem(k, v); } catch (e) {} };
|
|
|
|
|
|
|
|
|
|
|
|
const clamp = (v, lo, hi) => Math.min(hi, Math.max(lo, v));
|
2026-07-11 03:17:17 +00:00
|
|
|
|
const BASES = ["tmax", "tmin", "mean", "feels"];
|
2026-07-11 02:58:56 +00:00
|
|
|
|
let comfort = clamp(+lsGet(CMP.comfort) || 68, 30, 100);
|
|
|
|
|
|
let tol = clamp(lsGet(CMP.tol) == null ? 5 : +lsGet(CMP.tol), 0, 15);
|
2026-07-11 03:17:17 +00:00
|
|
|
|
let basis = BASES.includes(lsGet(CMP.basis)) ? lsGet(CMP.basis) : "tmax";
|
2026-07-11 02:58:56 +00:00
|
|
|
|
|
|
|
|
|
|
// Locations being compared: {lat, lon, name, series|null, loading, error}.
|
|
|
|
|
|
// `series` is a compact per-day array of {hi, lo, feels} (°F, any may be null).
|
2026-07-11 03:17:17 +00:00
|
|
|
|
// A location with no series and not loading is "pending" — it needs a Refresh tap.
|
2026-07-11 02:58:56 +00:00
|
|
|
|
let locations = [];
|
|
|
|
|
|
// Bumped on every (re)load so a superseded in-flight fetch drops its result.
|
|
|
|
|
|
let loadToken = 0;
|
|
|
|
|
|
|
|
|
|
|
|
const BASIS_LABEL = { tmax: "daytime high", mean: "daily mean", tmin: "overnight low", feels: "feels-like" };
|
|
|
|
|
|
|
|
|
|
|
|
// ---- date range (month granularity) ----
|
2026-07-11 03:17:17 +00:00
|
|
|
|
const isYM = (s) => typeof s === "string" && /^\d{4}-\d{2}$/.test(s);
|
2026-07-11 02:58:56 +00:00
|
|
|
|
function defaultRange() {
|
|
|
|
|
|
const now = new Date();
|
|
|
|
|
|
const s = new Date(now.getFullYear(), now.getMonth() - 11, 1); // last 12 whole months
|
|
|
|
|
|
return { start: `${s.getFullYear()}-${pad(s.getMonth() + 1)}`, end: `${now.getFullYear()}-${pad(now.getMonth() + 1)}` };
|
|
|
|
|
|
}
|
|
|
|
|
|
function loadRange() {
|
2026-07-11 03:17:17 +00:00
|
|
|
|
try { const o = JSON.parse(lsGet(CMP.range)); if (o && isYM(o.start) && isYM(o.end)) return o; } catch (e) {}
|
2026-07-11 02:58:56 +00:00
|
|
|
|
return defaultRange();
|
|
|
|
|
|
}
|
2026-07-11 03:17:17 +00:00
|
|
|
|
let range = loadRange(); // the APPLIED range (what loaded data reflects), "YYYY-MM"
|
2026-07-11 02:58:56 +00:00
|
|
|
|
|
|
|
|
|
|
// Pretty "Jul 2025 – Jun 2026" for a YYYY-MM range.
|
Extract shared.js: one home for tier colors, scales, formatters, helpers (#47)
The page scripts each re-declared the shared presentation layer — the tier
color table existed in four places (app.js, calendar.js, day.js, style.css)
and the scale label tables in three (plus legend.html's own drifting copy),
alongside per-page copies of the dryness ramp, formatters, ord, todayISO,
esc, the weather icons/summary, month helpers and the 2-year range chunker.
~240 duplicated lines deleted (net -236 with the new module included).
- frontend/shared.js (IIFE, extends window.Thermograph): tier colors read
from style.css's :root custom properties at load — the CSS is now the
single source of truth; the JS map exists only because inline-SVG work
(chart + PNG export) needs literal values, with hex fallbacks for a
missing stylesheet. Plus SCALE_TEMP/SCALE_RAIN, drynessColor, fmt*, ord,
todayISO, esc, placeLabel, month/chunk date helpers, clickOpensPicker,
and the weatherType summary (dsr-aware; the day page just omits dsr).
- nav.js: wrapped in an IIFE — its ~15 top-level functions were globals in
the shared classic-script scope, and a leaked locHash collided with page
destructuring (caught by the browser smoke, not by node --check).
locHash is now exported and used by all 8 former hand-built hash sites.
- mappicker.js: initFindButton/setFindLabel replace the Find-button block
each page rebuilt.
- legend.html renders its scales from the shared tables, so the guide can
no longer drift from what the app shows.
Verified: node --check on all JS; 108 backend tests; headless-Chromium
smoke over all five pages against live data — no console/page errors,
legend rows 9/9, weekly 7 cards + colored chart/key/table + metric toggle,
calendar grid + key + metric switch, day 7 ladders + weather icon,
compare seeded rank card.
2026-07-11 20:21:48 +00:00
|
|
|
|
const monthLabel = (ym) => `${MONTHS[+ym.slice(5, 7) - 1]} ${ym.slice(0, 4)}`;
|
2026-07-11 02:58:56 +00:00
|
|
|
|
|
2026-07-11 03:17:17 +00:00
|
|
|
|
// ---- shareable URL state ----
|
|
|
|
|
|
// The hash carries the full comparison: c=comfort, t=band, b=basis, s/e=range,
|
|
|
|
|
|
// loc=lat,lon;lat,lon. Written on every state change; read once on load (a link
|
|
|
|
|
|
// wins over localStorage). A plain lat/lon hash from cross-view nav is ignored
|
|
|
|
|
|
// here and handled by the seed path instead.
|
|
|
|
|
|
function writeHashState() {
|
|
|
|
|
|
const p = new URLSearchParams();
|
|
|
|
|
|
p.set("c", comfort); p.set("t", tol); p.set("b", basis);
|
|
|
|
|
|
p.set("s", range.start); p.set("e", range.end);
|
|
|
|
|
|
if (locations.length) p.set("loc", locations.map((l) => `${l.lat.toFixed(4)},${l.lon.toFixed(4)}`).join(";"));
|
|
|
|
|
|
history.replaceState(null, "", "#" + p.toString());
|
|
|
|
|
|
}
|
|
|
|
|
|
function readHashState() {
|
|
|
|
|
|
const p = new URLSearchParams(location.hash.slice(1));
|
|
|
|
|
|
if (!["c", "t", "b", "s", "e", "loc"].some((k) => p.has(k))) return null; // not a compare link
|
|
|
|
|
|
const st = {};
|
|
|
|
|
|
if (p.has("c")) st.comfort = clamp(+p.get("c") || 68, 30, 100);
|
|
|
|
|
|
if (p.has("t") && p.get("t") !== "") st.tol = clamp(+p.get("t"), 0, 15);
|
|
|
|
|
|
if (BASES.includes(p.get("b"))) st.basis = p.get("b");
|
|
|
|
|
|
if (isYM(p.get("s"))) st.start = p.get("s");
|
|
|
|
|
|
if (isYM(p.get("e"))) st.end = p.get("e");
|
|
|
|
|
|
if (p.has("loc")) {
|
|
|
|
|
|
st.locs = p.get("loc").split(";").map((pair) => {
|
|
|
|
|
|
const [a, b] = pair.split(",").map(Number);
|
|
|
|
|
|
return { lat: a, lon: b };
|
|
|
|
|
|
}).filter((l) => !isNaN(l.lat) && !isNaN(l.lon));
|
|
|
|
|
|
}
|
|
|
|
|
|
return st;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-11 02:58:56 +00:00
|
|
|
|
// ---- elements ----
|
|
|
|
|
|
const addBtn = document.getElementById("cmp-add");
|
2026-07-11 03:17:17 +00:00
|
|
|
|
const refreshBtn = document.getElementById("cmp-refresh");
|
2026-07-11 02:58:56 +00:00
|
|
|
|
const locList = document.getElementById("cmp-loc-list");
|
|
|
|
|
|
const params = document.getElementById("cmp-params");
|
|
|
|
|
|
const placeholder = document.getElementById("cmp-placeholder");
|
|
|
|
|
|
const head = document.getElementById("cmp-head");
|
|
|
|
|
|
const results = document.getElementById("cmp-results");
|
|
|
|
|
|
const comfortInput = document.getElementById("cmp-comfort");
|
|
|
|
|
|
const comfortVal = document.getElementById("cmp-comfort-val");
|
|
|
|
|
|
const tolInput = document.getElementById("cmp-tol");
|
|
|
|
|
|
const tolVal = document.getElementById("cmp-tol-val");
|
|
|
|
|
|
const basisToggle = document.getElementById("cmp-basis");
|
|
|
|
|
|
const startInput = document.getElementById("cmp-start");
|
|
|
|
|
|
const endInput = document.getElementById("cmp-end");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ---- data ----
|
|
|
|
|
|
// Fetch one location's range (chunked) and reduce it to the compact per-day series.
|
|
|
|
|
|
async function fetchSeries(loc, token) {
|
|
|
|
|
|
const chunks = buildChunks(monthStart(range.start), monthEnd(range.end));
|
|
|
|
|
|
const days = [];
|
|
|
|
|
|
let place = null;
|
|
|
|
|
|
for (const ch of chunks) {
|
|
|
|
|
|
const url = `api/v2/calendar?lat=${loc.lat}&lon=${loc.lon}&start=${ch.start}&end=${ch.end}`;
|
Convert the frontend to ES modules; split nav.js by concern (#48)
The frontend was classic scripts sharing one global scope, with a
load-order contract enforced only by comments (leaflet -> nav ->
shared -> mappicker -> page) and hand-rolled window.Thermograph /
window.LocationPicker namespaces. Page scripts now import what they use;
the dependency graph replaces the ordering contract, and no app globals
remain (Leaflet stays a classic script / global L, loaded first).
nav.js had grown four concerns; it's now three single-purpose modules:
- nav.js: last-location memory + header view-links + locHash.
- units.js: the °F/°C toggle and unit-aware formatting. The compare
special case is gone — pages that want the toggle import units.js;
compare simply doesn't.
- cache.js: the IndexedDB response cache, SWR getJSON, bundle-seeded
view prefetch and neighbor warming. prefetchViews(lat, lon, ownViews)
now takes the calling page's own view names instead of a page-identity
map (VIEW_OWN) — adding a page no longer means editing this module.
The slice->URL map stays here as bundle-contract knowledge.
frontend/package.json ({"type": "module"}) makes CI's node --check
parse the files as modules.
Verified: node --check on all files as modules; 108 backend tests;
headless-Chromium smoke across all five pages against live data — zero
console/page errors, all render assertions pass (cards, chart, calendar
grid + metric switch, ladders, compare ranking, legend scales).
2026-07-11 20:28:33 +00:00
|
|
|
|
const d = await getJSON(url, TTL.calendar, true);
|
2026-07-11 02:58:56 +00:00
|
|
|
|
if (token !== loadToken) return null; // superseded
|
|
|
|
|
|
place = place || d.place || `${d.cell.center_lat.toFixed(2)}, ${d.cell.center_lon.toFixed(2)}`;
|
|
|
|
|
|
for (const r of d.days) days.push(r);
|
|
|
|
|
|
}
|
|
|
|
|
|
return {
|
|
|
|
|
|
name: place,
|
|
|
|
|
|
series: days.map((r) => ({
|
|
|
|
|
|
hi: r.tmax ? r.tmax.v : null,
|
|
|
|
|
|
lo: r.tmin ? r.tmin.v : null,
|
|
|
|
|
|
feels: r.feels ? r.feels.v : null,
|
|
|
|
|
|
})),
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function persistLocations() {
|
|
|
|
|
|
lsSet(CMP.locs, JSON.stringify(locations.map((l) => ({ lat: l.lat, lon: l.lon, name: l.name }))));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function loadLocation(loc, token) {
|
|
|
|
|
|
loc.loading = true; loc.error = null;
|
|
|
|
|
|
renderAll();
|
|
|
|
|
|
try {
|
|
|
|
|
|
const res = await fetchSeries(loc, token);
|
2026-07-11 03:17:17 +00:00
|
|
|
|
if (!res || token !== loadToken) return; // superseded (a newer load owns this loc)
|
|
|
|
|
|
loc.name = res.name; loc.series = res.series;
|
2026-07-11 02:58:56 +00:00
|
|
|
|
} catch (e) {
|
2026-07-11 03:17:17 +00:00
|
|
|
|
if (token === loadToken) loc.error = e.message || "couldn't load";
|
2026-07-11 02:58:56 +00:00
|
|
|
|
}
|
2026-07-11 03:17:17 +00:00
|
|
|
|
if (token === loadToken) loc.loading = false;
|
2026-07-11 02:58:56 +00:00
|
|
|
|
persistLocations();
|
|
|
|
|
|
renderAll();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-11 03:17:17 +00:00
|
|
|
|
// Load every location that still needs data under the current range.
|
|
|
|
|
|
function loadPending(token) {
|
|
|
|
|
|
for (const loc of locations) if (!loc.series && !loc.loading) loadLocation(loc, token);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-11 02:58:56 +00:00
|
|
|
|
function addLocation(lat, lon) {
|
|
|
|
|
|
// Ignore a spot already on the list (same grid neighborhood).
|
|
|
|
|
|
if (locations.some((l) => Math.abs(l.lat - lat) < 0.05 && Math.abs(l.lon - lon) < 0.05)) return;
|
2026-07-11 14:50:48 +00:00
|
|
|
|
const loc = { lat, lon, name: null, series: null, loading: false, error: null };
|
|
|
|
|
|
locations.push(loc);
|
Convert the frontend to ES modules; split nav.js by concern (#48)
The frontend was classic scripts sharing one global scope, with a
load-order contract enforced only by comments (leaflet -> nav ->
shared -> mappicker -> page) and hand-rolled window.Thermograph /
window.LocationPicker namespaces. Page scripts now import what they use;
the dependency graph replaces the ordering contract, and no app globals
remain (Leaflet stays a classic script / global L, loaded first).
nav.js had grown four concerns; it's now three single-purpose modules:
- nav.js: last-location memory + header view-links + locHash.
- units.js: the °F/°C toggle and unit-aware formatting. The compare
special case is gone — pages that want the toggle import units.js;
compare simply doesn't.
- cache.js: the IndexedDB response cache, SWR getJSON, bundle-seeded
view prefetch and neighbor warming. prefetchViews(lat, lon, ownViews)
now takes the calling page's own view names instead of a page-identity
map (VIEW_OWN) — adding a page no longer means editing this module.
The slice->URL map stays here as bundle-contract knowledge.
frontend/package.json ({"type": "module"}) makes CI's node --check
parse the files as modules.
Verified: node --check on all files as modules; 108 backend tests;
headless-Chromium smoke across all five pages against live data — zero
console/page errors, all render assertions pass (cards, chart, calendar
grid + metric switch, ladders, compare ranking, legend scales).
2026-07-11 20:28:33 +00:00
|
|
|
|
saveLastLocation(lat, lon);
|
2026-07-11 03:17:17 +00:00
|
|
|
|
persistLocations();
|
|
|
|
|
|
writeHashState();
|
|
|
|
|
|
renderAll(); // pending → the Refresh button appears; no fetch until it's tapped
|
2026-07-11 14:50:48 +00:00
|
|
|
|
resolveName(loc); // but resolve the neighborhood+city right away (before Refresh)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Resolve just the place name for a freshly-added location, independent of its
|
|
|
|
|
|
// (deferred, heavier) series load, so the chip flips from coordinates to the
|
|
|
|
|
|
// neighborhood + city immediately on add. Best-effort — the series load resolves
|
|
|
|
|
|
// the name too, so a failure here is harmless.
|
|
|
|
|
|
async function resolveName(loc) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const res = await fetch(`api/v2/place?lat=${loc.lat}&lon=${loc.lon}`);
|
|
|
|
|
|
if (!res.ok) return;
|
|
|
|
|
|
const d = await res.json();
|
|
|
|
|
|
// Skip if the location was removed meanwhile, or already got named by a load.
|
|
|
|
|
|
if (!locations.includes(loc) || loc.name || !d || !d.place) return;
|
|
|
|
|
|
loc.name = d.place;
|
|
|
|
|
|
persistLocations();
|
|
|
|
|
|
renderAll();
|
|
|
|
|
|
} catch (e) { /* best-effort — the series load will still resolve the name */ }
|
2026-07-11 02:58:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function removeLocation(i) {
|
|
|
|
|
|
locations.splice(i, 1);
|
|
|
|
|
|
persistLocations();
|
2026-07-11 03:17:17 +00:00
|
|
|
|
writeHashState();
|
2026-07-11 02:58:56 +00:00
|
|
|
|
renderAll();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-11 03:17:17 +00:00
|
|
|
|
// ---- pending / refresh state ----
|
|
|
|
|
|
const dateChanged = () => startInput.value !== range.start || endInput.value !== range.end;
|
|
|
|
|
|
const isPending = (l) => !l.series && !l.loading && !l.error;
|
|
|
|
|
|
const isDirty = () => locations.length > 0 && (dateChanged() || locations.some(isPending));
|
|
|
|
|
|
|
|
|
|
|
|
// The Refresh tap: adopt any edited dates (which invalidates every series) and load
|
|
|
|
|
|
// whatever now needs data.
|
|
|
|
|
|
function refresh() {
|
|
|
|
|
|
let s = startInput.value || range.start, e = endInput.value || range.end;
|
|
|
|
|
|
if (!isYM(s) || !isYM(e)) return;
|
|
|
|
|
|
if (s > e) { const t = s; s = e; e = t; }
|
|
|
|
|
|
const rangeChanged = s !== range.start || e !== range.end;
|
|
|
|
|
|
range = { start: s, end: e };
|
|
|
|
|
|
startInput.value = s; endInput.value = e;
|
|
|
|
|
|
lsSet(CMP.range, JSON.stringify(range));
|
2026-07-11 02:58:56 +00:00
|
|
|
|
const token = ++loadToken;
|
2026-07-11 03:17:17 +00:00
|
|
|
|
if (rangeChanged) for (const loc of locations) loc.series = null;
|
|
|
|
|
|
writeHashState();
|
|
|
|
|
|
loadPending(token);
|
|
|
|
|
|
renderAll();
|
2026-07-11 02:58:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---- stats ----
|
|
|
|
|
|
function basisTemp(pt) {
|
|
|
|
|
|
if (basis === "tmax") return pt.hi;
|
|
|
|
|
|
if (basis === "tmin") return pt.lo;
|
|
|
|
|
|
if (basis === "feels") return pt.feels;
|
|
|
|
|
|
return pt.hi != null && pt.lo != null ? (pt.hi + pt.lo) / 2 : null; // mean
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function computeStats(series) {
|
|
|
|
|
|
let n = 0, below = 0, above = 0, comf = 0, sumBelow = 0, sumAbove = 0, sumAbs = 0;
|
|
|
|
|
|
for (const pt of series) {
|
|
|
|
|
|
const t = basisTemp(pt);
|
|
|
|
|
|
if (t == null || isNaN(t)) continue;
|
|
|
|
|
|
n++;
|
|
|
|
|
|
const d = t - comfort;
|
|
|
|
|
|
sumAbs += Math.abs(d);
|
|
|
|
|
|
if (d < -tol) { below++; sumBelow += -d; }
|
|
|
|
|
|
else if (d > tol) { above++; sumAbove += d; }
|
|
|
|
|
|
else comf++;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!n) return null;
|
|
|
|
|
|
return {
|
|
|
|
|
|
n, comf, below, above,
|
|
|
|
|
|
comfortPct: 100 * comf / n, belowPct: 100 * below / n, abovePct: 100 * above / n,
|
|
|
|
|
|
avgBelow: below ? sumBelow / below : 0,
|
|
|
|
|
|
avgAbove: above ? sumAbove / above : 0,
|
|
|
|
|
|
mad: sumAbs / n,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---- render ----
|
|
|
|
|
|
const pct = (v) => `${v < 10 ? v.toFixed(1) : Math.round(v)}%`;
|
2026-07-11 21:48:21 +00:00
|
|
|
|
// Temperature *differences* (avg miss, typical miss) in the active unit.
|
|
|
|
|
|
const deg = (v) => fmtDelta(v);
|
2026-07-11 02:58:56 +00:00
|
|
|
|
|
|
|
|
|
|
function renderLocList() {
|
|
|
|
|
|
locList.innerHTML = locations.map((l, i) => {
|
2026-07-11 03:17:17 +00:00
|
|
|
|
let label, cls = "cmp-chip";
|
Picker map: smaller labels, darker roads; location label coords→place (#27)
* Picker map: smaller city labels, darker/bolder roads (split tile layers)
Split the CARTO Voyager basemap into two raster layers so label size and road
weight are independent: a labels-free base upscaled via tileSize 512 + zoomOffset
-1 (bold, prominent roads) plus a labels-only layer at natural size on top (small,
crisp city names). The darken/saturate filter now targets the base layer only
(.mp-base: brightness .92, contrast 1.12), deepening the roads while leaving label
text at full contrast.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B3Q2EkHHnTUX2BfhV5q1Zc
* Location label: show coordinates immediately, then resolve to place name
On selecting a location, write the raw coordinates to the location label right
away, so the user sees the picked spot instantly instead of a stale/blank label.
The existing post-fetch render already overwrites it with the reverse-geocoded
"neighborhood, city, region" string (data.place), so the label now reads
coords → place name live.
- app.js / calendar.js / day.js: set coords in the selection handler before the
fetch; the render/initOnce overwrite is unchanged.
- compare.js: show coordinates through the pending + loading states (instead of
"Locating…") so each chip's coords are replaced live by the place name once
scored.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B3Q2EkHHnTUX2BfhV5q1Zc
---------
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-11 08:35:43 +00:00
|
|
|
|
// Show the raw coordinates while pending/loading (with the loading class still
|
|
|
|
|
|
// driving the spinner); once scored, l.name holds the resolved neighborhood +
|
|
|
|
|
|
// city, so the coordinates are replaced live by the place name.
|
|
|
|
|
|
const coords = `${l.lat.toFixed(2)}, ${l.lon.toFixed(2)}`;
|
|
|
|
|
|
if (l.loading) { label = l.name || coords; cls += " loading"; }
|
2026-07-11 03:17:17 +00:00
|
|
|
|
else if (l.error) { label = "Couldn't load"; cls += " error"; }
|
Picker map: smaller labels, darker roads; location label coords→place (#27)
* Picker map: smaller city labels, darker/bolder roads (split tile layers)
Split the CARTO Voyager basemap into two raster layers so label size and road
weight are independent: a labels-free base upscaled via tileSize 512 + zoomOffset
-1 (bold, prominent roads) plus a labels-only layer at natural size on top (small,
crisp city names). The darken/saturate filter now targets the base layer only
(.mp-base: brightness .92, contrast 1.12), deepening the roads while leaving label
text at full contrast.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B3Q2EkHHnTUX2BfhV5q1Zc
* Location label: show coordinates immediately, then resolve to place name
On selecting a location, write the raw coordinates to the location label right
away, so the user sees the picked spot instantly instead of a stale/blank label.
The existing post-fetch render already overwrites it with the reverse-geocoded
"neighborhood, city, region" string (data.place), so the label now reads
coords → place name live.
- app.js / calendar.js / day.js: set coords in the selection handler before the
fetch; the render/initOnce overwrite is unchanged.
- compare.js: show coordinates through the pending + loading states (instead of
"Locating…") so each chip's coords are replaced live by the place name once
scored.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B3Q2EkHHnTUX2BfhV5q1Zc
---------
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-11 08:35:43 +00:00
|
|
|
|
else if (!l.series) { label = l.name || coords; cls += " pending"; }
|
2026-07-11 03:17:17 +00:00
|
|
|
|
else { label = l.name; }
|
2026-07-11 02:58:56 +00:00
|
|
|
|
return `<li class="${cls}"><span class="cmp-chip-name">${label}</span>` +
|
|
|
|
|
|
`<button type="button" class="cmp-chip-x" data-i="${i}" aria-label="Remove ${label}">×</button></li>`;
|
|
|
|
|
|
}).join("");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function rankCard(loc, s, rank) {
|
|
|
|
|
|
const bar = `<div class="cmp-bar" role="img" aria-label="${pct(s.belowPct)} colder, ${pct(s.comfortPct)} in comfort, ${pct(s.abovePct)} warmer">` +
|
|
|
|
|
|
`<span class="cmp-seg cmp-below" style="flex-basis:${s.belowPct}%"></span>` +
|
|
|
|
|
|
`<span class="cmp-seg cmp-comfort" style="flex-basis:${s.comfortPct}%"></span>` +
|
|
|
|
|
|
`<span class="cmp-seg cmp-above" style="flex-basis:${s.abovePct}%"></span></div>`;
|
|
|
|
|
|
const stats =
|
|
|
|
|
|
`<div class="cmp-stats">` +
|
|
|
|
|
|
`<div class="cmp-stat cmp-s-below"><span class="cmp-k">Colder</span><span class="cmp-v">${pct(s.belowPct)}</span><span class="cmp-d">${s.below ? `avg ${deg(s.avgBelow)} below` : "—"}</span></div>` +
|
2026-07-11 21:48:21 +00:00
|
|
|
|
`<div class="cmp-stat cmp-s-comfort"><span class="cmp-k">In comfort</span><span class="cmp-v">${pct(s.comfortPct)}</span><span class="cmp-d">±${fmtDelta(tol)} of ${fmtTemp(comfort)}</span></div>` +
|
2026-07-11 02:58:56 +00:00
|
|
|
|
`<div class="cmp-stat cmp-s-above"><span class="cmp-k">Warmer</span><span class="cmp-v">${pct(s.abovePct)}</span><span class="cmp-d">${s.above ? `avg ${deg(s.avgAbove)} above` : "—"}</span></div>` +
|
|
|
|
|
|
`</div>`;
|
|
|
|
|
|
return `<div class="cmp-card">` +
|
|
|
|
|
|
`<div class="cmp-card-top">` +
|
|
|
|
|
|
`<span class="cmp-rank">#${rank}</span>` +
|
|
|
|
|
|
`<div class="cmp-card-name"><span class="cmp-name">${loc.name}</span><span class="cmp-daycount">${s.n} days</span></div>` +
|
|
|
|
|
|
`<div class="cmp-big"><b>${pct(s.comfortPct)}</b><span>in comfort</span></div>` +
|
|
|
|
|
|
`</div>` +
|
|
|
|
|
|
bar + stats +
|
|
|
|
|
|
`<div class="cmp-mad">Typical day misses comfort by <b>${deg(s.mad)}</b></div>` +
|
|
|
|
|
|
`</div>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function renderResults() {
|
|
|
|
|
|
const loading = locations.filter((l) => l.loading).length;
|
|
|
|
|
|
const scored = locations
|
|
|
|
|
|
.map((l) => ({ loc: l, s: l.series ? computeStats(l.series) : null }))
|
|
|
|
|
|
.filter((x) => x.s);
|
|
|
|
|
|
// Best comfort share first; break ties by the smaller typical miss.
|
|
|
|
|
|
scored.sort((a, b) => (b.s.comfortPct - a.s.comfortPct) || (a.s.mad - b.s.mad));
|
|
|
|
|
|
|
|
|
|
|
|
if (!scored.length) {
|
|
|
|
|
|
head.hidden = true;
|
|
|
|
|
|
results.innerHTML = loading ? `<p class="spinner">Loading daily weather…</p>` : "";
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Headline: the winner + the parameters it was judged under.
|
|
|
|
|
|
const win = scored[0];
|
|
|
|
|
|
const span = `${monthLabel(range.start)} – ${monthLabel(range.end)}`;
|
|
|
|
|
|
const lead = scored.length > 1
|
2026-07-11 21:48:21 +00:00
|
|
|
|
? `<b>${win.loc.name}</b> best matches ${fmtTemp(comfort)} — ${pct(win.s.comfortPct)} of days within ±${fmtDelta(tol)}`
|
|
|
|
|
|
: `<b>${win.loc.name}</b>: ${pct(win.s.comfortPct)} of days within ±${fmtDelta(tol)} of ${fmtTemp(comfort)}`;
|
2026-07-11 02:58:56 +00:00
|
|
|
|
head.hidden = false;
|
|
|
|
|
|
head.innerHTML = `<h2>${lead}</h2>` +
|
|
|
|
|
|
`<p class="meta">By ${BASIS_LABEL[basis]} · ${span}${loading ? ` · loading ${loading} more…` : ""}</p>`;
|
|
|
|
|
|
|
|
|
|
|
|
results.innerHTML = scored.map((x, i) => rankCard(x.loc, x.s, i + 1)).join("");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function renderAll() {
|
|
|
|
|
|
const has = locations.length > 0;
|
|
|
|
|
|
params.hidden = !has;
|
|
|
|
|
|
placeholder.hidden = has;
|
|
|
|
|
|
addBtn.querySelector("span").textContent = has ? "Add another location" : "Add location";
|
2026-07-11 03:17:17 +00:00
|
|
|
|
refreshBtn.hidden = !isDirty();
|
2026-07-11 02:58:56 +00:00
|
|
|
|
renderLocList();
|
|
|
|
|
|
renderResults();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---- events ----
|
Convert the frontend to ES modules; split nav.js by concern (#48)
The frontend was classic scripts sharing one global scope, with a
load-order contract enforced only by comments (leaflet -> nav ->
shared -> mappicker -> page) and hand-rolled window.Thermograph /
window.LocationPicker namespaces. Page scripts now import what they use;
the dependency graph replaces the ordering contract, and no app globals
remain (Leaflet stays a classic script / global L, loaded first).
nav.js had grown four concerns; it's now three single-purpose modules:
- nav.js: last-location memory + header view-links + locHash.
- units.js: the °F/°C toggle and unit-aware formatting. The compare
special case is gone — pages that want the toggle import units.js;
compare simply doesn't.
- cache.js: the IndexedDB response cache, SWR getJSON, bundle-seeded
view prefetch and neighbor warming. prefetchViews(lat, lon, ownViews)
now takes the calling page's own view names instead of a page-identity
map (VIEW_OWN) — adding a page no longer means editing this module.
The slice->URL map stays here as bundle-contract knowledge.
frontend/package.json ({"type": "module"}) makes CI's node --check
parse the files as modules.
Verified: node --check on all files as modules; 108 backend tests;
headless-Chromium smoke across all five pages against live data — zero
console/page errors, all render assertions pass (cards, chart, calendar
grid + metric switch, ladders, compare ranking, legend scales).
2026-07-11 20:28:33 +00:00
|
|
|
|
initFindButton(addBtn, "Add location",
|
|
|
|
|
|
() => (locations.length ? locations[locations.length - 1] : loadLastLocation()),
|
|
|
|
|
|
(lat, lon) => addLocation(lat, lon));
|
2026-07-11 02:58:56 +00:00
|
|
|
|
|
2026-07-11 03:17:17 +00:00
|
|
|
|
refreshBtn.addEventListener("click", refresh);
|
|
|
|
|
|
|
2026-07-11 02:58:56 +00:00
|
|
|
|
locList.addEventListener("click", (e) => {
|
|
|
|
|
|
const x = e.target.closest(".cmp-chip-x");
|
|
|
|
|
|
if (x) removeLocation(+x.dataset.i);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-07-11 03:17:17 +00:00
|
|
|
|
// Comfort / band / basis: instant re-rank over data already in hand — no refetch.
|
2026-07-11 02:58:56 +00:00
|
|
|
|
comfortInput.addEventListener("input", () => {
|
|
|
|
|
|
comfort = +comfortInput.value;
|
2026-07-11 21:48:21 +00:00
|
|
|
|
comfortVal.textContent = fmtTemp(comfort);
|
2026-07-11 02:58:56 +00:00
|
|
|
|
lsSet(CMP.comfort, String(comfort));
|
2026-07-11 03:17:17 +00:00
|
|
|
|
writeHashState();
|
2026-07-11 02:58:56 +00:00
|
|
|
|
renderResults();
|
|
|
|
|
|
});
|
|
|
|
|
|
tolInput.addEventListener("input", () => {
|
|
|
|
|
|
tol = +tolInput.value;
|
2026-07-11 21:48:21 +00:00
|
|
|
|
tolVal.textContent = `±${fmtDelta(tol)}`;
|
2026-07-11 02:58:56 +00:00
|
|
|
|
lsSet(CMP.tol, String(tol));
|
2026-07-11 03:17:17 +00:00
|
|
|
|
writeHashState();
|
2026-07-11 02:58:56 +00:00
|
|
|
|
renderResults();
|
|
|
|
|
|
});
|
2026-07-11 21:48:21 +00:00
|
|
|
|
|
|
|
|
|
|
// The °F/°C toggle only flips what's shown — comfort/tol/series all stay in °F
|
|
|
|
|
|
// (the API's unit), so there's nothing to refetch or re-rank, just re-render the
|
|
|
|
|
|
// numbers: the two slider labels and every card.
|
|
|
|
|
|
onUnitChange(() => {
|
|
|
|
|
|
comfortVal.textContent = fmtTemp(comfort);
|
|
|
|
|
|
tolVal.textContent = `±${fmtDelta(tol)}`;
|
|
|
|
|
|
renderResults();
|
|
|
|
|
|
});
|
2026-07-11 02:58:56 +00:00
|
|
|
|
basisToggle.addEventListener("click", (e) => {
|
|
|
|
|
|
const btn = e.target.closest("button[data-basis]");
|
|
|
|
|
|
if (!btn) return;
|
|
|
|
|
|
basis = btn.dataset.basis;
|
|
|
|
|
|
lsSet(CMP.basis, basis);
|
|
|
|
|
|
basisToggle.querySelectorAll("button").forEach((b) => b.classList.toggle("active", b === btn));
|
2026-07-11 03:17:17 +00:00
|
|
|
|
writeHashState();
|
2026-07-11 02:58:56 +00:00
|
|
|
|
renderResults();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-07-11 03:17:17 +00:00
|
|
|
|
// Editing the dates doesn't load — it just arms Refresh (re-eval via renderAll).
|
2026-07-11 02:58:56 +00:00
|
|
|
|
startInput.max = endInput.max = `${new Date().getFullYear()}-12`;
|
2026-07-11 03:17:17 +00:00
|
|
|
|
startInput.addEventListener("change", renderAll);
|
|
|
|
|
|
endInput.addEventListener("change", renderAll);
|
Extract shared.js: one home for tier colors, scales, formatters, helpers (#47)
The page scripts each re-declared the shared presentation layer — the tier
color table existed in four places (app.js, calendar.js, day.js, style.css)
and the scale label tables in three (plus legend.html's own drifting copy),
alongside per-page copies of the dryness ramp, formatters, ord, todayISO,
esc, the weather icons/summary, month helpers and the 2-year range chunker.
~240 duplicated lines deleted (net -236 with the new module included).
- frontend/shared.js (IIFE, extends window.Thermograph): tier colors read
from style.css's :root custom properties at load — the CSS is now the
single source of truth; the JS map exists only because inline-SVG work
(chart + PNG export) needs literal values, with hex fallbacks for a
missing stylesheet. Plus SCALE_TEMP/SCALE_RAIN, drynessColor, fmt*, ord,
todayISO, esc, placeLabel, month/chunk date helpers, clickOpensPicker,
and the weatherType summary (dsr-aware; the day page just omits dsr).
- nav.js: wrapped in an IIFE — its ~15 top-level functions were globals in
the shared classic-script scope, and a leaked locHash collided with page
destructuring (caught by the browser smoke, not by node --check).
locHash is now exported and used by all 8 former hand-built hash sites.
- mappicker.js: initFindButton/setFindLabel replace the Find-button block
each page rebuilt.
- legend.html renders its scales from the shared tables, so the guide can
no longer drift from what the app shows.
Verified: node --check on all JS; 108 backend tests; headless-Chromium
smoke over all five pages against live data — no console/page errors,
legend rows 9/9, weekly 7 cards + colored chart/key/table + metric toggle,
calendar grid + key + metric switch, day 7 ladders + weather icon,
compare seeded rank card.
2026-07-11 20:21:48 +00:00
|
|
|
|
clickOpensPicker(startInput, endInput); // whole-field tap opens the month picker
|
2026-07-11 02:58:56 +00:00
|
|
|
|
|
|
|
|
|
|
// ---- init ----
|
|
|
|
|
|
(function restore() {
|
2026-07-11 03:17:17 +00:00
|
|
|
|
const hash = readHashState();
|
|
|
|
|
|
if (hash) {
|
|
|
|
|
|
if (hash.comfort != null) comfort = hash.comfort;
|
|
|
|
|
|
if (hash.tol != null) tol = hash.tol;
|
|
|
|
|
|
if (hash.basis) basis = hash.basis;
|
|
|
|
|
|
if (hash.start && hash.end) range = { start: hash.start, end: hash.end };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-11 21:48:21 +00:00
|
|
|
|
comfortInput.value = comfort; comfortVal.textContent = fmtTemp(comfort);
|
|
|
|
|
|
tolInput.value = tol; tolVal.textContent = `±${fmtDelta(tol)}`;
|
2026-07-11 02:58:56 +00:00
|
|
|
|
basisToggle.querySelectorAll("button").forEach((b) => b.classList.toggle("active", b.dataset.basis === basis));
|
|
|
|
|
|
startInput.value = range.start; endInput.value = range.end;
|
|
|
|
|
|
|
2026-07-11 03:17:17 +00:00
|
|
|
|
// A shared link's locations win; else the last-used set; else seed from the spot
|
|
|
|
|
|
// the other views were on so the page isn't empty. All three auto-load on arrival
|
|
|
|
|
|
// (the tap-to-refresh rule is for later interactive edits, not the initial view).
|
|
|
|
|
|
let list = hash && hash.locs;
|
|
|
|
|
|
if (!list) { try { const saved = JSON.parse(lsGet(CMP.locs)); if (Array.isArray(saved)) list = saved; } catch (e) {} }
|
Convert the frontend to ES modules; split nav.js by concern (#48)
The frontend was classic scripts sharing one global scope, with a
load-order contract enforced only by comments (leaflet -> nav ->
shared -> mappicker -> page) and hand-rolled window.Thermograph /
window.LocationPicker namespaces. Page scripts now import what they use;
the dependency graph replaces the ordering contract, and no app globals
remain (Leaflet stays a classic script / global L, loaded first).
nav.js had grown four concerns; it's now three single-purpose modules:
- nav.js: last-location memory + header view-links + locHash.
- units.js: the °F/°C toggle and unit-aware formatting. The compare
special case is gone — pages that want the toggle import units.js;
compare simply doesn't.
- cache.js: the IndexedDB response cache, SWR getJSON, bundle-seeded
view prefetch and neighbor warming. prefetchViews(lat, lon, ownViews)
now takes the calling page's own view names instead of a page-identity
map (VIEW_OWN) — adding a page no longer means editing this module.
The slice->URL map stays here as bundle-contract knowledge.
frontend/package.json ({"type": "module"}) makes CI's node --check
parse the files as modules.
Verified: node --check on all files as modules; 108 backend tests;
headless-Chromium smoke across all five pages against live data — zero
console/page errors, all render assertions pass (cards, chart, calendar
grid + metric switch, ladders, compare ranking, legend scales).
2026-07-11 20:28:33 +00:00
|
|
|
|
if (!list || !list.length) { const last = loadLastLocation(); list = last ? [{ lat: last.lat, lon: last.lon }] : []; }
|
2026-07-11 03:17:17 +00:00
|
|
|
|
|
|
|
|
|
|
locations = list
|
|
|
|
|
|
.filter((l) => l && typeof l.lat === "number" && typeof l.lon === "number")
|
|
|
|
|
|
.map((l) => ({ lat: l.lat, lon: l.lon, name: l.name || null, series: null, loading: false, error: null }));
|
|
|
|
|
|
|
|
|
|
|
|
writeHashState();
|
|
|
|
|
|
const token = ++loadToken;
|
|
|
|
|
|
loadPending(token);
|
2026-07-11 02:58:56 +00:00
|
|
|
|
renderAll();
|
|
|
|
|
|
})();
|