Skip to content

Commit

Permalink
feat: update URL
Browse files Browse the repository at this point in the history
  • Loading branch information
dhdaines committed Feb 19, 2024
1 parent 23bb209 commit 84969c7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
29 changes: 20 additions & 9 deletions js/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,6 @@ let zonage = {
milieu: {},
}

window.addEventListener("load", async () => {
const response = await fetch(`${ALEXI_URL}/index.json`);
if (response.ok) {
zonage = await response.json();
}
});

function categorieTexte(info) {
if (info === null)
return "inconnu";
Expand Down Expand Up @@ -118,7 +111,7 @@ const zonesource = new VectorSource();
const zonelayer = new VectorLayer<any>({ // stfu tsc
source: zonesource,
});
function updateInfo(info) {
function updateInfo(info, coords) {
const infoDiv = document.getElementById("info");
if (infoDiv === null)
throw "Element not found: info";
Expand All @@ -139,6 +132,8 @@ function updateInfo(info) {
maxZoom: 16,
duration: 500,
});
const [lon, lat] = coords;
history.replaceState(null, "", "?g=" + encodeURICompoenent(`${lat},${lon}`));
}

function infoError(txt: string) {
Expand All @@ -154,7 +149,7 @@ async function getInfo(coords: GeoJSONPosition) {
const response = await fetch(url);
if (response.ok) {
const info = await response.json();
updateInfo(info);
updateInfo(info, coords);
}
else if (response.status == 404) {
infoError(`L'endroit choisi ne se situe pas à Sainte-Adèle. Veuillez réessayer.`);
Expand Down Expand Up @@ -209,3 +204,19 @@ map.on("click", async (evt) => {
const coords = toLonLat(evt.coordinate);
getInfo(coords);
});

window.addEventListener("load", async () => {
const response = await fetch(`${ALEXI_URL}/index.json`);
if (response.ok) {
zonage = await response.json();
}
const urlParams = new URLSearchParams(window.location.search);
const centroid = urlParams.get("g");
if (centroid !== null) {
const [lat, lon] = centroid.split(",").map(parseFloat);
const coords = [lon, lat];
const projPos = fromLonLat(coords);
view.animate({center: projPos});
getInfo(coords);
}
});
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 84969c7

Please sign in to comment.