From 3961b21c001f2624220990e1e89cdc8e0d7e51c9 Mon Sep 17 00:00:00 2001 From: Martin Artnik Date: Mon, 21 Oct 2024 17:23:13 +0200 Subject: [PATCH] Add confirmation modal for repository file delete [SCI-11198] --- .../repositories/repository_datatable.js | 35 +++++++++++++++++++ .../javascripts/repositories/row_editor.js | 18 +++------- .../_delete_asset_value_modal.html.erb | 19 ++++++++++ app/views/repositories/show.html.erb | 1 + config/locales/en.yml | 4 +++ 5 files changed, 64 insertions(+), 13 deletions(-) create mode 100644 app/views/repositories/_delete_asset_value_modal.html.erb diff --git a/app/assets/javascripts/repositories/repository_datatable.js b/app/assets/javascripts/repositories/repository_datatable.js index 933bed39a..d030666ce 100644 --- a/app/assets/javascripts/repositories/repository_datatable.js +++ b/app/assets/javascripts/repositories/repository_datatable.js @@ -328,6 +328,40 @@ var RepositoryDatatable = (function(global) { }); } + function initDeleteAssetValueConfirmModal() { + $('#deleteRepositoryAssetValueModal').on('shown.bs.modal', function() { + let $fileBtn = $(this).data('cellFileBtn'); + let $input = $(this).data('cellInput'); + let $label = $(this).data('cellLabel'); + + $('#confirmAssetValueDelete').one('click', function() { + $fileBtn.addClass('new-file'); + $label.text(''); + $input.val(''); + $fileBtn.removeClass('error'); + + if (!$input.data('is-empty')) { // set hidden field for deletion only if original value has been set on rendering + $input + .prev('.file-hidden-field-container') + .html(``); + } + + $('#deleteRepositoryAssetValueModal').modal('hide'); + }); + }); + + $('#deleteRepositoryAssetValueModal').on('hidden.bs.modal', function() { + const $deleteRepositoryAssetValueModal = $('#deleteRepositoryAssetValueModal'); + + $deleteRepositoryAssetValueModal.data('cellFileBtn', null); + $deleteRepositoryAssetValueModal.data('cellInput', null); + $deleteRepositoryAssetValueModal.data('cellLabel', null); + }); + } + function initActiveRemindersFilter() { $(TABLE_WRAPPER_ID).find('#only_reminders').on('change', function() { var $activeRemindersFilter = $(this).closest('.active-reminders-filter'); @@ -804,6 +838,7 @@ var RepositoryDatatable = (function(global) { initSaveButton(); initCancelButton(); initBSTooltips(); + initDeleteAssetValueConfirmModal(); window.initRepositoryStateMenu(); DataTableHelpers.initLengthAppearance($(TABLE_ID).closest('.dataTables_wrapper')); diff --git a/app/assets/javascripts/repositories/row_editor.js b/app/assets/javascripts/repositories/row_editor.js index 490b6f63d..422b01acf 100644 --- a/app/assets/javascripts/repositories/row_editor.js +++ b/app/assets/javascripts/repositories/row_editor.js @@ -80,25 +80,17 @@ var RepositoryDatatableRowEditor = (function() { $fileBtn.removeClass('error'); }); - deleteButtons.on('click', function() { + const $deleteRepositoryAssetValueModal = $('#deleteRepositoryAssetValueModal'); let $fileBtn = $(this).parent(); let $input = $fileBtn.prev('input[type=file]'); let $label = $fileBtn.find('label'); - $fileBtn.addClass('new-file'); - $label.text(''); - $input.val(''); - $fileBtn.removeClass('error'); + $deleteRepositoryAssetValueModal.data('cellFileBtn', $fileBtn); + $deleteRepositoryAssetValueModal.data('cellInput', $input); + $deleteRepositoryAssetValueModal.data('cellLabel', $label); - if (!$input.data('is-empty')) { // set hidden field for deletion only if original value has been set on rendering - $input - .prev('.file-hidden-field-container') - .html(``); - } + $('#deleteRepositoryAssetValueModal').modal('show'); }); } diff --git a/app/views/repositories/_delete_asset_value_modal.html.erb b/app/views/repositories/_delete_asset_value_modal.html.erb new file mode 100644 index 000000000..81ccb9356 --- /dev/null +++ b/app/views/repositories/_delete_asset_value_modal.html.erb @@ -0,0 +1,19 @@ + diff --git a/app/views/repositories/show.html.erb b/app/views/repositories/show.html.erb index 6e4ff35b1..8948c2a85 100644 --- a/app/views/repositories/show.html.erb +++ b/app/views/repositories/show.html.erb @@ -78,6 +78,7 @@ <%= render partial: "repositories/delete_record_modal" %> +<%= render partial: "repositories/delete_asset_value_modal" %> <%= render partial: 'repositories/export_repository_rows_modal', locals: { repository: @repository } %> diff --git a/config/locales/en.yml b/config/locales/en.yml index c8c99ec10..62f4a64ee 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2425,6 +2425,10 @@ en: title: "Delete items" notice: "Are you sure you want to delete the selected item(s)?" delete: "Delete" + modal_delete_asset_value: + title: "Confirm file deletion?" + notice_html: "

Deleting this file will remove the entire column history.

This action is irreversible.

To retain version history, replace the file with a new one instead.

" + delete: "Delete" modal_delete_column: title: "Delete a column" message: "Are you sure you wish to permanently delete selected column %{column}? This action is irreversible."