Skip to content

Commit

Permalink
Persist selected travel mode (foot, bicycle, etc.) in URL parameters (c…
Browse files Browse the repository at this point in the history
…lose #12)
  • Loading branch information
quincylvania committed Nov 17, 2023
1 parent a933e36 commit ea94453
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
5 changes: 4 additions & 1 deletion css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ h1, h2, h3, h4, h5, h6 {
width: 100%;
appearance: none;
-webkit-appearance: none;
border-color: #ddd;
-moz-appearance:none;
background: white;
border: 1px solid #ddd;
border-radius: 5px;
padding: 4px;
box-shadow: 0 0 2px rgba(0, 0, 0, 0.2);
}
Expand Down
14 changes: 10 additions & 4 deletions js/hashController.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ function setHashParameters(params) {
}
}

function entityInfoFromHash() {
function hashValue(key) {
var searchParams = new URLSearchParams(window.location.hash.slice(1));
if (searchParams.has("selected")) {
var value = searchParams.get("selected");
if (searchParams.has(key)) return searchParams.get(key);
return null;
}

function entityInfoFromHash() {
var value = hashValue("selected");
if (value) {
var components = value.split("/");
if (components.length == 2) {
var type = components[0];
Expand All @@ -32,6 +37,7 @@ function entityInfoFromHash() {
return null;
}

function updateForHash() {
function updateForHash() {
setTravelMode(hashValue("mode"));
selectEntity(entityInfoFromHash());
}
17 changes: 14 additions & 3 deletions js/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var map;

var mode = "foot";
const defaultMode = "foot";
var mode = defaultMode;

const colors = {
public: "#005908",
Expand Down Expand Up @@ -57,13 +58,23 @@ function selectEntity(entityInfo) {
});
}

function setTravelMode(newMode) {
if (newMode === null) newMode = defaultMode;
if (mode === newMode) return;
mode = newMode;

document.getElementById("travel-mode").value = mode;

updateMapLayers();
setHashParameters({ mode: mode === defaultMode ? null : mode });
}

window.onload = (event) => {

window.addEventListener("hashchange", updateForHash);

document.getElementById("travel-mode").onchange = function(e) {
mode = e.target.value;
updateMapLayers();
setTravelMode(e.target.value);
}

try {
Expand Down

0 comments on commit ea94453

Please sign in to comment.