Public
Edited
Feb 22, 2023
1 fork
Importers
15 stars
Insert cell
Changed
# Linking Multiple Ranges Created in response to the forum question [Synchronized Inputs range sliders](https://talk.observablehq.com/t/synchronized-inputs-range-sliders/7652).
-
A workaround to provide a multi-range input. Mostly a proof of concept, has some minor glitches.
+
A workaround to provide a multi-range input.
~~~js import {linkRanges} from "c826aea92a8ae6d4" ~~~
Insert cell
Insert cell
Insert cell
Insert cell
viewof demo = inputTemplate(
Inputs.form(
linkRanges(
d3.pairs([0, .25, .5, .75, 1]).map(
(value, i) => interval([0, 1], {
value,
color: ["#f00", "#0c0", "#fa0", "#5af"][i],
// Hide values
format: d => htl.html.fragment``,
step: .01,
width: 240
})
)
)
),
{ label: "Ranges", width: 120 }
)
Insert cell
demo
Insert cell
Insert cell
Changed
function linkRanges(inputs) { const {min, max} = Math;
+
const clamp = (a, b, v) => v < a ? a : v > b ? b : v;
inputs.forEach((view, i, views) => { view.addEventListener("input", () => { let [a, b] = view.value; const p = views[i - 1]; const n = views[i + 1];
-
if(p) { p.value = [p.value[0], max(p.value[0], a)]; b = max(b, a = p.value[1]); } if(n) { n.value = [min(b, n.value[1]), n.value[1]]; a = min(a, b = n.value[0]); }
+
const lo = p?.value[0] ?? -Infinity; const hi = n?.value[1] ?? Infinity; a = clamp(lo, hi, a); b = clamp(lo, hi, b); if(p) p.value = [lo, max(lo, a)]; if(n) n.value = [min(b, hi), hi];
view.value = [a, b]; }); }); return inputs; }
Insert cell