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
This commit is contained in:
Yohan Boniface 2023-06-23 08:07:49 +02:00
parent bbf4b72e07
commit 666fed3d9c
4 changed files with 33 additions and 4 deletions

View file

@ -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",

View file

@ -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)
}

View file

@ -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 })
},

View file

@ -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)
})
})
})