snappymail/dev/View/Popup/Plugin.js

111 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';
import { isNonEmptyArray } 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, isPopupVisible, showScreenPopup } from 'Knoin/Knoin';
import { AbstractViewPopup } from 'Knoin/AbstractViews';
import { AskPopupView } from 'View/Popup/Ask';
class PluginPopupView extends AbstractViewPopup {
constructor() {
super('Plugin');
2014-08-21 23:08:34 +08:00
this.onPluginSettingsUpdateResponse = this.onPluginSettingsUpdateResponse.bind(this);
2014-08-21 23:08:34 +08:00
this.addObservables({
saveError: '',
name: '',
readme: ''
});
2014-08-21 23:08:34 +08:00
this.configures = ko.observableArray();
2014-08-21 23:08:34 +08:00
this.hasReadme = ko.computed(() => !!this.readme());
2021-03-02 16:51:52 +08:00
this.hasConfiguration = ko.computed(() => 0 < this.configures().length);
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
this.bDisabeCloseOnEsc = true;
this.sDefaultScope = 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() {
const list = {};
list.Name = this.name();
2014-08-21 23:08:34 +08:00
this.configures.forEach(oItem => {
2016-09-10 06:38:16 +08:00
let value = oItem.value();
2019-07-05 03:19:24 +08:00
if (false === value || true === value) {
2016-09-10 06:38:16 +08:00
value = value ? '1' : '0';
}
list['_' + oItem.Name] = value;
});
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
this.saveError('');
Remote.pluginSettingsUpdate(this.onPluginSettingsUpdateResponse, list);
2016-06-30 08:02:45 +08:00
}
onPluginSettingsUpdateResponse(iError) {
if (iError) {
this.saveError(getNotification(iError));
2019-07-05 03:19:24 +08:00
} else {
this.cancelCommand();
}
2016-06-30 08:02:45 +08:00
}
onShow(oPlugin) {
this.name('');
this.readme('');
this.configures([]);
2014-08-27 23:59:44 +08:00
2019-07-05 03:19:24 +08:00
if (oPlugin) {
this.name(oPlugin.Name);
this.readme(oPlugin.Readme);
2016-06-30 08:02:45 +08:00
const config = oPlugin.Config;
if (isNonEmptyArray(config)) {
2019-07-05 03:19:24 +08:00
this.configures(
config.map(item => ({
value: ko.observable(item[0]),
placeholder: ko.observable(item[6]),
Name: item[1],
Type: item[2],
Label: item[3],
Default: item[4],
Desc: item[5]
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 (!isPopupVisible(AskPopupView)) {
showScreenPopup(AskPopupView, [
2019-07-05 03:19:24 +08:00
i18n('POPUPS_ASK/DESC_WANT_CLOSE_THIS_WINDOW'),
() => this.modalVisibility() && this.cancelCommand && 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();
}
2016-07-16 05:29:42 +08:00
return false;
});
}
}
2014-08-21 23:08:34 +08:00
2019-07-05 03:19:24 +08:00
export { PluginPopupView, PluginPopupView as default };