From a96bc95d29304e8501d908252c24457cf909adf8 Mon Sep 17 00:00:00 2001 From: Joonas Date: Thu, 21 Mar 2024 15:46:34 +0200 Subject: [PATCH] Add answer option modications / Fix json validation for location form in case of empty geojson --- app/cells/decidim/locations/map_cell.rb | 6 +++--- app/forms/decidim/locations/location_form.rb | 13 +++++++++++++ .../decidim/locations/map/controller/location.js | 4 +++- .../locations/map/integration/add_input_group.js | 2 +- .../locations/map/integration/build_geojson.js | 2 +- app/packs/src/decidim/map/controller/markers.js | 7 ++++--- spec/forms/decidim/locations/location_form_spec.rb | 5 ++--- 7 files changed, 27 insertions(+), 12 deletions(-) diff --git a/app/cells/decidim/locations/map_cell.rb b/app/cells/decidim/locations/map_cell.rb index 2105521..23d17ea 100644 --- a/app/cells/decidim/locations/map_cell.rb +++ b/app/cells/decidim/locations/map_cell.rb @@ -90,10 +90,10 @@ def markers_center def markers_data_for_map format_map_locations(model).map do |data| - if data.instance_of?(Decidim::Forms::LocationOption) + if data.instance_of?(Decidim::Forms::AnswerOption) { - location: data.title, - geojson: JSON.parse(data.body) + location: data.body, + geojson: JSON.parse(data.geojson) } else body = data[2] diff --git a/app/forms/decidim/locations/location_form.rb b/app/forms/decidim/locations/location_form.rb index b1817b4..7f758e9 100644 --- a/app/forms/decidim/locations/location_form.rb +++ b/app/forms/decidim/locations/location_form.rb @@ -29,6 +29,19 @@ def to_param private def json_validation + if geojson.blank? + geojson = { + type: "Feature", + geometry: { + type: shape, + coordinates: + [latitude, longitude] + } + } + + return + end + # check if GeoJSON is valid begin geo_factory = RGeo::Geographic.spherical_factory diff --git a/app/packs/src/decidim/locations/map/controller/location.js b/app/packs/src/decidim/locations/map/controller/location.js index e98a558..6746074 100644 --- a/app/packs/src/decidim/locations/map/controller/location.js +++ b/app/packs/src/decidim/locations/map/controller/location.js @@ -24,12 +24,14 @@ export default class ModelLocMapController extends MapController { // Position the center of the map const lat = mapEl.dataset.lat; const lng = mapEl.dataset.lng; + const selectLocation = mapEl.dataset.selectLocation; let defaultLat = 0; let defaultLng = 0; let zoom = 0; - if (lat !== defaultLat.toFixed(1) || lng !== defaultLng.toFixed(1)) { + if (selectLocation === "false" && (lat !== defaultLat.toFixed(1) || lng !== defaultLng.toFixed(1))) { + console.log("aASD") defaultLat = lat; defaultLng = lng; zoom = 14; diff --git a/app/packs/src/decidim/locations/map/integration/add_input_group.js b/app/packs/src/decidim/locations/map/integration/add_input_group.js index ef4c316..055e919 100644 --- a/app/packs/src/decidim/locations/map/integration/add_input_group.js +++ b/app/packs/src/decidim/locations/map/integration/add_input_group.js @@ -11,7 +11,7 @@ const addInputGroup = function (shapeFieldContainer, addressData, wrapperEl) { const coordinates = JSON.stringify(addressData.coordinates); const shapeField = shapeFieldContainer.querySelector(`[data-shape-id="${shapeId}"]`); - if (parentNodeId === "location-option-selector") { + if (parentNodeId === "answer-option-map-selector") { const currentGeo = document.querySelector("button.location-selector").parentNode.querySelector("label > textarea"); currentGeo.value = JSON.stringify(buildGeoJson(coordinates, objectShape, address)); diff --git a/app/packs/src/decidim/locations/map/integration/build_geojson.js b/app/packs/src/decidim/locations/map/integration/build_geojson.js index ef0576c..37637b4 100644 --- a/app/packs/src/decidim/locations/map/integration/build_geojson.js +++ b/app/packs/src/decidim/locations/map/integration/build_geojson.js @@ -18,7 +18,7 @@ const coordinatesToObject = function (coordinates, shape) { } const buildGeoJson = function (coordinates, shape, address) { - + console.log(address) if (address) { return { "type": "Feature", diff --git a/app/packs/src/decidim/map/controller/markers.js b/app/packs/src/decidim/map/controller/markers.js index 3d6cc33..45f211b 100644 --- a/app/packs/src/decidim/map/controller/markers.js +++ b/app/packs/src/decidim/map/controller/markers.js @@ -50,7 +50,7 @@ export default class MapMarkersController extends MapController { const bounds = new L.LatLngBounds( markersData.map( (markerData) => { - if (markerData.geojson.type) { + if (markerData.location) { return markerData.geojson.geometry.coordinates } @@ -70,7 +70,7 @@ export default class MapMarkersController extends MapController { markersData.forEach((markerData) => { let shape = {} - if (markerData.geojson.type) { + if (markerData.location) { const coordinates = markerData.geojson.geometry.coordinates; const location = markerData.location; const objectShape = markerData.geojson.geometry.type; @@ -95,7 +95,7 @@ export default class MapMarkersController extends MapController { shape: objectShape}) } - shape.bindTooltip(location, {permanent: true, interactive: true}); + shape.bindTooltip(location.en, {permanent: true, interactive: true}); this.markerClusters.addLayer(shape); } else { @@ -141,6 +141,7 @@ export default class MapMarkersController extends MapController { // mobile). Make sure there is at least the same amount of width and // height available on both sides + the padding (i.e. 4x padding in // total). + if (this.selectLocation()) { this.map.fitBounds(bounds); } else { diff --git a/spec/forms/decidim/locations/location_form_spec.rb b/spec/forms/decidim/locations/location_form_spec.rb index 08819a6..4af9253 100644 --- a/spec/forms/decidim/locations/location_form_spec.rb +++ b/spec/forms/decidim/locations/location_form_spec.rb @@ -63,12 +63,11 @@ address: "", latitude: 50.149792, longitude: 24.887430, - shape: "Point", - geojson: "" + shape: "Point" } end - it { is_expected.not_to be_valid } + it { is_expected.to be_valid } end context "when geojson is not correct" do