From 379ef7a9cc1172daff3a39bfd90dfc0147c2a9c9 Mon Sep 17 00:00:00 2001 From: Giga Chubinidze Date: Wed, 24 May 2023 11:57:06 +0400 Subject: [PATCH] moved to develop, fixed pagination style --- app/assets/javascripts/repositories/index.js | 10 +- app/assets/stylesheets/repository/index.scss | 19 +++ app/controllers/repositories_controller.rb | 16 ++- app/services/toolbars/repositories_service.rb | 115 ++++++++++++++++++ app/views/repositories/index.html.erb | 66 +++------- config/routes.rb | 1 + 6 files changed, 178 insertions(+), 49 deletions(-) create mode 100644 app/services/toolbars/repositories_service.rb diff --git a/app/assets/javascripts/repositories/index.js b/app/assets/javascripts/repositories/index.js index 9a8a1726c..9bfa14e2f 100644 --- a/app/assets/javascripts/repositories/index.js +++ b/app/assets/javascripts/repositories/index.js @@ -7,6 +7,11 @@ var CHECKBOX_SELECTOR; function updateActionButtons() { + if (window.actionToolbarComponent) { + window.actionToolbarComponent.fetchActions({ repository_ids: CHECKBOX_SELECTOR.selectedRows }); + $('.dataTables_scrollBody').css('padding-bottom', `${CHECKBOX_SELECTOR.selectedRows.length > 0 ? 68 : 0}px`); + } + var rowsCount = CHECKBOX_SELECTOR.selectedRows.length; var row; $('#renameRepoBtn').attr('href', '#'); @@ -98,6 +103,9 @@ } }], fnInitComplete: function(e) { + initActionToolbar(); + actionToolbarComponent.setBottomOffset(75); + var dataTableWrapper = $(e.nTableWrapper); CHECKBOX_SELECTOR = new DataTableCheckboxes(dataTableWrapper, { checkboxSelector: '.repository-row-selector', @@ -188,4 +196,4 @@ if (notTurbolinksPreview()) { initRepositoriesDataTable('#repositoriesList', $('.repositories-index').hasClass('archived')); } -}()); +}()); \ No newline at end of file diff --git a/app/assets/stylesheets/repository/index.scss b/app/assets/stylesheets/repository/index.scss index 879e516c3..6fba5bcc4 100644 --- a/app/assets/stylesheets/repository/index.scss +++ b/app/assets/stylesheets/repository/index.scss @@ -133,3 +133,22 @@ font-size: 14px; } } + +.content-body { + height: calc(100vh - var(--navbar-height) - 5em); +} + +#repositoriesList_wrapper { + display: flex; + flex-direction: column; + height: 100%; + width: 100%; + .dataTables_scroll { + display: flex; + flex-direction: column; + flex-grow: 1; + height: calc(100% - var(--datatable-pagination-row) - 4em); + padding-bottom: 12px; + z-index: 1; + } +} diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index aa8a24185..8bb526dff 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -9,12 +9,13 @@ class RepositoriesController < ApplicationController include RepositoriesDatatableHelper include MyModulesHelper - before_action :load_repository, except: %i(index create create_modal sidebar archive restore) + before_action :load_repository, except: %i(index create create_modal sidebar archive restore actions_toolbar) before_action :load_repositories, only: %i(index show sidebar) before_action :load_repositories_for_archiving, only: :archive before_action :load_repositories_for_restoring, only: :restore before_action :check_view_all_permissions, only: %i(index sidebar) - before_action :check_view_permissions, except: %i(index create_modal create update destroy parse_sheet import_records sidebar archive restore) + before_action :check_view_permissions, except: %i(index create_modal create update destroy parse_sheet import_records + sidebar archive restore actions_toolbar) before_action :check_manage_permissions, only: %i(rename_modal update) before_action :check_delete_permissions, only: %i(destroy destroy_modal) before_action :check_archive_permissions, only: %i(archive restore) @@ -410,6 +411,17 @@ class RepositoriesController < ApplicationController end } end + def actions_toolbar + render json: { + actions: + Toolbars::RepositoriesService.new( + current_user, + current_team, + repository_ids: params[:repository_ids].split(',') + ).actions + } + end + private def repostiory_import_actions diff --git a/app/services/toolbars/repositories_service.rb b/app/services/toolbars/repositories_service.rb new file mode 100644 index 000000000..63c6864d0 --- /dev/null +++ b/app/services/toolbars/repositories_service.rb @@ -0,0 +1,115 @@ +# frozen_string_literal: true + +module Toolbars + class RepositoriesService + attr_reader :current_user + + include Canaid::Helpers::PermissionsHelper + include Rails.application.routes.url_helpers + + def initialize(current_user, current_team, repository_ids: []) + @current_user = current_user + @current_team = current_team + @repositories = Repository.readable_by_user(current_user) + .where(id: repository_ids) + @repository = @repositories.length == 1 ? @repositories.first : nil + @archived_state = @repositories.all.any?(&:archived?) + end + + def actions + return [] if @repositories.none? + + if @archived_state + [restore_action, delete_action] + else + [rename_action, duplicate_action, archive_action, share_action] + end.compact + end + + private + + def rename_action + return unless @repository + + { + name: 'rename', + label: I18n.t('libraries.index.buttons.edit'), + button_id: 'renameRepoBtn', + icon: 'fas fa-pencil-alt', + path: team_repository_rename_modal_path(@current_team, repository_id: @repository), + type: 'remote-modal' + } + end + + def duplicate_action + return unless @repository + + return unless can_create_repositories?(@current_team) + + { + name: 'duplicate', + label: I18n.t('libraries.index.buttons.duplicate'), + button_id: 'copyRepoBtn', + icon: 'fas fa-copy', + path: team_repository_copy_modal_path(@current_team, repository_id: @repository), + type: 'remote-modal' + } + end + + def archive_action + return unless @repositories.all? { |repository| can_archive_repository?(repository) } + + { + name: 'archive', + label: I18n.t('libraries.index.buttons.archive'), + button_id: 'archiveRepoBtn', + icon: 'fas fa-archive', + path: archive_team_repositories_path(@current_team), + type: :request, + request_method: :post + } + end + + def share_action + return unless @repository + + return unless can_share_repository?(@repository) + + { + name: 'share', + label: I18n.t('repositories.index.share_inventory'), + icon: 'fas fa-user-plus', + button_class: 'share-repository-button', + path: team_repository_share_modal_path(@current_team, repository_id: @repository), + type: 'remote-modal' + } + end + + def restore_action + return unless @repositories.all? { |repository| can_archive_repository?(repository) } + + { + name: 'restore', + label: I18n.t('libraries.index.buttons.restore'), + icon: 'fas fa-undo', + button_id: 'restoreRepoBtn', + path: restore_team_repositories_path(@current_team), + type: :request, + request_method: :post + } + end + + def delete_action + return unless @repository + + { + name: 'delete', + label: I18n.t('libraries.index.buttons.delete'), + icon: 'fas fa-trash', + button_id: 'deleteRepoBtn', + path: team_repository_destroy_modal_path(@current_team, repository_id: @repository), + type: 'remote-modal' + } + end + end + end \ No newline at end of file diff --git a/app/views/repositories/index.html.erb b/app/views/repositories/index.html.erb index 24716308d..ba316df46 100644 --- a/app/views/repositories/index.html.erb +++ b/app/views/repositories/index.html.erb @@ -37,56 +37,30 @@ +
+ +
+ <%= javascript_include_tag "repositories/index" %> <%= stylesheet_link_tag 'datatables' %> +<%= javascript_include_tag "vue_components_action_toolbar" %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index f3f85ad65..40eeadbde 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -196,6 +196,7 @@ Rails.application.routes.draw do defaults: { format: 'json' } get 'create_modal', to: 'repositories#create_modal', defaults: { format: 'json' } + get 'actions_toolbar' end get 'destroy_modal', to: 'repositories#destroy_modal', defaults: { format: 'json' }