From 94d56354d6e137f8c573da1d26751fee93f9908d Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Wed, 12 Apr 2023 13:58:49 -0700 Subject: [PATCH] Switch to using a simple modal dialog for name entry, so that we can expand it later --- mapmakr/database.py | 3 +- mapmakr/settings.py | 1 + mapmakr/static/js/mapmakr.js | 80 +++++++++++++++++++----------------- mapmakr/templates/index.html | 10 +++++ 4 files changed, 55 insertions(+), 39 deletions(-) diff --git a/mapmakr/database.py b/mapmakr/database.py index 9d37ed0..57de7ae 100644 --- a/mapmakr/database.py +++ b/mapmakr/database.py @@ -8,5 +8,6 @@ _engine = None def get_engine(): global _engine if not _engine: - _engine = create_engine(settings.database_uri, echo=settings.database_echo) + _engine = create_engine(settings.database_uri, echo=settings.database_echo, + connect_args=settings.database_options) return _engine diff --git a/mapmakr/settings.py b/mapmakr/settings.py index bbaab8e..3a2c082 100644 --- a/mapmakr/settings.py +++ b/mapmakr/settings.py @@ -4,6 +4,7 @@ from pydantic import BaseSettings class Settings(BaseSettings): database_uri: str = 'sqlite:///mapmakr.sqlite' database_echo: bool = False + database_options: dict = {'check_same_thread': False} site_title: str = 'mapmakr' diff --git a/mapmakr/static/js/mapmakr.js b/mapmakr/static/js/mapmakr.js index 21ef09c..3be8f7b 100644 --- a/mapmakr/static/js/mapmakr.js +++ b/mapmakr/static/js/mapmakr.js @@ -1,4 +1,4 @@ -let map, drawnItems, drawControl; +let map, drawnItems, drawControl, currentLayer; function reload_map() { @@ -53,9 +53,9 @@ function setup_map() { refreshButton.button.style.fontSize = '18px'; map.addControl(refreshButton); - const helpButton = new L.easyButton('icon ion-help-circled', function() {open("about.html");}); - helpButton.button.style.fontSize = '18px'; - map.addControl(helpButton); + // const helpButton = new L.easyButton('icon ion-help-circled', function() {open("about.html");}); + // helpButton.button.style.fontSize = '18px'; + // map.addControl(helpButton); const controlSearch = new L.Control.Search({layer: drawnItems, propertyName: 'id', initial: false, zoom: 12}); map.addControl(controlSearch); @@ -85,45 +85,15 @@ function setup_map() { return; } - let name = prompt("Marker name:", ""); - if (name) { - layer.options.id = null; - layer.options.title = name; - layer.bindPopup(name); - drawnItems.addLayer(layer); - let requestOptions = { - method: "POST", - headers: { - "Accept": "application/json", - "Content-Type": "application/json" - }, - body: JSON.stringify({ - "name": name, - "latitude": layer.getLatLng()["lat"], - "longitude": layer.getLatLng()["lng"], - "options": {} - }) - }; - fetch("/marker", requestOptions).then(() => reload_map()); - } + currentLayer = layer; + $("#edit-form").modal(); }); map.on('draw:edited', function (e) { e.layers.eachLayer(layer => { let type = layer.layerType, id = layer.options.id; - let requestOptions = { - method: "PATCH", - headers: { - "Accept": "application/json", - "Content-Type": "application/json" - }, - body: JSON.stringify({ - "name": layer.options.name, - "latitude": layer.getLatLng()["lat"], - "longitude": layer.getLatLng()["lng"] - }) - }; - fetch("/marker/" + id, requestOptions).then(() => reload_map()); + currentLayer = layer; + $("#edit-form").modal(); }); }); @@ -133,4 +103,38 @@ function setup_map() { fetch("/marker/" + id, {method: "DELETE"}).then(() => reload_map()); }); }); + + $("#marker-save").on("click", (event) => { + event.preventDefault(); + let name = $("#marker-name").val(); + const markerAttributes = { + "name": name, + "latitude": currentLayer.getLatLng()["lat"], + "longitude": currentLayer.getLatLng()["lng"], + "options": {} + }; + if (name) { + currentLayer.options.title = name; + currentLayer.bindPopup(name); + drawnItems.addLayer(currentLayer); + let requestOptions = { + headers: { + "Accept": "application/json", + "Content-Type": "application/json" + }, + body: JSON.stringify(markerAttributes) + }; + console.log(currentLayer); + if (currentLayer.options.id) { + requestOptions["method"] = "PATCH"; + fetch("/marker/" + currentLayer.options.id, requestOptions).then(() => reload_map()); + } + else { + requestOptions["method"] = "POST"; + fetch("/marker", requestOptions).then(() => reload_map()); + } + } + $.modal.close(); + return false; + }); } diff --git a/mapmakr/templates/index.html b/mapmakr/templates/index.html index b8cc5a6..637e9ba 100644 --- a/mapmakr/templates/index.html +++ b/mapmakr/templates/index.html @@ -7,6 +7,7 @@ + @@ -15,9 +16,18 @@
+ + +