2022-11-28 08:51:31 +00:00
|
|
|
const Login = require('./login');
|
|
|
|
const Gui = require('./gui');
|
2016-09-26 09:28:47 +00:00
|
|
|
|
2019-09-05 16:16:10 +00:00
|
|
|
module.exports = new Class({
|
2016-09-24 14:32:31 +00:00
|
|
|
Extends: Vn.Object,
|
2019-09-06 09:22:34 +00:00
|
|
|
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-11-17 22:31:48 +00:00
|
|
|
|
2022-05-30 01:30:33 +00:00
|
|
|
this._hash = new Vn.Hash({window: window});
|
2015-09-11 09:37:16 +00:00
|
|
|
|
2022-11-28 08:51:31 +00:00
|
|
|
const conn = new Db.Connection();
|
2019-09-05 16:16:10 +00:00
|
|
|
this.link({_conn: conn}, {'error': this._onConnError});
|
2016-11-07 18:35:43 +00:00
|
|
|
|
2019-09-05 16:16:10 +00:00
|
|
|
this.initAutoLogin();
|
2015-09-11 09:37:16 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
});
|
2019-09-05 16:16:10 +00:00
|
|
|
login.on('login', this._onLogin, this);
|
|
|
|
login.show();
|
2015-09-11 09:37:16 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 08:51:31 +00:00
|
|
|
,async _onLogin() {
|
2019-09-05 16:16:10 +00:00
|
|
|
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
|
|
|
|
});
|
2019-09-05 16:16:10 +00:00
|
|
|
gui.on('logout', this._onLogout, this);
|
2022-11-28 08:51:31 +00:00
|
|
|
await gui.show();
|
2015-09-11 09:37:16 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 08:51:31 +00:00
|
|
|
,async _onLogout() {
|
2019-09-05 16:16:10 +00:00
|
|
|
this.clearAutoLogin();
|
2022-11-28 08:51:31 +00:00
|
|
|
await this._freeGui();
|
|
|
|
this.loggingOut = false;
|
|
|
|
this.showLogin();
|
2015-09-11 09:37:16 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_onWindowUnload() {
|
2019-09-05 16:16:10 +00:00
|
|
|
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();
|
2015-12-15 15:22:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_newVersion() {
|
2022-11-28 08:51:31 +00:00
|
|
|
if (this.ignoreVersion) return;
|
2016-10-20 08:37:08 +00:00
|
|
|
this.ignoreVersion = true;
|
2015-12-15 15:22:46 +00:00
|
|
|
|
2022-11-28 08:51:31 +00:00
|
|
|
const dialog = new Htk.Dialog({
|
2016-10-20 08:37:08 +00:00
|
|
|
message: _('New version available')
|
|
|
|
,buttons: Htk.Dialog.Button.ACCEPT
|
|
|
|
,icon: 'warning'
|
|
|
|
});
|
2019-09-05 16:16:10 +00:00
|
|
|
dialog.on('response', this._onNewVersionResponse, this);
|
|
|
|
dialog.open();
|
2016-10-20 08:37:08 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_onNewVersionResponse() {
|
2019-09-05 16:16:10 +00:00
|
|
|
location.reload();
|
2015-12-15 15:22:46 +00:00
|
|
|
}
|
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() {
|
2019-09-05 16:16:10 +00:00
|
|
|
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();
|
2015-09-11 09:37:16 +00:00
|
|
|
}
|
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
|
|
|
});
|
2019-09-05 16:16:10 +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
|
|
|
});
|
2019-09-05 16:16:10 +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)
|
2019-09-05 16:16:10 +00:00
|
|
|
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)
|
2019-09-05 16:16:10 +00:00
|
|
|
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
|
|
|
|
2019-09-05 16:16:10 +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
|
2019-09-05 16:16:10 +00:00
|
|
|
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;
|
|
|
|
|
2019-09-05 16:16:10 +00:00
|
|
|
this._onLogin();
|
2016-11-07 18:35:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,clearAutoLogin() {
|
2019-09-05 16:16:10 +00:00
|
|
|
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) {
|
2023-01-31 13:09:01 +00:00
|
|
|
case 'UserDisabledError':
|
2022-11-28 08:51:31 +00:00
|
|
|
Htk.Toast.showError(_('User disabled'));
|
|
|
|
await this._logout();
|
|
|
|
return;
|
2023-01-31 13:09:01 +00:00
|
|
|
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(_('You don\'t have enough privileges'));
|
|
|
|
else {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2015-09-11 09:37:16 +00:00
|
|
|
});
|
2016-09-26 09:28:47 +00:00
|
|
|
|