From 0429fd50f36f6bcd898ea8920e2a37f94801e7dd Mon Sep 17 00:00:00 2001
From: Kyle M Hall <kyle@bywatersolutions.com>
Date: Thu, 29 Jun 2023 15:07:04 -0400
Subject: [PATCH] Bug 21246: Extend the 'Last patron' navigation feature to
 'Last 10 patrons'

This retains the "last patron" link, but extends it to add a dropdown
containg the last 10 patrons.

A future enhancement to control how many patrons to retain would
complement this well.

1) Enable showLastPatron
2) Visit two patrons details pages
3) Note "Last patron" link displays and links to the last visited patron
4) Log out
5) Apply this patch
6) Log in
7) Visit the patron details page for a few patrons
8) Note the "Last patron" link behaves as is did previously
9) Note the split button has a pulldown with the other previous patrons
10) Verify that only the last 10 patrons are retained in the pulldown
11) Verify that if you visit a patron who is already in the list
    they get moved to the top of the list
---
 .../intranet-tmpl/prog/css/src/_header.scss   | 11 +---
 .../prog/en/includes/last-borrower.inc        | 13 ++++-
 .../intranet-tmpl/prog/js/staff-global.js     | 51 +++++++++++++------
 3 files changed, 48 insertions(+), 27 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/css/src/_header.scss b/koha-tmpl/intranet-tmpl/prog/css/src/_header.scss
index 8234f8a023..012890b1c2 100644
--- a/koha-tmpl/intranet-tmpl/prog/css/src/_header.scss
+++ b/koha-tmpl/intranet-tmpl/prog/css/src/_header.scss
@@ -271,24 +271,15 @@ a.navbar-toggle {
 
 #lastborrower-window {
     display: none;
-    background-color: $background-color-primary;
-    box-shadow: 1px 1px 1px 0 #999;
-    color: #FFFFFF;
-    padding: .2em;
-    border-radius: 5px 5px 5px 5px;
 
     > * {
         padding: 0 .4em;
     }
 }
 
-#lastborrower-remove {
-    cursor: pointer;
-    border-left: 1px solid #fff;
-}
-
 #lastborrowerlink {
     color: #FFFFFF;
+    background-color: $background-color-primary;
 }
 
 @media (max-width: 768px) {
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/last-borrower.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/last-borrower.inc
index 61dec6b700..7bd739bb4e 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/last-borrower.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/last-borrower.inc
@@ -1,4 +1,13 @@
 <div id="lastborrower-window">
-    <div id="lastborrower-ref" class="lastborrower"><a id="lastborrowerlink" href="" title=""><i class="fa fa-arrow-right"></i> Last patron</a></div>
-    <div id="lastborrower-remove" class="lastborrower"><i class="fa fa-times"></i></div>
+    <div class="btn-group">
+        <a class="btn btn-default navbar-btn lastborrower" id="lastborrowerlink" href="#" title=""><i class="fa fa-arrow-right"></i> Last patron</a>
+        <button type="button" data-toggle="dropdown" class="btn btn-default navbar-btn dropdown-toggle" aria-haspopup="true" aria-expanded="false">
+            <span class="caret"></span>
+            <span class="sr-only">Toggle Dropdown</span>
+        </button>
+        <ul id="lastBorrowerList" class="dropdown-menu">
+            <li role="separator" class="divider"></li>
+            <li><a id="lastborrower-remove" class="lastborrower" href="#">Clear list</a></li>
+        </ul>
+    </div>
 </div>
diff --git a/koha-tmpl/intranet-tmpl/prog/js/staff-global.js b/koha-tmpl/intranet-tmpl/prog/js/staff-global.js
index c4890ae20e..5a2d2fd677 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/staff-global.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/staff-global.js
@@ -151,20 +151,44 @@ $(document).ready(function() {
         $("#catalog-search-dropdown a").toggleClass("catalog-search-dropdown-hover");
     });
 
-    if ( localStorage.getItem("lastborrowernumber") ){
-        if( $("#hiddenborrowernumber").val() != localStorage.getItem("lastborrowernumber") ) {
+    console.log("HIDDEN BORROWER: " + $("#hiddenborrowernumber").val() );
+    if ( localStorage.getItem("previousPatrons") || $("#hiddenborrowernumber").val() ){
+        var previous_patrons = [];
+        if ( localStorage.getItem("previousPatrons") ) {
+            previous_patrons = JSON.parse(localStorage.getItem("previousPatrons"));
+        }
+
+        if ( $("#hiddenborrowernumber").val() ) {
+            // Remove this patron from the list if they are already there
+            previous_patrons = previous_patrons.filter(function (p) {
+                return p["borrowernumber"] != $("#hiddenborrowernumber").val();
+            });
+
+            const previous_patron = {
+                "borrowernumber": $("#hiddenborrowernumber").val(),
+                "name": $("#hiddenborrowername").val(),
+                "card": $("#hiddenborrowercard").val()
+            };
+        
+            previous_patrons.unshift( previous_patron );
+            // Limit to last 10 patrons
+            if ( previous_patrons.length > 10 ) previous_patrons.pop();
+            localStorage.setItem("previousPatrons", JSON.stringify(previous_patrons));
+        }
+
+        if ( previous_patrons.length ) {
+            let p = previous_patrons[0];
             $("#lastborrowerlink").show();
-            $("#lastborrowerlink").prop("title", localStorage.getItem("lastborrowername") + " (" + localStorage.getItem("lastborrowercard") + ")");
-            $("#lastborrowerlink").prop("href", "/cgi-bin/koha/circ/circulation.pl?borrowernumber=" + localStorage.getItem("lastborrowernumber"));
+            $("#lastborrowerlink").prop("title", `${p["name"]} (${p["card"]})`);
+            $("#lastborrowerlink").prop("href", `/cgi-bin/koha/circ/circulation.pl?borrowernumber=${p["borrowernumber"]}`);
             $("#lastborrower-window").css("display", "inline-flex");
-        }
-    }
 
-    if( !localStorage.getItem("lastborrowernumber") || ( $("#hiddenborrowernumber").val() != localStorage.getItem("lastborrowernumber") && localStorage.getItem("currentborrowernumber") != $("#hiddenborrowernumber").val())) {
-        if( $("#hiddenborrowernumber").val() ){
-            localStorage.setItem("lastborrowernumber", $("#hiddenborrowernumber").val() );
-            localStorage.setItem("lastborrowername", $("#hiddenborrowername").val() );
-            localStorage.setItem("lastborrowercard", $("#hiddenborrowercard").val() );
+            previous_patrons.reverse();
+            for ( i in previous_patrons ) {
+                p = previous_patrons[i];
+                const el = `<li><a href="/cgi-bin/koha/circ/circulation.pl?borrowernumber=${p["borrowernumber"]}">${p["name"]} (${p["card"]})</a></li>`;
+                $("#lastBorrowerList").prepend(el);
+            }
         }
     }
 
@@ -212,10 +236,7 @@ $(document).ready(function() {
 });
 
 function removeLastBorrower(){
-    localStorage.removeItem("lastborrowernumber");
-    localStorage.removeItem("lastborrowername");
-    localStorage.removeItem("lastborrowercard");
-    localStorage.removeItem("currentborrowernumber");
+    localStorage.removeItem("previousPatrons");
 }
 
 // http://jennifermadden.com/javascript/stringEnterKeyDetector.html
-- 
2.30.2