snappymail/dev/Settings/Admin/Packages.js

81 lines
2.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
2019-07-05 03:19:24 +08:00
import { StorageResultType, Notification } from 'Common/Enums';
import { getNotification } from 'Common/Translator';
2016-06-30 08:02:45 +08:00
2016-07-16 05:29:42 +08:00
import PackageStore from 'Stores/Admin/Package';
import Remote from 'Remote/Admin/Fetch';
2016-06-30 08:02:45 +08:00
2019-07-05 03:19:24 +08:00
class PackagesAdminSettings {
2016-07-16 05:29:42 +08:00
constructor() {
this.packagesError = ko.observable('');
2016-06-30 08:02:45 +08:00
2016-07-16 05:29:42 +08:00
this.packages = PackageStore.packages;
this.packagesReal = PackageStore.packagesReal;
this.packagesMainUpdatable = PackageStore.packagesMainUpdatable;
2016-06-30 08:02:45 +08:00
this.packagesCurrent = ko.computed(() =>
this.packages().filter(item => item && item.installed && !item.compare)
);
this.packagesAvailableForUpdate = ko.computed(() =>
this.packages().filter(item => item && item.installed && !!item.compare)
);
this.packagesAvailableForInstallation = ko.computed(() =>
this.packages().filter(item => item && !item.installed)
);
2016-06-30 08:02:45 +08:00
2016-07-16 05:29:42 +08:00
this.visibility = ko.computed(() => (PackageStore.packages.loading() ? 'visible' : 'hidden'));
}
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
2016-07-16 05:29:42 +08:00
onBuild() {
2020-09-15 15:29:25 +08:00
rl.app.reloadPackagesList();
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 (result, data) => {
2019-07-05 03:19:24 +08:00
if (StorageResultType.Success !== result || !data || !data.Result) {
if (data && data.ErrorCode) {
2016-07-16 05:29:42 +08:00
this.packagesError(getNotification(data.ErrorCode));
2019-07-05 03:19:24 +08:00
} else {
this.packagesError(
getNotification(install ? Notification.CantInstallPackage : Notification.CantDeletePackage)
);
2016-07-16 05:29:42 +08:00
}
}
2016-06-30 08:02:45 +08:00
this.packages().forEach(item => {
2019-07-05 03:19:24 +08:00
if (item && packageToRequest && item.loading && 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
2019-07-05 03:19:24 +08:00
if (StorageResultType.Success === result && data && data.Result && data.Result.Reload) {
location.reload();
2019-07-05 03:19:24 +08:00
} else {
2020-09-15 15:29:25 +08:00
rl.app.reloadPackagesList();
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);
Remote.packageDelete(this.requestHelper(packageToDelete, false), packageToDelete);
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);
Remote.packageInstall(this.requestHelper(packageToInstall, true), packageToInstall);
}
2016-06-30 08:02:45 +08:00
}
2016-07-16 05:29:42 +08:00
}
2015-05-20 21:09:21 +08:00
2019-07-05 03:19:24 +08:00
export { PackagesAdminSettings, PackagesAdminSettings as default };