snappymail/dev/Settings/Admin/Plugins.js

103 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-07-16 05:29:42 +08:00
import _ from '_';
import ko from 'ko';
2016-07-16 05:29:42 +08:00
import {StorageResultType, Notification} from 'Common/Enums';
import {getNotification} from 'Common/Translator';
import {boolToAjax} from 'Common/Utils';
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
import {settingsGet} from 'Storage/Settings';
import {showScreenPopup} from 'Knoin/Knoin';
2014-08-25 15:10:51 +08:00
2016-07-16 05:29:42 +08:00
import AppStore from 'Stores/Admin/App';
import PluginStore from 'Stores/Admin/Plugin';
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
import Remote from 'Remote/Admin/Ajax';
2015-05-03 04:22:32 +08:00
import {getApp} from 'Helper/Apps/Admin';
2016-07-16 05:29:42 +08:00
class PluginsAdminSettings
2016-06-30 08:02:45 +08:00
{
2016-07-16 05:29:42 +08:00
constructor() {
this.enabledPlugins = ko.observable(!!settingsGet('EnabledPlugins'));
2015-11-19 01:32:29 +08:00
2016-07-16 05:29:42 +08:00
this.plugins = PluginStore.plugins;
this.pluginsError = PluginStore.plugins.error;
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
this.community = RL_COMMUNITY || AppStore.community();
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
this.visibility = ko.computed(() => (PluginStore.plugins.loading() ? 'visible' : 'hidden'));
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
this.onPluginLoadRequest = _.bind(this.onPluginLoadRequest, this);
this.onPluginDisableRequest = _.bind(this.onPluginDisableRequest, this);
}
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
disablePlugin(plugin) {
plugin.disabled(!plugin.disabled());
Remote.pluginDisable(this.onPluginDisableRequest, plugin.name, plugin.disabled());
}
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
configurePlugin(plugin) {
Remote.plugin(this.onPluginLoadRequest, plugin.name);
}
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
onBuild(oDom) {
2016-07-16 05:29:42 +08:00
const self = this;
2016-07-16 05:29:42 +08:00
oDom
.on('click', '.e-item .configure-plugin-action', function() { // eslint-disable-line prefer-arrow-callback
const plugin = ko.dataFor(this); // eslint-disable-line no-invalid-this
2016-07-16 05:29:42 +08:00
if (plugin)
{
self.configurePlugin(plugin);
}
})
.on('click', '.e-item .disabled-plugin', function() { // eslint-disable-line prefer-arrow-callback
const plugin = ko.dataFor(this); // eslint-disable-line no-invalid-this
2016-07-16 05:29:42 +08:00
if (plugin)
{
self.disablePlugin(plugin);
}
});
this.enabledPlugins.subscribe((value) => {
Remote.saveAdminConfig(null, {
'EnabledPlugins': boolToAjax(value)
});
2016-06-30 08:02:45 +08:00
});
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() {
PluginStore.plugins.error('');
getApp().reloadPluginList();
2016-07-16 05:29:42 +08:00
}
2016-06-30 08:02:45 +08:00
2016-07-16 05:29:42 +08:00
onPluginLoadRequest(result, data) {
if (StorageResultType.Success === result && data && data.Result)
{
showScreenPopup(require('View/Popup/Plugin'), [data.Result]);
}
2016-06-30 08:02:45 +08:00
}
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
onPluginDisableRequest(result, data) {
if (StorageResultType.Success === result && data)
2014-08-21 23:08:34 +08:00
{
2016-07-16 05:29:42 +08:00
if (!data.Result && data.ErrorCode)
2014-08-21 23:08:34 +08:00
{
2016-07-16 05:29:42 +08:00
if (Notification.UnsupportedPluginPackage === data.ErrorCode && data.ErrorMessage && '' !== data.ErrorMessage)
{
PluginStore.plugins.error(data.ErrorMessage);
}
else
{
PluginStore.plugins.error(getNotification(data.ErrorCode));
}
2014-08-21 23:08:34 +08:00
}
}
getApp().reloadPluginList();
2016-07-16 05:29:42 +08:00
}
}
2014-08-21 23:08:34 +08:00
2016-07-16 03:54:37 +08:00
export {PluginsAdminSettings, PluginsAdminSettings as default};