From 12a05cdf0aefa111baaeb146c0d273ca46f1d415 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 8 Oct 2024 12:45:43 +0200 Subject: [PATCH] Bug 33484: Adjust columns_settings.inc Signed-off-by: Pedro Amorim Signed-off-by: Jonathan Druart Signed-off-by: Martin Renvoize --- .../prog/en/includes/columns_settings.inc | 49 ++++++++++++++++++- koha-tmpl/intranet-tmpl/prog/js/datatables.js | 44 +++++++++-------- 2 files changed, 70 insertions(+), 23 deletions(-) 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 c082277b9aa..0ed07f38671 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/columns_settings.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/columns_settings.inc @@ -26,11 +26,29 @@ function KohaTable(id_selector, dt_parameters, table_settings, add_filters) { dt_parameters.stateSaveCallback = function( settings, data ) { localStorage.setItem( table_key, JSON.stringify(data) ) } + + function set_default(table_settings, settings){ + let columns = new Array(table_settings.columns.length).fill({visible: true}); + let hidden_ids, included_ids; + [hidden_ids, included_ids] = _dt_visibility(table_settings, settings, $("#"+settings.nTable.id)); + hidden_ids.forEach((id, i) => { columns[id] = { visible: false } } ); + // State is not loaded if time is not passed + return { columns, time: new Date() }; + } dt_parameters.stateLoadCallback = function(settings) { - if (!default_save_state) return {}; + + // Load state from URL + const url = new URL(window.location.href); + let state_from_url = url.searchParams.get( settings.nTable.id + '_state'); + if ( state_from_url ) { + settings.loaded_from_state = true; + return JSON.parse(atob(state_from_url)); + } + + if (!default_save_state) return set_default(table_settings, settings); let state = localStorage.getItem(table_key); - if (!state) return {}; + if (!state) return set_default(table_settings, settings); state = JSON.parse(state); @@ -38,6 +56,7 @@ function KohaTable(id_selector, dt_parameters, table_settings, add_filters) { delete state.search; state.columns.forEach(c => delete c.search ); } + settings.loaded_from_state = true; return state; } @@ -193,6 +212,30 @@ function KohaTable(id_selector, dt_parameters, table_settings, add_filters) { } ); + if ( table_settings ) { + dt_parameters["buttons"].push( + { + autoClose: true, + fade: 100, + className: "copyConditions_controls", + titleAttr: __("Copy conditions"), + text: ' ' + __("Copy conditions") + '', + action: function (e, dt, node, config) { + let state = JSON.stringify(dt.state()); + delete state.time; + let searchParams = new URLSearchParams(window.location.search); + let table_id = dt.table().node().id; + searchParams.set(table_id + '_state', btoa(state)); + let url = window.location.origin + window.location.pathname + '?' + searchParams.toString() + window.location.hash; + if( navigator.clipboard && navigator.clipboard.writeText){ + navigator.clipboard.writeText( url ); + // TODO Add tooltip "State copied to the clipboard" + } + }, + } + ); + } + if ( table_settings && CAN_user_parameters_manage_column_config ) { dt_parameters[ "buttons" ].push( { @@ -233,6 +276,8 @@ function KohaTable(id_selector, dt_parameters, table_settings, add_filters) { }); } + new_parameters["loaded_from_state"] = false; + if ( table_settings ) { if ( table_settings.hasOwnProperty('default_display_length') && table_settings['default_display_length'] != null ) { // pageLength needs to be a number, not a string, or it can cause issues with DataTable's next button. diff --git a/koha-tmpl/intranet-tmpl/prog/js/datatables.js b/koha-tmpl/intranet-tmpl/prog/js/datatables.js index 5b7de460846..31e61f3463c 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/datatables.js +++ b/koha-tmpl/intranet-tmpl/prog/js/datatables.js @@ -758,27 +758,29 @@ function _dt_buttons(params){ } ); - buttons.push( - { - autoClose: true, - fade: 100, - className: "copyConditions_controls", - titleAttr: __("Copy conditions"), - text: ' ' + __("Copy conditions") + '', - action: function (e, dt, node, config) { - let state = JSON.stringify(dt.state()); - delete state.time; - let searchParams = new URLSearchParams(window.location.search); - let table_id = dt.table().node().id; - searchParams.set(table_id + '_state', btoa(state)); - let url = window.location.origin + window.location.pathname + '?' + searchParams.toString() + window.location.hash; - if( navigator.clipboard && navigator.clipboard.writeText){ - navigator.clipboard.writeText( url ); - // TODO Add tooltip "State copied to the clipboard" - } - }, - } - ); + if ( table_settings ) { + buttons.push( + { + autoClose: true, + fade: 100, + className: "copyConditions_controls", + titleAttr: __("Copy conditions"), + text: ' ' + __("Copy conditions") + '', + action: function (e, dt, node, config) { + let state = JSON.stringify(dt.state()); + delete state.time; + let searchParams = new URLSearchParams(window.location.search); + let table_id = dt.table().node().id; + searchParams.set(table_id + '_state', btoa(state)); + let url = window.location.origin + window.location.pathname + '?' + searchParams.toString() + window.location.hash; + if( navigator.clipboard && navigator.clipboard.writeText){ + navigator.clipboard.writeText( url ); + // TODO Add tooltip "State copied to the clipboard" + } + }, + } + ); + } if ( table_settings && CAN_user_parameters_manage_column_config ) { buttons.push( -- 2.34.1