2015-09-11 09:37:16 +00:00
|
|
|
|
2019-09-05 16:16:10 +00:00
|
|
|
var Login = require('./login');
|
|
|
|
var 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-09 08:14:33 +00:00
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,initialize() {
|
2019-09-05 16:16:10 +00:00
|
|
|
window.onerror = this._onWindowError.bind(this);
|
2022-11-17 22:31:48 +00:00
|
|
|
window.onunhandledrejection = e => this._onWindowRejection(e);
|
2019-09-05 16:16:10 +00:00
|
|
|
window.onunload = this._onWindowUnload.bind(this);
|
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
|
|
|
|
2019-09-05 16:16:10 +00:00
|
|
|
var conn = new Db.Connection();
|
|
|
|
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() {
|
2019-09-05 16:16:10 +00:00
|
|
|
if (this.tryAutoLogin())
|
2016-11-07 18:35:43 +00:00
|
|
|
return;
|
|
|
|
|
2022-05-30 01:30:33 +00:00
|
|
|
var login = this._login = new Login({
|
|
|
|
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-16 01:46:44 +00:00
|
|
|
,_onLogin() {
|
2019-09-05 16:16:10 +00:00
|
|
|
this._freeLogin();
|
2018-01-12 11:35:58 +00:00
|
|
|
|
|
|
|
if (this._gui)
|
|
|
|
return;
|
2015-12-10 23:24:14 +00:00
|
|
|
|
2022-05-30 01:30:33 +00:00
|
|
|
var gui = this._gui = new Gui({
|
|
|
|
conn: this._conn,
|
|
|
|
hash: this._hash
|
|
|
|
});
|
2019-09-05 16:16:10 +00:00
|
|
|
gui.on('logout', this._onLogout, this);
|
|
|
|
gui.show();
|
2015-09-11 09:37:16 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_onLogout() {
|
2019-09-05 16:16:10 +00:00
|
|
|
this.clearAutoLogin();
|
|
|
|
this._freeGui();
|
|
|
|
this.run();
|
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-16 01:46:44 +00:00
|
|
|
,_onWindowError(message, file, line) {
|
2019-09-05 16:16:10 +00:00
|
|
|
var error = new Error(message);
|
2015-09-11 09:37:16 +00:00
|
|
|
error.fileName = file;
|
|
|
|
error.lineNumber = line;
|
2019-09-05 16:16:10 +00:00
|
|
|
this._notifyError(error);
|
2015-09-11 09:37:16 +00:00
|
|
|
}
|
2022-11-17 22:31:48 +00:00
|
|
|
|
|
|
|
,_onWindowRejection(event) {
|
|
|
|
const err = event.reason;
|
|
|
|
this._onConnError(null, err);
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2015-12-15 15:22:46 +00:00
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_onConnError(conn, error) {
|
2022-05-26 08:00:19 +00:00
|
|
|
if (error instanceof Vn.JsonException) {
|
|
|
|
if (error.message)
|
2019-09-05 16:16:10 +00:00
|
|
|
Htk.Toast.showError(error.message);
|
2022-05-26 08:00:19 +00:00
|
|
|
else
|
|
|
|
switch (error.exception) {
|
|
|
|
case 'BadLogin':
|
|
|
|
Htk.Toast.showError(_('Invalid login'));
|
|
|
|
this._logout();
|
|
|
|
break;
|
|
|
|
case 'Forbidden':
|
|
|
|
Htk.Toast.showError(_('You don\'t have enough privileges'));
|
|
|
|
break;
|
|
|
|
case 'UserDisabled':
|
|
|
|
Htk.Toast.showError(_('User disabled'));
|
|
|
|
this._logout();
|
|
|
|
break;
|
|
|
|
case 'SessionExpired':
|
|
|
|
Htk.Toast.showError(_('Session expired'));
|
|
|
|
this._logout();
|
|
|
|
break;
|
|
|
|
case 'OutdatedVersion':
|
|
|
|
this._newVersion(error);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Htk.Toast.showError(error.message);
|
|
|
|
}
|
|
|
|
} else if (error.statusCode) {
|
|
|
|
switch (error.statusCode) {
|
2022-05-05 13:56:17 +00:00
|
|
|
case 401:
|
|
|
|
Htk.Toast.showError(_('Invalid login'));
|
|
|
|
this._logout();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Htk.Toast.showError(error.message);
|
2022-05-26 08:00:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-09-05 16:16:10 +00:00
|
|
|
console.error(error);
|
|
|
|
this._notifyError(error);
|
2015-12-15 15:22:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_logout() {
|
2016-07-22 20:00:27 +00:00
|
|
|
if (this._gui)
|
2019-09-05 16:16:10 +00:00
|
|
|
this._gui.logout();
|
2016-07-22 20:00:27 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_newVersion() {
|
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
|
|
|
|
2019-09-05 16:16:10 +00:00
|
|
|
var 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
|
|
|
,_notifyError(error) {
|
2019-09-05 16:16:10 +00:00
|
|
|
Htk.Toast.showError(_('Something went wrong'));
|
2016-05-05 15:47:45 +00:00
|
|
|
|
|
|
|
var params = {
|
2018-01-12 11:35:58 +00:00
|
|
|
file: error.fileName
|
|
|
|
,line: error.lineNumber
|
|
|
|
,message: error.message
|
|
|
|
,stack: error.stack
|
2019-09-05 16:16:10 +00:00
|
|
|
,agent: navigator.userAgent
|
|
|
|
,location: location.href
|
2016-05-05 15:47:45 +00:00
|
|
|
};
|
2019-09-05 16:16:10 +00:00
|
|
|
this._conn.send('core/log', params);
|
2015-09-11 09:37:16 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_freeLogin() {
|
2019-09-05 16:16:10 +00:00
|
|
|
if (this._login) {
|
|
|
|
this._login.disconnectByInstance(this);
|
|
|
|
this._login.hide();
|
|
|
|
this._login.unref();
|
2015-12-10 13:48:43 +00:00
|
|
|
this._login = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_freeGui() {
|
2019-09-05 16:16:10 +00:00
|
|
|
if (this._gui) {
|
|
|
|
this._gui.disconnectByInstance(this);
|
|
|
|
this._gui.hide();
|
|
|
|
this._gui.unref();
|
2015-12-10 13:48:43 +00:00
|
|
|
this._gui = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,_destroy() {
|
2019-09-05 16:16:10 +00:00
|
|
|
this._freeLogin();
|
|
|
|
this._freeGui();
|
|
|
|
this.deinitAutoLogin();
|
|
|
|
this._conn.unref();
|
2022-05-30 01:30:33 +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
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,initAutoLogin() {
|
2022-05-30 01:30:33 +00:00
|
|
|
var isGuest = new Vn.Param({
|
|
|
|
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-05-30 01:30:33 +00:00
|
|
|
var token = new Vn.Param({
|
|
|
|
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() {
|
2019-09-05 16:16:10 +00:00
|
|
|
this._isGuest.unref();
|
|
|
|
this._token.unref();
|
2016-11-07 18:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,autoLogin() {
|
2019-09-05 16:16:10 +00:00
|
|
|
var 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() {
|
2019-09-05 16:16:10 +00:00
|
|
|
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;
|
|
|
|
|
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
|
|
|
}
|
2015-09-11 09:37:16 +00:00
|
|
|
});
|
2016-09-26 09:28:47 +00:00
|
|
|
|