hedera-web/js/hedera/gui.js

519 lines
11 KiB
JavaScript
Raw Normal View History

2022-11-16 01:44:39 +00:00
const Module = require('./module');
const Tpl = require('./gui.xml').default;
const formMap = require('../../import').formMap;
2022-05-21 21:31:56 +00:00
require('./gui.scss');
2018-05-11 09:25:10 +00:00
module.exports = new Class({
2022-06-06 12:49:18 +00:00
Extends: Vn.Component,
Properties: {
conn: {
2016-09-24 14:32:31 +00:00
type: Db.Connection
,set: function(x) {
this.link({_conn: x}, {'loading-changed': this._onConnLoadChange });
2015-12-10 13:48:43 +00:00
}
,get: function() {
return this._conn;
}
}
}
,forms: {}
,activeForm: null
,activeCss: null
,requestedForm: null
,menuShown: false
,menuOptions: {}
,choosedOption: null
,_shown: false
,_scrollTimeout: null
,_navbarVisible: true
,initialize: function(props) {
2022-06-06 12:49:18 +00:00
this.loadTemplateFromString(Tpl);
this.loadingCount = 0;
2022-05-28 01:18:06 +00:00
this.$.background.onclick = function() {};
2022-05-28 01:18:06 +00:00
this.$.leftPanel.addEventListener('click', function(event) {
event.stopPropagation();
});
2015-12-02 17:26:58 +00:00
2022-06-06 16:02:17 +00:00
Vn.Component.prototype.initialize.call(this, props);
2016-10-14 10:58:35 +00:00
2022-05-05 13:56:17 +00:00
var sql = 'SELECT id, name, nickname FROM account.myUser;'
2018-05-11 09:25:10 +00:00
+'SELECT defaultForm FROM config;'
2017-12-20 11:34:04 +00:00
+'SELECT url FROM imageConfig;'
2022-05-21 21:31:56 +00:00
+'SELECT dbproduccion FROM vn.config;'
2018-05-11 09:25:10 +00:00
+'SELECT productionDomain, testDomain FROM config;';
this._conn.execQuery(sql, this.onMainQueryDone.bind(this));
2016-10-14 10:58:35 +00:00
this.loadMenu();
}
,show: function() {
if (this._shown)
return;
this._shown = true;
this.doc.body.appendChild(this.node);
2022-05-28 01:18:06 +00:00
Htk.Toast.pushTop(this.$.formHolder);
if (Vn.isMobile()) {
this._onScrollHandler = this._onScroll.bind(this);
window.addEventListener('scroll', this._onScrollHandler );
}
2022-05-30 01:30:33 +00:00
this.formParam = new Vn.Param({
lot: this.hash,
name: 'form',
});
this.formParam.on('changed', this._onFormChange, this);
if (!localStorage.getItem('hederaCookies')) {
localStorage.setItem('hederaCookies', true);
Htk.Toast.showWarning(_('By using this site you accept cookies'));
}
2018-01-15 08:24:15 +00:00
this.supplantInit();
}
,hide: function() {
if (!this._shown)
return;
this._shown = false;
if (Vn.isMobile())
window.removeEventListener('scroll', this._onScrollHandler);
Htk.Toast.popTop();
this.formParam.unref();
this.closeForm();
this.hideMenu();
Vn.Node.remove(this.node);
}
,logout: function() {
this.onLogoutClick();
}
,onLogoutClick: function() {
this._conn.close(this._onConnClose.bind(this));
}
,_onConnClose: function() {
2022-05-30 01:30:33 +00:00
this.emit('logout');
}
,_onConnLoadChange: function(conn, isLoading) {
if (isLoading)
this.loaderPush();
else
this.loaderPop();
}
,onMainQueryDone: function(resultSet) {
2016-10-04 15:27:49 +00:00
// Retrieving the user name
2022-05-05 13:56:17 +00:00
this.user = resultSet.fetchObject();
2022-05-28 01:18:06 +00:00
Vn.Node.setText(this.$.userName, this.user.nickname);
2016-10-04 15:27:49 +00:00
// Retrieving configuration parameters
var res = resultSet.fetchResult();
var columns = res.columns;
if (res.next())
for (var i = 0; i < res.columns.length; i++)
Vn.Config[columns[i].name] = res.get(columns[i].name);
// Retrieving configuration parameters
Vn.Config.imageUrl = resultSet.fetchValue();
2017-12-20 11:34:04 +00:00
// Retrieving configuration parameters
var isTesting = !resultSet.fetchValue();
2018-01-17 14:00:13 +00:00
if (isTesting) {
2022-05-28 01:18:06 +00:00
this.$.devInfo.style.display = 'block';
this.$.version.textContent = Vn.Cookie.get('vnVersion');
2018-01-17 14:00:13 +00:00
}
// Retrieving configuration parameters
2022-05-28 15:49:46 +00:00
var res = resultSet.fetchObject();
2022-05-28 15:49:46 +00:00
if (res && res.testDomain) {
if (location.host != res.productionDomain) {
2018-01-17 14:00:13 +00:00
var linkText = 'Old website';
2018-05-11 09:25:10 +00:00
var linkField = 'productionDomain';
} else {
2018-01-17 14:00:13 +00:00
var linkText = 'Test the new website';
2018-05-11 09:25:10 +00:00
var linkField = 'testDomain';
}
2022-05-28 01:18:06 +00:00
Vn.Node.setText(this.$.testLink, _(linkText));
this.$.testLink.href = '//'+ res.get(linkField);
this.$.testLink.style.display = 'block';
} else
2022-05-28 01:18:06 +00:00
this.$.testLink.style.display = 'none';
2016-10-04 15:27:49 +00:00
// Loading the default form
this._onFormChange();
2016-10-04 15:27:49 +00:00
}
,loadMenu: function() {
2017-12-20 11:34:04 +00:00
var sql = 'SELECT * FROM myMenu';
this._conn.execQuery(sql, this._onMenuLoad.bind(this));
2016-10-04 15:27:49 +00:00
}
,_onMenuLoad: function(resultSet) {
// Retrieving menu sections
2022-05-28 15:49:46 +00:00
var res = resultSet.fetchData();
var sectionMap = {};
2022-05-28 15:49:46 +00:00
let i = 0;
for (const row of res) {
var parent = row.parentFk;
if (!sectionMap[parent])
sectionMap[parent] = [];
2022-05-28 15:49:46 +00:00
sectionMap[parent].push(i++);
}
2022-05-28 01:18:06 +00:00
Vn.Node.removeChilds(this.$.mainMenu);
this.createMenu(res, sectionMap, null, this.$.mainMenu);
}
2015-12-02 17:26:58 +00:00
//++++++++++++++++++++++++++++++++++++++++++++++++++++++ Menu
,createMenu: function(res, sectionMap, parent, ul) {
var sections = sectionMap[parent];
2022-04-20 13:03:29 +00:00
if (!sections) return;
for (var i = 0; i < sections.length; i++) {
res.row = sections[i];
2022-05-28 15:49:46 +00:00
const row = res[sections[i]];
var li = this.createElement('li');
ul.appendChild(li);
2022-05-28 15:49:46 +00:00
var text = this.createTextNode(_(row.description));
var a = this.createElement('a');
2022-05-28 15:49:46 +00:00
if (row.path) {
2022-05-30 01:30:33 +00:00
a.href = this.hash.make({form: row.path});
2022-05-28 15:49:46 +00:00
this.menuOptions[row.path] = a;
}
a.appendChild(text);
li.appendChild(a);
2022-05-28 15:49:46 +00:00
var formId = row.id;
if (sectionMap[formId]) {
var submenu = this.createElement('ul');
submenu.className = 'submenu';
li.appendChild(submenu);
li.addEventListener('mouseover',
this._onLiMouseHover.bind(this, submenu, a));
li.addEventListener('mouseout',
this._onLiMouseOut.bind(this));
this.createMenu(res, sectionMap, formId, submenu);
}
}
}
,_onLiMouseHover: function(submenu, parent) {
2015-09-16 16:11:15 +00:00
if (this.menuShown)
return;
this.hideSubmenu();
this.activeSubmenu = submenu;
var rect = parent.getBoundingClientRect();
Vn.Node.addClass(submenu, 'popup');
submenu.style.left = rect.right +'px';
submenu.style.top = rect.top +'px';
}
,_onLiMouseOut: function() {
this.timeout = setTimeout(this.hideSubmenu.bind(this), 160);
}
,hideSubmenu: function() {
var submenu = this.activeSubmenu;
if (submenu) {
Vn.Node.removeClass(submenu, 'popup');
submenu.style.left = '';
submenu.style.top = '';
clearTimeout(this.timeout);
this.activeSubmenu = null;
this.timeout = 0;
}
}
2016-10-04 15:27:49 +00:00
2022-05-26 06:08:31 +00:00
,onMenuClick(event) {
event.stopPropagation();
this.showMenu();
}
,showMenu: function() {
this.showBackground();
2022-05-28 01:18:06 +00:00
Vn.Node.addClass(this.$.leftPanel, 'show');
2016-10-04 15:27:49 +00:00
this.menuShown = true;
this.hideMenuCallback = this.hideMenu.bind(this);
this.doc.addEventListener('click', this.hideMenuCallback);
2016-10-04 15:27:49 +00:00
}
,hideMenu: function() {
2016-10-04 15:27:49 +00:00
if (!this.menuShown)
return;
this.hideBackground();
2022-05-28 01:18:06 +00:00
Vn.Node.removeClass(this.$.leftPanel, 'show');
2016-10-04 15:27:49 +00:00
this.menuShown = false;
this.doc.removeEventListener('click', this.hideMenuCallback);
2016-10-04 15:27:49 +00:00
this.hideMenuCallback = null;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++ Navigation bar
,_onScroll: function() {
2016-10-04 15:27:49 +00:00
if (this._scrollTimeout === null)
this._scrollTimeout = setTimeout(
this._scrollTimeoutFunc.bind(this), 150);
2016-10-04 15:27:49 +00:00
}
,_scrollTimeoutFunc: function() {
2016-10-04 15:27:49 +00:00
if (!this._shown)
return;
2022-05-28 01:18:06 +00:00
var navbar = this.$.topBar;
var yOffset = Vn.Browser.getPageYOffset();
2016-10-04 15:27:49 +00:00
var showNavbar = this._lastYOffset > yOffset || yOffset < navbar.offsetHeight;
if (showNavbar !== this._navbarVisible) {
2016-10-04 15:27:49 +00:00
if (showNavbar)
var translateY = 0;
else
var translateY = -navbar.offsetHeight;
navbar.style.transform =
'translateZ(0) translateY('+ translateY +'px)';
}
this._navbarVisible = showNavbar;
this._lastYOffset = yOffset;
this._scrollTimeout = null;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++ Background
,showBackground: function() {
2022-05-28 01:18:06 +00:00
Vn.Node.addClass(this.$.background, 'show');
2016-10-04 15:27:49 +00:00
}
,hideBackground: function() {
2022-05-28 01:18:06 +00:00
Vn.Node.removeClass(this.$.background, 'show');
2016-10-04 15:27:49 +00:00
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++ Spinner
,loaderPush: function() {
2016-10-04 15:27:49 +00:00
this.loadingCount++;
if (this.loadingCount == 1)
2022-05-28 01:18:06 +00:00
this.$.loader.start();
2016-10-04 15:27:49 +00:00
}
,loaderPop: function() {
2016-10-04 15:27:49 +00:00
if (this.loadingCount == 0)
return;
this.loadingCount--;
if (this.loadingCount == 0)
2022-05-28 01:18:06 +00:00
this.$.loader.stop();
2016-10-04 15:27:49 +00:00
}
2015-12-02 17:26:58 +00:00
//++++++++++++++++++++++++++++++++++++++++++++++++++++++ Forms
2022-11-16 01:44:39 +00:00
,async _onFormChange() {
var formPath = this.formParam.value;
if (!formPath)
2018-05-11 09:25:10 +00:00
formPath = Vn.Config.defaultForm;
this.hideMenu();
this.loaderPush();
2015-09-16 16:11:15 +00:00
this.closeForm();
this.requestedForm = formPath;
var newChoosedOption = this.menuOptions[formPath];
if (newChoosedOption) {
Vn.Node.addClass(newChoosedOption, 'selected');
this.choosedOption = newChoosedOption;
}
2022-11-16 01:44:39 +00:00
let FormKlass;
2022-11-16 01:44:39 +00:00
try {
FormKlass = (await this.importForm(formPath)).default;
} catch (err) {
this.loaderPop();
return Htk.Toast.showError(_('Error loading form'));
}
this.loaderPop();
if (!this._shown)
return;
2022-11-16 01:44:39 +00:00
this.activeForm = new FormKlass(this);
this.activeForm.open();
}
2022-11-16 01:44:39 +00:00
,async importForm(path) {
const fn = formMap[path];
return fn ? fn() : null;
}
,setForm: function(form) {
2022-11-15 21:26:48 +00:00
const holder = this.$.formHolder;
Vn.Node.removeChilds(holder);
if (form) {
2022-11-15 21:26:48 +00:00
holder.appendChild(form);
holder.classList.add('move-start');
setTimeout(() => this._onSetFormTimeout(), 10);
}
}
,_onSetFormTimeout: function() {
2022-11-15 21:26:48 +00:00
const holder = this.$.formHolder;
holder.classList.remove('move-start');
holder.classList.add('move-end');
holder.addEventListener('transitionend',
() => holder.classList.remove('move-end'),
{once: true}
);
}
,setTitle: function(title) {
2022-05-28 01:18:06 +00:00
Vn.Node.removeChilds(this.$.title);
if (title)
2022-05-28 01:18:06 +00:00
this.$.title.appendChild(title);
}
,setActions: function(actions) {
2022-05-28 01:18:06 +00:00
Vn.Node.removeChilds(this.$.actionBar);
if (actions)
2022-05-28 01:18:06 +00:00
this.$.actionBar.appendChild(actions);
2015-09-16 16:11:15 +00:00
}
,closeForm: function() {
if (this.activeForm) {
2022-05-28 01:18:06 +00:00
Vn.Node.removeClass(this.$.formHolder, 'show');
this.activeForm.close();
this.activeForm.unref();
2015-09-16 16:11:15 +00:00
this.activeForm = null;
}
if (this.activeCss) {
Vn.excludeCss(this.activeCss);
2015-09-16 16:11:15 +00:00
this.activeCss = null;
}
if (this.choosedOption) {
Vn.Node.removeClass(this.choosedOption, 'selected');
this.choosedOption = null;
}
}
2015-12-02 17:26:58 +00:00
//++++++++++++++++++++++++++++++++++++++++++++++++++++++ Reports
2022-05-30 01:30:33 +00:00
,openReport: function(reportName, lot) {
this.loaderPush();
var module = new Module('reports', reportName);
2022-05-30 01:30:33 +00:00
module.addCallback(this._onReportLoad.bind(this, lot));
}
2022-05-30 01:30:33 +00:00
,_onReportLoad: function(lot, module) {
this.loaderPop();
if (module.error) {
Htk.Toast.showError(_('Error loading report'));
return;
}
var report = new module.klass(module, this);
2022-05-30 01:30:33 +00:00
report.open(lot);
}
2015-12-02 17:26:58 +00:00
//++++++++++++++++++++++++++++++++++++++++++++++++++++++ Supplant
,supplantInit: function() {
var user = sessionStorage.getItem('supplantUser');
2018-01-15 08:24:15 +00:00
if (user != null)
this.supplantUser(user);
2018-01-15 08:24:15 +00:00
}
,supplantUser: function(user, callback) {
this._conn.supplantUser(user,
this._onUserSupplant.bind(this, callback, user));
}
2022-05-26 08:00:19 +00:00
,_onUserSupplant: function(callback, user, supplantOk, err) {
if (err) throw err;
sessionStorage.setItem('supplantUser', user);
this.loadMenu();
2016-10-04 15:27:49 +00:00
2018-05-11 09:25:10 +00:00
var sql = 'SELECT nickname FROM account.myUser';
this._conn.execQuery(sql, this._onSupplantName.bind(this));
if (callback)
callback();
}
,_onSupplantName: function(resultSet) {
var userName = resultSet.fetchValue();
2022-05-28 01:18:06 +00:00
Vn.Node.setText(this.$.supplanted, userName);
this.$.supplant.classList.toggle('show', true);
}
,onSupplantExitClick: function() {
2022-05-28 01:18:06 +00:00
this.$.supplant.classList.toggle('show', false);
this._conn.supplantEnd();
sessionStorage.removeItem('supplantUser',
sessionStorage.getItem('supplantUser'));
this.loadMenu();
this._onFormChange();
}
2015-12-02 17:26:58 +00:00
//++++++++++++++++++++++++++++++++++++++++++++++++++++++ Destroy
,_destroy: function() {
this.hide();
2022-06-06 16:02:17 +00:00
Vn.Component.prototype.initialize.call(this);
}
});