hedera-web/js/hedera/app.js

274 lines
5.2 KiB
JavaScript
Raw Permalink Normal View History

2022-11-28 08:51:31 +00:00
const Login = require('./login');
const 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: {
2016-09-24 14:32:31 +00:00
type: Db.Connection
2022-11-16 01:46:44 +00:00
,get() {
2016-09-24 14:32:31 +00:00
return this._conn;
}
2023-01-30 10:08:26 +00:00
},
hash: {
type: Vn.Hash
,get() {
return this._hash;
}
},
gui: {
type: Gui
,get() {
return this._gui;
}
},
login: {
type: Login
,get() {
return this._login;
}
2016-09-24 14:32:31 +00:00
}
}
2015-11-09 08:14:33 +00:00
2022-11-16 01:46:44 +00:00
,initialize() {
2022-11-28 08:51:31 +00:00
window.addEventListener('error',
e => this._onWindowError(e));
window.addEventListener('unhandledrejection',
e => this._onWindowRejection(e));
window.addEventListener('unload',
() => this._onWindowUnload());
2022-05-30 01:30:33 +00:00
this._hash = new Vn.Hash({window: window});
2022-11-28 08:51:31 +00:00
const conn = new Db.Connection();
this.link({_conn: conn}, {'error': this._onConnError});
2016-11-07 18:35:43 +00:00
this.initAutoLogin();
}
2022-11-16 01:46:44 +00:00
,run() {
2022-11-28 08:51:31 +00:00
if (this.tryAutoLogin()) return;
this.showLogin();
}
,showLogin() {
const login = this._login = new Login({
2023-01-30 10:08:26 +00:00
app: this,
2022-05-30 01:30:33 +00:00
conn: this._conn,
hash: this._hash
});
login.on('login', this._onLogin, this);
login.show();
}
2022-11-28 08:51:31 +00:00
,async _onLogin() {
this._freeLogin();
2022-11-28 08:51:31 +00:00
if (this._gui) return;
2015-12-10 23:24:14 +00:00
2022-11-28 08:51:31 +00:00
const gui = this._gui = new Gui({
2023-01-30 10:08:26 +00:00
app: this,
2022-05-30 01:30:33 +00:00
conn: this._conn,
hash: this._hash
});
gui.on('logout', this._onLogout, this);
2022-11-28 08:51:31 +00:00
await gui.show();
}
2022-11-28 08:51:31 +00:00
,async _onLogout() {
this.clearAutoLogin();
2022-11-28 08:51:31 +00:00
await this._freeGui();
this.loggingOut = false;
this.showLogin();
}
2022-11-16 01:46:44 +00:00
,_onWindowUnload() {
this.unref();
2016-07-22 20:00:27 +00:00
}
2022-11-28 08:51:31 +00:00
,async _logout() {
if (this._gui && !this.loggingOut) {
this.loggingOut = true;
await this._gui.logout();
}
}
2022-11-16 01:46:44 +00:00
,_newVersion() {
2022-11-28 08:51:31 +00:00
if (this.ignoreVersion) return;
this.ignoreVersion = true;
2022-11-28 08:51:31 +00:00
const dialog = new Htk.Dialog({
message: _('New version available')
,buttons: Htk.Dialog.Button.ACCEPT
,icon: 'warning'
});
dialog.on('response', this._onNewVersionResponse, this);
dialog.open();
}
2022-11-16 01:46:44 +00:00
,_onNewVersionResponse() {
location.reload();
}
2015-12-10 13:48:43 +00:00
2022-11-16 01:46:44 +00:00
,_freeLogin() {
2022-11-28 08:51:31 +00:00
if (!this._login) return;
this._login.disconnectByInstance(this);
this._login.hide();
this._login.unref();
this._login = null;
2015-12-10 13:48:43 +00:00
}
2022-11-28 08:51:31 +00:00
,async _freeGui() {
if (!this._gui) return;
this._gui.disconnectByInstance(this);
await this._gui.hide();
this._gui.unref();
this._gui = null;
2015-12-10 13:48:43 +00:00
}
2022-11-16 01:46:44 +00:00
,_destroy() {
this._freeLogin();
this._freeGui();
this.deinitAutoLogin();
2022-11-28 08:51:31 +00:00
if (this._conn) this._conn.unref();
if (this._hash) this._hash.unref();
}
2016-11-07 18:35:43 +00:00
2022-11-28 08:51:31 +00:00
// Auto login
2016-11-07 18:35:43 +00:00
,_firstLogin: true
2022-11-16 01:46:44 +00:00
,initAutoLogin() {
2022-11-28 08:51:31 +00:00
const isGuest = new Vn.Param({
2022-05-30 01:30:33 +00:00
lot: this._hash,
2016-11-07 18:35:43 +00:00
type: Boolean,
2022-05-30 01:30:33 +00:00
name: 'guest'
2016-11-07 18:35:43 +00:00
});
this.link({_isGuest: isGuest}, {'changed': this._onGuestChange});
2016-11-07 18:35:43 +00:00
2022-11-28 08:51:31 +00:00
const token = new Vn.Param({
2022-05-30 01:30:33 +00:00
lot: this._hash,
2016-11-07 18:35:43 +00:00
type: String,
2022-05-30 01:30:33 +00:00
name: 'token'
2016-11-07 18:35:43 +00:00
});
this.link({_token: token}, {'changed': this._onTokenChange});
2016-11-07 18:35:43 +00:00
}
2022-11-16 01:46:44 +00:00
,_onGuestChange() {
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
}
2022-11-16 01:46:44 +00:00
,_onTokenChange() {
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
}
2022-11-16 01:46:44 +00:00
,deinitAutoLogin() {
2022-11-28 08:51:31 +00:00
if (this._isGuest) this._isGuest.unref();
if (this._token) this._token.unref();
2016-11-07 18:35:43 +00:00
}
2022-11-16 01:46:44 +00:00
,autoLogin() {
2022-11-28 08:51:31 +00:00
const 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;
}
2022-11-16 01:46:44 +00:00
,tryAutoLogin() {
2022-11-28 08:51:31 +00:00
const 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;
}
2022-11-16 01:46:44 +00:00
,clearAutoLogin() {
localStorage.removeItem('hederaGuest');
2016-11-07 18:35:43 +00:00
}
2022-11-28 08:51:31 +00:00
// Error management
,_onWindowError(event) {
this.globalHandler(event.error);
}
,_onWindowRejection(event) {
this.globalHandler(event.reason);
}
,async _onConnError(conn, err) {
if (!(err instanceof Vn.JsonException)) return;
switch (err.exception) {
case 'UserDisabledError':
2022-11-28 08:51:31 +00:00
Htk.Toast.showError(_('User disabled'));
await this._logout();
return;
case 'OutdatedVersionError':
2022-11-28 08:51:31 +00:00
this._newVersion();
return;
}
if (err.statusCode == 401 && !this._login) {
Htk.Toast.showError(_('Session expired'));
this._logout();
}
}
,async globalHandler(err) {
try {
if (!err) return;
if (err instanceof Vn.JsonException) {
const statusCode = err.statusCode;
if (statusCode >= 400 && statusCode < 500) {
if (err.statusCode == 403) {
Htk.Toast.showError(err.message ||
_('You don\'t have enough privileges'));
} else {
2022-11-28 08:51:31 +00:00
switch (err.exception) {
2023-01-16 12:59:11 +00:00
case 'UserDisabledError':
case 'OutdatedVersionError':
2022-11-28 08:51:31 +00:00
return;
}
if (err.statusCode == 401)
return;
Htk.Toast.showError(err.message);
}
} else
Htk.Toast.showError(err.message);
} else {
Htk.Toast.showError(_('Something went wrong'));
if (this._conn)
await this._conn.send('core/log', {
file: err.fileName
,line: err.lineNumber
,message: err.message
,stack: err.stack
,agent: navigator.userAgent
,location: location.href
});
}
} catch(e) {
console.error(e);
}
}
});
2016-09-26 09:28:47 +00:00