snappymail/dev/ViewModels/AdminLoginViewModel.js

121 lines
2.5 KiB
JavaScript
Raw Normal View History

2014-08-25 23:49:01 +08:00
(function (module, require) {
'use strict';
2014-08-21 23:08:34 +08:00
var
2014-08-25 23:49:01 +08:00
_ = require('_'),
ko = require('ko'),
2014-08-25 15:10:51 +08:00
2014-08-25 23:49:01 +08:00
Enums = require('Enums'),
Utils = require('Utils'),
2014-08-27 23:59:44 +08:00
Remote = require('Storage:Admin:Remote'),
2014-08-22 23:08:56 +08:00
2014-08-27 23:59:44 +08:00
kn = require('App:Knoin'),
KnoinAbstractViewModel = require('Knoin:AbstractViewModel')
2014-08-21 23:08:34 +08:00
;
2014-08-21 23:08:34 +08:00
/**
* @constructor
* @extends KnoinAbstractViewModel
*/
function AdminLoginViewModel()
{
KnoinAbstractViewModel.call(this, 'Center', 'AdminLogin');
2014-08-21 23:08:34 +08:00
this.login = ko.observable('');
this.password = ko.observable('');
2014-08-21 23:08:34 +08:00
this.loginError = ko.observable(false);
this.passwordError = ko.observable(false);
2014-08-21 23:08:34 +08:00
this.loginFocus = ko.observable(false);
2014-08-21 23:08:34 +08:00
this.login.subscribe(function () {
this.loginError(false);
}, this);
2014-07-29 18:28:02 +08:00
2014-08-21 23:08:34 +08:00
this.password.subscribe(function () {
this.passwordError(false);
}, this);
2014-08-21 23:08:34 +08:00
this.submitRequest = ko.observable(false);
this.submitError = ko.observable('');
2014-08-21 23:08:34 +08:00
this.submitCommand = Utils.createCommand(this, function () {
2014-08-21 23:08:34 +08:00
Utils.triggerAutocompleteInputChange();
2014-08-21 23:08:34 +08:00
this.loginError('' === Utils.trim(this.login()));
this.passwordError('' === Utils.trim(this.password()));
if (this.loginError() || this.passwordError())
{
2014-08-21 23:08:34 +08:00
return false;
}
this.submitRequest(true);
Remote.adminLogin(_.bind(function (sResult, oData) {
if (Enums.StorageResultType.Success === sResult && oData && 'AdminLogin' === oData.Action)
{
2014-08-21 23:08:34 +08:00
if (oData.Result)
{
2014-08-27 23:59:44 +08:00
require('App:Admin').loginAndLogoutReload();
2014-08-21 23:08:34 +08:00
}
else if (oData.ErrorCode)
{
this.submitRequest(false);
this.submitError(Utils.getNotification(oData.ErrorCode));
}
}
2014-08-21 23:08:34 +08:00
else
{
this.submitRequest(false);
2014-08-21 23:08:34 +08:00
this.submitError(Utils.getNotification(Enums.Notification.UnknownError));
}
2014-08-21 23:08:34 +08:00
}, this), this.login(), this.password());
return true;
}, function () {
return !this.submitRequest();
});
2014-08-21 23:08:34 +08:00
kn.constructorEnd(this);
}
kn.extendAsViewModel(['View:Admin:Login', 'AdminLoginViewModel'], AdminLoginViewModel);
_.extend(AdminLoginViewModel.prototype, KnoinAbstractViewModel.prototype);
2014-08-21 23:08:34 +08:00
AdminLoginViewModel.prototype.onShow = function ()
{
kn.routeOff();
2014-08-21 23:08:34 +08:00
_.delay(_.bind(function () {
this.loginFocus(true);
}, this), 100);
2014-08-21 23:08:34 +08:00
};
2014-08-21 23:08:34 +08:00
AdminLoginViewModel.prototype.onHide = function ()
{
this.loginFocus(false);
};
2014-08-21 23:08:34 +08:00
AdminLoginViewModel.prototype.onBuild = function ()
{
Utils.triggerAutocompleteInputChange(true);
};
2014-08-21 23:08:34 +08:00
AdminLoginViewModel.prototype.submitForm = function ()
{
this.submitCommand();
};
2014-08-22 23:08:56 +08:00
module.exports = AdminLoginViewModel;
2014-08-25 23:49:01 +08:00
}(module, require));