2015-09-11 09:37:16 +00:00
|
|
|
|
2016-09-26 09:28:47 +00:00
|
|
|
var Login = require ('./login');
|
|
|
|
var Gui = require ('./gui');
|
|
|
|
|
|
|
|
module.exports = new Class
|
2015-09-11 09:37:16 +00:00
|
|
|
({
|
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-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);
|
2017-04-05 14:06:07 +00:00
|
|
|
this._hash = new Vn.Hash ({window: window});
|
2015-09-11 09:37:16 +00:00
|
|
|
|
2016-09-24 14:32:31 +00:00
|
|
|
var conn = new Db.Connection ();
|
2015-12-15 15:22:46 +00:00
|
|
|
this.link ({_conn: conn}, {'error': this._onConnError});
|
2016-11-07 18:35:43 +00:00
|
|
|
|
|
|
|
this.initAutoLogin ();
|
2015-09-11 09:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
,run: function ()
|
|
|
|
{
|
2016-11-07 18:35:43 +00:00
|
|
|
if (this.tryAutoLogin ())
|
|
|
|
return;
|
|
|
|
|
2017-04-05 14:06:07 +00:00
|
|
|
var login = this._login = new Login ({
|
|
|
|
conn: this._conn,
|
|
|
|
hash: this._hash
|
|
|
|
});
|
2016-10-13 15:07:48 +00:00
|
|
|
login.on ('login', this._onLogin, this);
|
|
|
|
login.show ();
|
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
|
|
|
|
2017-04-05 14:06:07 +00:00
|
|
|
var gui = this._gui = new Gui ({
|
|
|
|
conn: this._conn,
|
|
|
|
hash: this._hash
|
|
|
|
});
|
2015-12-10 13:48:43 +00:00
|
|
|
gui.on ('logout', this._onLogout, this);
|
2015-09-11 09:37:16 +00:00
|
|
|
gui.show ();
|
|
|
|
}
|
|
|
|
|
2017-03-09 12:30:39 +00:00
|
|
|
,_onLogout: function ()
|
2015-09-11 09:37:16 +00:00
|
|
|
{
|
2016-11-07 18:35:43 +00:00
|
|
|
this.clearAutoLogin ();
|
2016-10-13 15:07:48 +00:00
|
|
|
this._freeGui ();
|
2015-09-11 09:37:16 +00:00
|
|
|
this.run ();
|
|
|
|
}
|
|
|
|
|
2016-07-22 20:00:27 +00:00
|
|
|
,_onWindowUnload: function ()
|
|
|
|
{
|
|
|
|
this.unref ();
|
|
|
|
}
|
|
|
|
|
2016-10-14 10:58:35 +00:00
|
|
|
,_onWindowError: function (message, file, line, col, err)
|
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)
|
|
|
|
{
|
2016-08-22 10:41:05 +00:00
|
|
|
if (error instanceof Vn.JsonException)
|
2016-10-20 08:37:08 +00:00
|
|
|
switch (error.exception)
|
2015-12-15 15:22:46 +00:00
|
|
|
{
|
2016-10-20 08:37:08 +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 'OutdatedVersion':
|
2017-04-05 14:06:07 +00:00
|
|
|
this._newVersion ();
|
2016-10-20 08:37:08 +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 ();
|
|
|
|
}
|
|
|
|
|
2017-04-05 14:06:07 +00:00
|
|
|
,_newVersion: function ()
|
2015-12-15 15:22:46 +00:00
|
|
|
{
|
2016-10-20 08:37:08 +00:00
|
|
|
if (this.ignoreVersion)
|
2015-12-15 15:22:46 +00:00
|
|
|
return;
|
|
|
|
|
2016-10-20 08:37:08 +00:00
|
|
|
this.ignoreVersion = true;
|
2015-12-15 15:22:46 +00:00
|
|
|
|
2016-10-20 08:37:08 +00:00
|
|
|
var dialog = new Htk.Dialog ({
|
|
|
|
message: _('New version available')
|
|
|
|
,buttons: Htk.Dialog.Button.ACCEPT
|
|
|
|
,icon: 'warning'
|
|
|
|
});
|
|
|
|
dialog.on ('response', this._onNewVersionResponse, this);
|
|
|
|
dialog.open ();
|
|
|
|
}
|
|
|
|
|
|
|
|
,_onNewVersionResponse: function ()
|
|
|
|
{
|
|
|
|
location.reload ();
|
2015-12-15 15:22:46 +00:00
|
|
|
}
|
2015-12-10 13:48:43 +00:00
|
|
|
|
|
|
|
,_notifyError: function (error)
|
2015-09-11 09:37:16 +00:00
|
|
|
{
|
2016-10-20 08:37:08 +00:00
|
|
|
Htk.Toast.showError (_('Something went wrong'));
|
2016-05-05 15:47:45 +00:00
|
|
|
|
|
|
|
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-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 ();
|
2016-11-07 18:35:43 +00:00
|
|
|
this.deinitAutoLogin ();
|
2015-12-10 13:48:43 +00:00
|
|
|
this._conn.unref ();
|
2017-04-05 14:06:07 +00:00
|
|
|
this._hash.unref ();
|
2015-09-11 09:37:16 +00:00
|
|
|
}
|
2016-11-07 18:35:43 +00:00
|
|
|
|
|
|
|
// Auto login functionality
|
|
|
|
|
|
|
|
,_firstLogin: true
|
|
|
|
|
|
|
|
,initAutoLogin: function ()
|
|
|
|
{
|
2017-04-05 14:06:07 +00:00
|
|
|
var isGuest = new Vn.Param
|
2016-11-07 18:35:43 +00:00
|
|
|
({
|
2017-04-05 14:06:07 +00:00
|
|
|
lot: this._hash,
|
2016-11-07 18:35:43 +00:00
|
|
|
type: Boolean,
|
|
|
|
key: 'guest'
|
|
|
|
});
|
|
|
|
this.link ({_isGuest: isGuest}, {'changed': this._onGuestChange});
|
|
|
|
|
2017-04-05 14:06:07 +00:00
|
|
|
var token = new Vn.Param
|
2016-11-07 18:35:43 +00:00
|
|
|
({
|
2017-04-05 14:06:07 +00:00
|
|
|
lot: this._hash,
|
2016-11-07 18:35:43 +00:00
|
|
|
type: String,
|
|
|
|
key: 'token'
|
|
|
|
});
|
|
|
|
this.link ({_token: token}, {'changed': this._onTokenChange});
|
|
|
|
}
|
|
|
|
|
|
|
|
,_onGuestChange: function ()
|
|
|
|
{
|
|
|
|
if (this._isGuest.value)
|
|
|
|
setTimeout (this.tryAutoLogin.bind (this));
|
|
|
|
}
|
|
|
|
|
|
|
|
,_onTokenChange: function ()
|
|
|
|
{
|
|
|
|
if (this._token.value)
|
|
|
|
setTimeout (this.tryAutoLogin.bind (this));
|
|
|
|
}
|
|
|
|
|
|
|
|
,deinitAutoLogin: function ()
|
|
|
|
{
|
|
|
|
this._isGuest.unref ();
|
|
|
|
this._token.unref ();
|
|
|
|
}
|
|
|
|
|
|
|
|
,autoLogin: function ()
|
|
|
|
{
|
|
|
|
var guest = localStorage.getItem ('hederaGuest');
|
|
|
|
|
|
|
|
if (this._isGuest.value || guest)
|
|
|
|
{
|
|
|
|
localStorage.setItem ('hederaGuest', true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._token.value)
|
|
|
|
this._conn.token = this._token.value;
|
|
|
|
else
|
|
|
|
this._conn.fetchToken ();
|
|
|
|
|
|
|
|
if (this._conn.token)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
,tryAutoLogin: function ()
|
|
|
|
{
|
|
|
|
var ok = this.autoLogin ();
|
|
|
|
|
|
|
|
this._firstLogin = false;
|
|
|
|
this._isGuest.value = undefined;
|
|
|
|
this._token.value = undefined;
|
|
|
|
|
|
|
|
if (!ok)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
this._onLogin ();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
,clearAutoLogin: function ()
|
|
|
|
{
|
|
|
|
localStorage.removeItem ('hederaGuest');
|
|
|
|
}
|
2015-09-11 09:37:16 +00:00
|
|
|
});
|
2016-09-26 09:28:47 +00:00
|
|
|
|