2013-11-16 06:21:12 +08:00
|
|
|
|
2014-08-25 23:49:01 +08:00
|
|
|
(function (module, require) {
|
|
|
|
|
|
|
|
'use strict';
|
2013-11-16 06:21:12 +08:00
|
|
|
|
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'),
|
2013-11-16 06:21:12 +08:00
|
|
|
|
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
|
|
|
;
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @extends KnoinAbstractViewModel
|
|
|
|
*/
|
|
|
|
function AdminLoginViewModel()
|
|
|
|
{
|
|
|
|
KnoinAbstractViewModel.call(this, 'Center', 'AdminLogin');
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
this.login = ko.observable('');
|
|
|
|
this.password = ko.observable('');
|
2014-07-29 04:56:18 +08:00
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
this.loginError = ko.observable(false);
|
|
|
|
this.passwordError = ko.observable(false);
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
this.loginFocus = ko.observable(false);
|
2014-07-29 04:56:18 +08:00
|
|
|
|
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);
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
this.submitRequest = ko.observable(false);
|
|
|
|
this.submitError = ko.observable('');
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
this.submitCommand = Utils.createCommand(this, function () {
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
Utils.triggerAutocompleteInputChange();
|
2013-11-16 06:21:12 +08:00
|
|
|
|
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())
|
2013-11-16 06:21:12 +08:00
|
|
|
{
|
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)
|
2013-11-16 06:21:12 +08:00
|
|
|
{
|
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));
|
|
|
|
}
|
2013-11-16 06:21:12 +08:00
|
|
|
}
|
2014-08-21 23:08:34 +08:00
|
|
|
else
|
2013-11-16 06:21:12 +08:00
|
|
|
{
|
|
|
|
this.submitRequest(false);
|
2014-08-21 23:08:34 +08:00
|
|
|
this.submitError(Utils.getNotification(Enums.Notification.UnknownError));
|
2013-11-16 06:21:12 +08:00
|
|
|
}
|
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
}, this), this.login(), this.password());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}, function () {
|
|
|
|
return !this.submitRequest();
|
|
|
|
});
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
kn.constructorEnd(this);
|
|
|
|
}
|
2014-07-29 04:56:18 +08:00
|
|
|
|
2014-09-02 08:15:31 +08:00
|
|
|
kn.extendAsViewModel(['View:Admin:Login', 'AdminLoginViewModel'], AdminLoginViewModel);
|
|
|
|
_.extend(AdminLoginViewModel.prototype, KnoinAbstractViewModel.prototype);
|
2013-12-09 23:16:58 +08:00
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
AdminLoginViewModel.prototype.onShow = function ()
|
|
|
|
{
|
|
|
|
kn.routeOff();
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
_.delay(_.bind(function () {
|
|
|
|
this.loginFocus(true);
|
|
|
|
}, this), 100);
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
};
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
AdminLoginViewModel.prototype.onHide = function ()
|
|
|
|
{
|
|
|
|
this.loginFocus(false);
|
|
|
|
};
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
AdminLoginViewModel.prototype.onBuild = function ()
|
|
|
|
{
|
|
|
|
Utils.triggerAutocompleteInputChange(true);
|
|
|
|
};
|
2013-11-16 06:21:12 +08:00
|
|
|
|
2014-08-21 23:08:34 +08:00
|
|
|
AdminLoginViewModel.prototype.submitForm = function ()
|
|
|
|
{
|
|
|
|
this.submitCommand();
|
|
|
|
};
|
2014-07-29 04:56:18 +08:00
|
|
|
|
2014-08-22 23:08:56 +08:00
|
|
|
module.exports = AdminLoginViewModel;
|
2014-07-29 04:56:18 +08:00
|
|
|
|
2014-08-25 23:49:01 +08:00
|
|
|
}(module, require));
|