From 8580b61b2580125f7ddac5ad8b1a0ccc6a3fe36d Mon Sep 17 00:00:00 2001 From: Owen Leonard Date: Fri, 28 Jan 2022 13:00:04 +0000 Subject: [PATCH] Bug 22276: Add client storage of user-selected DataTables configuration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch enables use of DataTables' built-in session storage of table configurations. DataTables by default uses the browser's localstorage feature to store the state of each DataTable, including which columns have been hidden by the user. Our table settings system has always overridden this saved state. This patch adds a check for a saved configuration state and if present loads the saved state instead of the default state. The system is enabled in this patch on the libraries administration page. To test, apply the patch and open go to Administration -> Libraries. - Use the "Columns" button to change the visibility setting of multiple columns. - Navigate away from the page and then back to test that the visibility settings have been kept. - Go to Administration -> Table settings -> Administration and change the visibility settings for the "libraries" table in a way that it differs from the visibility settings you set manually. - Return to the libraries administration page and confirm that your manual settings are still being loaded instead of the defaults from table settings. Signed-off-by: Solène Desvaux --- .../prog/en/includes/columns_settings.inc | 39 +++++++++++++++++++ .../prog/en/modules/admin/branches.tt | 6 ++- 2 files changed, 44 insertions(+), 1 deletion(-) 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 0e21d84011..bf82526b01 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/columns_settings.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/columns_settings.inc @@ -168,5 +168,44 @@ function KohaTable(id_selector, dt_parameters, columns_settings, add_filters) { return table; } + +/* get_columns_saved_state checks for a DataTables configuration saved +* in the browser's local storage. If it is present, the columns +* configuration supplied by Koha is overwritten +* +* It takes two parameters: +* - localstorage_config, the DataTables saved state object from local storage +* - columns_settings, the columns settings object supplied by the template +* +* An example: +* +* var columns_settings = [% ColumnsSettings.GetColumns( 'module', 'page', 'table', 'json' ) | $raw %]; +* var saved_table = localStorage.getItem("DataTables_TABLE_ID_/cgi-bin/koha/PATH/TO/SCRIPT.pl"); +* var updated_settings = get_columns_saved_state( saved_table, columns_settings ); +* +* KohaTable("TABLE_ID", { +* "stateSave": true +* }, updated_settings); +*/ + +function get_columns_saved_state( localstorage_config, columns_settings ){ + var tables = JSON.parse( localstorage_config ); + // if a table configuration was found in local storage, parse it + if( tables ){ + var stateSave_column_visibility = []; + $(tables.columns).each(function(){ + stateSave_column_visibility.push( this.visible === true ? 0 : 1 ); + }); + $.each( columns_settings, function( index, key ){ + if( stateSave_column_visibility[ index ] !== columns_settings[key] ){ + columns_settings[ index ].is_hidden = stateSave_column_visibility[ index ]; + } + }); + return columns_settings; + } else { + return columns_settings; + } +} + diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tt index 7f5db486e4..c2ab0350da 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tt @@ -342,6 +342,9 @@ Libraries › Administration › Koha [% INCLUDE 'str/tinymce_i18n.inc' %]