From 2261c5c0324e781448712f68c3d11561a65c2c20 Mon Sep 17 00:00:00 2001
From: Markus Rullkoetter <markus.rullkoetter@dataport.de>
Date: Thu, 1 Dec 2022 13:44:38 +0100
Subject: [PATCH] wmsTime: setting the target layer for the swiper directly
 instead of retrieving it in mounted hook to prevent load time issues

---
 CHANGELOG.md                                              | 1 +
 src/modules/wmsTime/components/LayerSwiper.vue            | 1 -
 src/modules/wmsTime/store/actionsWmsTime.js               | 2 ++
 src/modules/wmsTime/store/mutationsWmsTime.js             | 2 +-
 .../wmsTime/tests/unit/store/actionsWmsTime.spec.js       | 8 ++++++--
 5 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8f542c6d8b..64b8c6e63a 100755
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -66,6 +66,7 @@
   - moving the swiper after the playback ended updates the view correctly now
   - if the last slider position is clicked, the playing status becomes false
   - added a loading spinner and disabled all inputs while the layer is not fully loaded
+  - setting the target layer for the swiper directly instead of retrieving it in mounted hook to prevent load time issues
 - Issue #821: Sorting the table in featureLister tool now works again
 - Issue #844: fileImport now converts the projection of geoJSON if necessary
 - Issue #845: The buffer analysis tool now shows results correctly again
diff --git a/src/modules/wmsTime/components/LayerSwiper.vue b/src/modules/wmsTime/components/LayerSwiper.vue
index d4c5f08ff2..7d14b69e3a 100644
--- a/src/modules/wmsTime/components/LayerSwiper.vue
+++ b/src/modules/wmsTime/components/LayerSwiper.vue
@@ -13,7 +13,6 @@ export default {
     mounted () {
         const target = document.getElementById("wmsTime-layerSwiper-button");
 
-        this.setLayerSwiperTargetLayer(this.getVisibleLayerList.find(element => element.values_.id === this.currentTimeSliderObject.layerId + this.layerAppendix));
         this.setLayerSwiperValueX(mapCollection.getMap(this.mode).getSize()[0] / 2);
         mapCollection.getMap(this.mode).on("postcompose", this.updateMap);
 
diff --git a/src/modules/wmsTime/store/actionsWmsTime.js b/src/modules/wmsTime/store/actionsWmsTime.js
index 2e439be26a..b86e604211 100644
--- a/src/modules/wmsTime/store/actionsWmsTime.js
+++ b/src/modules/wmsTime/store/actionsWmsTime.js
@@ -49,6 +49,7 @@ const actions = {
                 }
             );
             Radio.trigger("ModelList", "addModelsByAttributes", {id: secondId});
+            commit("setLayerSwiperTargetLayer", Radio.request("ModelList", "getModelByAttributes", {id: secondId}).get("layer"));
         }
         else {
             // If the button of the "original" window is clicked, it is assumed, that the time value selected in the added window is desired to be further displayed.
@@ -90,6 +91,7 @@ const actions = {
         if (!state.timeSlider.playing) {
             mapCollection.getMap(rootGetters["Maps/mode"]).render();
         }
+
         state.layerSwiper.targetLayer?.once("prerender", renderEvent => dispatch("drawLayer", renderEvent));
         state.layerSwiper.targetLayer?.once("postrender", ({context}) => {
             context.restore();
diff --git a/src/modules/wmsTime/store/mutationsWmsTime.js b/src/modules/wmsTime/store/mutationsWmsTime.js
index 4ab10123ee..678c04841b 100644
--- a/src/modules/wmsTime/store/mutationsWmsTime.js
+++ b/src/modules/wmsTime/store/mutationsWmsTime.js
@@ -25,7 +25,7 @@ const mutations = {
 
         // NOTE: This is needed when the LayerSwiper is closed and no new value was selected in the second
         // TimeSlider thus newValue would be the same as defaultValue and would not trigger the update Event.
-        currentObject.defaultValue = currentObject.timeRange[0];
+        currentObject.defaultValue = currentObject.timeRange?.[0];
         Vue.nextTick(() => {
             currentObject.defaultValue = newValue;
         });
diff --git a/src/modules/wmsTime/tests/unit/store/actionsWmsTime.spec.js b/src/modules/wmsTime/tests/unit/store/actionsWmsTime.spec.js
index b862ea4b52..72c97d39f5 100644
--- a/src/modules/wmsTime/tests/unit/store/actionsWmsTime.spec.js
+++ b/src/modules/wmsTime/tests/unit/store/actionsWmsTime.spec.js
@@ -109,13 +109,17 @@ describe("src/modules/wmsTime/store/actionsWmsTime.js", () => {
         });
 
         it("should trigger the Parser to add a layer, add said layer to the ModelList and refresh tree if the swiper was activated", () => {
+            const secondId = id + state.layerAppendix;
+
             actions.toggleSwiper({commit, getters, state}, id);
 
-            expect(commitSpy.calledTwice).to.be.true;
+            expect(commitSpy.calledThrice).to.be.true;
             expect(commitSpy.firstCall.args).to.eql(["setLayerSwiperActive", true]);
             expect(commitSpy.secondCall.args[0]).to.eql("setLayerSwiperSourceLayer");
-            expect(requestSpy.calledOnce).to.be.true;
+            expect(commitSpy.thirdCall.args[0]).to.eql("setLayerSwiperTargetLayer");
+            expect(requestSpy.calledTwice).to.be.true;
             expect(requestSpy.firstCall.args).to.eql(["ModelList", "getModelByAttributes", {id}]);
+            expect(requestSpy.secondCall.args).to.eql(["ModelList", "getModelByAttributes", {id: secondId}]);
             expect(trigger.calledThrice).to.be.true;
             expect(trigger.firstCall.args[0]).to.equal("Parser");
             expect(trigger.firstCall.args[1]).to.equal("addLayer");
-- 
GitLab