2015-09-11 09:37:16 +00:00
|
|
|
|
|
|
|
Vn.App = new Class
|
|
|
|
({
|
2015-11-09 08:14:33 +00:00
|
|
|
Extends: Vn.Object
|
|
|
|
|
|
|
|
,initialize: function ()
|
2015-09-11 09:37:16 +00:00
|
|
|
{
|
2015-12-10 13:48:43 +00:00
|
|
|
window.onerror = this._onWindowError.bind (this);
|
|
|
|
window.onunload = this._onWindowUnload.bind (this);
|
2015-09-11 09:37:16 +00:00
|
|
|
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);
|
|
|
|
|
2015-12-15 15:22:46 +00:00
|
|
|
var conn = new Db.Conn ();
|
|
|
|
this.link ({_conn: conn}, {'error': this._onConnError});
|
2015-09-11 09:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
,run: function ()
|
|
|
|
{
|
2015-12-15 15:22:46 +00:00
|
|
|
var guest = Vn.Cookie.check ('hedera_guest');
|
2015-12-19 15:42:38 +00:00
|
|
|
|
|
|
|
if (guest)
|
|
|
|
Vn.Cookie.unset ('hedera_guest');
|
2015-12-15 15:22:46 +00:00
|
|
|
|
2015-09-11 09:37:16 +00:00
|
|
|
if (Vn.Cookie.check ('vn_pass'))
|
|
|
|
{
|
2015-12-10 13:48:43 +00:00
|
|
|
this._conn.open (null, null, null,
|
|
|
|
this._onAutoLogin.bind (this));
|
|
|
|
}
|
2015-12-15 15:22:46 +00:00
|
|
|
else if (this._isGuest.value || guest)
|
2015-12-10 13:48:43 +00:00
|
|
|
{
|
|
|
|
this._guestLogin ();
|
2015-09-11 09:37:16 +00:00
|
|
|
}
|
|
|
|
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-09-11 09:37:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2015-12-19 15:42:38 +00:00
|
|
|
this._isGuest.value = undefined;
|
|
|
|
|
|
|
|
if (success)
|
2015-12-10 13:48:43 +00:00
|
|
|
{
|
|
|
|
Vn.Cookie.set ('hedera_guest', true);
|
2015-12-10 23:24:14 +00:00
|
|
|
this._onLogin ();
|
2015-12-10 13:48:43 +00:00
|
|
|
}
|
2015-12-19 15:42:38 +00:00
|
|
|
else
|
|
|
|
this.run ();
|
2015-12-10 13:48:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
,_onAutoLogin: function (success)
|
2015-09-11 09:37:16 +00:00
|
|
|
{
|
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
Vn.Cookie.unset ('vn_pass');
|
|
|
|
this.run ();
|
|
|
|
}
|
|
|
|
else
|
2015-12-10 23:24:14 +00:00
|
|
|
this._onLogin ();
|
2015-09-11 09:37:16 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 13:48:43 +00:00
|
|
|
,_onLogin: function ()
|
2015-09-11 09:37:16 +00:00
|
|
|
{
|
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);
|
2015-09-11 09:37:16 +00:00
|
|
|
gui.show ();
|
|
|
|
}
|
|
|
|
|
2015-12-10 13:48:43 +00:00
|
|
|
,_onLogout: function (gui)
|
2015-09-11 09:37:16 +00:00
|
|
|
{
|
2015-12-10 13:48:43 +00:00
|
|
|
this._freeGui ();
|
|
|
|
Vn.Cookie.unset ('hedera_guest');
|
2015-09-11 09:37:16 +00:00
|
|
|
Vn.Cookie.unset ('vn_pass');
|
|
|
|
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)
|
2015-09-11 09:37:16 +00:00
|
|
|
{
|
|
|
|
var error = new Error (message);
|
|
|
|
error.fileName = file;
|
|
|
|
error.lineNumber = line;
|
2015-12-10 13:48:43 +00:00
|
|
|
this._notifyError (error);
|
2015-09-11 09:37:16 +00:00
|
|
|
}
|
2015-12-15 15:22:46 +00:00
|
|
|
|
|
|
|
,_onConnError: function (conn, error)
|
|
|
|
{
|
|
|
|
if (error instanceof Vn.Error)
|
2016-07-22 20:00:27 +00:00
|
|
|
switch (error.code)
|
2015-12-15 15:22:46 +00:00
|
|
|
{
|
2016-07-22 20:00:27 +00:00
|
|
|
case 'badLogin':
|
|
|
|
Htk.Toast.showError (_('Invalid login'));
|
|
|
|
this._logout ();
|
|
|
|
break;
|
|
|
|
case 'sessionExpired':
|
|
|
|
Htk.Toast.showError (_('You\'ve been too idle'));
|
|
|
|
this._logout ();
|
|
|
|
break;
|
|
|
|
case 'newVersion':
|
2015-12-15 15:22:46 +00:00
|
|
|
this._newVersion (error);
|
2016-07-22 20:00:27 +00:00
|
|
|
break;
|
|
|
|
case 'internalError':
|
2015-12-15 15:22:46 +00:00
|
|
|
Htk.Toast.showError (_('There was an internal error'));
|
2016-07-22 20:00:27 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Htk.Toast.showError (error.message);
|
2015-12-15 15:22:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
console.error (error);
|
|
|
|
this._notifyError (error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 20:00:27 +00:00
|
|
|
,_logout: function ()
|
|
|
|
{
|
|
|
|
if (this._gui)
|
|
|
|
this._gui.logout ();
|
|
|
|
}
|
|
|
|
|
2015-12-15 15:22:46 +00:00
|
|
|
,_newVersion: function (error)
|
|
|
|
{
|
|
|
|
if (this.newVersionBlock || this.skipVersion)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.newVersionBlock = true;
|
|
|
|
|
|
|
|
var reload;
|
|
|
|
var message = _('New version available') +"\n\n"+ error.message;
|
|
|
|
|
|
|
|
if (error.code == 'criticalVersion')
|
|
|
|
{
|
|
|
|
alert (message)
|
|
|
|
reload = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reload = confirm (message);
|
|
|
|
this.skipVersion = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reload)
|
|
|
|
location.reload ();
|
|
|
|
|
|
|
|
this.newVersionBlock = false;
|
|
|
|
}
|
2015-12-10 13:48:43 +00:00
|
|
|
|
|
|
|
,_notifyError: function (error)
|
2015-09-11 09:37:16 +00:00
|
|
|
{
|
2016-05-05 15:47:45 +00:00
|
|
|
Htk.Toast.showError (_('There was an internal error'));
|
|
|
|
|
|
|
|
var params = {
|
|
|
|
'file': error.fileName
|
|
|
|
,'line': error.lineNumber
|
|
|
|
,'message': error.message
|
|
|
|
,'stack': error.stack
|
|
|
|
};
|
|
|
|
|
2016-07-22 20:00:27 +00:00
|
|
|
var request = new Vn.JsonRequest ('log');
|
|
|
|
request.send (params);
|
2015-09-11 09:37:16 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-11 09:37:16 +00:00
|
|
|
,_destroy: function ()
|
|
|
|
{
|
2015-12-10 13:48:43 +00:00
|
|
|
this._freeLogin ();
|
|
|
|
this._freeGui ();
|
|
|
|
this._conn.unref ();
|
2015-09-11 09:37:16 +00:00
|
|
|
}
|
|
|
|
});
|