From 99aecf6960fc75224456f54854dfa7bb6e7d9350 Mon Sep 17 00:00:00 2001
From: Lucas Gass <lucas@bywatersolutions.com>
Date: Wed, 26 Feb 2025 23:28:17 +0000
Subject: [PATCH] Bug 37883: Use localStorage to remember user selection

---
 .../prog/en/modules/catalogue/results.tt            | 13 ++++++++++++-
 koha-tmpl/intranet-tmpl/prog/js/staff-global.js     |  1 +
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tt
index 72cbc5fa7ba..0957aaa67cd 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tt
@@ -555,7 +555,7 @@
             <div id="searchresults">
                 [% INCLUDE 'page-numbers.inc' %]
                 [% IF Koha.Preference('FilterSearchResultsByLoggedInBranch') %]
-                    <button href="#" id="toggleitems" class="btn btn-primary btn-xs" style="float: right;">Show local items only</button>
+                    <button href="#" id="toggleitems" class="btn btn-primary btn-xs show-local-items" style="float: right;">Show local items only</button>
                 [% END %]
                 <form action="/cgi-bin/koha/catalogue/search.pl" method="get" name="bookbag_form" id="bookbag_form">
                     [% IF ( searchdesc ) %]
@@ -970,6 +970,11 @@
         var holdforclub = "[% holdforclub | html %]";
         var userbranch = "[% userbranch | html %]";
         [% IF Koha.Preference('FilterSearchResultsByLoggedInBranch') %]
+            $(document).ready( function() {
+                if (localStorage.getItem("show_local_items") === 'local') {
+                    $('#toggleitems').click();
+                }
+            });
             $('#toggleitems').click( function() {
                 $('#toggleitems').text() === 'Show local items only' ? $('#toggleitems').text("Show items in all libraries") : $('#toggleitems').text("Show local items only");
                 $('.branch_specific').toggle();
@@ -979,6 +984,12 @@
                         $(this).toggle();
                     }
                 });
+                $(this).toggleClass('show-local-items show-all-items');
+                if ($(this).hasClass('show-local-items')) {
+                    localStorage.setItem("show_local_items", 'all');
+                } else {
+                    localStorage.setItem("show_local_items", 'local');
+                }
             });
         [% END %]
         [% IF SEARCH_RESULTS %]
diff --git a/koha-tmpl/intranet-tmpl/prog/js/staff-global.js b/koha-tmpl/intranet-tmpl/prog/js/staff-global.js
index b660011277a..6647d1715c4 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/staff-global.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/staff-global.js
@@ -506,6 +506,7 @@ function logOut() {
     localStorage.removeItem("copiedPermissions");
     localStorage.removeItem("popup_window_width");
     localStorage.removeItem("popup_window_height");
+    localStorage.removeItem("show_local_items");
 
     // Remove DataTables states
     Object.keys(localStorage).forEach(k => {
-- 
2.39.5