snappymail/dev/View/Popup/Domain.js

467 lines
11 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'),
Globals = require('Common/Globals'),
2014-09-05 06:49:03 +08:00
Utils = require('Common/Utils'),
2014-08-21 23:08:34 +08:00
2015-03-28 06:06:56 +08:00
Translator = require('Common/Translator'),
CapaAdminStore = require('Stores/Admin/Capa'),
2015-02-23 00:35:17 +08:00
Remote = require('Remote/Admin/Ajax'),
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);
2015-01-29 00:27:23 +08:00
this.testingSieveError = ko.observable(false);
2014-08-21 23:08:34 +08:00
this.testingSmtpError = ko.observable(false);
this.testingImapErrorDesc = ko.observable('');
2015-01-29 00:27:23 +08:00
this.testingSieveErrorDesc = ko.observable('');
2014-08-21 23:08:34 +08:00
this.testingSmtpErrorDesc = ko.observable('');
this.testingImapError.subscribe(function (bValue) {
if (!bValue)
{
this.testingImapErrorDesc('');
}
}, this);
2015-01-29 00:27:23 +08:00
this.testingSieveError.subscribe(function (bValue) {
if (!bValue)
{
this.testingSieveErrorDesc('');
}
}, this);
2014-08-21 23:08:34 +08:00
this.testingSmtpError.subscribe(function (bValue) {
if (!bValue)
{
this.testingSmtpErrorDesc('');
}
}, this);
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);
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.sieveAllowRaw = ko.observable(false);
2014-11-20 05:32:20 +08:00
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);
this.allowSieve = ko.computed(function () {
return CapaAdminStore.filters() && CapaAdminStore.sieve();
}, this);
2014-08-21 23:08:34 +08:00
this.headerText = ko.computed(function () {
var sName = this.name();
2015-03-28 06:06:56 +08:00
return this.edit() ? Translator.i18n('POPUPS_DOMAIN/TITLE_EDIT_DOMAIN', {'NAME': sName}) :
('' === sName ? Translator.i18n('POPUPS_DOMAIN/TITLE_ADD_DOMAIN') :
Translator.i18n('POPUPS_DOMAIN/TITLE_ADD_DOMAIN_WITH_NAME', {'NAME': sName}));
2014-08-21 23:08:34 +08:00
}, this);
this.domainIsComputed = ko.computed(function () {
2014-11-20 05:32:20 +08:00
var
bPhpMail = this.smtpPhpMail(),
bAllowSieve = this.allowSieve(),
bUseSieve = this.useSieve()
2014-11-20 05:32:20 +08:00
;
2014-08-21 23:08:34 +08:00
return '' !== this.name() &&
'' !== this.imapServer() &&
'' !== this.imapPort() &&
(bAllowSieve && bUseSieve ? ('' !== 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.sieveAllowRaw(),
2014-11-20 05:32:20 +08:00
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');
2014-08-21 23:08:34 +08:00
this.testingDone(false);
this.testingImapError(false);
2015-04-10 06:05:49 +08:00
this.testingSieveError(false);
2014-08-21 23:08:34 +08:00
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.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.imapServer() && '' === this.sieveServer())
2014-11-20 05:32:20 +08:00
{
this.sieveServer(this.imapServer());
2014-11-20 05:32:20 +08:00
}
}, 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)
{
2015-01-29 00:27:23 +08:00
var
bImap = false,
bSieve = false
;
2014-08-21 23:08:34 +08:00
this.testingDone(true);
this.testingImapError(true !== oData.Result.Imap);
2015-01-29 00:27:23 +08:00
this.testingSieveError(true !== oData.Result.Sieve);
2015-04-10 06:05:49 +08:00
this.testingSmtpError(true !== oData.Result.Smtp);
2014-08-21 23:08:34 +08:00
if (this.testingImapError() && oData.Result.Imap)
{
2015-01-29 00:27:23 +08:00
bImap = true;
2015-04-10 06:05:49 +08:00
this.testingImapErrorDesc('');
2014-08-21 23:08:34 +08:00
this.testingImapErrorDesc(oData.Result.Imap);
}
2015-01-29 00:27:23 +08:00
if (this.testingSieveError() && oData.Result.Sieve)
{
bSieve = true;
2015-04-10 06:05:49 +08:00
this.testingSieveErrorDesc('');
2015-01-29 00:27:23 +08:00
this.testingSieveErrorDesc(oData.Result.Sieve);
}
2015-04-10 06:05:49 +08:00
if (this.testingSmtpError() && oData.Result.Smtp)
{
this.testingSmtpErrorDesc('');
this.testingSmtpErrorDesc(oData.Result.Smtp);
}
2015-01-29 05:24:58 +08:00
if (this.sieveSettings())
2015-01-29 00:27:23 +08:00
{
2015-01-29 05:24:58 +08:00
if (!bSieve && bImap)
{
this.sieveSettings(false);
}
2015-01-29 00:27:23 +08:00
}
2015-01-29 05:24:58 +08:00
else
2015-01-29 00:27:23 +08:00
{
2015-01-29 05:24:58 +08:00
if (bSieve && !bImap)
{
this.sieveSettings(true);
}
2015-01-29 00:27:23 +08:00
}
}
2014-08-21 23:08:34 +08:00
else
{
2014-08-21 23:08:34 +08:00
this.testingImapError(true);
2015-01-29 00:27:23 +08:00
this.testingSieveError(true);
2015-04-10 06:05:49 +08:00
this.testingSmtpError(true);
2015-01-29 00:27:23 +08:00
this.sieveSettings(false);
}
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)
{
2015-03-28 06:06:56 +08:00
this.savingError(Translator.i18n('ERRORS/DOMAIN_ALREADY_EXISTS'));
2014-08-21 23:08:34 +08:00
}
}
2014-08-21 23:08:34 +08:00
else
{
2015-03-28 06:06:56 +08:00
this.savingError(Translator.i18n('ERRORS/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);
2015-01-29 00:27:23 +08:00
this.testingSieveError(false);
2015-04-10 06:05:49 +08:00
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.sieveAllowRaw(!!oDomain.SieveAllowRaw);
2014-11-20 05:32:20 +08:00
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
};
2015-02-16 05:55:59 +08:00
DomainPopupView.prototype.onShowWithDelay = function ()
{
if ('' === this.name() && !Globals.bMobile)
{
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.sieveAllowRaw(false);
2014-11-20 05:32:20 +08:00
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
}());