2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
var
|
|
|
|
_ = require('_'),
|
|
|
|
ko = require('ko'),
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
Enums = require('Common/Enums'),
|
|
|
|
Utils = require('Common/Utils'),
|
|
|
|
Translator = require('Common/Translator'),
|
2016-08-09 06:54:25 +08:00
|
|
|
HtmlEditor = require('Common/HtmlEditor').default,
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
Remote = require('Remote/User/Ajax'),
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
kn = require('Knoin/Knoin'),
|
|
|
|
AbstractView = require('Knoin/AbstractView');
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @extends AbstractView
|
|
|
|
*/
|
|
|
|
function TemplatePopupView()
|
|
|
|
{
|
|
|
|
AbstractView.call(this, 'Popups', 'PopupsTemplate');
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
this.editor = null;
|
|
|
|
this.signatureDom = ko.observable(null);
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
this.id = ko.observable('');
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +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-06-30 08:02:45 +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-06-30 08:02:45 +08:00
|
|
|
this.name.subscribe(function() {
|
|
|
|
this.name.error(false);
|
|
|
|
}, this);
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
this.body.subscribe(function() {
|
|
|
|
this.body.error(false);
|
|
|
|
}, this);
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
this.submitRequest = ko.observable(false);
|
|
|
|
this.submitError = ko.observable('');
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
this.addTemplateCommand = Utils.createCommand(this, function() {
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
this.populateBodyFromEditor();
|
2015-02-12 05:39:27 +08:00
|
|
|
|
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()));
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
if (this.name.error() || this.body.error())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
this.submitRequest(true);
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
Remote.templateSetup(_.bind(function(sResult, oData) {
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
this.submitRequest(false);
|
|
|
|
if (Enums.StorageResultType.Success === sResult && oData)
|
|
|
|
{
|
|
|
|
if (oData.Result)
|
2015-02-08 09:11:13 +08:00
|
|
|
{
|
2016-06-30 08:02:45 +08:00
|
|
|
require('App/User').default.templates();
|
|
|
|
this.cancelCommand();
|
2015-02-08 09:11:13 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
else if (oData.ErrorCode)
|
2015-02-08 09:11:13 +08:00
|
|
|
{
|
2016-06-30 08:02:45 +08:00
|
|
|
this.submitError(Translator.getNotification(oData.ErrorCode));
|
2015-02-08 09:11:13 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.submitError(Translator.getNotification(Enums.Notification.UnknownError));
|
|
|
|
}
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
}, this), this.id(), this.name(), this.body());
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
return true;
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
}, function() {
|
|
|
|
return !this.submitRequest();
|
|
|
|
});
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
kn.constructorEnd(this);
|
|
|
|
}
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
kn.extendAsViewModel(['View/Popup/Template'], TemplatePopupView);
|
|
|
|
_.extend(TemplatePopupView.prototype, AbstractView.prototype);
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
TemplatePopupView.prototype.clearPopup = function()
|
|
|
|
{
|
|
|
|
this.id('');
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
this.name('');
|
|
|
|
this.name.error(false);
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
this.body('');
|
|
|
|
this.body.loading(false);
|
|
|
|
this.body.error(false);
|
2015-02-12 05:39:27 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
this.submitRequest(false);
|
|
|
|
this.submitError('');
|
2015-02-12 05:39:27 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
if (this.editor)
|
2015-02-12 05:39:27 +08:00
|
|
|
{
|
2016-06-30 08:02:45 +08:00
|
|
|
this.editor.setPlain('', false);
|
|
|
|
}
|
|
|
|
};
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
TemplatePopupView.prototype.populateBodyFromEditor = function()
|
|
|
|
{
|
|
|
|
if (this.editor)
|
2015-02-12 05:39:27 +08:00
|
|
|
{
|
2016-06-30 08:02:45 +08:00
|
|
|
this.body(this.editor.getDataWithHtmlMark());
|
|
|
|
}
|
|
|
|
};
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
TemplatePopupView.prototype.editorSetBody = function(sBody)
|
|
|
|
{
|
2016-07-16 05:29:42 +08:00
|
|
|
var self = this;
|
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
if (!this.editor && this.signatureDom())
|
2015-02-08 09:11:13 +08:00
|
|
|
{
|
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);
|
|
|
|
}
|
|
|
|
};
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
TemplatePopupView.prototype.onShow = function(oTemplate)
|
|
|
|
{
|
|
|
|
var self = this;
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
this.clearPopup();
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
if (oTemplate && oTemplate.id)
|
|
|
|
{
|
|
|
|
this.id(oTemplate.id);
|
|
|
|
this.name(oTemplate.name);
|
|
|
|
this.body(oTemplate.body);
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
if (oTemplate.populated)
|
|
|
|
{
|
|
|
|
self.editorSetBody(this.body());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.body.loading(true);
|
|
|
|
self.body.error(false);
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
Remote.templateGetById(function(sResult, oData) {
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
self.body.loading(false);
|
2015-02-08 09:11:13 +08:00
|
|
|
|
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;
|
2015-02-12 05:39:27 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
self.body(oTemplate.body);
|
|
|
|
self.body.error(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
self.body('');
|
|
|
|
self.body.error(true);
|
|
|
|
}
|
2015-02-12 05:39:27 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
self.editorSetBody(self.body());
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
}, this.id());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2015-02-08 09:11:13 +08:00
|
|
|
{
|
2016-06-30 08:02:45 +08:00
|
|
|
self.editorSetBody('');
|
|
|
|
}
|
|
|
|
};
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
TemplatePopupView.prototype.onShowWithDelay = function()
|
|
|
|
{
|
|
|
|
this.name.focus(true);
|
|
|
|
};
|
2015-02-08 09:11:13 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
module.exports = TemplatePopupView;
|