scinote-web/app/assets/javascripts/repositories/index.js

191 lines
7 KiB
JavaScript
Raw Normal View History

/* global I18n animateSpinner HelperModule
DataTableHelpers DataTableCheckboxes notTurbolinksPreview */
/* eslint-disable no-param-reassign */
2020-06-19 21:54:45 +08:00
(function() {
'use strict';
2020-06-01 16:24:03 +08:00
var REPOSITORIES_TABLE;
2020-06-09 19:16:50 +08:00
var CHECKBOX_SELECTOR;
2020-06-01 16:24:03 +08:00
2020-06-19 21:54:45 +08:00
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`);
}
2020-06-19 21:54:45 +08:00
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 hidden');
$('.main-actions [data-action-mode="multiple"]').addClass('disabled hidden');
2020-06-19 21:54:45 +08:00
break;
case 1:
row = $('#repositoriesList').find('tr#' + CHECKBOX_SELECTOR.selectedRows[0]);
$('.main-actions [data-action-mode="single"]').removeClass('disabled hidden');
$('.main-actions [data-action-mode="multiple"]').removeClass('disabled hidden');
2020-06-19 21:54:45 +08:00
$('#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"]').removeClass('hidden').addClass('disabled');
$('.main-actions [data-action-mode="multiple"]').removeClass('disabled hidden');
2020-06-19 21:54:45 +08:00
}
}
function initRepositoriesDataTable(tableContainer, archived = false) {
var tableTemplate = $('#RepositoriesListTable').html();
$.get($(tableTemplate).data('source'), { archived: archived }, function(data) {
2020-06-05 01:07:10 +08:00
if (REPOSITORIES_TABLE) REPOSITORIES_TABLE.destroy();
2020-06-09 19:16:50 +08:00
CHECKBOX_SELECTOR = null;
2020-06-05 01:07:10 +08:00
$('.content-body').html(tableTemplate);
2020-06-01 16:24:03 +08:00
REPOSITORIES_TABLE = $(tableContainer).DataTable({
aaData: data,
dom: "R<'main-actions hidden'<'toolbar'><'filter-container'f>>t<'pagination-row hidden'<'pagination-info'li><'pagination-actions'p>>",
processing: true,
stateSave: true,
2020-06-01 16:24:03 +08:00
pageLength: 25,
colReorder: {
enable: false
},
2020-06-01 16:24:03 +08:00
sScrollX: '100%',
sScrollXInner: '100%',
order: [[1, 'asc']],
destroy: true,
language: {
emptyTable: archived ? I18n.t('repositories.index.no_archived_inventories') : I18n.t('repositories.index.no_inventories'),
zeroRecords: archived ? I18n.t('repositories.index.no_archived_inventories_matched') : I18n.t('repositories.index.no_inventories_matched')
},
2020-06-01 16:24:03 +08:00
columnDefs: [{
targets: 0,
visible: !$('.repositories-index').data('readonly'),
2020-06-01 16:24:03 +08:00
searchable: false,
orderable: false,
render: function() {
return `<div class="sci-checkbox-container">
<input class='repository-row-selector sci-checkbox' type='checkbox'>
<span class='sci-checkbox-label'></span>
</div>`;
}
2020-06-01 16:24:03 +08:00
}, {
targets: 1,
className: 'item-name',
2020-06-01 16:24:03 +08:00
render: function(value, type, row) {
return `<a href="${row.repositoryUrl}">${value}</a>`;
}
}, {
targets: 5,
render: {
_: 'display',
sort: 'sort'
}
}, {
targets: 7,
visible: archived,
render: {
_: 'display',
sort: 'sort'
}
}, {
targets: 8,
visible: archived
},
{
visible: true,
searchable: false,
data: 'stock',
render: {
_: 'display',
sort: 'sort'
}
2020-06-01 16:24:03 +08:00
}],
2020-06-09 19:16:50 +08:00
fnInitComplete: function(e) {
initActionToolbar();
window.actionToolbarComponent.setReloadCallback(() =>
initRepositoriesDataTable('#repositoriesList', archived));
window.actionToolbarComponent.setBottomOffset(68);
2020-06-09 19:16:50 +08:00
var dataTableWrapper = $(e.nTableWrapper);
CHECKBOX_SELECTOR = new DataTableCheckboxes(dataTableWrapper, {
checkboxSelector: '.repository-row-selector',
2020-06-19 21:54:45 +08:00
selectAllSelector: '.select-all-checkbox',
onChanged: function() {
updateActionButtons();
}
2020-06-09 19:16:50 +08:00
});
updateActionButtons();
DataTableHelpers.initLengthAppearance(dataTableWrapper);
DataTableHelpers.initSearchField(dataTableWrapper, I18n.t('repositories.index.filter_inventory'));
$('.content-body .toolbar').html($('#repositoriesListButtons').html());
2020-06-01 16:24:03 +08:00
dataTableWrapper.find('.main-actions, .pagination-row').removeClass('hidden');
$('#createRepoBtn').initSubmitModal('#create-repo-modal', 'repository');
$('#deleteRepoBtn').initSubmitModal('#delete-repo-modal', 'repository');
$('#renameRepoBtn').initSubmitModal('#rename-repo-modal', 'repository');
$('#copyRepoBtn').initSubmitModal('#copy-repo-modal', 'repository');
2020-06-09 19:16:50 +08:00
},
drawCallback: function() {
if (CHECKBOX_SELECTOR) CHECKBOX_SELECTOR.checkSelectAllStatus();
},
rowCallback: function(row) {
let $row = $(row);
let checkbox = $row.find('.repository-row-selector');
if ($row.attr('data-shared') === 'true') {
checkbox.attr('disabled', 'disabled');
}
2020-06-09 19:16:50 +08:00
if (CHECKBOX_SELECTOR) CHECKBOX_SELECTOR.checkRowStatus(row);
},
stateLoadParams: function(_, state) {
state.search.search = '';
}
});
});
2019-08-05 19:07:29 +08:00
}
$('#wrapper').on('sideBar::hidden sideBar::shown', function() {
if (REPOSITORIES_TABLE) {
REPOSITORIES_TABLE.columns.adjust();
}
});
$(document).on('shown.bs.modal', function() {
var inputField = $('#repository_name');
var value = inputField.val();
inputField.focus().val('').val(value);
}).on('shown.bs.modal', '#export-repositories-modal', function() {
2023-06-23 18:41:37 +08:00
if (!CHECKBOX_SELECTOR) return;
const exportButton = $(this).find('#export-repositories-modal-submit');
const exportURL = exportButton.data('export-url');
exportButton.on('click', function() {
$.ajax({
url: exportURL,
method: 'POST',
data: { repository_ids: CHECKBOX_SELECTOR.selectedRows },
success: function(data) {
HelperModule.flashAlertMsg(data.message, 'success');
CHECKBOX_SELECTOR.clearSelection();
$('#export-repositories-modal').modal('hide');
},
error: function(data) {
HelperModule.flashAlertMsg(data.responseJSON.message, 'danger');
}
});
});
});
$('.create-new-repository').initSubmitModal('#create-repo-modal', 'repository');
if (notTurbolinksPreview()) {
initRepositoriesDataTable('#repositoriesList', $('.repositories-index').hasClass('archived'));
}
2023-05-24 17:48:32 +08:00
}());