'use strict';
if ($('#plugin-table').length === 1) {
$(document).ready(initPluginsTable);
}
function initPluginsTable() {
$.pluginsTable = $('#plugin-table').DataTable({
columnDefs: pluginsTableColumnDefs,
fnDrawCallback: () => $('[data-toggle="tooltip"]').tooltip(),
rowCallback: (row, data) => $(row).addClass(data.enabled ? 'plugin-enabled' : ''),
ajax: {
url: url('admin/plugins/data'),
type: 'POST'
}
}).on('xhr.dt', handleDataTablesAjaxError);
}
const pluginsTableColumnDefs = [
{
targets: 0,
data: 'title',
title: trans('admin.pluginTitle'),
width: '10%',
render: (title, type, row) => {
const actions = [];
if (row.enabled) {
row.config && actions.push(`${ trans('admin.configurePlugin') }`);
actions.push(`${ trans('admin.disablePlugin') }`);
} else {
actions.push(
`${ trans('admin.enablePlugin') }`,
`${ trans('admin.deletePlugin') }`
);
}
return `
${ $.fn.dataTable.render.text().filter(title) }
${ actions.join(' | ') }
`;
}
},
{
targets: 1,
data: 'description',
title: trans('admin.pluginDescription'),
orderable: false,
render: (description, type, row) => {
return `
${ $.fn.dataTable.render.text().filter(description) }
${ trans('admin.pluginVersion') }
${ row.version } |
${ trans('admin.pluginAuthor') }
${ row.author } |
${ trans('admin.pluginName') }
${ row.name }
`;
}
},
{
targets: 2,
data: 'dependencies',
title: trans('admin.pluginDependencies'),
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;
}
}
];
async function enablePlugin(name) {
const dataTable = $.pluginsTable || $.marketTable;
try {
const { requirements } = dataTable.row(`#plugin-${name}`).data().dependencies;
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);
dataTable.ajax.reload(null, false);
} else {
swal({ type: 'warning', html: `${msg}
` });
}
} catch (error) {
showAjaxError(error);
}
}
async function disablePlugin(name) {
try {
const { errno, msg } = await fetch({
type: 'POST',
url: url(`admin/plugins/manage?action=disable&name=${name}`),
dataType: 'json'
});
if (errno === 0) {
toastr.success(msg);
$.pluginsTable.ajax.reload(null, false);
} else {
swal({ type: 'warning', html: msg });
}
} catch (error) {
showAjaxError(error);
}
}
async function deletePlugin(name) {
try {
await swal({
text: trans('admin.confirmDeletion'),
type: 'warning',
showCancelButton: true
});
} catch (error) {
return;
}
try {
const { errno, msg } = await fetch({
type: 'POST',
url: url(`admin/plugins/manage?action=delete&name=${name}`),
dataType: 'json'
});
if (errno === 0) {
toastr.success(msg);
$.pluginsTable.ajax.reload(null, false);
} else {
swal({ type: 'warning', html: msg });
}
} catch (error) {
showAjaxError(error);
}
}
if (process.env.NODE_ENV === 'test') {
module.exports = {
initPluginsTable,
deletePlugin,
enablePlugin,
disablePlugin,
};
}