snappymail/dev/View/Popup/DomainAlias.js

86 lines
2 KiB
JavaScript
Raw Normal View History

import _ from '_';
import ko from 'ko';
2016-06-11 06:20:09 +08:00
2019-07-05 03:19:24 +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
2019-07-05 03:19:24 +08:00
import { getApp } from 'Helper/Apps/Admin';
2016-06-11 06:20:09 +08:00
2019-07-05 03:19:24 +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'
})
2019-07-05 03:19:24 +08:00
class DomainAliasPopupView extends AbstractViewNext {
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
2019-07-05 03:19:24 +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 = this.onDomainAliasCreateOrSaveResponse.bind(this);
}
2016-09-10 06:38:16 +08:00
@command((self) => self.canBeSaved())
createCommand() {
this.saving(true);
2019-07-05 03:19:24 +08:00
Remote.createDomainAlias(this.onDomainAliasCreateOrSaveResponse, this.name(), this.alias());
2016-09-10 06:38:16 +08:00
}
onDomainAliasCreateOrSaveResponse(result, data) {
this.saving(false);
2019-07-05 03:19:24 +08:00
if (StorageResultType.Success === result && data) {
if (data.Result) {
getApp().reloadDomainList();
this.closeCommand();
2019-07-05 03:19:24 +08:00
} else if (Notification.DomainAlreadyExists === data.ErrorCode) {
this.savingError(i18n('ERRORS/DOMAIN_ALREADY_EXISTS'));
}
2019-07-05 03:19:24 +08:00
} else {
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() {
2019-07-05 03:19:24 +08:00
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
2019-07-05 03:19:24 +08:00
export { DomainAliasPopupView, DomainAliasPopupView as default };