snappymail/dev/View/Popup/Template.js

199 lines
3.9 KiB
JavaScript
Raw Normal View History

2016-06-30 08:02:45 +08:00
var
_ = require('_'),
ko = require('ko'),
2016-06-30 08:02:45 +08:00
Enums = require('Common/Enums'),
Utils = require('Common/Utils'),
Translator = require('Common/Translator'),
HtmlEditor = require('Common/HtmlEditor'),
2016-06-30 08:02:45 +08:00
Remote = require('Remote/User/Ajax'),
2016-06-30 08:02:45 +08:00
kn = require('Knoin/Knoin'),
AbstractView = require('Knoin/AbstractView');
2016-06-30 08:02:45 +08:00
/**
* @constructor
* @extends AbstractView
*/
function TemplatePopupView()
{
AbstractView.call(this, 'Popups', 'PopupsTemplate');
2016-06-30 08:02:45 +08:00
this.editor = null;
this.signatureDom = ko.observable(null);
2016-06-30 08:02:45 +08:00
this.id = ko.observable('');
2016-06-30 08:02:45 +08:00
this.name = ko.observable('');
this.name.error = ko.observable(false);
this.name.focus = ko.observable(false);
2016-06-30 08:02:45 +08:00
this.body = ko.observable('');
this.body.loading = ko.observable(false);
this.body.error = ko.observable(false);
2016-06-30 08:02:45 +08:00
this.name.subscribe(function() {
this.name.error(false);
}, this);
2016-06-30 08:02:45 +08:00
this.body.subscribe(function() {
this.body.error(false);
}, this);
2016-06-30 08:02:45 +08:00
this.submitRequest = ko.observable(false);
this.submitError = ko.observable('');
2016-06-30 08:02:45 +08:00
this.addTemplateCommand = Utils.createCommand(this, function() {
2016-06-30 08:02:45 +08:00
this.populateBodyFromEditor();
2016-06-30 08:02:45 +08:00
this.name.error('' === Utils.trim(this.name()));
this.body.error('' === Utils.trim(this.body()) ||
':HTML:' === Utils.trim(this.body()));
2016-06-30 08:02:45 +08:00
if (this.name.error() || this.body.error())
{
return false;
}
2016-06-30 08:02:45 +08:00
this.submitRequest(true);
2016-06-30 08:02:45 +08:00
Remote.templateSetup(_.bind(function(sResult, oData) {
2016-06-30 08:02:45 +08:00
this.submitRequest(false);
if (Enums.StorageResultType.Success === sResult && oData)
{
if (oData.Result)
{
2016-06-30 08:02:45 +08:00
require('App/User').default.templates();
this.cancelCommand();
}
2016-06-30 08:02:45 +08:00
else if (oData.ErrorCode)
{
2016-06-30 08:02:45 +08:00
this.submitError(Translator.getNotification(oData.ErrorCode));
}
2016-06-30 08:02:45 +08:00
}
else
{
this.submitError(Translator.getNotification(Enums.Notification.UnknownError));
}
2016-06-30 08:02:45 +08:00
}, this), this.id(), this.name(), this.body());
2016-06-30 08:02:45 +08:00
return true;
2016-06-30 08:02:45 +08:00
}, function() {
return !this.submitRequest();
});
2016-06-30 08:02:45 +08:00
kn.constructorEnd(this);
}
2016-06-30 08:02:45 +08:00
kn.extendAsViewModel(['View/Popup/Template'], TemplatePopupView);
_.extend(TemplatePopupView.prototype, AbstractView.prototype);
2016-06-30 08:02:45 +08:00
TemplatePopupView.prototype.clearPopup = function()
{
this.id('');
2016-06-30 08:02:45 +08:00
this.name('');
this.name.error(false);
2016-06-30 08:02:45 +08:00
this.body('');
this.body.loading(false);
this.body.error(false);
2016-06-30 08:02:45 +08:00
this.submitRequest(false);
this.submitError('');
2016-06-30 08:02:45 +08:00
if (this.editor)
{
2016-06-30 08:02:45 +08:00
this.editor.setPlain('', false);
}
};
2016-06-30 08:02:45 +08:00
TemplatePopupView.prototype.populateBodyFromEditor = function()
{
if (this.editor)
{
2016-06-30 08:02:45 +08:00
this.body(this.editor.getDataWithHtmlMark());
}
};
2016-06-30 08:02:45 +08:00
TemplatePopupView.prototype.editorSetBody = function(sBody)
{
if (!this.editor && this.signatureDom())
{
var self = this;
2016-06-30 08:02:45 +08:00
this.editor = new HtmlEditor(self.signatureDom(), function() {
self.populateBodyFromEditor();
}, function() {
self.editor.setHtmlOrPlain(sBody);
});
}
else
{
this.editor.setHtmlOrPlain(sBody);
}
};
2016-06-30 08:02:45 +08:00
TemplatePopupView.prototype.onShow = function(oTemplate)
{
var self = this;
2016-06-30 08:02:45 +08:00
this.clearPopup();
2016-06-30 08:02:45 +08:00
if (oTemplate && oTemplate.id)
{
this.id(oTemplate.id);
this.name(oTemplate.name);
this.body(oTemplate.body);
2016-06-30 08:02:45 +08:00
if (oTemplate.populated)
{
self.editorSetBody(this.body());
}
else
{
this.body.loading(true);
self.body.error(false);
2016-06-30 08:02:45 +08:00
Remote.templateGetById(function(sResult, oData) {
2016-06-30 08:02:45 +08:00
self.body.loading(false);
2016-06-30 08:02:45 +08:00
if (Enums.StorageResultType.Success === sResult && oData && oData.Result &&
'Object/Template' === oData.Result['@Object'] && Utils.isNormal(oData.Result.Body))
{
oTemplate.body = oData.Result.Body;
oTemplate.populated = true;
2016-06-30 08:02:45 +08:00
self.body(oTemplate.body);
self.body.error(false);
}
else
{
self.body('');
self.body.error(true);
}
2016-06-30 08:02:45 +08:00
self.editorSetBody(self.body());
2016-06-30 08:02:45 +08:00
}, this.id());
}
}
else
{
2016-06-30 08:02:45 +08:00
self.editorSetBody('');
}
};
2016-06-30 08:02:45 +08:00
TemplatePopupView.prototype.onShowWithDelay = function()
{
this.name.focus(true);
};
2016-06-30 08:02:45 +08:00
module.exports = TemplatePopupView;