2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
import ko from 'ko';
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
import {StorageResultType, Notification} from 'Common/Enums';
|
|
|
|
import {trim, isNormal, createCommand} from 'Common/Utils';
|
|
|
|
import {getNotification} from 'Common/Translator';
|
|
|
|
import {HtmlEditor} from 'Common/HtmlEditor';
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
import Remote from 'Remote/User/Ajax';
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
import {getApp} from 'Helper/Apps/User';
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
import {view, ViewType} from 'Knoin/Knoin';
|
|
|
|
import {AbstractViewNext} from 'Knoin/AbstractViewNext';
|
|
|
|
|
|
|
|
@view({
|
|
|
|
name: 'View/Popup/Template',
|
|
|
|
type: ViewType.Popup,
|
|
|
|
templateID: 'PopupsTemplate'
|
|
|
|
})
|
|
|
|
class TemplatePopupView extends AbstractViewNext
|
2016-06-30 08:02:45 +08:00
|
|
|
{
|
2016-08-17 06:01:20 +08:00
|
|
|
constructor() {
|
|
|
|
super();
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.editor = null;
|
|
|
|
this.signatureDom = ko.observable(null);
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.id = ko.observable('');
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.name = ko.observable('');
|
|
|
|
this.name.error = ko.observable(false);
|
|
|
|
this.name.focus = ko.observable(false);
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.body = ko.observable('');
|
|
|
|
this.body.loading = ko.observable(false);
|
|
|
|
this.body.error = ko.observable(false);
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.name.subscribe(() => {
|
|
|
|
this.name.error(false);
|
|
|
|
});
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.body.subscribe(() => {
|
|
|
|
this.body.error(false);
|
|
|
|
});
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.submitRequest = ko.observable(false);
|
|
|
|
this.submitError = ko.observable('');
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.addTemplateCommand = createCommand(() => {
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.populateBodyFromEditor();
|
2015-02-12 05:39:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.name.error('' === trim(this.name()));
|
|
|
|
this.body.error('' === trim(this.body()) || ':HTML:' === trim(this.body()));
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
if (this.name.error() || this.body.error())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.submitRequest(true);
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
Remote.templateSetup((result, data) => {
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.submitRequest(false);
|
|
|
|
if (StorageResultType.Success === result && data)
|
2015-02-08 09:11:13 +08:00
|
|
|
{
|
2016-08-17 06:01:20 +08:00
|
|
|
if (data.Result)
|
|
|
|
{
|
|
|
|
getApp().templates();
|
|
|
|
this.cancelCommand();
|
|
|
|
}
|
|
|
|
else if (data.ErrorCode)
|
|
|
|
{
|
|
|
|
this.submitError(getNotification(data.ErrorCode));
|
|
|
|
}
|
2015-02-08 09:11:13 +08:00
|
|
|
}
|
2016-08-17 06:01:20 +08:00
|
|
|
else
|
2015-02-08 09:11:13 +08:00
|
|
|
{
|
2016-08-17 06:01:20 +08:00
|
|
|
this.submitError(getNotification(Notification.UnknownError));
|
2015-02-08 09:11:13 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
}, this.id(), this.name(), this.body());
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
return true;
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
}, () => !this.submitRequest());
|
|
|
|
}
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
clearPopup() {
|
|
|
|
this.id('');
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.name('');
|
|
|
|
this.name.error(false);
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.body('');
|
|
|
|
this.body.loading(false);
|
|
|
|
this.body.error(false);
|
2015-02-12 05:39:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.submitRequest(false);
|
|
|
|
this.submitError('');
|
2015-02-12 05:39:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
if (this.editor)
|
|
|
|
{
|
|
|
|
this.editor.setPlain('', false);
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
populateBodyFromEditor() {
|
|
|
|
if (this.editor)
|
|
|
|
{
|
|
|
|
this.body(this.editor.getDataWithHtmlMark());
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
editorSetBody(sBody) {
|
|
|
|
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
|
|
|
}
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
onShow(template) {
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.clearPopup();
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
if (template && template.id)
|
2016-06-30 08:02:45 +08:00
|
|
|
{
|
2016-08-17 06:01:20 +08:00
|
|
|
this.id(template.id);
|
|
|
|
this.name(template.name);
|
|
|
|
this.body(template.body);
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
if (template.populated)
|
|
|
|
{
|
|
|
|
this.editorSetBody(this.body());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.body.loading(true);
|
|
|
|
this.body.error(false);
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
Remote.templateGetById((result, data) => {
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.body.loading(false);
|
2015-02-12 05:39:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +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);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.body('');
|
|
|
|
this.body.error(true);
|
|
|
|
}
|
2015-02-12 05:39:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.editorSetBody(this.body());
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
}, this.id());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.editorSetBody('');
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
|
|
|
}
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
onShowWithDelay() {
|
|
|
|
this.name.focus(true);
|
|
|
|
}
|
|
|
|
}
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
module.exports = TemplatePopupView;
|