19 lines
877 B
JavaScript
19 lines
877 B
JavaScript
|
|
// Client-side temperature converter for the server-rendered climate/city/record
|
||
|
|
// pages. Those pages render every temperature as `<span class="temp"
|
||
|
|
// data-temp-f="72.3">72°F</span>` (see content.py `_temp`/`_temp_bare`) so the
|
||
|
|
// value is real in the HTML for crawlers and no-JS visitors. Here we rewrite each
|
||
|
|
// span to the active unit on load and whenever the °F/°C toggle changes — reusing
|
||
|
|
// the same unit machinery as the interactive pages. Importing units.js also boots
|
||
|
|
// its toggle-injection IIFE, so this one script gives the pages a working toggle.
|
||
|
|
import { fmtTemp, onUnitChange } from "./units.js";
|
||
|
|
|
||
|
|
function paint() {
|
||
|
|
for (const el of document.querySelectorAll(".temp[data-temp-f]")) {
|
||
|
|
const f = parseFloat(el.dataset.tempF);
|
||
|
|
if (!Number.isNaN(f)) el.textContent = fmtTemp(f, !el.hasAttribute("data-bare"));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
paint();
|
||
|
|
onUnitChange(paint);
|