2013-11-16 06:21:12 +08:00
|
|
|
/* RainLoop Webmail (c) RainLoop Team | Licensed under CC BY-NC-SA 3.0 */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @extends KnoinAbstractViewModel
|
|
|
|
*/
|
|
|
|
function PopupsDomainViewModel()
|
|
|
|
{
|
|
|
|
KnoinAbstractViewModel.call(this, 'Popups', 'PopupsDomain');
|
2014-06-24 05:32:09 +08:00
|
|
|
|
2013-11-16 06:21:12 +08:00
|
|
|
this.edit = ko.observable(false);
|
|
|
|
this.saving = ko.observable(false);
|
|
|
|
this.savingError = ko.observable('');
|
|
|
|
this.whiteListPage = ko.observable(false);
|
|
|
|
|
|
|
|
this.testing = ko.observable(false);
|
|
|
|
this.testingDone = ko.observable(false);
|
|
|
|
this.testingImapError = ko.observable(false);
|
|
|
|
this.testingSmtpError = ko.observable(false);
|
2014-05-01 00:24:45 +08:00
|
|
|
this.testingImapErrorDesc = ko.observable('');
|
|
|
|
this.testingSmtpErrorDesc = ko.observable('');
|
2014-06-24 05:32:09 +08:00
|
|
|
|
2014-05-01 00:24:45 +08:00
|
|
|
this.testingImapError.subscribe(function (bValue) {
|
|
|
|
if (!bValue)
|
|
|
|
{
|
|
|
|
this.testingImapErrorDesc('');
|
|
|
|
}
|
|
|
|
}, this);
|
2014-06-24 05:32:09 +08:00
|
|
|
|
2014-05-01 00:24:45 +08:00
|
|
|
this.testingSmtpError.subscribe(function (bValue) {
|
|
|
|
if (!bValue)
|
|
|
|
{
|
|
|
|
this.testingSmtpErrorDesc('');
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
this.testingImapErrorDesc = ko.observable('');
|
|
|
|
this.testingSmtpErrorDesc = ko.observable('');
|
2013-11-16 06:21:12 +08:00
|
|
|
|
|
|
|
this.imapServerFocus = ko.observable(false);
|
|
|
|
this.smtpServerFocus = ko.observable(false);
|
|
|
|
|
|
|
|
this.name = ko.observable('');
|
2014-02-08 07:28:11 +08:00
|
|
|
this.name.focused = ko.observable(false);
|
|
|
|
|
2013-11-16 06:21:12 +08:00
|
|
|
this.imapServer = ko.observable('');
|
2014-06-24 05:32:09 +08:00
|
|
|
this.imapPort = ko.observable('' + Consts.Values.ImapDefaulPort);
|
2013-11-16 06:21:12 +08:00
|
|
|
this.imapSecure = ko.observable(Enums.ServerSecure.None);
|
|
|
|
this.imapShortLogin = ko.observable(false);
|
|
|
|
this.smtpServer = ko.observable('');
|
2014-06-24 05:32:09 +08:00
|
|
|
this.smtpPort = ko.observable('' + Consts.Values.SmtpDefaulPort);
|
2013-11-16 06:21:12 +08:00
|
|
|
this.smtpSecure = ko.observable(Enums.ServerSecure.None);
|
|
|
|
this.smtpShortLogin = ko.observable(false);
|
|
|
|
this.smtpAuth = ko.observable(true);
|
|
|
|
this.whiteList = ko.observable('');
|
|
|
|
|
|
|
|
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 () {
|
|
|
|
return '' !== this.name() &&
|
|
|
|
'' !== this.imapServer() &&
|
|
|
|
'' !== this.imapPort() &&
|
|
|
|
'' !== this.smtpServer() &&
|
|
|
|
'' !== this.smtpPort();
|
|
|
|
}, 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);
|
|
|
|
RL.remote().createOrUpdateDomain(
|
|
|
|
_.bind(this.onDomainCreateOrSaveResponse, this),
|
|
|
|
!this.edit(),
|
|
|
|
this.name(),
|
|
|
|
this.imapServer(),
|
2014-06-24 05:32:09 +08:00
|
|
|
Utils.pInt(this.imapPort()),
|
2013-11-16 06:21:12 +08:00
|
|
|
this.imapSecure(),
|
|
|
|
this.imapShortLogin(),
|
|
|
|
this.smtpServer(),
|
2014-06-24 05:32:09 +08:00
|
|
|
Utils.pInt(this.smtpPort()),
|
2013-11-16 06:21:12 +08:00
|
|
|
this.smtpSecure(),
|
|
|
|
this.smtpShortLogin(),
|
|
|
|
this.smtpAuth(),
|
|
|
|
this.whiteList()
|
|
|
|
);
|
|
|
|
}, this.canBeSaved);
|
|
|
|
|
|
|
|
this.testConnectionCommand = Utils.createCommand(this, function () {
|
|
|
|
this.whiteListPage(false);
|
|
|
|
this.testingDone(false);
|
|
|
|
this.testingImapError(false);
|
|
|
|
this.testingSmtpError(false);
|
|
|
|
this.testing(true);
|
|
|
|
RL.remote().testConnectionForDomain(
|
|
|
|
_.bind(this.onTestConnectionResponse, this),
|
2014-04-26 07:41:47 +08:00
|
|
|
this.name(),
|
2013-11-16 06:21:12 +08:00
|
|
|
this.imapServer(),
|
2014-06-24 05:32:09 +08:00
|
|
|
Utils.pInt(this.imapPort()),
|
2013-11-16 06:21:12 +08:00
|
|
|
this.imapSecure(),
|
|
|
|
this.smtpServer(),
|
2014-06-24 05:32:09 +08:00
|
|
|
Utils.pInt(this.smtpPort()),
|
2013-11-16 06:21:12 +08:00
|
|
|
this.smtpSecure(),
|
|
|
|
this.smtpAuth()
|
|
|
|
);
|
|
|
|
}, this.canBeTested);
|
|
|
|
|
|
|
|
this.whiteListCommand = Utils.createCommand(this, function () {
|
|
|
|
this.whiteListPage(!this.whiteListPage());
|
|
|
|
});
|
2013-12-09 23:16:58 +08:00
|
|
|
|
2014-04-26 07:41:47 +08:00
|
|
|
// smart form improvements
|
|
|
|
this.imapServerFocus.subscribe(function (bValue) {
|
|
|
|
if (bValue && '' !== this.name() && '' === this.imapServer())
|
|
|
|
{
|
|
|
|
this.imapServer(this.name().replace(/[.]?[*][.]?/g, ''));
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
this.smtpServerFocus.subscribe(function (bValue) {
|
|
|
|
if (bValue && '' !== this.imapServer() && '' === this.smtpServer())
|
|
|
|
{
|
|
|
|
this.smtpServer(this.imapServer().replace(/imap/ig, 'smtp'));
|
|
|
|
}
|
|
|
|
}, this);
|
2014-06-24 05:32:09 +08:00
|
|
|
|
2014-04-26 07:41:47 +08:00
|
|
|
this.imapSecure.subscribe(function (sValue) {
|
|
|
|
var iPort = Utils.pInt(this.imapPort());
|
|
|
|
sValue = Utils.pString(sValue);
|
|
|
|
switch (sValue)
|
|
|
|
{
|
|
|
|
case '0':
|
|
|
|
if (993 === iPort)
|
|
|
|
{
|
2014-06-24 05:32:09 +08:00
|
|
|
this.imapPort('143');
|
2014-04-26 07:41:47 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
if (143 === iPort)
|
|
|
|
{
|
2014-06-24 05:32:09 +08:00
|
|
|
this.imapPort('993');
|
2014-04-26 07:41:47 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
this.smtpSecure.subscribe(function (sValue) {
|
|
|
|
var iPort = Utils.pInt(this.smtpPort());
|
|
|
|
sValue = Utils.pString(sValue);
|
|
|
|
switch (sValue)
|
|
|
|
{
|
|
|
|
case '0':
|
|
|
|
if (465 === iPort || 587 === iPort)
|
|
|
|
{
|
2014-06-24 05:32:09 +08:00
|
|
|
this.smtpPort('25');
|
2014-04-26 07:41:47 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
if (25 === iPort || 587 === iPort)
|
|
|
|
{
|
2014-06-24 05:32:09 +08:00
|
|
|
this.smtpPort('465');
|
2014-04-26 07:41:47 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '2':
|
|
|
|
if (25 === iPort || 465 === iPort)
|
|
|
|
{
|
2014-06-24 05:32:09 +08:00
|
|
|
this.smtpPort('587');
|
2014-04-26 07:41:47 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
|
2013-12-09 23:16:58 +08:00
|
|
|
Knoin.constructorEnd(this);
|
2013-11-16 06:21:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Utils.extendAsViewModel('PopupsDomainViewModel', PopupsDomainViewModel);
|
|
|
|
|
|
|
|
PopupsDomainViewModel.prototype.onTestConnectionResponse = function (sResult, oData)
|
|
|
|
{
|
|
|
|
this.testing(false);
|
|
|
|
if (Enums.StorageResultType.Success === sResult && oData.Result)
|
|
|
|
{
|
|
|
|
this.testingDone(true);
|
2014-05-01 00:24:45 +08:00
|
|
|
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);
|
|
|
|
}
|
2013-11-16 06:21:12 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.testingImapError(true);
|
|
|
|
this.testingSmtpError(true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
PopupsDomainViewModel.prototype.onDomainCreateOrSaveResponse = function (sResult, oData)
|
|
|
|
{
|
|
|
|
this.saving(false);
|
|
|
|
if (Enums.StorageResultType.Success === sResult && oData)
|
|
|
|
{
|
|
|
|
if (oData.Result)
|
|
|
|
{
|
|
|
|
RL.reloadDomainList();
|
|
|
|
this.closeCommand();
|
|
|
|
}
|
|
|
|
else if (Enums.Notification.DomainAlreadyExists === oData.ErrorCode)
|
|
|
|
{
|
|
|
|
this.savingError('Domain already exists');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.savingError('Unknown error');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
PopupsDomainViewModel.prototype.onHide = function ()
|
|
|
|
{
|
|
|
|
this.whiteListPage(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
PopupsDomainViewModel.prototype.onShow = function (oDomain)
|
|
|
|
{
|
|
|
|
this.saving(false);
|
|
|
|
this.whiteListPage(false);
|
|
|
|
|
|
|
|
this.testing(false);
|
|
|
|
this.testingDone(false);
|
|
|
|
this.testingImapError(false);
|
|
|
|
this.testingSmtpError(false);
|
|
|
|
|
|
|
|
this.clearForm();
|
|
|
|
if (oDomain)
|
|
|
|
{
|
|
|
|
this.edit(true);
|
|
|
|
|
|
|
|
this.name(Utils.trim(oDomain.Name));
|
|
|
|
this.imapServer(Utils.trim(oDomain.IncHost));
|
2014-06-24 05:32:09 +08:00
|
|
|
this.imapPort('' + Utils.pInt(oDomain.IncPort));
|
2013-11-16 06:21:12 +08:00
|
|
|
this.imapSecure(Utils.trim(oDomain.IncSecure));
|
|
|
|
this.imapShortLogin(!!oDomain.IncShortLogin);
|
|
|
|
this.smtpServer(Utils.trim(oDomain.OutHost));
|
2014-06-24 05:32:09 +08:00
|
|
|
this.smtpPort('' + Utils.pInt(oDomain.OutPort));
|
2013-11-16 06:21:12 +08:00
|
|
|
this.smtpSecure(Utils.trim(oDomain.OutSecure));
|
|
|
|
this.smtpShortLogin(!!oDomain.OutShortLogin);
|
|
|
|
this.smtpAuth(!!oDomain.OutAuth);
|
|
|
|
this.whiteList(Utils.trim(oDomain.WhiteList));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-08 07:28:11 +08:00
|
|
|
PopupsDomainViewModel.prototype.onFocus = function ()
|
|
|
|
{
|
|
|
|
if ('' === this.name())
|
|
|
|
{
|
|
|
|
this.name.focused(true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-11-16 06:21:12 +08:00
|
|
|
PopupsDomainViewModel.prototype.clearForm = function ()
|
|
|
|
{
|
|
|
|
this.edit(false);
|
|
|
|
this.whiteListPage(false);
|
2014-06-24 05:32:09 +08:00
|
|
|
|
2013-11-16 06:21:12 +08:00
|
|
|
this.savingError('');
|
|
|
|
|
|
|
|
this.name('');
|
2014-02-08 07:28:11 +08:00
|
|
|
this.name.focused(false);
|
|
|
|
|
2013-11-16 06:21:12 +08:00
|
|
|
this.imapServer('');
|
2014-06-24 05:32:09 +08:00
|
|
|
this.imapPort('' + Consts.Values.ImapDefaulPort);
|
2013-11-16 06:21:12 +08:00
|
|
|
this.imapSecure(Enums.ServerSecure.None);
|
|
|
|
this.imapShortLogin(false);
|
|
|
|
this.smtpServer('');
|
2014-06-24 05:32:09 +08:00
|
|
|
this.smtpPort('' + Consts.Values.SmtpDefaulPort);
|
2013-11-16 06:21:12 +08:00
|
|
|
this.smtpSecure(Enums.ServerSecure.None);
|
|
|
|
this.smtpShortLogin(false);
|
|
|
|
this.smtpAuth(true);
|
|
|
|
this.whiteList('');
|
|
|
|
};
|