From 2e41ddaade8eba0cc5f92c8de2c797a94498ff2d Mon Sep 17 00:00:00 2001
From: Benoit Alessandroni <benoit@happy-dev.fr>
Date: Wed, 3 Mar 2021 14:55:55 +0100
Subject: [PATCH] update: fixing the connector script

---
 js/connector-script.js | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/js/connector-script.js b/js/connector-script.js
index 67b1b17..1340359 100644
--- a/js/connector-script.js
+++ b/js/connector-script.js
@@ -78,6 +78,42 @@ function manageAccordionByStep(){
   }
 }
 
+/**
+ * Handle local / federated data search
+ */
+function searchDataSource(){
+  //select checkbox
+  //add clic event listener
+  let searchCheckbox = document.querySelector(".searchLocalInstance");
+  searchCheckbox.addEventListener("click",(e) => {
+    //get the last element & use it as ID in the queryselector
+    let apiURL = document
+      .querySelector('solid-display.resource_by_step')
+      .getAttribute("data-src").split("/").slice(0,3).join("/");
+    console.log(apiURL);
+    //select search data sources
+    let searchDataSources = document.querySelectorAll("div.step > solid-display.resource_by_step");
+    
+    //for each data source, replace it with the wanted value
+    searchDataSources.forEach(function(searchDataSource, index) {
+      //change host to localhost
+      if (e.target.checked == true){
+        //create new data-src
+        var newLocalDataSrc =  apiURL + "/steps/" + (index + 1) +"/resources/validated/";  
+        //push it
+        searchDataSource.attributes['data-src'].value = newLocalDataSrc;
+      }
+      //change host to federated
+      else if (e.target.checked == false) {
+        var newFederatedDataSrc = apiURL + "/sources/step" + (index + 1) +"/";
+        //push it
+        searchDataSource.attributes['data-src'].value = newFederatedDataSrc;
+      }
+    });
+    refreshPagination();
+  });
+}
+
 /**
  * Manage select hidden for type to fullfill them with more "more criterias" selection
  * @param {HTMLElement} tab - Selected type tabs.
@@ -159,6 +195,7 @@ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
       manageAccordionByStep();
       addProperFilterToSearchComponents("coopedia-search-results-component", "coopedia-search-box-component");
       initManagementForType("coopedia-search-box-component");
+      searchDataSource()
     }, 4000);
 })();
 
-- 
GitLab