snappymail/dev/View/Popup/DomainAlias.js

99 lines
2.1 KiB
JavaScript
Raw Normal View History

2016-06-11 06:20:09 +08:00
import _ from '_';
import ko from 'ko';
2016-06-11 06:20:09 +08:00
import {StorageResultType, Notification} from 'Common/Enums';
import {bMobileDevice} from 'Common/Globals';
import {i18n} from 'Common/Translator';
2016-06-11 06:20:09 +08:00
import DomainStore from 'Stores/Admin/Domain';
2016-06-11 06:20:09 +08:00
import Remote from 'Remote/Admin/Ajax';
2016-06-11 06:20:09 +08:00
import {getApp} from 'Helper/Apps/Admin';
2016-06-11 06:20:09 +08:00
2016-09-10 06:38:16 +08:00
import {popup, command} from 'Knoin/Knoin';
import {AbstractViewNext} from 'Knoin/AbstractViewNext';
2016-06-11 06:20:09 +08:00
2016-09-10 06:38:16 +08:00
@popup({
name: 'View/Popup/DomainAlias',
templateID: 'PopupsDomainAlias'
})
class DomainAliasPopupView extends AbstractViewNext
2016-06-30 08:02:45 +08:00
{
constructor() {
super();
2016-06-11 06:20:09 +08:00
this.saving = ko.observable(false);
this.savingError = ko.observable('');
2016-06-11 06:20:09 +08:00
this.name = ko.observable('');
this.name.focused = ko.observable(false);
2016-06-11 06:20:09 +08:00
this.alias = ko.observable('');
2016-06-11 06:20:09 +08:00
this.domains = DomainStore.domainsWithoutAliases;
2016-06-11 06:20:09 +08:00
this.domainsOptions = ko.computed(
() => _.map(this.domains(), (item) => ({optValue: item.name, optText: item.name}))
2016-06-30 08:02:45 +08:00
);
2016-06-11 06:20:09 +08:00
this.canBeSaved = ko.computed(() => !this.saving() && '' !== this.name() && '' !== this.alias());
2016-06-11 06:20:09 +08:00
this.onDomainAliasCreateOrSaveResponse = _.bind(this.onDomainAliasCreateOrSaveResponse, this);
}
2016-09-10 06:38:16 +08:00
@command((self) => self.canBeSaved())
createCommand() {
this.saving(true);
Remote.createDomainAlias(
this.onDomainAliasCreateOrSaveResponse,
this.name(),
this.alias()
);
}
onDomainAliasCreateOrSaveResponse(result, data) {
this.saving(false);
if (StorageResultType.Success === result && data)
2016-06-11 06:20:09 +08:00
{
if (data.Result)
{
getApp().reloadDomainList();
this.closeCommand();
}
else if (Notification.DomainAlreadyExists === data.ErrorCode)
{
this.savingError(i18n('ERRORS/DOMAIN_ALREADY_EXISTS'));
}
2016-06-11 06:20:09 +08:00
}
else
2016-06-11 06:20:09 +08:00
{
this.savingError(i18n('ERRORS/UNKNOWN_ERROR'));
2016-06-11 06:20:09 +08:00
}
2016-06-30 08:02:45 +08:00
}
2016-06-11 06:20:09 +08:00
onShow() {
this.clearForm();
}
2016-06-11 06:20:09 +08:00
onShowWithDelay() {
if ('' === this.name() && !bMobileDevice)
{
this.name.focused(true);
}
2016-06-30 08:02:45 +08:00
}
2016-06-11 06:20:09 +08:00
clearForm() {
this.saving(false);
this.savingError('');
2016-06-11 06:20:09 +08:00
this.name('');
this.name.focused(false);
2016-06-11 06:20:09 +08:00
this.alias('');
}
}
2016-06-11 06:20:09 +08:00
2016-09-10 06:38:16 +08:00
export {DomainAliasPopupView, DomainAliasPopupView as default};