snappymail/dev/View/Popup/Plugin.js

119 lines
2.4 KiB
JavaScript
Raw Normal View History

import ko from 'ko';
2014-08-25 23:49:01 +08:00
import { Scope } from 'Common/Enums';
2019-07-05 03:19:24 +08:00
import { getNotification, i18n } from 'Common/Translator';
2021-07-22 03:34:17 +08:00
import { arrayLength } from 'Common/Utils';
2014-08-21 23:08:34 +08:00
import Remote from 'Remote/Admin/Fetch';
2014-08-21 23:08:34 +08:00
import { decorateKoCommands, showScreenPopup } from 'Knoin/Knoin';
import { AbstractViewPopup } from 'Knoin/AbstractViews';
import { AskPopupView } from 'View/Popup/Ask';
2022-02-24 21:01:41 +08:00
export class PluginPopupView extends AbstractViewPopup {
constructor() {
super('Plugin');
2014-08-21 23:08:34 +08:00
this.addObservables({
saveError: '',
2021-08-09 17:13:10 +08:00
id: '',
name: '',
readme: ''
});
2014-08-21 23:08:34 +08:00
this.config = ko.observableArray();
2014-08-21 23:08:34 +08:00
this.addComputables({
hasReadme: () => !!this.readme(),
hasConfiguration: () => 0 < this.config().length
});
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
this.bDisabeCloseOnEsc = true;
this.keyScope.scope = Scope.All;
2014-08-21 23:08:34 +08:00
this.tryToClosePopup = this.tryToClosePopup.debounce(200);
decorateKoCommands(this, {
saveCommand: self => self.hasConfiguration()
});
2016-09-10 06:38:16 +08:00
}
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
saveCommand() {
2021-12-03 07:11:19 +08:00
const oConfig = {
2021-08-09 17:13:10 +08:00
Id: this.id(),
Settings: {}
2022-02-19 20:23:13 +08:00
},
setItem = item => {
let value = item.value();
if (false === value || true === value) {
value = value ? 1 : 0;
}
oConfig.Settings[item.Name] = value;
2021-08-09 17:13:10 +08:00
};
2014-08-21 23:08:34 +08:00
this.config.forEach(oItem => {
2022-02-19 20:23:13 +08:00
if (7 == oItem.Type) {
// Group
oItem.config.forEach(oSubItem => setItem(oSubItem));
} else {
setItem(oItem);
2016-09-10 06:38:16 +08:00
}
});
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
this.saveError('');
2021-12-03 07:11:19 +08:00
Remote.request('AdminPluginSettingsUpdate',
iError => iError
2021-04-23 16:47:24 +08:00
? this.saveError(getNotification(iError))
2021-12-03 07:11:19 +08:00
: this.cancelCommand(),
oConfig);
2016-06-30 08:02:45 +08:00
}
onShow(oPlugin) {
2021-08-09 17:13:10 +08:00
this.id('');
this.name('');
this.readme('');
this.config([]);
2014-08-27 23:59:44 +08:00
2019-07-05 03:19:24 +08:00
if (oPlugin) {
2021-08-09 17:13:10 +08:00
this.id(oPlugin.Id);
this.name(oPlugin.Name);
this.readme(oPlugin.Readme);
2016-06-30 08:02:45 +08:00
const config = oPlugin.Config;
2021-07-22 03:34:17 +08:00
if (arrayLength(config)) {
this.config(
config.map(item => {
2022-02-19 20:23:13 +08:00
if (7 == item.Type) {
// Group
item.config.forEach(subItem => {
subItem.value = ko.observable(subItem.value);
});
} else {
item.value = ko.observable(item.value);
}
return item;
})
2019-07-05 03:19:24 +08:00
);
2014-08-21 23:08:34 +08:00
}
}
2016-06-30 08:02:45 +08:00
}
2014-08-21 23:08:34 +08:00
tryToClosePopup() {
if (AskPopupView.hidden()) {
showScreenPopup(AskPopupView, [
2019-07-05 03:19:24 +08:00
i18n('POPUPS_ASK/DESC_WANT_CLOSE_THIS_WINDOW'),
() => this.modalVisibility() && this.cancelCommand()
2019-07-05 03:19:24 +08:00
]);
2016-06-30 08:02:45 +08:00
}
}
onBuild() {
shortcuts.add('escape', '', Scope.All, () => {
2019-07-05 03:19:24 +08:00
if (this.modalVisibility()) {
this.tryToClosePopup();
2021-08-13 16:03:13 +08:00
return false;
}
});
}
}