snappymail/dev/Settings/Admin/Packages.js

130 lines
3.2 KiB
JavaScript
Raw Normal View History

2016-07-16 05:29:42 +08:00
import ko from 'ko';
2016-06-30 08:02:45 +08:00
import { Notifications } from 'Common/Enums';
2019-07-05 03:19:24 +08:00
import { getNotification } from 'Common/Translator';
2016-06-30 08:02:45 +08:00
import { PackageAdminStore } from 'Stores/Admin/Package';
import Remote from 'Remote/Admin/Fetch';
2016-06-30 08:02:45 +08:00
import { showScreenPopup } from 'Knoin/Knoin';
import { PluginPopupView } from 'View/Popup/Plugin';
2022-02-17 16:36:29 +08:00
import { addObservablesTo, addComputablesTo } from 'External/ko';
2022-03-01 17:18:12 +08:00
import { AbstractViewSettings } from 'Knoin/AbstractViews';
export class AdminSettingsPackages extends AbstractViewSettings {
2016-07-16 05:29:42 +08:00
constructor() {
2022-03-01 17:18:12 +08:00
super();
this.addSettings(['pluginsEnable']);
2022-03-01 17:18:12 +08:00
addObservablesTo(this, {
2022-03-01 17:18:12 +08:00
packagesError: ''
});
2016-06-30 08:02:45 +08:00
this.packages = PackageAdminStore;
2016-06-30 08:02:45 +08:00
addComputablesTo(this, {
packagesCurrent: () => PackageAdminStore().filter(item => item?.installed && !item.canBeUpdated),
packagesUpdate: () => PackageAdminStore().filter(item => item?.installed && item.canBeUpdated),
packagesAvailable: () => PackageAdminStore().filter(item => !item?.installed),
visibility: () => (PackageAdminStore.loading() ? 'visible' : 'hidden')
});
2016-07-16 05:29:42 +08:00
}
2016-06-30 08:02:45 +08:00
2016-07-16 05:29:42 +08:00
onShow() {
this.packagesError('');
}
2016-06-30 08:02:45 +08:00
onBuild(oDom) {
PackageAdminStore.fetch();
oDom.addEventListener('click', event => {
// configurePlugin
let el = event.target.closestWithin('.package-configure', oDom),
2022-10-10 19:52:56 +08:00
data = el && ko.dataFor(el);
2021-12-03 07:11:19 +08:00
data && Remote.request('AdminPluginLoad',
(iError, data) => iError || showScreenPopup(PluginPopupView, [data.Result]),
{
id: data.id
2021-12-03 07:11:19 +08:00
}
);
// disablePlugin
el = event.target.closestWithin('.package-active', oDom);
2022-10-10 19:52:56 +08:00
data = el && ko.dataFor(el);
data && this.disablePlugin(data);
});
2016-07-16 05:29:42 +08:00
}
2016-06-30 08:02:45 +08:00
2016-07-16 05:29:42 +08:00
requestHelper(packageToRequest, install) {
return (iError, data) => {
PackageAdminStore.forEach(item => {
if (packageToRequest && item?.loading?.() && packageToRequest.file === item.file) {
2016-07-16 05:29:42 +08:00
packageToRequest.loading(false);
item.loading(false);
}
});
2016-06-30 08:02:45 +08:00
2021-08-30 20:01:42 +08:00
if (iError) {
this.packagesError(
getNotification(install ? Notifications.CantInstallPackage : Notifications.CantDeletePackage)
2021-08-30 20:01:42 +08:00
+ (data.ErrorMessage ? ':\n' + data.ErrorMessage : '')
);
} else if (data.Result.Reload) {
location.reload();
2019-07-05 03:19:24 +08:00
} else {
PackageAdminStore.fetch();
2015-05-20 21:09:21 +08:00
}
2016-07-16 05:29:42 +08:00
};
}
2015-05-20 21:09:21 +08:00
2016-07-16 05:29:42 +08:00
deletePackage(packageToDelete) {
2019-07-05 03:19:24 +08:00
if (packageToDelete) {
2016-07-16 05:29:42 +08:00
packageToDelete.loading(true);
2021-12-03 07:11:19 +08:00
Remote.request('AdminPackageDelete',
this.requestHelper(packageToDelete, false),
{
id: packageToDelete.id
2021-12-03 07:11:19 +08:00
}
);
2015-05-20 21:09:21 +08:00
}
2016-06-30 08:02:45 +08:00
}
2016-07-16 05:29:42 +08:00
installPackage(packageToInstall) {
2019-07-05 03:19:24 +08:00
if (packageToInstall) {
2016-07-16 05:29:42 +08:00
packageToInstall.loading(true);
2021-12-03 07:11:19 +08:00
Remote.request('AdminPackageInstall',
this.requestHelper(packageToInstall, true),
{
id: packageToInstall.id,
type: packageToInstall.type,
file: packageToInstall.file
2021-12-03 07:11:19 +08:00
},
60000
);
2016-07-16 05:29:42 +08:00
}
2016-06-30 08:02:45 +08:00
}
disablePlugin(plugin) {
2021-12-03 07:11:19 +08:00
let disable = plugin.enabled();
plugin.enabled(!disable);
Remote.request('AdminPluginDisable',
(iError, data) => {
2021-12-03 07:11:19 +08:00
if (iError) {
plugin.enabled(disable);
this.packagesError(
(Notifications.UnsupportedPluginPackage === iError && data?.ErrorMessage)
2021-12-03 07:11:19 +08:00
? data.ErrorMessage
: getNotification(iError)
);
}
// PackageAdminStore.fetch();
}, {
id: plugin.id,
disabled: disable ? 1 : 0
}
2021-12-03 07:11:19 +08:00
);
}
2016-07-16 05:29:42 +08:00
}