From ef4bb1783c9328f244375ee8b9f9a19986957ea8 Mon Sep 17 00:00:00 2001
From: Maurits Moeys <maurits.moeys@gmail.com>
Date: Thu, 28 Jan 2016 00:25:53 +0100
Subject: [PATCH] refactored jQuery switch

---
 .../commentaries/request_commentary.html      | 73 +++++++------------
 1 file changed, 28 insertions(+), 45 deletions(-)

diff --git a/commentaries/templates/commentaries/request_commentary.html b/commentaries/templates/commentaries/request_commentary.html
index 06c345c35..a9c9abd0a 100644
--- a/commentaries/templates/commentaries/request_commentary.html
+++ b/commentaries/templates/commentaries/request_commentary.html
@@ -4,61 +4,44 @@
 
 {% block bodysup %}
 
-<script>
+  <script>
   $(document).ready(function(){
-    $('label[for=id_pub_title]').hide()
-    $('input#id_pub_title').hide()
-    $('label[for=id_author_list]').hide()
-    $('input#id_author_list').hide()
-    $('label[for=id_pub_date]').hide()
-    $('input#id_pub_date').hide()
-    $('label[for=id_arxiv_link]').hide()
-    $('input#id_arxiv_link').hide()
-    $('label[for=id_pub_DOI_link]').hide()
-    $('input#id_pub_DOI_link').hide()
-    $('label[for=id_pub_abstract]').hide()
-    $('textarea#id_pub_abstract').hide()
+
+    // Looking at the DOM we can see that all the relevant stuff we want to hide/show is packed in a table. Hint: to play around with jquery, go to devtools, and just write your selectors there. $('tbody') will grab the table from the DOM I just mentioned.
+
+    // Hiding all the rows for initial set-up
+    var allToggableRows = $('tr').slice(1)   // Slices off the first element of the array. Read the documentation on this one, its quite useful. Same for .each and .map in upcoming code.
+
+    allToggableRows.each(function(index){
+      $(this).hide() // this is the object we're handling in the current iteration of the loop. It is wrapped in $(' ... ') to make it an jQuery object, so that we can call a jQuery function on it like .hide()
+    })
+
+    // Indices to remove in order to show thing named by variable name
+    var preprint = [2,4]
+    var published = [3]
+
+    function show(indices){
+      allToggableRows.each(function(index){
+        if ($.inArray( index, indices) != -1){
+          $(this).hide()
+        }else{
+          $(this).show()
+        }
+
+      })
+    }
 
     $('select#id_type').on('change', function() {
       var selection = $(this).val();
-      $('label[for=id_pub_title]').show()
-      $('input#id_pub_title').show()
-      $('label[for=id_author_list]').show()
-      $('input#id_author_list').show()
       switch(selection){
         case "published":
-          $('label[for=id_pub_date]').show()
-          $('input#id_pub_date').show()
-          $('label[for=id_arxiv_link]').hide()
-          $('input#id_arxiv_link').hide()
-          $('label[for=id_pub_DOI_link]').show()
-          $('input#id_pub_DOI_link').show()
-          $('label[for=id_pub_abstract]').show()
-          $('textarea#id_pub_abstract').show()
+          show(published)
           break;
         case "preprint":
-          $('label[for=id_pub_date]').hide()
-          $('input#id_pub_date').hide()
-          $('label[for=id_arxiv_link]').show()
-          $('input#id_arxiv_link').show()
-          $('label[for=id_pub_DOI_link]').hide()
-          $('input#id_pub_DOI_link').hide()
-          $('label[for=id_pub_abstract]').show()
-          $('textarea#id_pub_abstract').show()
+          show(preprint)
           break;
         default:
-          $('label[for=id_pub_title]').hide()
-          $('input#id_pub_title').hide()
-          $('label[for=id_author_list]').hide()
-          $('input#id_author_list').hide()
-          $('label[for=id_pub_date]').hide()
-          $('input#id_pub_date').hide()
-          $('label[for=id_arxiv_link]').hide()
-          $('input#id_arxiv_link').hide()
-          $('label[for=id_pub_DOI_link]').hide()
-          $('input#id_pub_DOI_link').hide()
-          $('label[for=id_pub_abstract]').hide()
-          $('textarea#id_pub_abstract').hide()
+          allToggableRows.hide()
       }
     });
   });
-- 
GitLab