snappymail/dev/View/Popup/Activate.js

121 lines
3.2 KiB
JavaScript
Raw Normal View History

import ko from 'ko';
2014-08-25 23:49:01 +08:00
2019-07-05 03:19:24 +08:00
import { StorageResultType, Notification } from 'Common/Enums';
import { trim, isUnd } from 'Common/Utils';
import { RAINLOOP_TRIAL_KEY } from 'Common/Consts';
import { i18n, getNotification } from 'Common/Translator';
2014-08-21 23:08:34 +08:00
import * as Settings from 'Storage/Settings';
2014-08-25 15:10:51 +08:00
import Remote from 'Remote/Admin/Ajax';
import LicenseStore from 'Stores/Admin/License';
2014-08-21 23:08:34 +08:00
2019-07-05 03:19:24 +08:00
import { popup, command } from 'Knoin/Knoin';
import { AbstractViewNext } from 'Knoin/AbstractViewNext';
2015-02-03 18:42:06 +08:00
2016-09-10 06:38:16 +08:00
@popup({
name: 'View/Popup/Activate',
templateID: 'PopupsActivate'
})
2019-07-05 03:19:24 +08:00
class ActivatePopupView extends AbstractViewNext {
constructor() {
super();
2014-08-25 15:10:51 +08:00
this.domain = ko.observable('');
this.key = ko.observable('');
this.key.focus = ko.observable(false);
this.activationSuccessed = ko.observable(false);
2014-08-21 23:08:34 +08:00
this.licenseTrigger = LicenseStore.licenseTrigger;
this.activateProcess = ko.observable(false);
this.activateText = ko.observable('');
this.activateText.isError = ko.observable(false);
2019-07-05 03:19:24 +08:00
this.htmlDescription = ko.computed(() => i18n('POPUPS_ACTIVATE/HTML_DESC', { 'DOMAIN': this.domain() }));
this.key.subscribe(() => {
this.activateText('');
this.activateText.isError(false);
});
2014-08-21 23:08:34 +08:00
this.activationSuccessed.subscribe((value) => {
2019-07-05 03:19:24 +08:00
if (value) {
this.licenseTrigger(!this.licenseTrigger());
}
});
2016-09-10 06:38:16 +08:00
}
2015-03-28 06:06:56 +08:00
2019-07-05 03:19:24 +08:00
@command(
(self) => !self.activateProcess() && '' !== self.domain() && '' !== self.key() && !self.activationSuccessed()
)
2016-09-10 06:38:16 +08:00
activateCommand() {
this.activateProcess(true);
2019-07-05 03:19:24 +08:00
if (this.validateSubscriptionKey()) {
Remote.licensingActivate(
(sResult, oData) => {
this.activateProcess(false);
if (StorageResultType.Success === sResult && oData.Result) {
if (true === oData.Result) {
this.activationSuccessed(true);
this.activateText(i18n('POPUPS_ACTIVATE/SUBS_KEY_ACTIVATED'));
this.activateText.isError(false);
} else {
this.activateText(oData.Result);
this.activateText.isError(true);
this.key.focus(true);
}
} else if (oData.ErrorCode) {
this.activateText(getNotification(oData.ErrorCode));
this.activateText.isError(true);
this.key.focus(true);
} else {
this.activateText(getNotification(Notification.UnknownError));
this.activateText.isError(true);
this.key.focus(true);
}
2019-07-05 03:19:24 +08:00
},
this.domain(),
this.key().replace(/[^A-Z0-9-]/gi, '')
);
} else {
2016-09-10 06:38:16 +08:00
this.activateProcess(false);
this.activateText(i18n('POPUPS_ACTIVATE/ERROR_INVALID_SUBS_KEY'));
this.activateText.isError(true);
this.key.focus(true);
}
}
2016-06-30 08:02:45 +08:00
onShow(isTrial) {
this.domain(Settings.settingsGet('AdminDomain'));
2019-07-05 03:19:24 +08:00
if (!this.activateProcess()) {
isTrial = isUnd(isTrial) ? false : !!isTrial;
2016-06-30 08:02:45 +08:00
this.key(isTrial ? RAINLOOP_TRIAL_KEY : '');
this.activateText('');
this.activateText.isError(false);
this.activationSuccessed(false);
}
2016-06-30 08:02:45 +08:00
}
2014-08-21 23:08:34 +08:00
onShowWithDelay() {
2019-07-05 03:19:24 +08:00
if (!this.activateProcess()) {
this.key.focus(true);
}
2016-06-30 08:02:45 +08:00
}
/**
* @returns {boolean}
*/
validateSubscriptionKey() {
const value = this.key();
2019-07-05 03:19:24 +08:00
return (
'' === value ||
RAINLOOP_TRIAL_KEY === value ||
!!/^RL[\d]+-[A-Z0-9-]+Z$/.test(trim(value).replace(/[^A-Z0-9-]/gi, ''))
);
}
}
2016-06-30 08:02:45 +08:00
2019-07-05 03:19:24 +08:00
export { ActivatePopupView, ActivatePopupView as default };