snappymail/dev/View/Popup/Plugin.js

142 lines
3.1 KiB
JavaScript
Raw Normal View History

import _ from '_';
import ko from 'ko';
import key from 'key';
2014-08-25 23:49:01 +08:00
import {KeyState, Magics, StorageResultType, Notification} from 'Common/Enums';
import {createCommand, isNonEmptyArray, delegateRun} from 'Common/Utils';
import {getNotification, i18n} from 'Common/Translator';
2014-08-21 23:08:34 +08:00
import Remote from 'Remote/Admin/Ajax';
2014-08-21 23:08:34 +08:00
import {view, ViewType, isPopupVisible, showScreenPopup} from 'Knoin/Knoin';
import {AbstractViewNext} from 'Knoin/AbstractViewNext';
2014-08-21 23:08:34 +08:00
@view({
name: 'View/Popup/Plugin',
type: ViewType.Popup,
templateID: 'PopupsPlugin'
})
class PluginPopupView extends AbstractViewNext
2016-06-30 08:02:45 +08:00
{
constructor() {
super();
2014-08-21 23:08:34 +08:00
this.onPluginSettingsUpdateResponse = _.bind(this.onPluginSettingsUpdateResponse, this);
2014-08-21 23:08:34 +08:00
this.saveError = ko.observable('');
2014-08-21 23:08:34 +08:00
this.name = ko.observable('');
this.readme = ko.observable('');
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());
this.hasConfiguration = ko.computed(() => 0 < this.configures().length);
2014-08-21 23:08:34 +08:00
this.readmePopoverConf = {
'placement': 'right',
'trigger': 'hover',
'title': i18n('POPUPS_PLUGIN/TOOLTIP_ABOUT_TITLE'),
'container': 'body',
'html': true,
'content': () => `<pre>${this.readme()}</pre>`
};
2014-08-21 23:08:34 +08:00
this.saveCommand = createCommand(() => {
const list = {};
list.Name = this.name();
2014-08-21 23:08:34 +08:00
_.each(this.configures(), (oItem) => {
let value = oItem.value();
if (false === value || true === value)
{
value = value ? '1' : '0';
}
list['_' + oItem.Name] = value;
});
2014-08-21 23:08:34 +08:00
this.saveError('');
Remote.pluginSettingsUpdate(this.onPluginSettingsUpdateResponse, list);
2014-08-21 23:08:34 +08:00
}, this.hasConfiguration);
2014-08-21 23:08:34 +08:00
this.bDisabeCloseOnEsc = true;
this.sDefaultKeyScope = KeyState.All;
2014-08-21 23:08:34 +08:00
this.tryToClosePopup = _.debounce(_.bind(this.tryToClosePopup, this), Magics.Time200ms);
2016-06-30 08:02:45 +08:00
}
onPluginSettingsUpdateResponse(result, data) {
if (StorageResultType.Success === result && data && data.Result)
{
this.cancelCommand();
}
else
{
this.saveError('');
if (data && data.ErrorCode)
{
this.saveError(getNotification(data.ErrorCode));
}
else
{
this.saveError(getNotification(Notification.CantSavePluginSettings));
}
}
2016-06-30 08:02:45 +08:00
}
onShow(oPlugin) {
this.name();
this.readme();
this.configures([]);
2014-08-27 23:59:44 +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))
2014-08-21 23:08:34 +08:00
{
this.configures(_.map(config, (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]
})));
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() {
const PopupsAskViewModel = require('View/Popup/Ask');
if (!isPopupVisible(PopupsAskViewModel))
2016-06-30 08:02:45 +08:00
{
showScreenPopup(PopupsAskViewModel, [i18n('POPUPS_ASK/DESC_WANT_CLOSE_THIS_WINDOW'), () => {
if (this.modalVisibility())
{
delegateRun(this, 'cancelCommand');
}
}]);
2016-06-30 08:02:45 +08:00
}
}
onBuild() {
key('esc', KeyState.All, () => {
if (this.modalVisibility())
{
this.tryToClosePopup();
}
2016-07-16 05:29:42 +08:00
return false;
});
}
}
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
module.exports = PluginPopupView;