From 9060d4a3050e16ff5fb09188771f3d13514de77c Mon Sep 17 00:00:00 2001
From: Jonathan Druart
Date: Thu, 3 Oct 2024 10:24:50 +0200
Subject: [PATCH] Bug 33484: Clearly separate search and configuration
We want to make distinguish the search terms and the
configuration/option of the table (number of entries, column visibility,
etc.)
Signed-off-by: Pedro Amorim
Signed-off-by: Jonathan Druart
Signed-off-by: Martin Renvoize
Signed-off-by: Katrin Fischer
---
C4/Utils/DataTables/TablesSettings.pm | 63 +++++++++++--------
admin/columns_settings.pl | 25 ++++----
.../prog/en/includes/columns_settings.inc | 43 ++++++++-----
.../prog/en/modules/admin/columns_settings.tt | 30 ++++++---
koha-tmpl/intranet-tmpl/prog/js/datatables.js | 45 +++++++------
5 files changed, 123 insertions(+), 83 deletions(-)
diff --git a/C4/Utils/DataTables/TablesSettings.pm b/C4/Utils/DataTables/TablesSettings.pm
index 9d7f23b4a3..4765c7f202 100644
--- a/C4/Utils/DataTables/TablesSettings.pm
+++ b/C4/Utils/DataTables/TablesSettings.pm
@@ -60,11 +60,16 @@ sub get_columns {
$columns = [
map {
{
- cannot_be_toggled => exists $_->{cannot_be_toggled} ? $_->{cannot_be_toggled} : 0,
- cannot_be_modified => exists $_->{cannot_be_modified} ? $_->{cannot_be_modified} : 0,
- is_hidden => exists $_->{is_hidden} ? $_->{is_hidden} : 0,
- default_save_state => exists $_->{default_save_state} ? $_->{default_save_state} : 1,
- columnname => $_->{columnname},
+ cannot_be_toggled => exists $_->{cannot_be_toggled} ? $_->{cannot_be_toggled} : 0,
+ cannot_be_modified => exists $_->{cannot_be_modified} ? $_->{cannot_be_modified} : 0,
+ is_hidden => exists $_->{is_hidden} ? $_->{is_hidden} : 0,
+ default_save_state => exists $_->{default_save_state} ? $_->{default_save_state} : 1,
+ default_save_state_search => exists $_->{default_save_state_search}
+ && exists $_->{default_save_state}
+ && $_->{default_save_state} # We need default_save_state
+ ? $_->{default_save_state_search}
+ : 0,
+ columnname => $_->{columnname},
}
} @$columns
];
@@ -112,6 +117,9 @@ sub get_table_settings {
default_save_state => $rs
? $rs->default_save_state
: 1,
+ default_save_state_search => $rs
+ ? $rs->default_save_state_search
+ : $list->{modules}{$module}{$page}{$tablename}{default_save_state_search},
};
}
@@ -158,40 +166,43 @@ sub update_columns {
=head3 update_table_settings
- C4::Utils::DataTables::TablesSettings::update_table_settings(
+C4::Utils::DataTables::TablesSettings::update_table_settings(
{
- module => $module,
- page => $page,
- tablename => $tablename,
- default_display_length => $default_display_length,
- default_sort_order => $default_sort_order,
- default_save_state => $default_save_state,
+ module => $module,
+ page => $page,
+ tablename => $tablename,
+ default_display_length => $default_display_length,
+ default_sort_order => $default_sort_order,
+ default_save_state => $default_save_state,
+ default_save_state_search => $default_save_state_search,
}
- );
+);
Will update the default_display_length and default_sort_order for the given table.
=cut
sub update_table_settings {
- my ($params) = @_;
- my $module = $params->{module};
- my $page = $params->{page};
- my $tablename = $params->{tablename};
- my $default_display_length = $params->{default_display_length};
- my $default_sort_order = $params->{default_sort_order};
- my $default_save_state = $params->{default_save_state};
+ my ($params) = @_;
+ my $module = $params->{module};
+ my $page = $params->{page};
+ my $tablename = $params->{tablename};
+ my $default_display_length = $params->{default_display_length};
+ my $default_sort_order = $params->{default_sort_order};
+ my $default_save_state = $params->{default_save_state};
+ my $default_save_state_search = $params->{default_save_state_search};
my $schema = Koha::Database->new->schema;
$schema->resultset('TablesSetting')->update_or_create(
{
- module => $module,
- page => $page,
- tablename => $tablename,
- default_display_length => $default_display_length,
- default_sort_order => $default_sort_order,
- default_save_state => $default_save_state,
+ module => $module,
+ page => $page,
+ tablename => $tablename,
+ default_display_length => $default_display_length,
+ default_sort_order => $default_sort_order,
+ default_save_state => $default_save_state,
+ default_save_state_search => $default_save_state_search,
}
);
}
diff --git a/admin/columns_settings.pl b/admin/columns_settings.pl
index 02e563882f..0bca8d11f3 100755
--- a/admin/columns_settings.pl
+++ b/admin/columns_settings.pl
@@ -48,24 +48,27 @@ if ( $op eq 'cud-save' ) {
my @table_ids = $input->multi_param('table_id');
for my $table_id (@table_ids) {
next unless $table_id =~ m{^([^\|]*)\|(.*)$};
- my $default_display_length = $input->param( $table_id . '_default_display_length' );
- my $default_sort_order = $input->param( $table_id . '_default_sort_order' );
- my $default_save_state = $input->param( $table_id . '_default_save_state' );
+ my $default_display_length = $input->param( $table_id . '_default_display_length' );
+ my $default_sort_order = $input->param( $table_id . '_default_sort_order' );
+ my $default_save_state = $input->param( $table_id . '_default_save_state' );
+ my $default_save_state_search = $input->param( $table_id . '_default_save_state_search' );
undef $default_display_length if defined $default_display_length && $default_display_length eq "";
- undef $default_sort_order if defined $default_sort_order && $default_sort_order eq "";
- $default_save_state = 0 if !defined($default_save_state);
+ undef $default_sort_order if defined $default_sort_order && $default_sort_order eq "";
+ $default_save_state //= 0;
+ $default_save_state_search //= 0;
if ( defined $default_display_length || defined $default_sort_order || defined $default_save_state )
{
C4::Utils::DataTables::TablesSettings::update_table_settings(
{
- module => $module,
- page => $1,
- tablename => $2,
- default_display_length => $default_display_length,
- default_sort_order => $default_sort_order,
- default_save_state => $default_save_state,
+ module => $module,
+ page => $1,
+ tablename => $2,
+ default_display_length => $default_display_length,
+ default_sort_order => $default_sort_order,
+ default_save_state => $default_save_state,
+ default_save_state_search => $default_save_state_search,
}
);
}
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/columns_settings.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/columns_settings.inc
index e3f98e6d17..562c4686e4 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/columns_settings.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/columns_settings.inc
@@ -12,25 +12,34 @@ function KohaTable(id_selector, dt_parameters, table_settings, add_filters) {
if ( table_settings ) {
- var table_key =
- 'DataTables_' +
- table_settings['module'] +'_' +
- table_settings['page'] +'_' +
- table_settings['table'];
-
- // enable stateSave
- dt_parameters['stateSave'] = true;
- dt_parameters['stateSaveCallback'] = function( settings, data ) {
- localStorage.setItem( table_key, JSON.stringify(data) )
- }
- dt_parameters['stateLoadCallback'] = function(settings) {
- return JSON.parse( localStorage.getItem(table_key) )
+ let columns_settings = table_settings["columns"];
+
+ let table_key = 'DataTables_%s_%s_%s'.format(
+ table_settings["module"],
+ table_settings["page"],
+ table_settings["table"]);
+
+ let default_save_state = table_settings.default_save_state;
+ let default_save_state_search = table_settings.default_save_state_search;
+
+ if ( default_save_state ) {
+ dt_parameters["stateSave"] = true;
+ dt_parameters["stateSaveCallback"] = function( settings, data ) {
+ if (!default_save_state_search ) {
+ delete data.search;
+ data.columns.forEach(c => delete c.search );
+ }
+ localStorage.setItem( table_key, JSON.stringify(data) )
+ }
+ dt_parameters["stateLoadCallback"] = function(settings) {
+ return JSON.parse( localStorage.getItem(table_key) )
+ }
+ let local_settings = localStorage.getItem(table_key);
+ columns_settings = get_columns_saved_state(local_settings, columns_settings);
+ } else {
+ localStorage.removeItem(table_key);
}
- // merge stateSave for column settings
- var local_settings = localStorage.getItem(table_key);
- var system_settings = table_settings['columns'];
- var columns_settings = get_columns_saved_state(local_settings, system_settings);
$(columns_settings).each( function() {
var named_id = $( 'thead th[data-colname="' + this.columnname + '"]', selector ).index( selector + ' th' );
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/columns_settings.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/columns_settings.tt
index 5e7cc7e6d3..8bf0b32d44 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/columns_settings.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/columns_settings.tt
@@ -148,16 +148,26 @@
[% END %]
- [% IF table_settings.default_save_state.defined %]
-
- Save state on page change:
- [%- IF table_settings.default_save_state -%]
-
- [%- ELSE -%]
-
- [%- END -%]
-
- [% END %]
+
+
+ [% SET option_name = pagename _ "|" _ tablename _ "_default_save_state" %]
+
+ [%- IF table_settings.default_save_state -%]
+
+ [%- ELSE -%]
+
+ [%- END -%]
+
+
+
+ [% SET option_name = pagename _ "|" _ tablename _ "_default_save_state_search" %]
+
+ [%- IF table_settings.default_save_state_search -%]
+
+ [%- ELSE -%]
+
+ [%- END -%]
+
[% END %]
diff --git a/koha-tmpl/intranet-tmpl/prog/js/datatables.js b/koha-tmpl/intranet-tmpl/prog/js/datatables.js
index a66ed803ab..3e3acda878 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/datatables.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/datatables.js
@@ -58,7 +58,7 @@ var dataTablesDefaults = {
initComplete: function( settings ) {
var tableId = settings.nTable.id
var state = settings.oLoadedState;
- state && toggledClearFilter(state.search.search, tableId);
+ state && state.search && toggledClearFilter(state.search.search, tableId);
if (settings.ajax) {
let table_node = $("#" + tableId);
@@ -1012,22 +1012,32 @@ function _dt_get_saved_state( localstorage_config, columns_settings ){
if ( table_settings ) {
- var table_key =
- 'DataTables_' +
- table_settings['module'] +'_' +
- table_settings['page'] +'_' +
- table_settings['table'];
-
- settings["stateSave"] = true;
- settings["stateSaveCallback"] = function( settings, data ) {
- localStorage.setItem( table_key, JSON.stringify(data) )
- }
- settings["stateLoadCallback"] = function(settings) {
- return JSON.parse( localStorage.getItem(table_key) )
+ let columns_settings = table_settings["columns"];
+ let table_key = 'DataTables_%s_%s_%s'.format(
+ table_settings["module"],
+ table_settings["page"],
+ table_settings["table"]);
+
+ let default_save_state = table_settings.default_save_state;
+ let default_save_state_search = table_settings.default_save_state_search;
+
+ if ( default_save_state ) {
+ settings["stateSave"] = true;
+ settings["stateSaveCallback"] = function( settings, data ) {
+ if (!default_save_state_search ) {
+ delete data.search;
+ data.columns.forEach(c => delete c.search );
+ }
+ localStorage.setItem( table_key, JSON.stringify(data) )
+ }
+ settings["stateLoadCallback"] = function(settings) {
+ return JSON.parse( localStorage.getItem(table_key) )
+ }
+ let local_settings = localStorage.getItem(table_key);
+ columns_settings = _dt_get_saved_state(local_settings, columns_settings);
+ } else {
+ localStorage.removeItem(table_key);
}
- var local_settings = localStorage.getItem(table_key);
- var system_settings = table_settings["columns"];
- var columns_settings = _dt_get_saved_state(local_settings, system_settings);
if ( table_settings.hasOwnProperty('default_display_length') && table_settings['default_display_length'] != null ) {
settings["pageLength"] = table_settings['default_display_length'];
@@ -1035,9 +1045,6 @@ function _dt_get_saved_state( localstorage_config, columns_settings ){
if ( table_settings.hasOwnProperty('default_sort_order') && table_settings['default_sort_order'] != null ) {
settings["order"] = [[ table_settings['default_sort_order'], 'asc' ]];
}
- if ( table_settings.hasOwnProperty('default_save_state') && table_settings['default_save_state'] === 0 ) {
- localStorage.removeItem(table_key);
- }
}
var table = $(this).dataTable(settings);
--
2.39.5