0
1
Fork 0
hedera-web-mindshore/js/hedera/login.js

183 lines
3.3 KiB
JavaScript

var Css = require ('./login.css');
var Tpl = require ('./login.xml');
module.exports = new Class
({
Extends: Htk.Widget,
Properties:
{
conn:
{
type: Db.Connection
,set: function (x)
{
this.link ({_conn: x}, {'loading-changed': this._onConnLoadChange});
}
,get: function ()
{
return this._conn;
}
}
}
,initialize: function (props)
{
this.parent (props);
this.builderInitString (Tpl);
var self = this;
this.$('form').onsubmit = function ()
{
self._onSubmit ();
return false;
};
}
,_onConnLoadChange: function (conn, isLoading)
{
if (isLoading)
this.$('spinner').start ();
else
this.$('spinner').stop ();
}
,show: function ()
{
document.body.appendChild (this.node);
var isGuest = new Vn.HashParam
({
type: Boolean,
key: 'guest'
});
this.link ({_isGuest: isGuest}, {'changed': this._onGuestChange});
var token = new Vn.HashParam
({
type: String,
key: 'token'
});
this.link ({_token: token}, {'changed': this._onTokenChange});
this._onGuestChange ();
this._onTokenChange ();
if (!this._loginStarted
&& (localStorage.getItem ('vnToken')
|| sessionStorage.getItem ('vnToken')))
this.login ();
if (!this._loginStarted)
{
var lastUser = localStorage.getItem ('hederaLastUser');
if (lastUser)
this.$('user').value = lastUser;
this._focusUserInput ();
}
}
,_onTokenChange: function ()
{
if (!this.loginStarted && this._token.value)
{
this._conn.token = this._token.value;
this.login ();
}
}
,_onGuestChange: function ()
{
var guest = localStorage.getItem ('hederaGuest');
if (!this.loginStarted && (this._isGuest.value || guest))
{
localStorage.setItem ('hederaGuest', true);
this.login ();
}
}
,_onSubmit: function ()
{
this.login (
this.$('user').value,
this.$('pass').value,
this.$('remember').checked
);
}
,login: function (user, pass, remember)
{
this._loginStarted = true;
this._conn.open (user, pass, remember,
this._onConnOpen.bind (this));
this._disableUi (true);
}
,_onConnOpen: function (conn, success, error)
{
this._token.value = undefined;
this._isGuest.value = undefined;
this._loginStarted = false;
this.$('pass').value = '';
this._disableUi (false);
if (success)
{
var user = this.$('user').value;
if (user && !localStorage.getItem ('hederaGuest'))
localStorage.setItem ('hederaLastUser', user);
this.signalEmit ('login');
}
else
{
localStorage.removeItem ('hederaGuest');
this._focusUserInput ();
}
}
,hide: function ()
{
this._isGuest.unref ();
this._token.unref ();
Vn.Node.remove (this.node);
}
,_focusUserInput: function ()
{
var userEntry = this.$('user');
userEntry.focus ();
userEntry.select ();
}
,_disableUi: function (disabled)
{
this.$('user').disabled = disabled;
this.$('pass').disabled = disabled;
this.$('submit').disabled = disabled;
}
,onPasswordLost: function ()
{
var user = this.$('user').value;
if (!user)
Htk.Toast.showError (_('Please write your user name'));
else
this._conn.send ('core/recover-password', {'user': user},
this._onPasswordRecovered.bind (this));
}
,_onPasswordRecovered: function (json, error)
{
if (json)
Htk.Toast.showMessage (_('A mail has been sent with your new password'));
else
Htk.Toast.showError (error.message);
}
});