snappymail/dev/ViewModels/Popups/PopupsActivateViewModel.js

139 lines
3.4 KiB
JavaScript
Raw Normal View History

/* RainLoop Webmail (c) RainLoop Team | Licensed under CC BY-NC-SA 3.0 */
2014-08-21 23:08:34 +08:00
(function (module) {
'use strict';
var
ko = require('../../External/ko.js'),
Enums = require('../../Common/Enums.js'),
Utils = require('../../Common/Utils.js'),
2014-08-22 23:08:56 +08:00
AppSettings = require('../../Storages/AppSettings.js'),
2014-08-21 23:08:34 +08:00
Data = require('../../Storages/AdminDataStorage.js'),
Remote = require('../../Storages/AdminAjaxRemoteStorage.js'),
kn = require('../../Knoin/Knoin.js'),
KnoinAbstractViewModel = require('../../Knoin/KnoinAbstractViewModel.js')
;
/**
* @constructor
* @extends KnoinAbstractViewModel
*/
function PopupsActivateViewModel()
{
KnoinAbstractViewModel.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);
}
2014-08-21 23:08:34 +08:00
kn.extendAsViewModel('PopupsActivateViewModel', PopupsActivateViewModel);
2014-08-21 23:08:34 +08:00
PopupsActivateViewModel.prototype.onShow = function ()
{
2014-08-22 23:08:56 +08:00
this.domain(AppSettings.settingsGet('AdminDomain'));
2014-08-21 23:08:34 +08:00
if (!this.activateProcess())
{
this.key('');
this.activateText('');
this.activateText.isError(false);
this.activationSuccessed(false);
}
};
2014-08-21 23:08:34 +08:00
PopupsActivateViewModel.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}
*/
PopupsActivateViewModel.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-22 23:08:56 +08:00
module.exports = PopupsActivateViewModel;
2014-08-21 23:08:34 +08:00
}(module));