snappymail/dev/View/Popup/Domain.js

421 lines
10 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
2014-08-25 23:49:01 +08:00
_ = require('_'),
ko = require('ko'),
2014-08-25 15:10:51 +08:00
2014-09-05 06:49:03 +08:00
Enums = require('Common/Enums'),
Consts = require('Common/Consts'),
Utils = require('Common/Utils'),
2014-08-21 23:08:34 +08:00
Remote = require('Storage/Admin/Remote'),
2014-08-21 23:08:34 +08:00
kn = require('Knoin/Knoin'),
AbstractView = require('Knoin/AbstractView')
2014-08-21 23:08:34 +08:00
;
/**
* @constructor
* @extends AbstractView
2014-08-21 23:08:34 +08:00
*/
function DomainPopupView()
2014-08-21 23:08:34 +08:00
{
AbstractView.call(this, 'Popups', 'PopupsDomain');
2014-08-21 23:08:34 +08:00
this.edit = ko.observable(false);
this.saving = ko.observable(false);
this.savingError = ko.observable('');
2014-11-20 05:32:20 +08:00
this.page = ko.observable('main');
this.sieveSettings = ko.observable(false);
2014-08-21 23:08:34 +08:00
this.testing = ko.observable(false);
this.testingDone = ko.observable(false);
this.testingImapError = ko.observable(false);
this.testingSmtpError = ko.observable(false);
this.testingImapErrorDesc = ko.observable('');
this.testingSmtpErrorDesc = ko.observable('');
this.testingImapError.subscribe(function (bValue) {
if (!bValue)
{
this.testingImapErrorDesc('');
}
}, this);
this.testingSmtpError.subscribe(function (bValue) {
if (!bValue)
{
this.testingSmtpErrorDesc('');
}
}, this);
this.testingImapErrorDesc = ko.observable('');
this.testingSmtpErrorDesc = ko.observable('');
this.imapServerFocus = ko.observable(false);
2014-11-20 05:32:20 +08:00
this.sieveServerFocus = ko.observable(false);
2014-08-21 23:08:34 +08:00
this.smtpServerFocus = ko.observable(false);
this.name = ko.observable('');
this.name.focused = ko.observable(false);
2014-11-20 05:32:20 +08:00
this.allowSieve = ko.observable(false);
2014-08-21 23:08:34 +08:00
this.imapServer = ko.observable('');
this.imapPort = ko.observable('' + Consts.Values.ImapDefaulPort);
this.imapSecure = ko.observable(Enums.ServerSecure.None);
this.imapShortLogin = ko.observable(false);
2014-11-20 05:32:20 +08:00
this.useSieve = ko.observable(false);
this.useImapServerForSieve = ko.observable(true);
this.sieveServer = ko.observable('');
this.sievePort = ko.observable('' + Consts.Values.SieveDefaulPort);
this.sieveSecure = ko.observable(Enums.ServerSecure.None);
2014-08-21 23:08:34 +08:00
this.smtpServer = ko.observable('');
this.smtpPort = ko.observable('' + Consts.Values.SmtpDefaulPort);
this.smtpSecure = ko.observable(Enums.ServerSecure.None);
this.smtpShortLogin = ko.observable(false);
this.smtpAuth = ko.observable(true);
2014-11-13 06:09:14 +08:00
this.smtpPhpMail = ko.observable(false);
2014-08-21 23:08:34 +08:00
this.whiteList = ko.observable('');
2014-10-14 06:39:23 +08:00
this.enableSmartPorts = ko.observable(false);
2014-08-21 23:08:34 +08:00
this.headerText = ko.computed(function () {
var sName = this.name();
return this.edit() ? 'Edit Domain "' + sName + '"' :
'Add Domain' + ('' === sName ? '' : ' "' + sName + '"');
}, this);
this.domainIsComputed = ko.computed(function () {
2014-11-20 05:32:20 +08:00
var
bPhpMail = this.smtpPhpMail(),
bAllowSieve = this.allowSieve(),
bUseSieve = this.useSieve(),
bIseImapServerForSieve = this.useImapServerForSieve()
;
2014-08-21 23:08:34 +08:00
return '' !== this.name() &&
'' !== this.imapServer() &&
'' !== this.imapPort() &&
2014-11-20 05:32:20 +08:00
(bAllowSieve && bUseSieve ? (bIseImapServerForSieve ? true : ('' !== this.sieveServer() && '' !== this.sievePort())) : true) &&
2014-11-18 02:38:45 +08:00
(('' !== this.smtpServer() && '' !== this.smtpPort()) || bPhpMail);
2014-11-20 05:32:20 +08:00
2014-08-21 23:08:34 +08:00
}, this);
this.canBeTested = ko.computed(function () {
return !this.testing() && this.domainIsComputed();
}, this);
this.canBeSaved = ko.computed(function () {
return !this.saving() && this.domainIsComputed();
}, this);
this.createOrAddCommand = Utils.createCommand(this, function () {
this.saving(true);
Remote.createOrUpdateDomain(
_.bind(this.onDomainCreateOrSaveResponse, this),
!this.edit(),
this.name(),
2014-11-20 05:32:20 +08:00
2014-08-21 23:08:34 +08:00
this.imapServer(),
Utils.pInt(this.imapPort()),
this.imapSecure(),
this.imapShortLogin(),
2014-11-20 05:32:20 +08:00
this.useSieve(),
this.useImapServerForSieve(),
this.sieveServer(),
Utils.pInt(this.sievePort()),
this.sieveSecure(),
2014-08-21 23:08:34 +08:00
this.smtpServer(),
Utils.pInt(this.smtpPort()),
this.smtpSecure(),
this.smtpShortLogin(),
this.smtpAuth(),
2014-11-13 06:09:14 +08:00
this.smtpPhpMail(),
2014-11-20 05:32:20 +08:00
2014-08-21 23:08:34 +08:00
this.whiteList()
);
}, this.canBeSaved);
this.testConnectionCommand = Utils.createCommand(this, function () {
2014-11-20 05:32:20 +08:00
this.page('main');
this.sieveSettings(false);
2014-08-21 23:08:34 +08:00
this.testingDone(false);
this.testingImapError(false);
this.testingSmtpError(false);
this.testing(true);
2014-11-20 05:32:20 +08:00
2014-08-21 23:08:34 +08:00
Remote.testConnectionForDomain(
_.bind(this.onTestConnectionResponse, this),
this.name(),
2014-11-20 05:32:20 +08:00
2014-08-21 23:08:34 +08:00
this.imapServer(),
Utils.pInt(this.imapPort()),
this.imapSecure(),
2014-11-20 05:32:20 +08:00
this.useSieve(),
this.useImapServerForSieve(),
this.sieveServer(),
Utils.pInt(this.sievePort()),
this.sieveSecure(),
2014-08-21 23:08:34 +08:00
this.smtpServer(),
Utils.pInt(this.smtpPort()),
this.smtpSecure(),
2014-11-13 06:09:14 +08:00
this.smtpAuth(),
this.smtpPhpMail()
2014-08-21 23:08:34 +08:00
);
}, this.canBeTested);
this.whiteListCommand = Utils.createCommand(this, function () {
2014-11-20 05:32:20 +08:00
this.page('white-list');
2014-08-21 23:08:34 +08:00
});
2014-11-20 05:32:20 +08:00
this.backCommand = Utils.createCommand(this, function () {
this.page('main');
});
this.sieveCommand = Utils.createCommand(this, function () {
this.sieveSettings(!this.sieveSettings());
this.clearTesting();
});
this.page.subscribe(function () {
this.sieveSettings(false);
}, this);
2014-08-21 23:08:34 +08:00
// smart form improvements
this.imapServerFocus.subscribe(function (bValue) {
if (bValue && '' !== this.name() && '' === this.imapServer())
{
this.imapServer(this.name().replace(/[.]?[*][.]?/g, ''));
}
}, this);
2014-11-20 05:32:20 +08:00
this.sieveServerFocus.subscribe(function (bValue) {
if (bValue && '' !== this.name() && '' === this.sieveServer())
{
this.sieveServer(this.name().replace(/[.]?[*][.]?/g, ''));
}
}, this);
2014-08-21 23:08:34 +08:00
this.smtpServerFocus.subscribe(function (bValue) {
if (bValue && '' !== this.imapServer() && '' === this.smtpServer())
{
this.smtpServer(this.imapServer().replace(/imap/ig, 'smtp'));
}
}, this);
this.imapSecure.subscribe(function (sValue) {
2014-10-14 06:39:23 +08:00
if (this.enableSmartPorts())
2014-08-21 23:08:34 +08:00
{
2014-10-14 06:39:23 +08:00
var iPort = Utils.pInt(this.imapPort());
sValue = Utils.pString(sValue);
switch (sValue)
{
case '0':
if (993 === iPort)
{
this.imapPort('143');
}
break;
case '1':
if (143 === iPort)
{
this.imapPort('993');
}
break;
}
2014-08-21 23:08:34 +08:00
}
}, this);
this.smtpSecure.subscribe(function (sValue) {
2014-10-14 06:39:23 +08:00
if (this.enableSmartPorts())
2014-08-21 23:08:34 +08:00
{
2014-10-14 06:39:23 +08:00
var iPort = Utils.pInt(this.smtpPort());
sValue = Utils.pString(sValue);
switch (sValue)
{
case '0':
if (465 === iPort || 587 === iPort)
{
this.smtpPort('25');
}
break;
case '1':
if (25 === iPort || 587 === iPort)
{
this.smtpPort('465');
}
break;
case '2':
if (25 === iPort || 465 === iPort)
{
this.smtpPort('587');
}
break;
}
2014-08-21 23:08:34 +08:00
}
}, this);
kn.constructorEnd(this);
}
kn.extendAsViewModel(['View/Popup/Domain', 'PopupsDomainViewModel'], DomainPopupView);
_.extend(DomainPopupView.prototype, AbstractView.prototype);
2014-08-21 23:08:34 +08:00
DomainPopupView.prototype.onTestConnectionResponse = function (sResult, oData)
2014-08-21 23:08:34 +08:00
{
this.testing(false);
if (Enums.StorageResultType.Success === sResult && oData.Result)
{
2014-08-21 23:08:34 +08:00
this.testingDone(true);
this.testingImapError(true !== oData.Result.Imap);
this.testingSmtpError(true !== oData.Result.Smtp);
if (this.testingImapError() && oData.Result.Imap)
{
this.testingImapErrorDesc(oData.Result.Imap);
}
if (this.testingSmtpError() && oData.Result.Smtp)
{
this.testingSmtpErrorDesc(oData.Result.Smtp);
}
}
2014-08-21 23:08:34 +08:00
else
{
2014-08-21 23:08:34 +08:00
this.testingImapError(true);
this.testingSmtpError(true);
}
2014-08-21 23:08:34 +08:00
};
DomainPopupView.prototype.onDomainCreateOrSaveResponse = function (sResult, oData)
2014-08-21 23:08:34 +08:00
{
this.saving(false);
if (Enums.StorageResultType.Success === sResult && oData)
{
2014-08-21 23:08:34 +08:00
if (oData.Result)
{
require('App/Admin').reloadDomainList();
2014-08-21 23:08:34 +08:00
this.closeCommand();
}
else if (Enums.Notification.DomainAlreadyExists === oData.ErrorCode)
{
this.savingError('Domain already exists');
}
}
2014-08-21 23:08:34 +08:00
else
{
2014-08-21 23:08:34 +08:00
this.savingError('Unknown error');
}
2014-08-21 23:08:34 +08:00
};
2014-11-20 05:32:20 +08:00
DomainPopupView.prototype.clearTesting = function ()
{
this.testing(false);
this.testingDone(false);
this.testingImapError(false);
this.testingSmtpError(false);
2014-11-20 07:25:39 +08:00
};
2014-11-20 05:32:20 +08:00
DomainPopupView.prototype.onHide = function ()
2014-08-21 23:08:34 +08:00
{
2014-11-20 05:32:20 +08:00
this.page('main');
this.sieveSettings(false);
2014-08-21 23:08:34 +08:00
};
2014-11-20 05:32:20 +08:00
DomainPopupView.prototype.onShow = function (oDomain)
{
2014-08-21 23:08:34 +08:00
this.saving(false);
2014-11-20 05:32:20 +08:00
this.page('main');
this.sieveSettings(false);
this.clearTesting();
2014-08-21 23:08:34 +08:00
this.clearForm();
if (oDomain)
{
2014-10-14 06:39:23 +08:00
this.enableSmartPorts(false);
2014-08-21 23:08:34 +08:00
this.edit(true);
this.name(Utils.trim(oDomain.Name));
this.imapServer(Utils.trim(oDomain.IncHost));
this.imapPort('' + Utils.pInt(oDomain.IncPort));
this.imapSecure(Utils.trim(oDomain.IncSecure));
this.imapShortLogin(!!oDomain.IncShortLogin);
2014-11-20 05:32:20 +08:00
this.useSieve(!!oDomain.UseSieve);
this.useImapServerForSieve(!!oDomain.UseImapServerForSieve);
this.sieveServer(Utils.trim(oDomain.SieveHost));
this.sievePort('' + Utils.pInt(oDomain.SievePort));
this.sieveSecure(Utils.trim(oDomain.SieveSecure));
2014-08-21 23:08:34 +08:00
this.smtpServer(Utils.trim(oDomain.OutHost));
this.smtpPort('' + Utils.pInt(oDomain.OutPort));
this.smtpSecure(Utils.trim(oDomain.OutSecure));
this.smtpShortLogin(!!oDomain.OutShortLogin);
this.smtpAuth(!!oDomain.OutAuth);
2014-11-13 06:09:14 +08:00
this.smtpPhpMail(!!oDomain.OutUsePhpMail);
2014-08-21 23:08:34 +08:00
this.whiteList(Utils.trim(oDomain.WhiteList));
2014-10-14 06:39:23 +08:00
this.enableSmartPorts(true);
}
2014-08-21 23:08:34 +08:00
};
DomainPopupView.prototype.onFocus = function ()
{
2014-08-21 23:08:34 +08:00
if ('' === this.name())
{
2014-08-21 23:08:34 +08:00
this.name.focused(true);
}
2014-08-21 23:08:34 +08:00
};
DomainPopupView.prototype.clearForm = function ()
{
2014-08-21 23:08:34 +08:00
this.edit(false);
2014-11-20 05:32:20 +08:00
this.page('main');
this.sieveSettings(false);
2014-10-14 06:39:23 +08:00
this.enableSmartPorts(false);
2014-08-21 23:08:34 +08:00
this.savingError('');
2014-08-21 23:08:34 +08:00
this.name('');
this.name.focused(false);
2014-08-21 23:08:34 +08:00
this.imapServer('');
this.imapPort('' + Consts.Values.ImapDefaulPort);
this.imapSecure(Enums.ServerSecure.None);
this.imapShortLogin(false);
2014-11-20 05:32:20 +08:00
this.useSieve(false);
this.useImapServerForSieve(true);
this.sieveServer('');
this.sievePort('' + Consts.Values.SieveDefaulPort);
this.sieveSecure(Enums.ServerSecure.None);
2014-08-21 23:08:34 +08:00
this.smtpServer('');
this.smtpPort('' + Consts.Values.SmtpDefaulPort);
this.smtpSecure(Enums.ServerSecure.None);
this.smtpShortLogin(false);
this.smtpAuth(true);
2014-11-13 06:09:14 +08:00
this.smtpPhpMail(false);
2014-10-14 06:39:23 +08:00
2014-11-20 05:32:20 +08:00
this.whiteList('');
2014-10-14 06:39:23 +08:00
this.enableSmartPorts(true);
2014-08-21 23:08:34 +08:00
};
module.exports = DomainPopupView;
2014-09-05 06:49:03 +08:00
}());