'use strict';
if ($('#plugin-table').length === 1) {
$(document).ready(initPluginsTable);
}
function initPluginsTable() {
$.pluginsTable = $('#plugin-table').DataTable({
ajax: url('admin/plugins/data'),
fnDrawCallback: () => $('[data-toggle="tooltip"]').tooltip(),
columnDefs: pluginsTableColumnDefs
}).on('xhr.dt', handleDataTablesAjaxError);
}
const pluginsTableColumnDefs = [
{
targets: 0,
data: 'title'
},
{
targets: 1,
data: 'description',
orderable: false,
width: '35%'
},
{
targets: 2,
data: 'author',
render: data => isEmpty(data.url) ? data.author : `${data.author}`
},
{
targets: 3,
data: 'version',
orderable: false
},
{
targets: 4,
data: 'dependencies',
searchable: false,
orderable: false,
render: data => {
if (data.requirements.length === 0) {
return `${trans('admin.noDependencies')}`;
}
let result = data.isRequirementsSatisfied ? '' : `${trans('admin.whyDependencies')}
`;
for (const name in data.requirements) {
const constraint = data.requirements[name];
const color = (name in data.unsatisfiedRequirements) ? 'red' : 'green';
result += `${name}: ${constraint}
`;
}
return result;
}
},
{
targets: 5,
data: 'status'
},
{
targets: 6,
data: 'operations',
searchable: false,
orderable: false,
render: (data, type, row) => {
let toggleButton, configViewButton;
if (data.enabled) {
toggleButton = `${trans('admin.disablePlugin')}`;
} else {
toggleButton = `${trans('admin.enablePlugin')}`;
}
if (data.enabled && data.hasConfigView) {
configViewButton = `${trans('admin.configurePlugin')}`;
} else {
configViewButton = `${trans('admin.configurePlugin')}`;
}
const deletePluginButton = `${trans('admin.deletePlugin')}`;
return toggleButton + configViewButton + deletePluginButton;
}
}
];
async function enablePlugin(name) {
try {
const { requirements } = await fetch({
type: 'POST',
url: url(`admin/plugins/manage?action=requirements&name=${name}`),
dataType: 'json'
});
if (requirements.length === 0) {
await swal({
text: trans('admin.noDependenciesNotice'),
type: 'warning',
showCancelButton: true
});
}
const { errno, msg, reason } = await fetch({
type: 'POST',
url: url(`admin/plugins/manage?action=enable&name=${name}`),
dataType: 'json'
});
if (errno === 0) {
toastr.success(msg);
$.pluginsTable.ajax.reload(null, false);
} else {
swal({ type: 'warning', html: `
${msg}