snappymail/dev/View/Popup/Identity.js

237 lines
4.9 KiB
JavaScript
Raw Normal View History

2014-09-05 06:49:03 +08:00
(function () {
2014-08-25 23:49:01 +08:00
'use strict';
2014-08-21 23:08:34 +08:00
var
_ = 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'),
Globals = require('Common/Globals'),
2014-09-05 06:49:03 +08:00
Utils = require('Common/Utils'),
Translator = require('Common/Translator'),
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'),
kn = require('Knoin/Knoin'),
AbstractView = require('Knoin/AbstractView')
2014-08-21 23:08:34 +08:00
;
2014-08-21 23:08:34 +08:00
/**
* @constructor
* @extends AbstractView
2014-08-21 23:08:34 +08:00
*/
function IdentityPopupView()
2014-08-21 23:08:34 +08:00
{
AbstractView.call(this, 'Popups', 'PopupsIdentity');
2014-08-21 23:08:34 +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);
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);
this.signature = ko.observable('');
this.signatureInsertBefore = ko.observable(false);
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('');
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 () {
this.populateSignatureFromEditor();
2014-08-21 23:08:34 +08:00
if (!this.email.hasError())
{
2014-08-21 23:08:34 +08:00
this.email.hasError('' === Utils.trim(this.email()));
}
if (this.email.hasError())
{
if (!this.owner())
{
2014-08-21 23:08:34 +08:00
this.email.focused(true);
}
2014-08-21 23:08:34 +08:00
return false;
}
2014-08-21 23:08:34 +08:00
if (this.replyTo.hasError())
{
2014-08-21 23:08:34 +08:00
this.replyTo.focused(true);
return false;
}
2014-08-21 23:08:34 +08:00
if (this.bcc.hasError())
{
this.bcc.focused(true);
return false;
}
2014-08-21 23:08:34 +08:00
this.submitRequest(true);
2014-08-21 23:08:34 +08:00
Remote.identityUpdate(_.bind(function (sResult, oData) {
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)
{
this.submitError(Translator.getNotification(oData.ErrorCode));
2014-08-21 23:08:34 +08:00
}
}
else
{
this.submitError(Translator.getNotification(Enums.Notification.UnknownError));
2014-08-21 23:08:34 +08:00
}
}, this), this.id, this.email(), this.name(), this.replyTo(), this.bcc(),
this.signature(), this.signatureInsertBefore());
2014-08-21 23:08:34 +08:00
return true;
2014-08-21 23:08:34 +08:00
}, function () {
return !this.submitRequest();
});
2014-08-21 23:08:34 +08:00
kn.constructorEnd(this);
}
kn.extendAsViewModel(['View/Popup/Identity', 'PopupsIdentityViewModel'], IdentityPopupView);
_.extend(IdentityPopupView.prototype, AbstractView.prototype);
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('');
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);
this.showBcc(false);
this.showReplyTo(false);
2014-08-21 23:08:34 +08:00
this.submitRequest(false);
this.submitError('');
if (this.editor)
{
this.editor.setPlain('', false);
}
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
*/
IdentityPopupView.prototype.onShow = function (oIdentity)
{
2014-08-21 23:08:34 +08:00
this.clearPopup();
2014-08-21 23:08:34 +08:00
if (oIdentity)
{
this.edit(true);
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());
this.signature(oIdentity.signature());
this.signatureInsertBefore(oIdentity.signatureInsertBefore());
2014-08-21 23:08:34 +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-16 05:55:59 +08:00
IdentityPopupView.prototype.onShowWithDelay = function ()
{
if (!this.owner() && !Globals.bMobile)
{
2015-02-16 05:55:59 +08:00
this.email.focused(true);
}
};
2015-02-16 05:55:59 +08:00
IdentityPopupView.prototype.onHideWithDelay = function ()
{
2015-02-16 05:55:59 +08:00
this.clearPopup();
2014-08-21 23:08:34 +08:00
};
module.exports = IdentityPopupView;
2014-08-21 23:08:34 +08:00
2014-09-05 06:49:03 +08:00
}());