Add action buttons to repository index

This commit is contained in:
aignatov-bio 2020-06-19 15:54:45 +02:00
parent 28f669393a
commit 6f2d5b359e
6 changed files with 79 additions and 57 deletions

View file

@ -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));
}());

View file

@ -109,6 +109,8 @@ class DataTableCheckboxes {
}
$(row).toggleClass('selected');
this.checkSelectAllStatus();
if (this.config.onChanged) this.config.onChanged();
}
#initSelectAllCheckbox = () => {

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -2,8 +2,8 @@
<% provide(:container_class, "no-second-nav-container") %>
<% if current_team %>
<%= render partial: "sidebar", locals: { repositories: @repositories, archived: false } %>
<div class="content-pane flexible active repositories-index">
<%= render partial: "sidebar", locals: { repositories: @repositories, archived: params[:archived] } %>
<div class="content-pane flexible <%= params[:archived] ? :archived : :active %> repositories-index">
<div class="content-header">
<h1 data-view-mode="active"><%= t('libraries.index.head_title') %></h1>
<h1 data-view-mode="archived"><%= t('libraries.index.head_title_archived') %></h1>
@ -77,7 +77,7 @@
<!-- Repositories action buttons -->
<template id="repositoriesListButtons">
<% if can_create_repositories?(current_team) %>
<a class="btn btn-primary btn-lg create-new-repository"
<a id="createRepoBtn" class="btn btn-primary"
data-remote="true"
data-view-mode="active"
href="<%= create_modal_team_repositories_path(current_team) %>">
@ -85,31 +85,31 @@
<span class="hidden-xs"><%= t('libraries.index.no_libraries.create_new_button') %></span>
</a>
<% end %>
<a class="btn btn-light" data-view-mode="active" href="#">
<a id="renameRepoBtn" class="btn btn-light disabled" data-view-mode="active" href="#" data-action-mode="single" data-remote="true">
<span class="fas fa-pencil-alt"></span>
<%= t('libraries.index.buttons.edit') %>
</a>
<a class="btn btn-light" data-view-mode="active" href="#">
<a id="copyRepoBtn" class="btn btn-light disabled" data-view-mode="active" href="#" data-action-mode="single" data-remote="true">
<span class="fas fa-copy"></span>
<%= t('libraries.index.buttons.duplicate') %>
</a>
<a id="archiveRepoBtn"
class="btn btn-light"
class="btn btn-light disabled"
data-view-mode="active"
onclick="onClickArchiveRepositories()"
data-action-mode="multiple"
data-archive-repositories="<%= archive_team_repositories_path(current_team) %>">
<span class="fas fa-archive"></span>
<%= t('libraries.index.buttons.archive') %>
</a>
<a id="restoreRepoBtn"
class="btn btn-light"
class="btn btn-light disabled"
data-view-mode="archived"
onclick="onClickRestoreRepositories()"
data-action-mode="multiple"
data-restore-repositories="<%= restore_team_repositories_path(current_team) %>">
<span class="fas fa-undo"></span>
<%= t('libraries.index.buttons.restore') %>
</a>
<a class="btn btn-light" data-view-mode="archived" href="#">
<a id="deleteRepoBtn" class="btn btn-light disabled" data-view-mode="archived" href="#" data-action-mode="single" data-remote="true">
<span class="fas fa-trash"></span>
<%= t('libraries.index.buttons.delete') %>
</a>