Skip to content

Commit 87fdb37

Browse files
committed
Store advanced search to local storage
1 parent 723a6de commit 87fdb37

8 files changed

Lines changed: 76 additions & 0 deletions

File tree

app/assets/javascripts/kaui/multi_functions_bar_utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ function clearAdvanceSearch() {
141141
// Remove all search labels
142142
$('#search-labels-container').empty();
143143

144+
// Clear persisted search for this page
145+
localStorage.removeItem('kaui_adv_search_' + window.location.pathname);
146+
144147
// Reload the page with the original URL (no parameters)
145148
window.location.href = window.location.pathname;
146149

@@ -226,8 +229,10 @@ $(document).on('click', '.filter-close-icon', function() {
226229
var pushParams = (searchParams || '').replace(/account_id/g, 'ac_id');
227230
var newUrl = window.location.protocol + "//" + window.location.host + window.location.pathname + '?' + pushParams;
228231
window.history.pushState({ path: newUrl }, '', newUrl);
232+
localStorage.setItem('kaui_adv_search_' + window.location.pathname, searchParams);
229233
} else {
230234
var newUrl = window.location.protocol + "//" + window.location.host + window.location.pathname;
231235
window.history.pushState({ path: newUrl }, '', newUrl);
236+
localStorage.removeItem('kaui_adv_search_' + window.location.pathname);
232237
}
233238
});

app/views/kaui/accounts/_account_filterbar.html.erb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@
8080

8181
<%= javascript_tag do %>
8282
$(document).ready(function() {
83+
// Restore advanced search from localStorage if no search params in current URL
84+
if (!window.location.search.includes('_q=1')) {
85+
var savedSearch = localStorage.getItem('kaui_adv_search_' + window.location.pathname);
86+
if (savedSearch) {
87+
window.location.replace(window.location.pathname + '?' + savedSearch);
88+
return;
89+
}
90+
}
91+
8392
populateSearchLabelsFromUrl();
8493
var dateFields = ['Created date', 'Updated date', 'Reference time'];
8594
// Handle the "Add" button click to add new search fields
@@ -128,6 +137,7 @@ $(document).ready(function() {
128137
searchParams = searchParams.replace(/account_id/g, 'ac_id');
129138
var newUrl = window.location.protocol + "//" + window.location.host + window.location.pathname + '?' + searchParams;
130139
window.history.pushState({ path: newUrl }, '', newUrl);
140+
localStorage.setItem('kaui_adv_search_' + window.location.pathname, searchParams);
131141
}
132142

133143
searchFields.each(function() {

app/views/kaui/accounts/index.html.erb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ $(document).ready(function() {
114114
}
115115
});
116116

117+
// If the page loaded with advanced search params in the URL (e.g. restored from
118+
// localStorage) but @advance_search_query was not set server-side, re-fire the
119+
// DataTable request with the correct filters.
120+
if (window.location.search.includes('_q=1') && '<%= @advance_search_query.to_s.strip %>' === '') {
121+
var urlSearch = window.location.search.substring(1).replace(/ac_id/g, 'account_id');
122+
var ajaxUrl = "<%= accounts_pagination_path(:ordering => @ordering, :format => :json) %>" + '?' + urlSearch;
123+
table.on('preXhr.dt.filter', function(e, settings, data) {
124+
data.search.value = urlSearch;
125+
});
126+
table.ajax.url(ajaxUrl).load();
127+
}
128+
117129
<!-- When we don't know the total number of pages, we need to hide the legend and next button manually -->
118130
$('#accounts-table').on('draw.dt', function() {
119131
<% if @max_nb_records.nil? %>

app/views/kaui/invoices/_invoice_filterbar.html.erb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@
9292

9393
<%= javascript_tag do %>
9494
$(document).ready(function() {
95+
// Restore advanced search from localStorage if no search params in current URL (global /invoices page only)
96+
if (!window.location.search.includes('_q=1') && !window.location.pathname.includes('/accounts/')) {
97+
var savedSearch = localStorage.getItem('kaui_adv_search_' + window.location.pathname);
98+
if (savedSearch) {
99+
window.location.replace(window.location.pathname + '?' + savedSearch);
100+
return;
101+
}
102+
}
103+
95104
populateSearchLabelsFromUrl();
96105
var dateFields = ['Invoice date', 'Target date'];
97106
// Handle the "Add" button click to add new search fields
@@ -136,6 +145,9 @@ $(document).ready(function() {
136145
searchParams = searchParams.replace(/account_id/g, 'ac_id');
137146
var newUrl = window.location.protocol + "//" + window.location.host + window.location.pathname + '?' + searchParams;
138147
window.history.pushState({ path: newUrl }, '', newUrl);
148+
if (!window.location.pathname.includes('/accounts/')) {
149+
localStorage.setItem('kaui_adv_search_' + window.location.pathname, searchParams);
150+
}
139151
}
140152

141153
searchFields.each(function() {

app/views/kaui/invoices/index.html.erb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,18 @@ $(document).ready(function() {
102102
}
103103
});
104104

105+
// If the page loaded with advanced search params in the URL (e.g. restored from
106+
// localStorage) but @advance_search_query was not set server-side, re-fire the
107+
// DataTable request with the correct filters.
108+
if (window.location.search.includes('_q=1') && '<%= @advance_search_query.to_s.strip %>' === '' && !window.location.pathname.includes('/accounts/')) {
109+
var urlSearch = window.location.search.substring(1).replace(/ac_id/g, 'account_id');
110+
var ajaxUrl = "<%= invoices_pagination_path(:ordering => @ordering, :format => :json) %>" + '?' + urlSearch;
111+
table.on('preXhr.dt.filter', function(e, settings, data) {
112+
data.search.value = urlSearch;
113+
});
114+
table.ajax.url(ajaxUrl).load();
115+
}
116+
105117
// Custom sorting functionality
106118
var currentSortColumn = invoiceDateColIndex;
107119
var currentSortDirection = 'desc';

app/views/kaui/payments/_payment_filterbar.html.erb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@
8787

8888
<%= javascript_tag do %>
8989
$(document).ready(function() {
90+
// Restore advanced search from localStorage if no search params in current URL (global /payments page only)
91+
if (!window.location.search.includes('_q=1') && !window.location.pathname.includes('/accounts/')) {
92+
var savedSearch = localStorage.getItem('kaui_adv_search_' + window.location.pathname);
93+
if (savedSearch) {
94+
window.location.replace(window.location.pathname + '?' + savedSearch);
95+
return;
96+
}
97+
}
98+
9099
populateSearchLabelsFromUrl();
91100

92101
// Handle the "Add" button click to add new search fields
@@ -130,6 +139,9 @@ $(document).ready(function() {
130139
searchParams = searchParams.replace(/account_id/g, 'ac_id');
131140
var newUrl = window.location.protocol + "//" + window.location.host + window.location.pathname + '?' + searchParams;
132141
window.history.pushState({ path: newUrl }, '', newUrl);
142+
if (!window.location.pathname.includes('/accounts/')) {
143+
localStorage.setItem('kaui_adv_search_' + window.location.pathname, searchParams);
144+
}
133145
}
134146

135147
searchFields.each(function() {

app/views/kaui/payments/index.html.erb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,18 @@ $(document).ready(function() {
117117
"search": {"search": "<%= @search_query %>"},
118118
});
119119

120+
// If the page loaded with advanced search params in the URL (e.g. restored from
121+
// localStorage) but @advance_search_query was not set server-side, re-fire the
122+
// DataTable request with the correct filters.
123+
if (window.location.search.includes('_q=1') && '<%= @advance_search_query.to_s.strip %>' === '' && !window.location.pathname.includes('/accounts/')) {
124+
var urlSearch = window.location.search.substring(1).replace(/ac_id/g, 'account_id');
125+
var ajaxUrl = "<%= payments_pagination_path(:ordering => @ordering, :format => :json) %>" + '?' + urlSearch;
126+
table.on('preXhr.dt.filter', function(e, settings, data) {
127+
data.search.value = urlSearch;
128+
});
129+
table.ajax.url(ajaxUrl).load();
130+
}
131+
120132
// Custom sorting functionality
121133
var currentSortColumn = -1;
122134
var currentSortDirection = 'asc';

config/locales/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ en:
5050
clock_reset_successfully: 'Clock was successfully reset'
5151
overdue_uploaded_successfully: 'Overdue config was successfully uploaded'
5252
overdue_added_successfully: 'Overdue config was successfully added'
53+
overdue_updated_successfully: 'Overdue config was successfully updated'
5354
invoice_template_uploaded_successfully: 'Invoice template was successfully uploaded'
5455
invoice_translation_uploaded_successfully: 'Invoice translation was successfully uploaded'
5556
catalog_translation_uploaded_successfully: 'Catalog translation was successfully uploaded'

0 commit comments

Comments
 (0)