From 2faa23a3576f14fc9757639fda8bde5b4fcc22f4 Mon Sep 17 00:00:00 2001
From: Alice <alice.poggioli@hotmail.fr>
Date: Tue, 17 Sep 2019 18:05:13 +0200
Subject: [PATCH] Patch after review.

---
 src/includes/mentor/resources/detail.pug |   2 +-
 src/scripts/coopstarter.js               | 131 +++++++----------------
 2 files changed, 41 insertions(+), 92 deletions(-)

diff --git a/src/includes/mentor/resources/detail.pug b/src/includes/mentor/resources/detail.pug
index 5a63d5df..abd9c623 100644
--- a/src/includes/mentor/resources/detail.pug
+++ b/src/includes/mentor/resources/detail.pug
@@ -5,7 +5,7 @@ sib-router
 
 dialog#confirm_suppress
     h1 confirm_suppress
-    sib-delete(data-label="Die bitch, die !!" bind-resources) 
+    sib-delete(data-label="Delete" bind-resources) 
     sib-link(class="backlink pull-right", next="mentor-resource-list") X
 
 .block_log.block_creat_count.no_shadow
diff --git a/src/scripts/coopstarter.js b/src/scripts/coopstarter.js
index f7ae8029..fc5334fa 100644
--- a/src/scripts/coopstarter.js
+++ b/src/scripts/coopstarter.js
@@ -37,32 +37,8 @@ function refreshList(formId, listId) {
   });
 }
 
-// //Refresh information after a deletion
-// function refreshAfterDeletion() {
-//   let list = document.getElementById("resources-history");
-//   console.log("list", list);
-
-//   let element = document.querySelectorAll("sib-delete");
-//   console.log("element", element);
-
-//   element.addEventListener("resourceDeleted", e => {
-//     console.log("Resource deleted: ", e.detail.resource);
-//     list.dataset.src = list.dataset.src;
-//   });
-
-// let form = document.querySelectorAll("button");
-// console.log("form", form);
-// form.addEventListener("click", event => {
-
-//   let list = document.getElementById("resources-history");
-//   console.log("delete");
-
-//   list.dataset.src = list.dataset.src;
-// });
-// }
-
 //Remove pagination when there is no resource in a step group
-function actualizePagination() {
+function refreshPagination() {
   var resources_containers = document.querySelectorAll(
     ".resource_by_step sib-form+div"
   );
@@ -80,13 +56,36 @@ function actualizePagination() {
 }
 
 //Manage select hidden to fullfill them with more "more criterias" selection
-function selectHiddenManagement() {
-  //Need to refactor the part
+function selectHiddenManagement(select_hidden, option_selected, form) {
+  options_hidden = select_hidden.getElementsByTagName("option");
+  for (let option_hidden of options_hidden) {
+    //Removing the selected attribute from previous selection
+    option_hidden.removeAttribute("selected");
+
+    if (option_hidden.value == option_selected.value) {
+      //Actually selecting the option if it is the good one
+      option_hidden.setAttribute("selected", "selected");
+      select_hidden.setAttribute("value", option_selected.value);
+    }
+
+    //Trigerring a reload of the associated form
+    let parent_form = select_hidden.closest("sib-form");
+    parent_form.component.inputChange();
+  }
+  refreshPagination();
+  form.querySelector("sib-form").component.inputChange();
+}
+
+//Manage input hidden field to fullfill them with more "more criterias" selection
+function inputHiddenManagement(field, field_search, form) {
+  field.setAttribute("value", field_search.value);
+  refreshPagination();
+  form.querySelector("sib-form").component.inputChange();
 }
 
 jQuery(document).ready(function($) {
-  //Actualize pagination
-  actualizePagination();
+  //Refresh pagination
+  refreshPagination();
 
   // Get the element with id="defaultOpen" and click on it
   document.getElementById("defaultOpen").click();
@@ -133,7 +132,7 @@ jQuery(document).ready(function($) {
   forms.forEach(form => {
     form.addEventListener("populate", e => {
       //On load in dashbord Entrepreneur
-      actualizePagination();
+      refreshPagination();
       //SEARCH BY KEYWORD
       //To retrieve keyword
       //https://git.happy-dev.fr/startinblox/framework/sib-core/issues/379
@@ -149,7 +148,7 @@ jQuery(document).ready(function($) {
       keyword_submit.addEventListener("click", function() {
         keyword_hidden_fields.forEach(hidden_field => {
           hidden_field.setAttribute("value", keyword_field.value);
-          actualizePagination();
+          refreshPagination();
           form.querySelector("sib-form").component.inputChange();
         });
       });
@@ -171,7 +170,6 @@ jQuery(document).ready(function($) {
 
       // https://git.happy-dev.fr/startinblox/framework/sib-core/issues/453
       window.setTimeout(() => {
-
         //Refresh data list on delete resources
         const deleteButton = document.querySelectorAll("sib-delete");
 
@@ -184,7 +182,9 @@ jQuery(document).ready(function($) {
             confirm_suppress.setAttribute("hidden", "hidden");
 
             //If we supress from the detail resource view, we close this view.
-            let mentor_resource_detail = document.getElementById("mentor-resource-detail");
+            let mentor_resource_detail = document.getElementById(
+              "mentor-resource-detail"
+            );
             mentor_resource_detail.setAttribute("hidden", "hidden");
             this.dispatchEvent(
               new CustomEvent("requestNavigation", {
@@ -212,22 +212,7 @@ jQuery(document).ready(function($) {
           );
 
           format_hidden_field.forEach(function(select_hidden) {
-            options_hidden = select_hidden.getElementsByTagName("option");
-            for (let option_hidden of options_hidden) {
-              //Removing the selected attribute from previous selection
-              option_hidden.removeAttribute("selected");
-              if (option_hidden.value == option_selected.value) {
-                //Actually selecting the option if it is the good one
-                option_hidden.setAttribute("selected", "selected");
-                select_hidden.setAttribute("value", option_selected.value);
-              }
-
-              //Trigerring a reload of the associated form
-              let parent_form = select_hidden.closest("sib-form");
-              parent_form.component.inputChange();
-            }
-            actualizePagination();
-            form.querySelector("sib-form").component.inputChange();
+            selectHiddenManagement(select_hidden, option_selected, form);
           });
         };
 
@@ -245,23 +230,7 @@ jQuery(document).ready(function($) {
           );
 
           language_hidden_field.forEach(function(select_hidden) {
-            options_hidden = select_hidden.getElementsByTagName("option");
-            for (let option_hidden of options_hidden) {
-              //Removing the selected attribute from previous selection
-              option_hidden.removeAttribute("selected");
-
-              if (option_hidden.value == option_selected.value) {
-                //Actually selecting the option if it is the good one
-                option_hidden.setAttribute("selected", "selected");
-                select_hidden.setAttribute("value", option_selected.value);
-              }
-
-              //Trigerring a reload of the associated form
-              let parent_form = select_hidden.closest("sib-form");
-              parent_form.component.inputChange();
-            }
-            actualizePagination();
-            form.querySelector("sib-form").component.inputChange();
+            selectHiddenManagement(select_hidden, option_selected, form);
           });
         };
 
@@ -279,23 +248,7 @@ jQuery(document).ready(function($) {
           );
 
           field_hidden_field.forEach(function(select_hidden) {
-            options_hidden = select_hidden.getElementsByTagName("option");
-            for (let option_hidden of options_hidden) {
-              //Removing the selected attribute from previous selection
-              option_hidden.removeAttribute("selected");
-
-              if (option_hidden.value == option_selected.value) {
-                //Actually selecting the option if it is the good one
-                option_hidden.setAttribute("selected", "selected");
-                select_hidden.setAttribute("value", option_selected.value);
-              }
-
-              //Trigerring a reload of the associated form
-              let parent_form = select_hidden.closest("sib-form");
-              parent_form.component.inputChange();
-            }
-            actualizePagination();
-            form.querySelector("sib-form").component.inputChange();
+            selectHiddenManagement(select_hidden, option_selected, form);
           });
         };
 
@@ -310,10 +263,8 @@ jQuery(document).ready(function($) {
         );
 
         year_field_search.addEventListener("input", function() {
-          year_hidden_fields.forEach(year_field => {
-            year_field.setAttribute("value", year_field_search.value);
-            actualizePagination();
-            form.querySelector("sib-form").component.inputChange();
+          year_hidden_fields.forEach(field => {
+            inputHiddenManagement(field, year_field_search, form);
           });
         });
 
@@ -326,10 +277,8 @@ jQuery(document).ready(function($) {
         );
 
         country_field_search.addEventListener("input", function() {
-          country_hidden_fields.forEach(country_field => {
-            country_field.setAttribute("value", country_field_search.value);
-            actualizePagination();
-            form.querySelector("sib-form").component.inputChange();
+          country_hidden_fields.forEach(field => {
+            inputHiddenManagement(field, country_field_search, form);
           });
         });
       }, 4000);
-- 
GitLab