From 123a6ea5082b5fb24ec3f6e591fc1600294db2dc Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Sat, 16 Nov 2024 10:30:15 +0100 Subject: [PATCH] fix: honour `rules` and `slideshow` when importing a umap file Side note (for another PR I'd say): this usually occurs on a non saved map, but in case the map already exist and is in sync mode, should we sync each property (safer but one call per property) or the whole properties object (lighter, but may override unwanted things in remote, and currently there is no method to do so). --- umap/static/umap/js/modules/rules.js | 4 ++-- umap/static/umap/js/modules/slideshow.js | 8 ++++++-- umap/static/umap/js/modules/umap.js | 25 ++++++++++++++---------- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/umap/static/umap/js/modules/rules.js b/umap/static/umap/js/modules/rules.js index 3f3244bd6..f8c6dbb4f 100644 --- a/umap/static/umap/js/modules/rules.js +++ b/umap/static/umap/js/modules/rules.js @@ -196,10 +196,10 @@ export default class Rules { constructor(umap) { this._umap = umap this.rules = [] - this.loadRules() + this.load() } - loadRules() { + load() { if (!this._umap.properties.rules?.length) return for (const { condition, options } of this._umap.properties.rules) { if (!condition) continue diff --git a/umap/static/umap/js/modules/slideshow.js b/umap/static/umap/js/modules/slideshow.js index 0d9a7766a..ef8ab0fab 100644 --- a/umap/static/umap/js/modules/slideshow.js +++ b/umap/static/umap/js/modules/slideshow.js @@ -18,7 +18,7 @@ export default class Slideshow extends WithTemplate { this._umap = umap this._id = null this.CLASSNAME = 'umap-slideshow-active' - this.setProperties(properties) + this.load() this._current = null if (this.properties.autoplay) { @@ -54,7 +54,11 @@ export default class Slideshow extends WithTemplate { return this.current.getNext() } - setProperties(properties) { + load() { + this.setProperties(this._umap.properties.slideshow) + } + + setProperties(properties = {}) { this.properties = Object.assign( { delay: 5000, diff --git a/umap/static/umap/js/modules/umap.js b/umap/static/umap/js/modules/umap.js index b8a03f2e0..d65ff0772 100644 --- a/umap/static/umap/js/modules/umap.js +++ b/umap/static/umap/js/modules/umap.js @@ -94,7 +94,7 @@ export default class Umap extends ServerStored { // Needed to render controls this.permissions = new MapPermissions(this) this.urls = new URLs(this.properties.urls) - this.slideshow = new Slideshow(this, this._leafletMap, this.properties.slideshow) + this.slideshow = new Slideshow(this, this._leafletMap) this._leafletMap.setup() @@ -671,6 +671,17 @@ export default class Umap extends ServerStored { this.permissions.properties = Object.assign({}, this._backupProperties.permissions) } + setProperties(newProperties) { + for (const key of Object.keys(SCHEMA)) { + if (newProperties[key] !== undefined) { + this.properties[key] = newProperties[key] + if (key === 'rules') this.rules.load() + if (key === 'slideshow') this.slideshow.load() + // TODO: sync ? + } + } + } + hasData() { for (const datalayer of this.datalayersIndex) { if (datalayer.hasData()) return true @@ -993,7 +1004,7 @@ export default class Umap extends ServerStored { ], ] const slideshowBuilder = new U.FormBuilder(this, slideshowFields, { - callback: () => this.slideshow.setProperties(this.properties.slideshow), + callback: () => this.slideshow.load(), umap: this, }) slideshow.appendChild(slideshowBuilder.build()) @@ -1483,15 +1494,9 @@ export default class Umap extends ServerStored { importRaw(rawData) { const importedData = JSON.parse(rawData) + const mustReindex = 'sortKey' in Object.keys(importedData.properties) - let mustReindex = false - - for (const option of Object.keys(SCHEMA)) { - if (typeof importedData.properties[option] !== 'undefined') { - this.properties[option] = importedData.properties[option] - if (option === 'sortKey') mustReindex = true - } - } + this.setProperties(importedData.properties) if (importedData.geometry) { this.properties.center = this._leafletMap.latLng(importedData.geometry)