snappymail/dev/View/Popup/Activate.js

141 lines
3.2 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'),
2014-08-21 23:08:34 +08:00
Settings = require('Storage/Settings'),
Data = require('Storage/Admin/Data'),
Remote = require('Storage/Admin/Remote'),
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);
2014-08-21 23:08:34 +08:00
this.licenseTrigger = Data.licenseTrigger;
2014-08-21 23:08:34 +08:00
this.activateProcess = ko.observable(false);
this.activateText = ko.observable('');
this.activateText.isError = ko.observable(false);
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);
self.activateText('Subscription Key Activated Successfully');
self.activateText.isError(false);
}
else
{
self.activateText(oData.Result);
self.activateText.isError(true);
self.key.focus(true);
}
}
else if (oData.ErrorCode)
{
self.activateText(Utils.getNotification(oData.ErrorCode));
self.activateText.isError(true);
self.key.focus(true);
}
else
{
2014-08-21 23:08:34 +08:00
self.activateText(Utils.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);
this.activateText('Invalid Subscription Key');
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);
ActivatePopupView.prototype.onShow = function ()
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())
{
this.key('');
this.activateText('');
this.activateText.isError(false);
this.activationSuccessed(false);
}
};
ActivatePopupView.prototype.onFocus = 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
/**
* @returns {boolean}
*/
ActivatePopupView.prototype.validateSubscriptionKey = function ()
2013-12-29 04:42:07 +08:00
{
2014-08-21 23:08:34 +08:00
var sValue = this.key();
return '' === sValue || !!/^RL[\d]+-[A-Z0-9\-]+Z$/.test(Utils.trim(sValue));
};
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
}());