From 9a6b9e411ab7c2778086e07f474c8714925967b9 Mon Sep 17 00:00:00 2001
From: enured <enis.nuredini@uzh.ch>
Date: Mon, 7 Feb 2022 20:28:19 +0100
Subject: [PATCH] F13440 Implemented JS-script to save forms informations and
 get existing informations from session storage. The last activated pill will
 be saved as information for every form with its own record Id.

---
 javascript/src/BSTabs.js  |  2 ++
 javascript/src/QfqPage.js | 76 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+)

diff --git a/javascript/src/BSTabs.js b/javascript/src/BSTabs.js
index 4ba1df972..fdb195551 100644
--- a/javascript/src/BSTabs.js
+++ b/javascript/src/BSTabs.js
@@ -35,6 +35,8 @@ var QfqNS = QfqNS || {};
         this.tabs = {};
         this.currentTab = this.getActiveTabFromDOM();
         this.eventEmitter = new EventEmitter();
+        this.currentFormName = $('#' + this.tabId + ' .active a[data-toggle="tab"]')[0].hash.slice(1).split("_")[0];
+        this.currentRecordId = $('#' + this.tabId + ' a[data-toggle="tab"]')[0].id.split("-")[2];
 
         // Fill this.tabs
         this.fillTabInformation();
diff --git a/javascript/src/QfqPage.js b/javascript/src/QfqPage.js
index 6914b6efb..ec3f21b53 100644
--- a/javascript/src/QfqPage.js
+++ b/javascript/src/QfqPage.js
@@ -45,7 +45,36 @@ var QfqNS = QfqNS || {};
         try {
             this.bsTabs = new n.BSTabs(this.settings.tabsId);
 
+            var storedFormInfos = [];
+
+            // get current state from session storage
+            if(sessionStorage.getItem("formInfos") !== null) {
+                storedFormInfos = JSON.parse(sessionStorage.getItem("formInfos"));
+            }
+
+            var currentForm = this.bsTabs.currentFormName;
+            var currentRecordId = this.bsTabs.currentRecordId;
+
+            var actualIndex = -1;
+            var indexNr = 0;
+            if(storedFormInfos.length !== 0){
+                if(storedFormInfos[0] !== ''){
+                    storedFormInfos.forEach(function callback(element){
+                        if(element === currentForm && storedFormInfos[indexNr+2] === currentRecordId){
+                            actualIndex = indexNr;
+                        }
+                        indexNr++;
+                    });
+                }
+            }
+
             var currentState = this.settings.pageState.getPageState();
+
+            // load from sessionStorage or from path given hash if not empty
+            if(actualIndex !== -1 && location.hash === "") {
+                currentState = storedFormInfos[actualIndex+1];
+            }
+
             if (currentState !== "") {
                 this.bsTabs.activateTab(currentState);
                 n.PageTitle.setSubTitle(this.bsTabs.getTabName(currentState));
@@ -151,6 +180,53 @@ var QfqNS = QfqNS || {};
         }
         var currentTabId = obj.target.getCurrentTab();
         n.Log.debug('Saving state: ' + currentTabId);
+
+        // Implementation save current state in session storage
+        var storedFormInfos = [];
+
+        if(sessionStorage.getItem("formInfos") !== null){
+            storedFormInfos = JSON.parse(sessionStorage.getItem("formInfos"));
+        }
+
+        var currentForm = obj.target.currentFormName;
+        var currentRecordId = obj.target.currentRecordId;
+
+        var actualIndex = -1;
+        var indexNr = 0;
+        if(storedFormInfos.length !== 0) {
+            if(storedFormInfos[0] !== ''){
+                storedFormInfos.forEach(function callback(element){
+                    if(element === currentForm && storedFormInfos[indexNr + 2] === currentRecordId){
+                        actualIndex = indexNr;
+                    }
+                    indexNr++;
+                });
+            }
+        }
+
+        // fill sessionStorage, there are 3 ways for filling the sessionStorage: 1.If empty - first time filling, 2.If there is anything - add it to them, 3
+        // 1.If array from storage is empty - fill it first time
+        if(storedFormInfos.length === 0){
+            storedFormInfos[0] = currentForm;
+            storedFormInfos[1] = currentTabId;
+            storedFormInfos[2] = currentRecordId;
+
+            // 2.If there is anything in storage but not the actual opened forms - add this new information to the existing array
+            }else if(actualIndex === -1) {
+                storedFormInfos[indexNr] = currentForm;
+                storedFormInfos[indexNr + 1] = currentTabId;
+                storedFormInfos[indexNr + 2] = currentRecordId;
+
+            // 3.If actual openend form is included in sessionStorage - only change the array values of the existing informations
+            }else{
+                storedFormInfos[actualIndex] = currentForm;
+                storedFormInfos[actualIndex + 1] = currentTabId;
+                storedFormInfos[actualIndex + 2] = currentRecordId;
+            }
+
+        // Set sessionStorage with customized array
+        sessionStorage.setItem("formInfos" , JSON.stringify(storedFormInfos));
+
         n.PageTitle.setSubTitle(obj.target.getTabName(currentTabId));
         this.settings.pageState.setPageState(currentTabId, n.PageTitle.get());
     };
-- 
GitLab