From 3ae31d23c602010c96d303e98eca469e0044e15d Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@biblibre.com>
Date: Fri, 9 Nov 2012 12:08:10 +0100
Subject: [PATCH] Bug 7825: FIX some ergonomic issues in opac-reserve

http://bugs.koha-community.org/show_bug.cgi?id=7825
Signed-off-by: Delaye Stephane <stephane.delaye@biblibre.com>
---
 .../opac-tmpl/prog/en/modules/opac-reserve.tt      |  150 +++++++++++---------
 1 files changed, 80 insertions(+), 70 deletions(-)

diff --git a/koha-tmpl/opac-tmpl/prog/en/modules/opac-reserve.tt b/koha-tmpl/opac-tmpl/prog/en/modules/opac-reserve.tt
index 09c150d..1cf2532 100644
--- a/koha-tmpl/opac-tmpl/prog/en/modules/opac-reserve.tt
+++ b/koha-tmpl/opac-tmpl/prog/en/modules/opac-reserve.tt
@@ -13,93 +13,100 @@
      return s.substring(index + 1);
  }
 
+// Select the first item available
+function select_first_available(id){
+    var radios = $("input:radio[name='checkitem_" + id + "']");
+    $(radios).first().attr("checked", "checked");
+}
+
  $(document).ready(function() {
     $('#hold-request-form').preventDoubleFormSubmit();
 
     var copiesRowId = null;
     var wasSpecific = false;
     var lastCopiesRowId = null;
-	$(".checkitem").parent().click(function(e){
-		if(e.target.tagName.toLowerCase() == 'td'){
+    $(".checkitem").parent().click(function(e){
+        if(e.target.tagName.toLowerCase() == 'td'){
            $(this).find("input.checkitem").each( function() {
-               $(this).attr('checked', !$(this).attr('checked'));
-			});
-		}
-	});
+               $(this).attr('checked', 'checked');
+            });
+        }
+    });
+
+    // click on a first td check the confirmjs checkbox
+    $("td.hold").click(function(e){
+      if(e.target.tagName.toLowerCase() == 'td'){
+        $(this).find("input.confirmjs").each( function() {
+           $(this).attr('checked', !$(this).attr('checked'));
+           $(this).change();
+        });
+      }
+    });
+
     // Hides all 'specific copy' table rows on load.
     $(".copiesrow").hide();
 
-    $("#place_on_hdr").show();
-    [% IF OPACItemHolds == '1' %]
-        $(".place_on_type").show();
-    [% END %]
-
     // Replace non-JS single-selection with multi-selection capability.
     $(".reserve_mode").val("multi");
     $(".confirm_nonjs").remove();
     $(".confirmjs_hold").each(function(){
         var bib = $(this).attr("title");
         var html = "<input type =\"checkbox\" class=\"confirmjs\" checked=\"checked\"";
-        html += "value=\"" + bib + "\"/>";
-        $(this).html(html);
-    });
-    $(".confirmjs_nohold").each(function(){
-        var bib = $(this).attr("title");
-        var html = "<input type =\"checkbox\" class=\"confirmjs\" disabled=\"disabled\"";
-        html += "value=\"" + bib + "\"/>";
+        html += "value=\"" + bib + "\" id=\"" + bib + "\"/>";
         $(this).html(html);
     });
 
-    // Make sure a specific item was selected where specified
-    // before moving on to a new item.
-    function changeSelection (newCopiesRowId, isSpecific) {
-        if (copiesRowId && ((copiesRowId != newCopiesRowId) || (wasSpecific != isSpecific))) {
-            var biblioNum = suffixOf(copiesRowId, "_");
-
-            // If the 'specific copy' radio button was checked
-              if (wasSpecific && (copiesRowId != newCopiesRowId)) {
-                // Find the selected copy
-                var item = $(".checkitem_" + biblioNum + ":checked");
-                if ($(item).size() == 0) {
-                    alert(MSG_NO_COPY_SELECTED);
-                    return false;
-                }
-            }
-        }
-        copiesRowId = newCopiesRowId;
-        wasSpecific = isSpecific;
-        return true;
-    }
-
-    // When 'specific copy' radio button is clicked
-    $(".selectspecific").click(function() {
-
-        // Make sure all other specific copy table rows are hidden
-        biblioNum = suffixOf($(this).attr("id"), "_");
+    // expand or collapse the copiesrow tr
+    function toggle_copiesrow(biblioNum) {
+        var checkbox = $("input:checkbox[value='"+biblioNum+"']");
         newCopiesRowId = "#copiesrow_" + biblioNum;
+        var select_specific = $("#reqspecific_"+biblioNum).is(":checked");
 
-        if (!changeSelection(newCopiesRowId, true)) {
-            return false;
+        // If the checkbox is checked AND we want a specific item, we display the items block
+        if ( $(checkbox).is(":checked") && select_specific ) {
+            $(newCopiesRowId).show();
+        } else {
+            $(newCopiesRowId).hide();
         }
-        $(".copiesrow:not(" + newCopiesRowId + ")").hide();
+    };
 
-        // Show the specific copy table for this radio button.
-        $(newCopiesRowId).show();
-    });
+    $("#place_on_hdr").show();
 
+    [% IF OPACItemHolds == '1' %]
+        $(".place_on_type").show();
+        // onload, selectany is checked
+        $(".selectany").attr("checked", "checked");
+    [% END %]
 
-    // When 'first available' radion button is clicked
-    $(".selectany").click(function() {
-        // Make sure all other specific copy table rows are hidden
-        biblioNum = suffixOf($(this).attr("id"), "_");
-        newCopiesRowId = "#copiesrow_" + biblioNum;
+    // If we can choose a specific item, we preselect the first one
+    [% IF OPACItemHolds =="1" or OPACItemHolds == 'force' %]
+        $("tr.hold").each(function(){
+            var id = suffixOf($(this).attr("id"), "_");
+            select_first_available(id);
+        });
+    [% END %]
 
-        if (!changeSelection(newCopiesRowId, false)) {
-            return false;
+    // On confirmsjs change
+    $(".confirmjs").change(function(){
+        var id = suffixOf($(this).attr("id"), "_");
+        // If I m checked, I enable radio buttons
+        if ( $(this).is(":checked") ) {
+            $("#reqspecific_" + id).attr("disabled", false);
+            $("#reqany_" + id).attr("disabled", false);
         }
-
-        // Hide the copies table row
-        $(".copiesrow").hide();
+        // Else its are disabled
+        else {
+            $("#reqspecific_" + id).attr("disabled", "disabled");
+            $("#reqany_" + id).attr("disabled", "disabled");
+        }
+        // expand or collaspe the items block
+        toggle_copiesrow(id);
+   });
+
+    // When 'specific copy' or 'first available' radio button is clicked
+    $(".selectspecific, .selectany").click(function() {
+        var id = suffixOf($(this).attr("id"), "_");
+        toggle_copiesrow(id);
     });
 
     // When 'Place Hold' button is clicked
@@ -236,6 +243,7 @@
             <input type="hidden" name="selecteditems" id="selections"/>
             <div id="bigloop">
               <table id="bibitemloop">
+                <thead>
                 [% UNLESS ( none_available ) %]<tr>
                   <th>Hold</th>
                   <th>Title</th>
@@ -257,16 +265,17 @@
                     <th id="place_on_hdr" style="display:none">Place On</th>
                   [% END %]
                   [% UNLESS ( singleBranchMode ) %]
-		    [% IF ( choose_branch ) %]
+                    [% IF ( choose_branch ) %]
                         <th>Pickup location</th>
-		    [% END %]
+                    [% END %]
                   [% END %]
                 </tr>[% ELSE %]<tr><th colspan="5">Title</th></tr>[% END %]
-
+                </thead>
+                <tbody>
                 [% FOREACH bibitemloo IN bibitemloop %]
-                  <tr>
-                      [% IF ( bibitemloo.holdable ) %]
-                                    <td class="hold">
+                  <tr id="hold_[% bibitemloo.biblionumber %]" class="hold">
+                    [% IF ( bibitemloo.holdable ) %]
+                    <td class="hold">
                       <input class="reserve_mode" name="reserve_mode" type="hidden" value="single"/>
                       <input class="single_bib" name="single_bib" type="hidden" value="[% bibitemloo.biblionumber %]"/>
                         <span class="confirmjs_hold" title="[% bibitemloo.biblionumber %]"></span>
@@ -277,10 +286,10 @@
                                  value="any" />
                           <label class="confirm_label" for="[% bibitemloo.checkitem_bib %]">Next available copy</label>
                         </span>
-					</td>
-                      [% ELSE %]
-                                      [% UNLESS ( none_available ) %]<td class="hold">&nbsp;</td>[% END %]
-                      [% END %]
+                    </td>
+                    [% ELSE %]
+                      [% UNLESS ( none_available ) %]<td class="hold">&nbsp;</td>[% END %]
+                    [% END %]
                     [% IF ( bibitemloo.holdable ) %]<td class="title">[% ELSE %]<td class="title" colspan="5">[% END %]
                       <a href="/cgi-bin/koha/opac-detail.pl?biblionumber=[% bibitemloo.biblionumber %]">[% bibitemloo.title |html %][% IF ( bibitemloo.subtitle ) %] [% FOREACH subtitl IN bibitemloo.subtitle %][% subtitl.subfield %][% END %][% END %]</a>
                       [% IF ( bibitemloo.author ) %],  by [% bibitemloo.author %][% END %]
@@ -485,6 +494,7 @@
                   [% END %]<!-- bib_available -->
                   [% END %]<!-- OPACItemHolds -->
                 [% END %]
+                </tbody>
               </table><!-- bibitemloop -->
               [% END %] <!-- if message -->
             </div><!-- bigloop -->
-- 
1.7.2.5