hedera-web/js/hedera/app.js

223 lines
4.1 KiB
JavaScript
Raw Normal View History

var Login = require('./login');
var Gui = require('./gui');
2016-09-26 09:28:47 +00:00
module.exports = new Class({
2016-09-24 14:32:31 +00:00
Extends: Vn.Object,
Properties:
{
conn:
{
type: Db.Connection
,get: function() {
2016-09-24 14:32:31 +00:00
return this._conn;
}
}
}
2015-11-09 08:14:33 +00:00
,initialize: function() {
window.onerror = this._onWindowError.bind(this);
window.onunload = this._onWindowUnload.bind(this);
Vn.Hash.initialize();
var conn = new Db.Connection();
this.link({_conn: conn}, {'error': this._onConnError});
2016-11-07 18:35:43 +00:00
this.initAutoLogin();
}
,run: function() {
if (this.tryAutoLogin())
2016-11-07 18:35:43 +00:00
return;
var login = this._login = new Login({conn: this._conn});
login.on('login', this._onLogin, this);
login.show();
}
,_onLogin: function() {
this._freeLogin();
2018-01-12 11:35:58 +00:00
if (this._gui)
return;
2015-12-10 23:24:14 +00:00
var gui = this._gui = new Gui({conn: this._conn});
gui.on('logout', this._onLogout, this);
gui.show();
}
,_onLogout: function() {
this.clearAutoLogin();
this._freeGui();
this.run();
}
,_onWindowUnload: function() {
this.unref();
2016-07-22 20:00:27 +00:00
}
,_onWindowError: function(message, file, line, col, err) {
var error = new Error(message);
error.fileName = file;
error.lineNumber = line;
this._notifyError(error);
}
,_onConnError: function(conn, error) {
2016-08-22 10:41:05 +00:00
if (error instanceof Vn.JsonException)
switch (error.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);
}
}
,_logout: function() {
2016-07-22 20:00:27 +00:00
if (this._gui)
this._gui.logout();
2016-07-22 20:00:27 +00:00
}
,_newVersion: function() {
if (this.ignoreVersion)
return;
this.ignoreVersion = true;
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-10 13:48:43 +00:00
,_notifyError: function(error) {
Htk.Toast.showError(_('Something went wrong'));
var params = {
2018-01-12 11:35:58 +00:00
file: error.fileName
,line: error.lineNumber
,message: error.message
,stack: error.stack
,agent: navigator.userAgent
,location: location.href
};
this._conn.send('core/log', params);
}
,_freeLogin: function() {
if (this._login) {
this._login.disconnectByInstance(this);
this._login.hide();
this._login.unref();
2015-12-10 13:48:43 +00:00
this._login = null;
}
}
,_freeGui: function() {
if (this._gui) {
this._gui.disconnectByInstance(this);
this._gui.hide();
this._gui.unref();
2015-12-10 13:48:43 +00:00
this._gui = null;
}
}
,_destroy: function() {
this._freeLogin();
this._freeGui();
this.deinitAutoLogin();
this._conn.unref();
}
2016-11-07 18:35:43 +00:00
// Auto login functionality
,_firstLogin: true
,initAutoLogin: function() {
var isGuest = new Vn.HashParam({
2016-11-07 18:35:43 +00:00
type: Boolean,
key: 'guest'
});
this.link({_isGuest: isGuest}, {'changed': this._onGuestChange});
2016-11-07 18:35:43 +00:00
var token = new Vn.HashParam({
2016-11-07 18:35:43 +00:00
type: String,
key: 'token'
});
this.link({_token: token}, {'changed': this._onTokenChange});
2016-11-07 18:35:43 +00:00
}
,_onGuestChange: function() {
2016-11-07 18:35:43 +00:00
if (this._isGuest.value)
setTimeout(this.tryAutoLogin.bind(this));
2016-11-07 18:35:43 +00:00
}
,_onTokenChange: function() {
2016-11-07 18:35:43 +00:00
if (this._token.value)
setTimeout(this.tryAutoLogin.bind(this));
2016-11-07 18:35:43 +00:00
}
,deinitAutoLogin: function() {
this._isGuest.unref();
this._token.unref();
2016-11-07 18:35:43 +00:00
}
,autoLogin: function() {
var guest = localStorage.getItem('hederaGuest');
2016-11-07 18:35:43 +00:00
if (this._isGuest.value || guest) {
localStorage.setItem('hederaGuest', true);
2016-11-07 18:35:43 +00:00
return true;
}
if (this._token.value)
this._conn.token = this._token.value;
else
this._conn.fetchToken();
2016-11-07 18:35:43 +00:00
if (this._conn.token)
return true;
return false;
}
,tryAutoLogin: function() {
var ok = this.autoLogin();
2016-11-07 18:35:43 +00:00
this._firstLogin = false;
this._isGuest.value = undefined;
this._token.value = undefined;
if (!ok)
return false;
this._onLogin();
2016-11-07 18:35:43 +00:00
return true;
}
,clearAutoLogin: function() {
localStorage.removeItem('hederaGuest');
2016-11-07 18:35:43 +00:00
}
});
2016-09-26 09:28:47 +00:00