snappymail/dev/View/Popup/Template.js

171 lines
3.5 KiB
JavaScript
Raw Normal View History

import ko from 'ko';
2019-07-05 03:19:24 +08:00
import { StorageResultType, Notification } from 'Common/Enums';
import { isNormal } from 'Common/Utils';
2019-07-05 03:19:24 +08:00
import { getNotification } from 'Common/Translator';
import { HtmlEditor } from 'Common/HtmlEditor';
import Remote from 'Remote/User/Ajax';
2019-07-05 03:19:24 +08:00
import { getApp } from 'Helper/Apps/User';
2019-07-05 03:19:24 +08:00
import { popup, command } from 'Knoin/Knoin';
import { AbstractViewNext } from 'Knoin/AbstractViewNext';
2016-09-10 06:38:16 +08:00
@popup({
name: 'View/Popup/Template',
templateID: 'PopupsTemplate'
})
2019-07-05 03:19:24 +08:00
class TemplatePopupView extends AbstractViewNext {
constructor() {
super();
this.editor = null;
this.signatureDom = ko.observable(null);
this.id = ko.observable('');
this.name = ko.observable('');
this.name.error = ko.observable(false);
this.name.focus = ko.observable(false);
this.body = ko.observable('');
this.body.loading = ko.observable(false);
this.body.error = ko.observable(false);
this.name.subscribe(() => {
this.name.error(false);
});
this.body.subscribe(() => {
this.body.error(false);
});
this.submitRequest = ko.observable(false);
this.submitError = ko.observable('');
2016-09-10 06:38:16 +08:00
}
2016-09-10 06:38:16 +08:00
@command((self) => !self.submitRequest())
addTemplateCommand() {
this.populateBodyFromEditor();
this.name.error(!this.name().trim());
this.body.error(!this.body().trim() || ':HTML:' === this.body().trim());
2019-07-05 03:19:24 +08:00
if (this.name.error() || this.body.error()) {
2016-09-10 06:38:16 +08:00
return false;
}
2016-09-10 06:38:16 +08:00
this.submitRequest(true);
2019-07-05 03:19:24 +08:00
Remote.templateSetup(
(result, data) => {
this.submitRequest(false);
if (StorageResultType.Success === result && data) {
if (data.Result) {
getApp().templates();
this.cancelCommand();
} else if (data.ErrorCode) {
this.submitError(getNotification(data.ErrorCode));
}
} else {
this.submitError(getNotification(Notification.UnknownError));
}
2019-07-05 03:19:24 +08:00
},
this.id(),
this.name(),
this.body()
);
2016-09-10 06:38:16 +08:00
return true;
}
clearPopup() {
this.id('');
this.name('');
this.name.error(false);
this.body('');
this.body.loading(false);
this.body.error(false);
this.submitRequest(false);
this.submitError('');
2019-07-05 03:19:24 +08:00
if (this.editor) {
this.editor.setPlain('', false);
}
2016-06-30 08:02:45 +08:00
}
populateBodyFromEditor() {
2019-07-05 03:19:24 +08:00
if (this.editor) {
this.body(this.editor.getDataWithHtmlMark());
}
2016-06-30 08:02:45 +08:00
}
editorSetBody(sBody) {
2019-07-05 03:19:24 +08:00
if (!this.editor && this.signatureDom()) {
this.editor = new HtmlEditor(
this.signatureDom(),
() => {
this.populateBodyFromEditor();
},
() => {
this.editor.setHtmlOrPlain(sBody);
}
);
} else {
this.editor.setHtmlOrPlain(sBody);
}
2016-06-30 08:02:45 +08:00
}
onShow(template) {
this.clearPopup();
2019-07-05 03:19:24 +08:00
if (template && template.id) {
this.id(template.id);
this.name(template.name);
this.body(template.body);
2019-07-05 03:19:24 +08:00
if (template.populated) {
this.editorSetBody(this.body());
2019-07-05 03:19:24 +08:00
} else {
this.body.loading(true);
this.body.error(false);
Remote.templateGetById((result, data) => {
this.body.loading(false);
2019-07-05 03:19:24 +08:00
if (
StorageResultType.Success === result &&
data &&
data.Result &&
'Object/Template' === data.Result['@Object'] &&
isNormal(data.Result.Body)
) {
template.body = data.Result.Body;
template.populated = true;
this.body(template.body);
this.body.error(false);
2019-07-05 03:19:24 +08:00
} else {
this.body('');
this.body.error(true);
}
this.editorSetBody(this.body());
}, this.id());
}
2019-07-05 03:19:24 +08:00
} else {
this.editorSetBody('');
2016-06-30 08:02:45 +08:00
}
}
onShowWithDelay() {
this.name.focus(true);
}
}
2019-07-05 03:19:24 +08:00
export { TemplatePopupView, TemplatePopupView as default };