From 666fed3d9c1f68441afdb91be17713b2bd6d6a04 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Fri, 23 Jun 2023 08:07:49 +0200 Subject: [PATCH 1/3] Be more strict when coordinates are set manually cf https://forum.openstreetmap.fr/t/impossible-de-charger-la-carte-carte-acteurs-naturels-batiment/15607/7 --- package.json | 2 +- umap/static/umap/js/umap.core.js | 2 +- umap/static/umap/js/umap.features.js | 9 +++++++-- umap/static/umap/test/Marker.js | 24 ++++++++++++++++++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 94c1ed9f..eebf3aac 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "leaflet-contextmenu": "^1.4.0", "leaflet-editable": "^1.2.0", "leaflet-editinosm": "0.2.3", - "leaflet-formbuilder": "0.2.4", + "leaflet-formbuilder": "0.2.6", "leaflet-fullscreen": "1.0.2", "leaflet-hash": "0.2.1", "leaflet-i18n": "0.3.1", diff --git a/umap/static/umap/js/umap.core.js b/umap/static/umap/js/umap.core.js index 84ef5f2e..e37a5044 100644 --- a/umap/static/umap/js/umap.core.js +++ b/umap/static/umap/js/umap.core.js @@ -645,5 +645,5 @@ L.U.Orderable = L.Evented.extend({ }) L.LatLng.prototype.isValid = function () { - return !isNaN(this.lat) && !isNaN(this.lng) + return (isFinite(this.lat) && Math.abs(this.lat) <= 90 && isFinite(this.lng) && Math.abs(this.lng) <= 180) } diff --git a/umap/static/umap/js/umap.features.js b/umap/static/umap/js/umap.features.js index 9b7ac8b2..b83a4452 100644 --- a/umap/static/umap/js/umap.features.js +++ b/umap/static/umap/js/umap.features.js @@ -606,17 +606,22 @@ L.U.Marker = L.Marker.extend({ appendEditFieldsets: function (container) { L.U.FeatureMixin.appendEditFieldsets.call(this, container) + const latbk = this._latlng.lat, + lngbk = this._latlng.lng const coordinatesOptions = [ ['_latlng.lat', { handler: 'FloatInput', label: L._('Latitude') }], ['_latlng.lng', { handler: 'FloatInput', label: L._('Longitude') }], ] const builder = new L.U.FormBuilder(this, coordinatesOptions, { callback: function () { - if (!this._latlng.isValid()) - return this.map.ui.alert({ + if (!this._latlng.isValid()) { + this.map.ui.alert({ content: L._('Invalid latitude or longitude'), level: 'error', }) + builder.resetField('_latlng.lat') + builder.resetField('_latlng.lng') + } this._redraw() this.zoomTo({ easing: false }) }, diff --git a/umap/static/umap/test/Marker.js b/umap/static/umap/test/Marker.js index ab219bdf..daf3d327 100644 --- a/umap/static/umap/test/Marker.js +++ b/umap/static/umap/test/Marker.js @@ -95,4 +95,28 @@ describe('L.U.Marker', function () { assert.equal(L.Util.formatNum(layer._latlng.lng), other._latlng.lng) }) }) + + describe('#edit()', function (done) { + it('should allow changing coordinates manually', function () { + var layer = new L.U.Marker(this.map, [10, 20], { + datalayer: this.datalayer, + }).addTo(this.datalayer) + enableEdit() + layer.edit() + changeInputValue(qs('form.umap-form input[name="lat"]'), '54.43') + assert.equal(layer._latlng.lat, 54.43) + }) + + it('should not allow invalid latitude nor longitude', function () { + var layer = new L.U.Marker(this.map, [10, 20], { + datalayer: this.datalayer, + }).addTo(this.datalayer) + enableEdit() + layer.edit() + changeInputValue(qs('form.umap-form input[name="lat"]'), '5443') + assert.equal(layer._latlng.lat, 10) + changeInputValue(qs('form.umap-form input[name="lng"]'), '5443') + assert.equal(layer._latlng.lng, 20) + }) + }) }) From b2676150623dd12e242692bd825cef0ad65c12d9 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Fri, 23 Jun 2023 15:24:50 +0200 Subject: [PATCH 2/3] Remove unused vars --- umap/static/umap/js/umap.features.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/umap/static/umap/js/umap.features.js b/umap/static/umap/js/umap.features.js index b83a4452..be6cde79 100644 --- a/umap/static/umap/js/umap.features.js +++ b/umap/static/umap/js/umap.features.js @@ -606,8 +606,6 @@ L.U.Marker = L.Marker.extend({ appendEditFieldsets: function (container) { L.U.FeatureMixin.appendEditFieldsets.call(this, container) - const latbk = this._latlng.lat, - lngbk = this._latlng.lng const coordinatesOptions = [ ['_latlng.lat', { handler: 'FloatInput', label: L._('Latitude') }], ['_latlng.lng', { handler: 'FloatInput', label: L._('Longitude') }], From c8f853cbaff875fabb5c48b24152b055a0997109 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Fri, 23 Jun 2023 21:02:54 +0200 Subject: [PATCH 3/3] prettier js --- umap/static/umap/js/umap.core.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/umap/static/umap/js/umap.core.js b/umap/static/umap/js/umap.core.js index e37a5044..37787d83 100644 --- a/umap/static/umap/js/umap.core.js +++ b/umap/static/umap/js/umap.core.js @@ -645,5 +645,10 @@ L.U.Orderable = L.Evented.extend({ }) L.LatLng.prototype.isValid = function () { - return (isFinite(this.lat) && Math.abs(this.lat) <= 90 && isFinite(this.lng) && Math.abs(this.lng) <= 180) + return ( + isFinite(this.lat) && + Math.abs(this.lat) <= 90 && + isFinite(this.lng) && + Math.abs(this.lng) <= 180 + ) }