From 7a7977c5cdf36fcee4d93d0ec15fae554a83d84e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20M=C3=A9taireau?= Date: Mon, 24 Jun 2024 19:08:25 +0200 Subject: [PATCH] fix(sync): do not send messages when loading data from the server This introduces a `sync=true/false` parameter that can be passed along when loading data from our own server, in which case we don't want to send "operation" messages. --- umap/static/umap/js/umap.features.js | 4 ++-- umap/static/umap/js/umap.js | 3 ++- umap/static/umap/js/umap.layer.js | 14 +++++++------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/umap/static/umap/js/umap.features.js b/umap/static/umap/js/umap.features.js index 89412135..ba30c4fe 100644 --- a/umap/static/umap/js/umap.features.js +++ b/umap/static/umap/js/umap.features.js @@ -23,7 +23,7 @@ U.FeatureMixin = { // event triggered to cause a sync event, as it would reintroduce // deleted features. // The `._marked_for_deletion` private property is here to track this status. - if (this._marked_for_deletion == true) { + if (this._marked_for_deletion === true) { this._marked_for_deletion = false return } @@ -37,7 +37,7 @@ U.FeatureMixin = { initialize: function (map, latlng, options, id) { this.map = map this.sync = map.sync_engine.proxy(this) - this._mark_for_deletion = false + this._marked_for_deletion = false if (typeof options === 'undefined') { options = {} diff --git a/umap/static/umap/js/umap.js b/umap/static/umap/js/umap.js index d58c4e57..57efc650 100644 --- a/umap/static/umap/js/umap.js +++ b/umap/static/umap/js/umap.js @@ -465,7 +465,8 @@ U.Map = L.Map.extend({ initDataLayers: async function (datalayers) { datalayers = datalayers || this.options.datalayers for (const options of datalayers) { - this.createDataLayer(options) + // `false` to not propagate syncing elements served from uMap + this.createDataLayer(options, false) } await this.loadDataLayers() }, diff --git a/umap/static/umap/js/umap.layer.js b/umap/static/umap/js/umap.layer.js index 810d530d..6992a2f2 100644 --- a/umap/static/umap/js/umap.layer.js +++ b/umap/static/umap/js/umap.layer.js @@ -730,8 +730,8 @@ U.DataLayer = L.Evented.extend({ } }, - fromGeoJSON: function (geojson) { - this.addData(geojson) + fromGeoJSON: function (geojson, sync = true) { + this.addData(geojson, sync) this._geojson = geojson this._dataloaded = true this.fire('dataloaded') @@ -742,7 +742,7 @@ U.DataLayer = L.Evented.extend({ if (geojson._storage) geojson._umap_options = geojson._storage // Retrocompat if (geojson._umap_options) this.setOptions(geojson._umap_options) if (this.isRemoteLayer()) await this.fetchRemoteData() - else this.fromGeoJSON(geojson) + else this.fromGeoJSON(geojson, false) this._loaded = true }, @@ -924,11 +924,11 @@ U.DataLayer = L.Evented.extend({ if (idx !== -1) this._propertiesIndex.splice(idx, 1) }, - addData: function (geojson) { + addData: function (geojson, sync) { try { // Do not fail if remote data is somehow invalid, // otherwise the layer becomes uneditable. - this.geojsonToFeatures(geojson) + this.geojsonToFeatures(geojson, sync) } catch (err) { console.log('Error with DataLayer', this.umap_id) console.error(err) @@ -1015,7 +1015,7 @@ U.DataLayer = L.Evented.extend({ // The choice of the name is not ours, because it is required by Leaflet. // It is misleading, as the returned objects are uMap objects, and not // GeoJSON features. - geojsonToFeatures: function (geojson) { + geojsonToFeatures: function (geojson, sync) { if (!geojson) return const features = geojson instanceof Array ? geojson : geojson.features let i @@ -1034,7 +1034,7 @@ U.DataLayer = L.Evented.extend({ const feature = this.geoJSONToLeaflet({ geometry, geojson }) if (feature) { this.addLayer(feature) - feature.onCommit() + if (sync) feature.onCommit() return feature } },