diff --git a/app/assets/javascripts/repositories/index.js b/app/assets/javascripts/repositories/index.js index 5b08815f7..00480663e 100644 --- a/app/assets/javascripts/repositories/index.js +++ b/app/assets/javascripts/repositories/index.js @@ -1,10 +1,35 @@ /* global I18n animateSpinner HelperModule DataTableHelpers DataTableCheckboxes */ -(function(global) { +(function() { 'use strict'; var REPOSITORIES_TABLE; var CHECKBOX_SELECTOR; + function updateActionButtons() { + var rowsCount = CHECKBOX_SELECTOR.selectedRows.length; + var row; + $('#renameRepoBtn').attr('href', '#'); + $('#deleteRepoBtn').attr('href', '#'); + $('#copyRepoBtn').attr('href', '#'); + switch (rowsCount) { + case 0: + $('.main-actions [data-action-mode="single"]').addClass('disabled'); + $('.main-actions [data-action-mode="multiple"]').addClass('disabled'); + break; + case 1: + row = $('#repositoriesList').find('tr#' + CHECKBOX_SELECTOR.selectedRows[0]); + $('.main-actions [data-action-mode="single"]').removeClass('disabled'); + $('.main-actions [data-action-mode="multiple"]').removeClass('disabled'); + $('#renameRepoBtn').attr('href', row.data('rename-modal-url')); + $('#deleteRepoBtn').attr('href', row.data('delete-modal-url')); + $('#copyRepoBtn').attr('href', row.data('copy-modal-url')); + break; + default: + $('.main-actions [data-action-mode="single"]').addClass('disabled'); + $('.main-actions [data-action-mode="multiple"]').removeClass('disabled'); + } + } + function initRepositoriesDataTable(tableContainer, archived = false) { var tableTemplate = archived ? $('#archivedRepositoriesListTable').html() : $('#activeRepositoriesListTable').html(); $.get($(tableTemplate).data('source'), function(data) { @@ -41,13 +66,19 @@ var dataTableWrapper = $(e.nTableWrapper); CHECKBOX_SELECTOR = new DataTableCheckboxes(dataTableWrapper, { checkboxSelector: '.repository-row-selector', - selectAllSelector: '.select-all-checkbox' + selectAllSelector: '.select-all-checkbox', + onChanged: function() { + updateActionButtons(); + } }); DataTableHelpers.initLengthApearance(dataTableWrapper); DataTableHelpers.initSearchField(dataTableWrapper); $('.content-body .toolbar').html($('#repositoriesListButtons').html()); dataTableWrapper.find('.main-actions, .pagination-row').removeClass('hidden'); - $('.create-new-repository').initializeModal('#create-repo-modal'); + $('#createRepoBtn').initializeModal('#create-repo-modal'); + $('#deleteRepoBtn').initializeModal('#delete-repo-modal'); + $('#renameRepoBtn').initializeModal('#rename-repo-modal'); + $('#copyRepoBtn').initializeModal('#copy-repo-modal'); }, drawCallback: function() { if (CHECKBOX_SELECTOR) CHECKBOX_SELECTOR.checkSelectAllStatus(); @@ -82,58 +113,40 @@ }); } - global.onClickArchiveRepositories = function() { - $.ajax({ - url: $('#archiveRepoBtn').data('archive-repositories'), - type: 'POST', - dataType: 'json', - data: { selected_repos: CHECKBOX_SELECTOR.selectedRows }, - success: function(data) { + $('.repositories-index') + .on('click', '#archiveRepoBtn', function() { + $.post($('#archiveRepoBtn').data('archive-repositories'), { + selected_repos: CHECKBOX_SELECTOR.selectedRows + }, function(data) { HelperModule.flashAlertMsg(data.flash, 'success'); initRepositoriesDataTable('#repositoriesList'); reloadSidebar(); - }, - error: function(ev) { + }).fail(function(ev) { if (ev.status === 403) { - HelperModule.flashAlertMsg( - I18n.t('repositories.js.permission_error'), ev.responseJSON.style - ); + HelperModule.flashAlertMsg(I18n.t('repositories.js.permission_error'), ev.responseJSON.style); } else if (ev.status === 422) { - HelperModule.flashAlertMsg( - ev.responseJSON.error, 'danger' - ); - animateSpinner(null, false); + HelperModule.flashAlertMsg(ev.responseJSON.error, 'danger'); } - } - }); - }; - - global.onClickRestoreRepositories = function() { - $.ajax({ - url: $('#restoreRepoBtn').data('restore-repositories'), - type: 'POST', - dataType: 'json', - data: { selected_repos: CHECKBOX_SELECTOR.selectedRows }, - success: function(data) { + animateSpinner(null, false); + }); + }) + .on('click', '#restoreRepoBtn', function() { + $.post($('#restoreRepoBtn').data('restore-repositories'), { + selected_repos: CHECKBOX_SELECTOR.selectedRows + }, function(data) { HelperModule.flashAlertMsg(data.flash, 'success'); initRepositoriesDataTable('#repositoriesList', true); reloadSidebar(); - }, - error: function(ev) { + }).fail(function(ev) { if (ev.status === 403) { - HelperModule.flashAlertMsg( - I18n.t('repositories.js.permission_error'), ev.responseJSON.style - ); + HelperModule.flashAlertMsg(I18n.t('repositories.js.permission_error'), ev.responseJSON.style); } else if (ev.status === 422) { - HelperModule.flashAlertMsg( - ev.responseJSON.error, 'danger' - ); - animateSpinner(null, false); + HelperModule.flashAlertMsg(ev.responseJSON.error, 'danger'); } - } + animateSpinner(null, false); + }); }); - }; - initRepositoriesDataTable('#repositoriesList'); + initRepositoriesDataTable('#repositoriesList', $('.repositories-index').hasClass('archived')); initRepositoryViewSwitcher(); -}(window)); +}()); diff --git a/app/assets/javascripts/sitewide/datatable_helpers.js b/app/assets/javascripts/sitewide/datatable_helpers.js index ba0dccdd2..725ee1316 100644 --- a/app/assets/javascripts/sitewide/datatable_helpers.js +++ b/app/assets/javascripts/sitewide/datatable_helpers.js @@ -109,6 +109,8 @@ class DataTableCheckboxes { } $(row).toggleClass('selected'); this.checkSelectAllStatus(); + + if (this.config.onChanged) this.config.onChanged(); } #initSelectAllCheckbox = () => { diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index babcb4ef7..c62d2bc61 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -157,7 +157,7 @@ class RepositoriesController < ApplicationController @repository.discard @repository.destroy_discarded(current_user.id) - redirect_to team_repositories_path + redirect_to team_repositories_path(archived: true) end def rename_modal @@ -181,7 +181,7 @@ class RepositoriesController < ApplicationController log_activity(:rename_inventory) # Acton only for renaming render json: { - url: team_repositories_path(repository: @repository) + url: team_repositories_path }, status: :ok else render json: @repository.errors, status: :unprocessable_entity @@ -232,7 +232,7 @@ class RepositoriesController < ApplicationController new: copied_repository.name ) render json: { - url: team_repositories_path(repository: copied_repository) + url: repository_path(copied_repository) }, status: :ok end end diff --git a/app/helpers/repositories_datatable_helper.rb b/app/helpers/repositories_datatable_helper.rb index 50a6af526..af27171b2 100644 --- a/app/helpers/repositories_datatable_helper.rb +++ b/app/helpers/repositories_datatable_helper.rb @@ -17,7 +17,12 @@ module RepositoriesDatatableHelper '6': escape_input(repository.created_by.full_name), '7': (I18n.l(repository.archived_on, format: :full) if repository.archived_on), '8': escape_input(repository.archived_by&.full_name), - 'repositoryUrl': repository_path(repository) + 'repositoryUrl': repository_path(repository), + 'DT_RowAttr': { + 'data-delete-modal-url': team_repository_destroy_modal_path(team, repository_id: repository), + 'data-copy-modal-url': team_repository_copy_modal_path(team, repository_id: repository), + 'data-rename-modal-url': team_repository_rename_modal_path(team, repository_id: repository) + } ) end result diff --git a/app/services/activities/create_activity_service.rb b/app/services/activities/create_activity_service.rb index 36edf3f65..921d32946 100644 --- a/app/services/activities/create_activity_service.rb +++ b/app/services/activities/create_activity_service.rb @@ -60,6 +60,8 @@ module Activities getter_method = 'name' end + const = const.with_archived if const == Repository + obj = const.find id @activity.message_items[k] = { type: const.to_s, value: obj.public_send(getter_method).to_s, id: id } @activity.message_items[k][:value_for] = getter_method diff --git a/app/views/repositories/index.html.erb b/app/views/repositories/index.html.erb index 6fed74a3e..fdcd7fa5d 100644 --- a/app/views/repositories/index.html.erb +++ b/app/views/repositories/index.html.erb @@ -2,8 +2,8 @@ <% provide(:container_class, "no-second-nav-container") %> <% if current_team %> - <%= render partial: "sidebar", locals: { repositories: @repositories, archived: false } %> -
+ <%= render partial: "sidebar", locals: { repositories: @repositories, archived: params[:archived] } %> +

<%= t('libraries.index.head_title') %>

<%= t('libraries.index.head_title_archived') %>

@@ -77,7 +77,7 @@