From eb5fb16b1b6d8e5d0f373eb028ca117c9643f5c4 Mon Sep 17 00:00:00 2001 From: Martin Artnik Date: Wed, 17 May 2023 14:12:40 +0200 Subject: [PATCH] Implement new toolbar for protocols [SCI-8399] --- app/assets/javascripts/protocols/index.js | 62 ++------- .../stylesheets/shared/action_toolbar.scss | 16 +++ app/controllers/protocols_controller.rb | 12 +- .../vue/components/action_toolbar.vue | 22 +++- app/services/toolbars/protocols_service.rb | 120 ++++++++++++++++++ app/views/protocols/index.html.erb | 7 +- .../protocols/index/_action_toolbar.html.erb | 29 ----- config/locales/en.yml | 4 +- config/routes.rb | 1 + 9 files changed, 179 insertions(+), 94 deletions(-) create mode 100644 app/services/toolbars/protocols_service.rb delete mode 100644 app/views/protocols/index/_action_toolbar.html.erb diff --git a/app/assets/javascripts/protocols/index.js b/app/assets/javascripts/protocols/index.js index d538b4a4e..009ff7b2c 100644 --- a/app/assets/javascripts/protocols/index.js +++ b/app/assets/javascripts/protocols/index.js @@ -6,7 +6,6 @@ // Global variables var ProtocolsIndex = (function() { const ALL_VERSIONS_VALUE = 'All'; - var PERMISSIONS = ['archivable', 'restorable', 'copyable', 'readable']; var rowsSelected = []; var protocolsTableEl = null; var protocolsDatatable = null; @@ -28,6 +27,10 @@ var ProtocolsIndex = (function() { * Initializes page */ function init() { + window.initActionToolbar(); + window.actionToolbarComponent.setReloadCallback(reloadTable); + // make room for pagination + window.actionToolbarComponent.setBottomOffset(75); updateButtons(); initProtocolsTable(); initKeywordFiltering(); @@ -256,9 +259,7 @@ var ProtocolsIndex = (function() { DataTableHelpers.initSearchField(dataTableWrapper, I18n.t('protocols.index.search_bar_placeholder')); dataTableWrapper.find('.main-actions, .pagination-row').removeClass('hidden'); - let actionToolBar = $($('#protocolActionToolbar').html()); let generalToolbar = $($('#protocolGeneralToolbar').html()); - $('.protocols-container .actions-toolbar').html(actionToolBar); $('.protocols-container .toolbar').html(generalToolbar); let protocolFilters = $($('#protocolFilters').html()); @@ -285,26 +286,6 @@ var ProtocolsIndex = (function() { }); } - function checkActionPermission(permission) { - let allProtocols; - - allProtocols = rowsSelected.every((id) => { - return protocolsTableEl.find(`tbody tr#${id}`).data(permission); - }); - - return allProtocols; - } - - function loadPermission(id) { - let row = protocolsTableEl.find(`tbody tr#${id}`); - $.get(row.data('permissions-url'), (result) => { - PERMISSIONS.forEach((permission) => { - row.data(permission, result[permission]); - }); - updateButtons(); - }); - } - function initRowSelection() { let protocolsTableScrollHead = protocolsTableEl.closest('.dataTables_scroll').find('.dataTables_scrollHead'); @@ -335,13 +316,12 @@ var ProtocolsIndex = (function() { updateDataTableSelectAllCheckbox(); if (this.checked) { - loadPermission(rowId); row.addClass('selected'); } else { row.removeClass('selected'); - updateButtons(); } + updateButtons(); e.stopPropagation(); }); @@ -435,7 +415,8 @@ var ProtocolsIndex = (function() { e.preventDefault(); }); - $(protocolsContainer).on('click', '#protocolVersions', function() { + $(protocolsContainer).on('click', '#protocolVersions', function(e) { + e.stopPropagation(); loadVersionModal($(`tr[data-row-id=${rowsSelected[0]}]`).data('versions-url')); }); } @@ -663,34 +644,7 @@ var ProtocolsIndex = (function() { } function updateButtons() { - let actionToolbar = $('.protocols-container .actions-toolbar'); - $('.dataTables_wrapper').addClass('show-actions'); - - if (rowsSelected.length === 0) { - $('.dataTables_wrapper').removeClass('show-actions'); - } else if (rowsSelected.length === 1) { - actionToolbar.find('.single-object-action, .multiple-object-action').removeClass('hidden'); - } else { - actionToolbar.find('.single-object-action').addClass('hidden'); - actionToolbar.find('.multiple-object-action').removeClass('hidden'); - } - - PERMISSIONS.forEach((permission) => { - if (!checkActionPermission(permission)) { - actionToolbar.find(`.btn[data-for="${permission}"]`).addClass('hidden'); - } - }); - - if (protocolsDatatable) protocolsDatatable.columns.adjust(); - - actionToolbar.find('.btn').addClass('notransition'); - actionToolbar.find('.btn').removeClass('btn-primary').addClass('btn-light'); - actionToolbar.find('.btn:visible').first().addClass('btn-primary').removeClass('btn-light'); - setTimeout(function() { - actionToolbar.find('.btn').removeClass('notransition'); - }, 500); - - actionToolbar.find('.emptyPlaceholder').toggleClass('hidden', actionToolbar.find('.btn:visible').length > 0); + window.actionToolbarComponent.fetchActions({ protocol_ids: rowsSelected.join(',') }); } function initLocalFileImport() { diff --git a/app/assets/stylesheets/shared/action_toolbar.scss b/app/assets/stylesheets/shared/action_toolbar.scss index 63a16991f..d76f29050 100644 --- a/app/assets/stylesheets/shared/action_toolbar.scss +++ b/app/assets/stylesheets/shared/action_toolbar.scss @@ -5,4 +5,20 @@ .btn.btn-light:hover { background: $color-white; } + + .icon-versions { + display: inline-block; + height: 14px; + background-image: image-url("icon_small/versions-black.svg"); + background-position: center center; + background-repeat: no-repeat; + margin-bottom: -2px; + width: 16px; + } +} + +@media (max-width: 1000px) { + .sn-action-toolbar__button-text { + display: none; + } } diff --git a/app/controllers/protocols_controller.rb b/app/controllers/protocols_controller.rb index cff328d6d..6b38cf9b1 100644 --- a/app/controllers/protocols_controller.rb +++ b/app/controllers/protocols_controller.rb @@ -921,6 +921,16 @@ class ProtocolsController < ApplicationController end end + def actions_toolbar + render json: { + actions: + Toolbars::ProtocolsService.new( + current_user, + protocol_ids: params[:protocol_ids].split(',') + ).actions + } + end + private def set_importer @@ -1001,7 +1011,7 @@ class ProtocolsController < ApplicationController def check_clone_permissions load_team_and_type - protocol = Protocol.find_by(id: params[:ids][0]) + protocol = Protocol.find_by(id: params[:protocol_ids].split(',')) @original = protocol.latest_published_version_or_self if @original.blank? || diff --git a/app/javascript/vue/components/action_toolbar.vue b/app/javascript/vue/components/action_toolbar.vue index 09b49cc5e..ad0ea8c0a 100644 --- a/app/javascript/vue/components/action_toolbar.vue +++ b/app/javascript/vue/components/action_toolbar.vue @@ -1,5 +1,5 @@