Skip to content

Commit f62f189

Browse files
feat: routes for each algo
1 parent d08a030 commit f62f189

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

main.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,20 @@ let settings: Settings
2828
let automaton: AutomatonBase
2929

3030
window.onload = () => {
31+
const getInitialAlgo = () => {
32+
const path = window.location.pathname.slice(1); // Remove leading slash
33+
const validAlgos = [
34+
"cca-1D", "cca-2D", "cca-3D", "conway",
35+
"immigration", "quadlife", "langton", "entropy"
36+
];
37+
return validAlgos.includes(path) ? path : "cca-2D";
38+
};
39+
3140
pane = new Pane({
3241
title: "Parameters",
3342
expanded: true,
3443
})
35-
const algoSelector = pane.addBinding({ algo: "cca-2D" }, "algo", {
44+
const algoSelector = pane.addBinding({ algo: getInitialAlgo() }, "algo", {
3645
index: 1,
3746
label: "Algorithm",
3847
options: {
@@ -225,6 +234,10 @@ window.onload = () => {
225234
reset()
226235

227236
algoSelector.on("change", (event) => {
237+
// Update URL when algorithm changes
238+
const newUrl = `/${event.value}`;
239+
window.history.pushState({}, '', newUrl);
240+
228241
switch (event.value) {
229242
case "cca-1D":
230243
setCca1dBlades()
@@ -254,6 +267,14 @@ window.onload = () => {
254267
reset()
255268
})
256269

270+
// Handle browser back/forward navigation
271+
window.addEventListener('popstate', () => {
272+
const newAlgo = getInitialAlgo();
273+
if (newAlgo !== settings.algo) {
274+
algoSelector.value = newAlgo;
275+
}
276+
});
277+
257278
addBlinkerBtn.on("click", () => {
258279
automaton.placePatternRandomly(blinkerPattern())
259280
})

0 commit comments

Comments
 (0)