snappymail/dev/Settings/App/SettingsIdentities.js

234 lines
5.2 KiB
JavaScript
Raw Normal View History

2014-08-21 23:08:34 +08:00
/* RainLoop Webmail (c) RainLoop Team | Licensed under CC BY-NC-SA 3.0 */
2014-08-25 23:49:01 +08:00
(function (module, require) {
'use strict';
2014-08-21 23:08:34 +08:00
var
2014-08-25 23:49:01 +08:00
ko = require('ko'),
2014-08-26 23:24:47 +08:00
_ = require('_'),
2014-08-25 15:10:51 +08:00
2014-08-25 23:49:01 +08:00
Enums = require('Enums'),
Utils = require('Utils'),
2014-08-27 23:59:44 +08:00
HtmlEditor = require('HtmlEditor'),
2014-08-21 23:08:34 +08:00
2014-08-27 23:59:44 +08:00
Data = require('Storage:RainLoop:Data'),
Remote = require('Storage:RainLoop:Remote')
2014-08-21 23:08:34 +08:00
;
/**
* @constructor
*/
function SettingsIdentities()
{
this.editor = null;
this.defautOptionsAfterRender = Utils.defautOptionsAfterRender;
2014-08-22 23:08:56 +08:00
this.accountEmail = Data.accountEmail;
this.displayName = Data.displayName;
this.signature = Data.signature;
this.signatureToAll = Data.signatureToAll;
this.replyTo = Data.replyTo;
2014-08-21 23:08:34 +08:00
this.signatureDom = ko.observable(null);
this.defaultIdentityIDTrigger = ko.observable(Enums.SaveSettingsStep.Idle);
this.displayNameTrigger = ko.observable(Enums.SaveSettingsStep.Idle);
this.replyTrigger = ko.observable(Enums.SaveSettingsStep.Idle);
this.signatureTrigger = ko.observable(Enums.SaveSettingsStep.Idle);
2014-08-22 23:08:56 +08:00
this.identities = Data.identities;
this.defaultIdentityID = Data.defaultIdentityID;
2014-08-21 23:08:34 +08:00
this.identitiesOptions = ko.computed(function () {
var
aList = this.identities(),
aResult = []
;
if (0 < aList.length)
{
aResult.push({
'id': this.accountEmail.peek(),
'name': this.formattedAccountIdentity(),
'seporator': false
});
aResult.push({
'id': '---',
'name': '---',
'seporator': true,
'disabled': true
});
_.each(aList, function (oItem) {
aResult.push({
'id': oItem.id,
'name': oItem.formattedNameForEmail(),
'seporator': false
});
});
}
return aResult;
}, this);
this.processText = ko.computed(function () {
2014-08-22 23:08:56 +08:00
return Data.identitiesLoading() ? Utils.i18n('SETTINGS_IDENTITIES/LOADING_PROCESS') : '';
2014-08-21 23:08:34 +08:00
}, this);
this.visibility = ko.computed(function () {
return '' === this.processText() ? 'hidden' : 'visible';
}, this);
this.identityForDeletion = ko.observable(null).extend({'falseTimeout': 3000}).extend({'toggleSubscribe': [this,
function (oPrev) {
if (oPrev)
{
oPrev.deleteAccess(false);
}
}, function (oNext) {
if (oNext)
{
oNext.deleteAccess(true);
}
}
]});
}
/**
*
* @return {string}
*/
SettingsIdentities.prototype.formattedAccountIdentity = function ()
{
var
sDisplayName = this.displayName.peek(),
sEmail = this.accountEmail.peek()
;
return '' === sDisplayName ? sEmail : '"' + Utils.quoteName(sDisplayName) + '" <' + sEmail + '>';
};
SettingsIdentities.prototype.addNewIdentity = function ()
{
2014-08-27 23:59:44 +08:00
require('App:Knoin').showScreenPopup(require('View:Popup:Identity'));
2014-08-21 23:08:34 +08:00
};
SettingsIdentities.prototype.editIdentity = function (oIdentity)
{
2014-08-27 23:59:44 +08:00
require('App:Knoin').showScreenPopup(require('View:Popup:Identity'), [oIdentity]);
2014-08-21 23:08:34 +08:00
};
/**
* @param {IdentityModel} oIdentityToRemove
*/
SettingsIdentities.prototype.deleteIdentity = function (oIdentityToRemove)
{
if (oIdentityToRemove && oIdentityToRemove.deleteAccess())
{
this.identityForDeletion(null);
var
fRemoveFolder = function (oIdentity) {
return oIdentityToRemove === oIdentity;
}
;
if (oIdentityToRemove)
{
this.identities.remove(fRemoveFolder);
Remote.identityDelete(function () {
2014-08-27 23:59:44 +08:00
require('App:RainLoop').accountsAndIdentities();
2014-08-21 23:08:34 +08:00
}, oIdentityToRemove.id);
}
}
};
SettingsIdentities.prototype.onFocus = function ()
{
if (!this.editor && this.signatureDom())
{
var
self = this,
2014-08-22 23:08:56 +08:00
sSignature = Data.signature()
2014-08-21 23:08:34 +08:00
;
2014-08-27 23:59:44 +08:00
this.editor = new HtmlEditor(self.signatureDom(), function () {
2014-08-22 23:08:56 +08:00
Data.signature(
2014-08-21 23:08:34 +08:00
(self.editor.isHtml() ? ':HTML:' : '') + self.editor.getData()
);
}, function () {
if (':HTML:' === sSignature.substr(0, 6))
{
self.editor.setHtml(sSignature.substr(6), false);
}
else
{
self.editor.setPlain(sSignature, false);
}
});
}
};
SettingsIdentities.prototype.onBuild = function (oDom)
{
var self = this;
oDom
.on('click', '.identity-item .e-action', function () {
var oIdentityItem = ko.dataFor(this);
if (oIdentityItem)
{
self.editIdentity(oIdentityItem);
}
})
;
_.delay(function () {
var
f1 = Utils.settingsSaveHelperSimpleFunction(self.displayNameTrigger, self),
f2 = Utils.settingsSaveHelperSimpleFunction(self.replyTrigger, self),
f3 = Utils.settingsSaveHelperSimpleFunction(self.signatureTrigger, self),
f4 = Utils.settingsSaveHelperSimpleFunction(self.defaultIdentityIDTrigger, self)
;
2014-08-22 23:08:56 +08:00
Data.defaultIdentityID.subscribe(function (sValue) {
2014-08-21 23:08:34 +08:00
Remote.saveSettings(f4, {
'DefaultIdentityID': sValue
});
});
2014-08-22 23:08:56 +08:00
Data.displayName.subscribe(function (sValue) {
2014-08-21 23:08:34 +08:00
Remote.saveSettings(f1, {
'DisplayName': sValue
});
});
2014-08-22 23:08:56 +08:00
Data.replyTo.subscribe(function (sValue) {
2014-08-21 23:08:34 +08:00
Remote.saveSettings(f2, {
'ReplyTo': sValue
});
});
2014-08-22 23:08:56 +08:00
Data.signature.subscribe(function (sValue) {
2014-08-21 23:08:34 +08:00
Remote.saveSettings(f3, {
'Signature': sValue
});
});
2014-08-22 23:08:56 +08:00
Data.signatureToAll.subscribe(function (bValue) {
2014-08-21 23:08:34 +08:00
Remote.saveSettings(null, {
'SignatureToAll': bValue ? '1' : '0'
});
});
}, 50);
};
module.exports = SettingsIdentities;
2014-08-25 23:49:01 +08:00
}(module, require));