hedera-web/js/hedera/app.js

220 lines
3.7 KiB
JavaScript
Raw Normal View History

Vn.App = new Class
({
2016-09-24 14:32:31 +00:00
Extends: Vn.Object,
Properties:
{
conn:
{
type: Db.Connection
,get: function ()
{
return this._conn;
}
}
}
2015-11-09 08:14:33 +00:00
,initialize: function ()
{
2015-12-10 13:48:43 +00:00
window.onerror = this._onWindowError.bind (this);
window.onunload = this._onWindowUnload.bind (this);
Vn.Hash.initialize ();
2015-12-10 13:48:43 +00:00
this._isGuest = new Vn.HashParam
({
type: Boolean,
key: 'guest'
});
this._isGuest.on ('changed', this._onGuestChange, this);
2016-09-24 14:32:31 +00:00
var conn = new Db.Connection ();
this.link ({_conn: conn}, {'error': this._onConnError});
}
,run: function ()
{
2016-09-24 14:32:31 +00:00
var guest = localStorage.getItem ('hederaGuest');
if (guest)
2016-09-24 14:32:31 +00:00
localStorage.removeItem ('hederaGuest');
2016-09-24 14:32:31 +00:00
if (localStorage.getItem ('vnToken'))
{
2015-12-10 13:48:43 +00:00
this._conn.open (null, null, null,
this._onAutoLogin.bind (this));
}
else if (this._isGuest.value || guest)
2015-12-10 13:48:43 +00:00
{
this._guestLogin ();
}
else
{
2015-12-10 13:48:43 +00:00
var login = this._login = new Vn.Login ({conn: this._conn});
login.on ('login', this._onLogin, this);
login.show ();
}
}
2015-12-10 13:48:43 +00:00
,_onGuestChange: function ()
{
if (this._isGuest.value)
this._guestLogin ();
}
,_guestLogin: function ()
{
this._conn.open (
null,
null,
null,
this._onGuestLogin.bind (this),
true
);
}
,_onGuestLogin: function (conn, success)
{
this._isGuest.value = undefined;
if (success)
2015-12-10 13:48:43 +00:00
{
2016-09-24 14:32:31 +00:00
localStorage.setItem ('hederaGuest', true);
2015-12-10 23:24:14 +00:00
this._onLogin ();
2015-12-10 13:48:43 +00:00
}
else
this.run ();
2015-12-10 13:48:43 +00:00
}
,_onAutoLogin: function (success)
{
if (!success)
this.run ();
else
2015-12-10 23:24:14 +00:00
this._onLogin ();
}
2015-12-10 13:48:43 +00:00
,_onLogin: function ()
{
2015-12-10 13:48:43 +00:00
this._freeLogin ();
2015-12-10 23:24:14 +00:00
2015-12-10 13:48:43 +00:00
var gui = this._gui = new Vn.Gui ({conn: this._conn});
gui.on ('logout', this._onLogout, this);
gui.show ();
}
2015-12-10 13:48:43 +00:00
,_onLogout: function (gui)
{
2015-12-10 13:48:43 +00:00
this._freeGui ();
2016-09-24 14:32:31 +00:00
localStorage.removeItem ('hederaGuest');
this.run ();
}
2016-07-22 20:00:27 +00:00
,_onWindowUnload: function ()
{
this.unref ();
}
2015-12-10 13:48:43 +00:00
,_onWindowError: function (message, file, line)
{
var error = new Error (message);
error.fileName = file;
error.lineNumber = line;
2015-12-10 13:48:43 +00:00
this._notifyError (error);
}
,_onConnError: function (conn, error)
{
2016-08-22 10:41:05 +00:00
if (error instanceof Vn.JsonException)
{
2016-09-24 14:32:31 +00:00
if (error.exception)
var exception = error.exception
.replace (/\\/g, '.')
.replace (/Exception$/, '')
.replace (/^Vn\.Web\./, '');
else
var exception = null;
2016-09-23 22:47:34 +00:00
switch (exception)
{
case 'BadLogin':
Htk.Toast.showError (_('Invalid login'));
this._logout ();
break;
case 'SessionExpired':
Htk.Toast.showError (_('You\'ve been too idle'));
this._logout ();
break;
case 'OutdatedVersion':
this._newVersion (error);
break;
default:
Htk.Toast.showError (error.message);
}
}
else
{
console.error (error);
this._notifyError (error);
}
}
2016-07-22 20:00:27 +00:00
,_logout: function ()
{
if (this._gui)
this._gui.logout ();
}
,_newVersion: function (error)
{
2016-08-22 10:41:05 +00:00
if (this.skipVersion)
return;
2016-08-22 10:41:05 +00:00
this.skipVersion = true;
2016-08-22 10:41:05 +00:00
if (confirm (_('New version available')))
location.reload ();
}
2015-12-10 13:48:43 +00:00
,_notifyError: function (error)
{
Htk.Toast.showError (_('There was an internal error'));
var params = {
'file': error.fileName
,'line': error.lineNumber
,'message': error.message
,'stack': error.stack
};
2016-09-23 22:47:34 +00:00
this._conn.send ('core/log', params);
}
2015-12-10 13:48:43 +00:00
,_freeLogin: function ()
{
if (this._login)
{
this._login.disconnectByInstance (this);
this._login.hide ();
this._login.unref ();
this._login = null;
}
}
,_freeGui: function ()
{
if (this._gui)
{
this._gui.disconnectByInstance (this);
this._gui.hide ();
this._gui.unref ();
this._gui = null;
}
}
,_destroy: function ()
{
2015-12-10 13:48:43 +00:00
this._freeLogin ();
this._freeGui ();
this._conn.unref ();
}
});