From f0626b175d21230dc68ecd83e2726ec7e7daae5a Mon Sep 17 00:00:00 2001 From: user Date: Mon, 20 May 2024 19:51:41 +0200 Subject: user input categories, refactor, remove zombie code --- src/static/index.html | 18 ------------ src/static/main.js | 79 ++++++++++++++++++++++++++++++++++----------------- 2 files changed, 53 insertions(+), 44 deletions(-) delete mode 100644 src/static/index.html (limited to 'src/static') diff --git a/src/static/index.html b/src/static/index.html deleted file mode 100644 index 5bfcc1f..0000000 --- a/src/static/index.html +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - Nearest shop map - - - - - - - - -
- - diff --git a/src/static/main.js b/src/static/main.js index a920476..be136a4 100644 --- a/src/static/main.js +++ b/src/static/main.js @@ -11,14 +11,40 @@ var markers = L.featureGroup().addTo(map); var polygons = L.featureGroup().addTo(map); var heatmap = L.featureGroup().addTo(map); +const CategoryField = document.getElementById('Category'); + +const DefaultSearchValue = "supermarket"; +var Categories = [] + var overlayMaps = { - "Supermarket icons": markers, - "Nearest supermarket polygons": polygons, - "Stores heatmap": heatmap + "POI markers": markers, + "Nearest POI polygons": polygons, +// "POI heatmap": heatmap }; var layerControl = L.control.layers({},overlayMaps).addTo(map); +function runQueryAfterLoad() { + fetchAll(CategoryField.value); +} + +async function fetchCategories() { + const response = await fetch("categories"); + try { + categories = await response.json(); + const CategoriesDataList = document.getElementById('CategoriesDataList'); + categories.forEach(function(item){ + var option = document.createElement('option'); + option.value = item; + option.innerHTML = item; + CategoriesDataList.appendChild(option); + }); + CategoryField.pattern = categories.join('|'); + } catch (error) { + alert(`Failed to fetch categories, are you online?\n${error.message}`) + } +} + async function fetchChains() { const response = await fetch("chains"); known_store_chains = await response.json(); @@ -33,7 +59,25 @@ async function fetchChains() { iconAnchor: [8, 8], }); } - fetchAll() + + const queryString = window.location.search; + const urlParams = new URLSearchParams(queryString); + const urlParam = urlParams.get('Category') + const fieldValue = CategoryField.value + // Retains saved value from input field on reload + if (urlParam) + { + fetchAll(urlParam); + CategoryField.value = urlParam; + + } else if (fieldValue !== "") + { + fetchAll(fieldValue); + } else + { + CategoryField.value = DefaultSearchValue; + fetchAll(DefaultSearchValue); + } } @@ -64,8 +108,8 @@ function addShop(shop) { } -async function fetchAll() { - const response = await fetch("all.json"); +async function fetchAll(Category) { + const response = await fetch(`all.json?Category=${Category}`) const shops = await response.json(); shops.forEach((shop) => addShop(shop)); } @@ -76,23 +120,6 @@ async function fetchShopHeatmap() { var heat = L.heatLayer(shops, {radius: 30}).addTo(heatmap); } - -async function fetch_voronoi_polygons() { - const response = await fetch("voronoi_polygons.json"); - const voronoi_polygons = await response.json(); - voronoi_polygons.forEach((polygon) => addPolygon(polygon)); -} - -function update(){ - if (map.getZoom() >= 14){ - //console.log('update') - } -} - -map.on('moveend', function() { - update() -}); - -fetchChains() - -fetchShopHeatmap(); +fetchChains(); +fetchCategories(); +//fetchShopHeatmap(); -- cgit v1.2.3