snappymail/dev/View/Popup/DomainAlias.js

111 lines
2.4 KiB
JavaScript
Raw Normal View History

2016-06-11 06:20:09 +08:00
2016-06-30 08:02:45 +08:00
var
_ = require('_'),
ko = require('ko'),
2016-06-11 06:20:09 +08:00
2016-06-30 08:02:45 +08:00
Enums = require('Common/Enums'),
Globals = require('Common/Globals'),
Utils = require('Common/Utils'),
2016-06-11 06:20:09 +08:00
2016-06-30 08:02:45 +08:00
Translator = require('Common/Translator'),
2016-06-11 06:20:09 +08:00
2016-06-30 08:02:45 +08:00
DomainStore = require('Stores/Admin/Domain'),
2016-06-11 06:20:09 +08:00
2016-06-30 08:02:45 +08:00
Remote = require('Remote/Admin/Ajax'),
2016-06-11 06:20:09 +08:00
2016-06-30 08:02:45 +08:00
kn = require('Knoin/Knoin'),
AbstractView = require('Knoin/AbstractView');
2016-06-11 06:20:09 +08:00
2016-06-30 08:02:45 +08:00
/**
* @constructor
* @extends AbstractView
*/
function DomainAliasPopupView()
{
AbstractView.call(this, 'Popups', 'PopupsDomainAlias');
2016-06-11 06:20:09 +08:00
2016-06-30 08:02:45 +08:00
this.saving = ko.observable(false);
this.savingError = ko.observable('');
2016-06-11 06:20:09 +08:00
2016-06-30 08:02:45 +08:00
this.name = ko.observable('');
this.name.focused = ko.observable(false);
2016-06-11 06:20:09 +08:00
2016-06-30 08:02:45 +08:00
this.alias = ko.observable('');
2016-06-11 06:20:09 +08:00
2016-06-30 08:02:45 +08:00
this.domains = DomainStore.domainsWithoutAliases;
2016-06-11 06:20:09 +08:00
2016-06-30 08:02:45 +08:00
this.domainsOptions = ko.computed(function() {
return _.map(this.domains(), function(item) {
return {
optValue: item.name,
optText: item.name
};
});
}, this);
2016-06-11 06:20:09 +08:00
2016-06-30 08:02:45 +08:00
this.canBeSaved = ko.computed(function() {
return !this.saving() && '' !== this.name() && '' !== this.alias();
}, this);
2016-06-11 06:20:09 +08:00
2016-06-30 08:02:45 +08:00
this.createCommand = Utils.createCommand(this, function() {
this.saving(true);
Remote.createDomainAlias(
_.bind(this.onDomainAliasCreateOrSaveResponse, this),
this.name(),
this.alias()
);
}, this.canBeSaved);
2016-06-11 06:20:09 +08:00
2016-06-30 08:02:45 +08:00
kn.constructorEnd(this);
}
2016-06-11 06:20:09 +08:00
2016-06-30 08:02:45 +08:00
kn.extendAsViewModel(['View/Popup/DomainAlias', 'PopupsDomainAliasViewModel'], DomainAliasPopupView);
_.extend(DomainAliasPopupView.prototype, AbstractView.prototype);
2016-06-11 06:20:09 +08:00
2016-06-30 08:02:45 +08:00
DomainAliasPopupView.prototype.onDomainAliasCreateOrSaveResponse = function(sResult, oData)
{
this.saving(false);
if (Enums.StorageResultType.Success === sResult && oData)
2016-06-11 06:20:09 +08:00
{
2016-06-30 08:02:45 +08:00
if (oData.Result)
2016-06-11 06:20:09 +08:00
{
2016-06-30 08:02:45 +08:00
require('App/Admin').default.reloadDomainList();
this.closeCommand();
2016-06-11 06:20:09 +08:00
}
2016-06-30 08:02:45 +08:00
else if (Enums.Notification.DomainAlreadyExists === oData.ErrorCode)
2016-06-11 06:20:09 +08:00
{
2016-06-30 08:02:45 +08:00
this.savingError(Translator.i18n('ERRORS/DOMAIN_ALREADY_EXISTS'));
2016-06-11 06:20:09 +08:00
}
2016-06-30 08:02:45 +08:00
}
else
2016-06-11 06:20:09 +08:00
{
2016-06-30 08:02:45 +08:00
this.savingError(Translator.i18n('ERRORS/UNKNOWN_ERROR'));
}
};
2016-06-11 06:20:09 +08:00
2016-06-30 08:02:45 +08:00
DomainAliasPopupView.prototype.onShow = function()
{
this.clearForm();
};
2016-06-11 06:20:09 +08:00
2016-06-30 08:02:45 +08:00
DomainAliasPopupView.prototype.onShowWithDelay = function()
{
if ('' === this.name() && !Globals.bMobile)
2016-06-11 06:20:09 +08:00
{
2016-06-30 08:02:45 +08:00
this.name.focused(true);
}
};
2016-06-11 06:20:09 +08:00
2016-06-30 08:02:45 +08:00
DomainAliasPopupView.prototype.clearForm = function()
{
this.saving(false);
this.savingError('');
2016-06-11 06:20:09 +08:00
2016-06-30 08:02:45 +08:00
this.name('');
this.name.focused(false);
2016-06-11 06:20:09 +08:00
2016-06-30 08:02:45 +08:00
this.alias('');
};
2016-06-11 06:20:09 +08:00
2016-06-30 08:02:45 +08:00
module.exports = DomainAliasPopupView;