snappymail/dev/View/Popup/Template.js

160 lines
3.1 KiB
JavaScript
Raw Normal View History

import { Notification } from 'Common/Enums';
2019-07-05 03:19:24 +08:00
import { getNotification } from 'Common/Translator';
import { HtmlEditor } from 'Common/Html';
import Remote from 'Remote/User/Fetch';
import { decorateKoCommands } from 'Knoin/Knoin';
import { AbstractViewPopup } from 'Knoin/AbstractViews';
import { TemplateModel } from 'Model/Template';
class TemplatePopupView extends AbstractViewPopup {
constructor() {
super('Template');
this.editor = null;
this.addObservables({
signatureDom: null,
id: '',
name: '',
nameError: false,
nameFocus: false,
body: '',
bodyLoading: false,
bodyError: false,
submitRequest: false,
submitError: ''
});
this.name.subscribe(() => this.nameError(false));
this.body.subscribe(() => this.bodyError(false));
decorateKoCommands(this, {
addTemplateCommand: self => !self.submitRequest()
});
2016-09-10 06:38:16 +08:00
}
2016-09-10 06:38:16 +08:00
addTemplateCommand() {
this.populateBodyFromEditor();
this.nameError(!this.name().trim());
this.bodyError(!this.body().trim() || ':HTML:' === this.body().trim());
if (this.nameError() || this.bodyError()) {
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(
(iError, data) => {
2019-07-05 03:19:24 +08:00
this.submitRequest(false);
if (!iError && data) {
2019-07-05 03:19:24 +08:00
if (data.Result) {
2020-09-15 15:29:25 +08:00
rl.app.templates();
2019-07-05 03:19:24 +08:00
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.nameError(false);
this.body('');
this.bodyLoading(false);
this.bodyError(false);
this.submitRequest(false);
this.submitError('');
2019-07-05 03:19:24 +08:00
if (this.editor) {
this.editor.setPlain('');
}
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)
2019-07-05 03:19:24 +08:00
);
} 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.bodyLoading(true);
this.bodyError(false);
Remote.templateGetById((iError, data) => {
this.bodyLoading(false);
2019-07-05 03:19:24 +08:00
if (
!iError &&
2019-07-05 03:19:24 +08:00
data &&
TemplateModel.validJson(data.Result) &&
null != data.Result.Body
2019-07-05 03:19:24 +08:00
) {
template.body = data.Result.Body;
template.populated = true;
this.body(template.body);
this.bodyError(false);
2019-07-05 03:19:24 +08:00
} else {
this.body('');
this.bodyError(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.nameFocus(true);
}
}
2019-07-05 03:19:24 +08:00
export { TemplatePopupView, TemplatePopupView as default };