2013-11-16 06:21:12 +08:00
|
|
|
|
2014-09-05 06:49:03 +08:00
|
|
|
(function () {
|
2014-08-25 23:49:01 +08:00
|
|
|
|
|
|
|
'use strict';
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
var
|
2014-09-02 08:15:31 +08:00
|
|
|
_ = require('_'),
|
2014-08-25 23:49:01 +08:00
|
|
|
ko = require('ko'),
|
2014-08-25 15:10:51 +08:00
|
|
|
|
2014-09-05 06:49:03 +08:00
|
|
|
Enums = require('Common/Enums'),
|
2015-02-17 21:16:38 +08:00
|
|
|
Globals = require('Common/Globals'),
|
2014-09-05 06:49:03 +08:00
|
|
|
Utils = require('Common/Utils'),
|
2015-01-26 07:09:22 +08:00
|
|
|
Translator = require('Common/Translator'),
|
2015-02-06 23:26:20 +08:00
|
|
|
HtmlEditor = require('Common/HtmlEditor'),
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2015-02-23 00:35:17 +08:00
|
|
|
Remote = require('Remote/User/Ajax'),
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-09-06 05:44:29 +08:00
|
|
|
kn = require('Knoin/Knoin'),
|
|
|
|
AbstractView = require('Knoin/AbstractView')
|
2014-08-21 23:08:34 +08:00
|
|
|
;
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
/**
|
|
|
|
* @constructor
|
2014-09-06 05:44:29 +08:00
|
|
|
* @extends AbstractView
|
2014-08-21 23:08:34 +08:00
|
|
|
*/
|
2014-09-06 05:44:29 +08:00
|
|
|
function IdentityPopupView()
|
2014-08-21 23:08:34 +08:00
|
|
|
{
|
2014-09-06 05:44:29 +08:00
|
|
|
AbstractView.call(this, 'Popups', 'PopupsIdentity');
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2015-02-08 09:11:13 +08:00
|
|
|
var self = this;
|
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
this.id = '';
|
|
|
|
this.edit = ko.observable(false);
|
|
|
|
this.owner = ko.observable(false);
|
|
|
|
|
2015-02-06 23:26:20 +08:00
|
|
|
this.editor = null;
|
|
|
|
this.signatureDom = ko.observable(null);
|
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
this.email = ko.observable('').validateEmail();
|
|
|
|
this.email.focused = ko.observable(false);
|
|
|
|
this.name = ko.observable('');
|
|
|
|
this.name.focused = ko.observable(false);
|
|
|
|
this.replyTo = ko.observable('').validateSimpleEmail();
|
|
|
|
this.replyTo.focused = ko.observable(false);
|
|
|
|
this.bcc = ko.observable('').validateSimpleEmail();
|
|
|
|
this.bcc.focused = ko.observable(false);
|
|
|
|
|
2015-02-06 23:26:20 +08:00
|
|
|
this.signature = ko.observable('');
|
2015-02-08 09:11:13 +08:00
|
|
|
this.signatureInsertBefore = ko.observable(false);
|
2015-02-06 23:26:20 +08:00
|
|
|
|
2015-02-08 09:11:13 +08:00
|
|
|
this.showBcc = ko.observable(false);
|
|
|
|
this.showReplyTo = ko.observable(false);
|
2014-08-21 23:08:34 +08:00
|
|
|
|
|
|
|
this.submitRequest = ko.observable(false);
|
|
|
|
this.submitError = ko.observable('');
|
|
|
|
|
2015-02-08 09:11:13 +08:00
|
|
|
this.bcc.subscribe(function (aValue) {
|
|
|
|
if (false === self.showBcc() && 0 < aValue.length)
|
|
|
|
{
|
|
|
|
self.showBcc(true);
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
this.replyTo.subscribe(function (aValue) {
|
|
|
|
if (false === self.showReplyTo() && 0 < aValue.length)
|
|
|
|
{
|
|
|
|
self.showReplyTo(true);
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
this.addOrEditIdentityCommand = Utils.createCommand(this, function () {
|
|
|
|
|
2015-02-08 09:11:13 +08:00
|
|
|
this.populateSignatureFromEditor();
|
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
if (!this.email.hasError())
|
2013-11-16 06:21:12 +08:00
|
|
|
{
|
2014-08-21 23:08:34 +08:00
|
|
|
this.email.hasError('' === Utils.trim(this.email()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.email.hasError())
|
|
|
|
{
|
|
|
|
if (!this.owner())
|
2013-11-16 06:21:12 +08:00
|
|
|
{
|
2014-08-21 23:08:34 +08:00
|
|
|
this.email.focused(true);
|
2013-11-16 06:21:12 +08:00
|
|
|
}
|
2014-08-21 23:08:34 +08:00
|
|
|
|
|
|
|
return false;
|
2013-11-16 06:21:12 +08:00
|
|
|
}
|
2014-08-21 23:08:34 +08:00
|
|
|
|
|
|
|
if (this.replyTo.hasError())
|
2013-11-16 06:21:12 +08:00
|
|
|
{
|
2014-08-21 23:08:34 +08:00
|
|
|
this.replyTo.focused(true);
|
|
|
|
return false;
|
2013-11-16 06:21:12 +08:00
|
|
|
}
|
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
if (this.bcc.hasError())
|
|
|
|
{
|
|
|
|
this.bcc.focused(true);
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
this.submitRequest(true);
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
Remote.identityUpdate(_.bind(function (sResult, oData) {
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
this.submitRequest(false);
|
|
|
|
if (Enums.StorageResultType.Success === sResult && oData)
|
|
|
|
{
|
|
|
|
if (oData.Result)
|
|
|
|
{
|
2014-10-18 21:43:44 +08:00
|
|
|
require('App/User').accountsAndIdentities();
|
2014-08-21 23:08:34 +08:00
|
|
|
this.cancelCommand();
|
|
|
|
}
|
|
|
|
else if (oData.ErrorCode)
|
|
|
|
{
|
2015-01-26 07:09:22 +08:00
|
|
|
this.submitError(Translator.getNotification(oData.ErrorCode));
|
2014-08-21 23:08:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-26 07:09:22 +08:00
|
|
|
this.submitError(Translator.getNotification(Enums.Notification.UnknownError));
|
2014-08-21 23:08:34 +08:00
|
|
|
}
|
2013-12-09 23:16:58 +08:00
|
|
|
|
2015-02-08 09:11:13 +08:00
|
|
|
}, this), this.id, this.email(), this.name(), this.replyTo(), this.bcc(),
|
|
|
|
this.signature(), this.signatureInsertBefore());
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
return true;
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
}, function () {
|
|
|
|
return !this.submitRequest();
|
|
|
|
});
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
kn.constructorEnd(this);
|
|
|
|
}
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-09-06 05:44:29 +08:00
|
|
|
kn.extendAsViewModel(['View/Popup/Identity', 'PopupsIdentityViewModel'], IdentityPopupView);
|
|
|
|
_.extend(IdentityPopupView.prototype, AbstractView.prototype);
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-09-06 05:44:29 +08:00
|
|
|
IdentityPopupView.prototype.clearPopup = function ()
|
2014-08-21 23:08:34 +08:00
|
|
|
{
|
|
|
|
this.id = '';
|
|
|
|
this.edit(false);
|
|
|
|
this.owner(false);
|
|
|
|
|
|
|
|
this.name('');
|
|
|
|
this.email('');
|
|
|
|
this.replyTo('');
|
|
|
|
this.bcc('');
|
2015-02-08 09:11:13 +08:00
|
|
|
this.signature('');
|
|
|
|
this.signatureInsertBefore(false);
|
2014-08-21 23:08:34 +08:00
|
|
|
|
|
|
|
this.email.hasError(false);
|
|
|
|
this.replyTo.hasError(false);
|
|
|
|
this.bcc.hasError(false);
|
|
|
|
|
2015-02-08 09:11:13 +08:00
|
|
|
this.showBcc(false);
|
|
|
|
this.showReplyTo(false);
|
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
this.submitRequest(false);
|
|
|
|
this.submitError('');
|
2015-02-06 23:26:20 +08:00
|
|
|
|
|
|
|
if (this.editor)
|
|
|
|
{
|
2015-02-08 09:11:13 +08:00
|
|
|
this.editor.setPlain('', false);
|
2015-02-06 23:26:20 +08:00
|
|
|
}
|
2014-08-21 23:08:34 +08:00
|
|
|
};
|
|
|
|
|
2015-02-16 05:55:59 +08:00
|
|
|
IdentityPopupView.prototype.populateSignatureFromEditor = function ()
|
|
|
|
{
|
|
|
|
if (this.editor)
|
|
|
|
{
|
|
|
|
this.signature(this.editor.getDataWithHtmlMark());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
IdentityPopupView.prototype.editorSetSignature = function (sSignature)
|
|
|
|
{
|
|
|
|
if (!this.editor && this.signatureDom())
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
this.editor = new HtmlEditor(self.signatureDom(), function () {
|
|
|
|
self.populateSignatureFromEditor();
|
|
|
|
}, function () {
|
|
|
|
self.editor.setHtmlOrPlain(sSignature);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.editor.setHtmlOrPlain(sSignature);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
/**
|
|
|
|
* @param {?IdentityModel} oIdentity
|
|
|
|
*/
|
2014-09-06 05:44:29 +08:00
|
|
|
IdentityPopupView.prototype.onShow = function (oIdentity)
|
2013-11-16 06:21:12 +08:00
|
|
|
{
|
2014-08-21 23:08:34 +08:00
|
|
|
this.clearPopup();
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
if (oIdentity)
|
|
|
|
{
|
|
|
|
this.edit(true);
|
|
|
|
|
2015-02-06 23:26:20 +08:00
|
|
|
this.id = oIdentity.id();
|
2014-08-21 23:08:34 +08:00
|
|
|
this.name(oIdentity.name());
|
|
|
|
this.email(oIdentity.email());
|
|
|
|
this.replyTo(oIdentity.replyTo());
|
|
|
|
this.bcc(oIdentity.bcc());
|
2015-02-08 09:11:13 +08:00
|
|
|
this.signature(oIdentity.signature());
|
|
|
|
this.signatureInsertBefore(oIdentity.signatureInsertBefore());
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2015-02-06 23:26:20 +08:00
|
|
|
this.owner(this.id === '');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.id = Utils.fakeMd5();
|
|
|
|
}
|
|
|
|
|
2015-02-16 05:55:59 +08:00
|
|
|
this.editorSetSignature(this.signature());
|
2015-02-06 23:26:20 +08:00
|
|
|
};
|
|
|
|
|
2015-02-16 05:55:59 +08:00
|
|
|
IdentityPopupView.prototype.onShowWithDelay = function ()
|
2015-02-06 23:26:20 +08:00
|
|
|
{
|
2015-02-17 21:16:38 +08:00
|
|
|
if (!this.owner() && !Globals.bMobile)
|
2015-02-08 09:11:13 +08:00
|
|
|
{
|
2015-02-16 05:55:59 +08:00
|
|
|
this.email.focused(true);
|
2015-02-08 09:11:13 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-16 05:55:59 +08:00
|
|
|
IdentityPopupView.prototype.onHideWithDelay = function ()
|
2013-11-16 06:21:12 +08:00
|
|
|
{
|
2015-02-16 05:55:59 +08:00
|
|
|
this.clearPopup();
|
2014-08-21 23:08:34 +08:00
|
|
|
};
|
|
|
|
|
2014-09-06 05:44:29 +08:00
|
|
|
module.exports = IdentityPopupView;
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2014-09-05 06:49:03 +08:00
|
|
|
}());
|