2020-07-10 17:05:27 +08:00
|
|
|
/* global I18n animateSpinner HelperModule
|
|
|
|
DataTableHelpers DataTableCheckboxes notTurbolinksPreview */
|
2020-06-19 21:54:45 +08:00
|
|
|
(function() {
|
2017-05-30 21:11:28 +08:00
|
|
|
'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() {
|
|
|
|
var rowsCount = CHECKBOX_SELECTOR.selectedRows.length;
|
|
|
|
var row;
|
|
|
|
$('#renameRepoBtn').attr('href', '#');
|
|
|
|
$('#deleteRepoBtn').attr('href', '#');
|
|
|
|
$('#copyRepoBtn').attr('href', '#');
|
|
|
|
switch (rowsCount) {
|
|
|
|
case 0:
|
2020-07-07 19:53:31 +08:00
|
|
|
$('.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]);
|
2020-07-07 19:53:31 +08:00
|
|
|
$('.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:
|
2020-07-07 19:53:31 +08:00
|
|
|
$('.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 19:42:09 +08:00
|
|
|
function initRepositoriesDataTable(tableContainer, archived = false) {
|
2020-07-02 20:35:48 +08:00
|
|
|
var tableTemplate = $('#RepositoriesListTable').html();
|
2020-07-10 17:05:27 +08:00
|
|
|
$.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,
|
|
|
|
pageLength: 25,
|
2020-07-02 17:50:40 +08:00
|
|
|
colReorder: {
|
|
|
|
enable: false
|
|
|
|
},
|
2020-06-01 16:24:03 +08:00
|
|
|
sScrollX: '100%',
|
|
|
|
sScrollXInner: '100%',
|
|
|
|
order: [[1, 'asc']],
|
|
|
|
destroy: true,
|
2020-07-02 21:51:07 +08:00
|
|
|
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,
|
2020-07-02 20:29:18 +08:00
|
|
|
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>`;
|
2019-08-06 21:17:13 +08:00
|
|
|
}
|
2020-06-01 16:24:03 +08:00
|
|
|
}, {
|
|
|
|
targets: 1,
|
2020-06-24 19:41:00 +08:00
|
|
|
className: 'item-name',
|
2020-06-01 16:24:03 +08:00
|
|
|
render: function(value, type, row) {
|
|
|
|
return `<a href="${row.repositoryUrl}">${value}</a>`;
|
|
|
|
}
|
2020-07-02 20:35:48 +08:00
|
|
|
}, {
|
|
|
|
targets: 5,
|
|
|
|
render: {
|
|
|
|
_: 'display',
|
|
|
|
sort: 'sort'
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
targets: 7,
|
|
|
|
visible: archived,
|
|
|
|
render: {
|
|
|
|
_: 'display',
|
|
|
|
sort: 'sort'
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
targets: 8,
|
|
|
|
visible: archived
|
2022-03-08 21:52:45 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
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) {
|
|
|
|
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
|
|
|
});
|
2022-02-02 17:41:29 +08:00
|
|
|
DataTableHelpers.initLengthAppearance(dataTableWrapper);
|
2020-07-15 16:52:47 +08:00
|
|
|
DataTableHelpers.initSearchField(dataTableWrapper, I18n.t('repositories.index.filter_inventory'));
|
2020-06-03 19:42:09 +08:00
|
|
|
$('.content-body .toolbar').html($('#repositoriesListButtons').html());
|
2020-06-01 16:24:03 +08:00
|
|
|
dataTableWrapper.find('.main-actions, .pagination-row').removeClass('hidden');
|
2020-12-01 16:59:08 +08:00
|
|
|
$('#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) {
|
2020-06-24 19:41:00 +08:00
|
|
|
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);
|
2019-08-06 21:17:13 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2019-08-05 19:07:29 +08:00
|
|
|
}
|
|
|
|
|
2020-06-04 20:39:56 +08:00
|
|
|
function reloadSidebar() {
|
2021-01-06 17:13:10 +08:00
|
|
|
var slidePanel = $('.sidebar-container');
|
2020-07-06 20:03:55 +08:00
|
|
|
$.get(slidePanel.data('sidebar-url'), {
|
|
|
|
archived: $('.repositories-index').hasClass('archived')
|
|
|
|
}, function(data) {
|
2021-01-06 17:13:10 +08:00
|
|
|
slidePanel.find('.sidebar-body').html(data.html);
|
2020-12-01 16:59:08 +08:00
|
|
|
$('.create-new-repository').initSubmitModal('#create-repo-modal', 'repository');
|
2020-06-04 20:39:56 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-03 19:42:09 +08:00
|
|
|
function initRepositoryViewSwitcher() {
|
|
|
|
var viewSwitch = $('.view-switch');
|
|
|
|
viewSwitch.on('click', '.view-switch-archived', function() {
|
2020-07-06 20:03:55 +08:00
|
|
|
$('.repositories-index').removeClass('active').addClass('archived');
|
2020-06-03 19:42:09 +08:00
|
|
|
initRepositoriesDataTable('#repositoriesList', true);
|
2020-06-04 20:39:56 +08:00
|
|
|
reloadSidebar();
|
2020-06-03 19:42:09 +08:00
|
|
|
});
|
|
|
|
viewSwitch.on('click', '.view-switch-active', function() {
|
2020-07-06 20:03:55 +08:00
|
|
|
$('.repositories-index').removeClass('archived').addClass('active');
|
2020-06-03 19:42:09 +08:00
|
|
|
initRepositoriesDataTable('#repositoriesList');
|
2020-06-04 20:39:56 +08:00
|
|
|
reloadSidebar();
|
2020-06-03 19:42:09 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-19 21:54:45 +08:00
|
|
|
$('.repositories-index')
|
|
|
|
.on('click', '#archiveRepoBtn', function() {
|
|
|
|
$.post($('#archiveRepoBtn').data('archive-repositories'), {
|
2020-06-24 17:32:20 +08:00
|
|
|
repository_ids: CHECKBOX_SELECTOR.selectedRows
|
2020-06-19 21:54:45 +08:00
|
|
|
}, function(data) {
|
2020-06-19 00:07:23 +08:00
|
|
|
HelperModule.flashAlertMsg(data.flash, 'success');
|
|
|
|
initRepositoriesDataTable('#repositoriesList');
|
|
|
|
reloadSidebar();
|
2020-06-19 21:54:45 +08:00
|
|
|
}).fail(function(ev) {
|
2020-06-19 15:04:42 +08:00
|
|
|
if (ev.status === 403) {
|
2020-06-19 21:54:45 +08:00
|
|
|
HelperModule.flashAlertMsg(I18n.t('repositories.js.permission_error'), ev.responseJSON.style);
|
2020-06-19 15:04:42 +08:00
|
|
|
} else if (ev.status === 422) {
|
2020-06-19 21:54:45 +08:00
|
|
|
HelperModule.flashAlertMsg(ev.responseJSON.error, 'danger');
|
2020-06-19 15:04:42 +08:00
|
|
|
}
|
2020-06-19 21:54:45 +08:00
|
|
|
animateSpinner(null, false);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.on('click', '#restoreRepoBtn', function() {
|
|
|
|
$.post($('#restoreRepoBtn').data('restore-repositories'), {
|
2020-06-24 17:32:20 +08:00
|
|
|
repository_ids: CHECKBOX_SELECTOR.selectedRows
|
2020-06-19 21:54:45 +08:00
|
|
|
}, function(data) {
|
2020-06-19 00:07:23 +08:00
|
|
|
HelperModule.flashAlertMsg(data.flash, 'success');
|
|
|
|
initRepositoriesDataTable('#repositoriesList', true);
|
|
|
|
reloadSidebar();
|
2020-06-19 21:54:45 +08:00
|
|
|
}).fail(function(ev) {
|
2020-06-19 15:04:42 +08:00
|
|
|
if (ev.status === 403) {
|
2020-06-19 21:54:45 +08:00
|
|
|
HelperModule.flashAlertMsg(I18n.t('repositories.js.permission_error'), ev.responseJSON.style);
|
2020-06-19 15:04:42 +08:00
|
|
|
} else if (ev.status === 422) {
|
2020-06-19 21:54:45 +08:00
|
|
|
HelperModule.flashAlertMsg(ev.responseJSON.error, 'danger');
|
2020-06-19 15:04:42 +08:00
|
|
|
}
|
2020-06-19 21:54:45 +08:00
|
|
|
animateSpinner(null, false);
|
|
|
|
});
|
2020-06-19 00:07:23 +08:00
|
|
|
});
|
|
|
|
|
2020-07-07 15:48:59 +08:00
|
|
|
|
|
|
|
$('#wrapper').on('sideBar::hidden sideBar::shown', function() {
|
|
|
|
if (REPOSITORIES_TABLE) {
|
|
|
|
REPOSITORIES_TABLE.columns.adjust();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-12-01 16:59:08 +08:00
|
|
|
$('.create-new-repository').initSubmitModal('#create-repo-modal', 'repository');
|
2020-07-10 17:05:27 +08:00
|
|
|
if (notTurbolinksPreview()) {
|
|
|
|
initRepositoriesDataTable('#repositoriesList', $('.repositories-index').hasClass('archived'));
|
|
|
|
}
|
2020-06-03 19:42:09 +08:00
|
|
|
initRepositoryViewSwitcher();
|
2020-06-19 21:54:45 +08:00
|
|
|
}());
|