snappymail/dev/View/Popup/Activate.js

151 lines
3.7 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
_ = require('_'),
2014-08-25 23:49:01 +08:00
ko = require('ko'),
2014-08-25 15:10:51 +08:00
2014-09-05 06:49:03 +08:00
Enums = require('Common/Enums'),
Utils = require('Common/Utils'),
2015-02-03 18:42:06 +08:00
Consts = require('Common/Consts'),
Translator = require('Common/Translator'),
2014-08-21 23:08:34 +08:00
Settings = require('Storage/Settings'),
2015-02-23 00:35:17 +08:00
Remote = require('Remote/Admin/Ajax'),
2015-02-03 18:42:06 +08:00
LicenseStore = require('Stores/Admin/License'),
2014-08-25 15:10:51 +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 ActivatePopupView()
2014-08-21 23:08:34 +08:00
{
AbstractView.call(this, 'Popups', 'PopupsActivate');
2014-08-21 23:08:34 +08:00
var self = this;
2014-08-21 23:08:34 +08:00
this.domain = ko.observable('');
this.key = ko.observable('');
this.key.focus = ko.observable(false);
this.activationSuccessed = ko.observable(false);
this.licenseTrigger = LicenseStore.licenseTrigger;
2014-08-21 23:08:34 +08:00
this.activateProcess = ko.observable(false);
this.activateText = ko.observable('');
this.activateText.isError = ko.observable(false);
2015-03-28 06:06:56 +08:00
this.htmlDescription = ko.computed(function () {
return Translator.i18n('POPUPS_ACTIVATE/HTML_DESC', {'DOMAIN': this.domain()});
}, this);
2014-08-21 23:08:34 +08:00
this.key.subscribe(function () {
this.activateText('');
this.activateText.isError(false);
}, this);
this.activationSuccessed.subscribe(function (bValue) {
if (bValue)
{
this.licenseTrigger(!this.licenseTrigger());
}
}, this);
2014-08-21 23:08:34 +08:00
this.activateCommand = Utils.createCommand(this, function () {
this.activateProcess(true);
if (this.validateSubscriptionKey())
{
Remote.licensingActivate(function (sResult, oData) {
self.activateProcess(false);
if (Enums.StorageResultType.Success === sResult && oData.Result)
{
2014-08-21 23:08:34 +08:00
if (true === oData.Result)
{
self.activationSuccessed(true);
2015-03-28 06:06:56 +08:00
self.activateText(Translator.i18n('POPUPS_ACTIVATE/SUBS_KEY_ACTIVATED'));
2014-08-21 23:08:34 +08:00
self.activateText.isError(false);
}
else
{
self.activateText(oData.Result);
self.activateText.isError(true);
self.key.focus(true);
}
}
else if (oData.ErrorCode)
{
self.activateText(Translator.getNotification(oData.ErrorCode));
2014-08-21 23:08:34 +08:00
self.activateText.isError(true);
self.key.focus(true);
}
else
{
self.activateText(Translator.getNotification(Enums.Notification.UnknownError));
self.activateText.isError(true);
self.key.focus(true);
}
2014-08-21 23:08:34 +08:00
}, this.domain(), this.key());
}
else
{
this.activateProcess(false);
2015-03-28 06:06:56 +08:00
this.activateText(Translator.i18n('POPUPS_ACTIVATE/ERROR_INVALID_SUBS_KEY'));
2014-08-21 23:08:34 +08:00
this.activateText.isError(true);
this.key.focus(true);
}
}, function () {
return !this.activateProcess() && '' !== this.domain() && '' !== this.key() && !this.activationSuccessed();
});
kn.constructorEnd(this);
}
kn.extendAsViewModel(['View/Popup/Activate', 'PopupsActivateViewModel'], ActivatePopupView);
_.extend(ActivatePopupView.prototype, AbstractView.prototype);
2015-02-03 18:42:06 +08:00
ActivatePopupView.prototype.onShow = function (bTrial)
2014-08-21 23:08:34 +08:00
{
2014-08-27 23:59:44 +08:00
this.domain(Settings.settingsGet('AdminDomain'));
2014-08-21 23:08:34 +08:00
if (!this.activateProcess())
{
2015-02-03 18:42:06 +08:00
bTrial = Utils.isUnd(bTrial) ? false : !!bTrial;
2015-11-15 08:23:16 +08:00
this.key(bTrial ? Consts.RAINLOOP_TRIAL_KEY : '');
2014-08-21 23:08:34 +08:00
this.activateText('');
this.activateText.isError(false);
this.activationSuccessed(false);
}
};
2015-02-16 05:55:59 +08:00
ActivatePopupView.prototype.onShowWithDelay = function ()
{
2014-08-21 23:08:34 +08:00
if (!this.activateProcess())
{
this.key.focus(true);
}
};
2013-12-29 04:42:07 +08:00
2014-08-21 23:08:34 +08:00
/**
2015-04-14 02:45:09 +08:00
* @return {boolean}
2014-08-21 23:08:34 +08:00
*/
ActivatePopupView.prototype.validateSubscriptionKey = function ()
2013-12-29 04:42:07 +08:00
{
2014-08-21 23:08:34 +08:00
var sValue = this.key();
2015-11-15 08:23:16 +08:00
return '' === sValue || Consts.RAINLOOP_TRIAL_KEY === sValue ||
2015-02-03 18:42:06 +08:00
!!/^RL[\d]+-[A-Z0-9\-]+Z$/.test(Utils.trim(sValue));
2014-08-21 23:08:34 +08:00
};
2014-08-25 15:10:51 +08:00
module.exports = ActivatePopupView;
2014-08-21 23:08:34 +08:00
2014-09-05 06:49:03 +08:00
}());