0
1
Fork 0
hedera-web-mindshore/js/hedera/gui.js

544 lines
10 KiB
JavaScript
Raw Normal View History

2016-09-26 09:28:47 +00:00
var Tpl = require ('./gui.xml');
2017-04-19 06:16:37 +00:00
var Module = require ('./module');
var Form = require ('./form');
2017-04-19 06:16:37 +00:00
require ('./gui.css');
/**
* The main screen component. It has a left menu, a navbar with configurable
2017-04-24 07:47:56 +00:00
* action buttons and the body where @Form childs can be displayed.
2017-04-19 06:16:37 +00:00
*/
2016-09-26 09:28:47 +00:00
module.exports = new Class
({
2017-10-18 16:01:21 +00:00
Extends: Vn.Component,
Properties:
{
2017-04-19 06:16:37 +00:00
/**
* The main connection object.
*/
conn:
{
2016-09-24 14:32:31 +00:00
type: Db.Connection
2015-12-10 13:48:43 +00:00
,set: function (x)
{
2016-10-14 10:58:35 +00:00
this.link ({_conn: x}, {'loading-changed': this._onConnLoadChange });
2015-12-10 13:48:43 +00:00
}
,get: function ()
{
return this._conn;
}
2017-04-19 06:16:37 +00:00
},
/**
* The current open form.
*/
activeForm:
{
type: Form
,get: function ()
{
return this._activeForm;
}
}
}
2017-04-19 06:16:37 +00:00
,_forms: {}
,_activeForm: null
,_requestedForm: null
,_menuShown: false
,_menuOptions: {}
,_choosedOption: null
,_shown: false
,_scrollTimeout: null
,_navbarVisible: true
,initialize: function (props)
{
2017-10-18 16:01:21 +00:00
this.loadTemplateFromString (Tpl);
this.loadingCount = 0;
2015-12-10 13:48:43 +00:00
this.parent (props);
2016-10-14 10:58:35 +00:00
2017-10-20 17:09:06 +00:00
this.$.socialBar.conn = this._conn;
2017-10-22 00:25:57 +00:00
this.$.user.conn = this._conn;
this.$.configQuery.conn = this._conn;
this.$.configQuery.execute ();
2016-10-14 10:58:35 +00:00
this.loadMenu ();
}
2017-10-22 00:25:57 +00:00
,show: function ()
{
if (this._shown)
return;
this._shown = true;
this.doc.body.appendChild (this.node);
2017-10-20 17:09:06 +00:00
Htk.Toast.pushTop (this.$.formHolder);
if (Vn.isMobile ())
{
this._onScrollHandler = this._onScroll.bind (this);
2017-04-08 11:42:27 +00:00
window.addEventListener ('scroll', this._onScrollHandler);
}
2017-04-05 14:06:07 +00:00
this.formParam = new Vn.Param ({
lot: this.hash,
2017-04-07 11:00:33 +00:00
name: 'form',
2017-04-05 14:06:07 +00:00
});
this.formParam.on ('changed', this._onFormChange, this);
2017-04-05 14:06:07 +00:00
2016-09-23 22:47:34 +00:00
if (!localStorage.getItem ('hederaCookies'))
{
2016-09-23 22:47:34 +00:00
localStorage.setItem ('hederaCookies', true);
Htk.Toast.showWarning (_('By using this site you accept cookies'));
}
}
,hide: function ()
{
if (!this._shown)
return;
this._shown = false;
if (Vn.isMobile ())
window.removeEventListener ('scroll', this._onScrollHandler);
2016-10-30 22:48:18 +00:00
Htk.Toast.popTop ();
this.formParam.unref ();
2015-09-16 16:11:15 +00:00
this.closeForm ();
2015-12-10 23:24:14 +00:00
this.hideMenu ();
Vn.Node.remove (this.node);
}
,logout: function ()
{
this.onLogoutClick ();
}
,onLogoutClick: function ()
{
this._conn.close (this._onConnClose.bind (this));
}
,_onConnClose: function ()
{
2017-04-19 06:16:37 +00:00
this.emit ('logout');
}
,_onConnLoadChange: function (conn, isLoading)
{
if (isLoading)
this.loaderPush ();
else
this.loaderPop ();
}
2017-10-22 00:25:57 +00:00
,onConfigQueryReady: function (query, resultSet)
{
// Retrieving configuration parameters
2017-04-08 11:42:27 +00:00
Object.assign (Vn.Config, resultSet.fetchObject ());
// Retrieving configuration parameters
2017-04-08 11:42:27 +00:00
var row = resultSet.fetchObject ();
2017-04-08 11:42:27 +00:00
if (row && row.test_domain)
{
2017-04-08 11:42:27 +00:00
if (location.host != row.production_domain)
{
var linkText = 'ReturnToOldWebsite';
var linkField = 'production_domain';
}
else
{
var linkText = 'TestTheNewWebsite';
var linkField = 'test_domain';
}
2017-10-22 00:25:57 +00:00
this.$.newVersion.assign ({
text: _(linkText),
link: '//'+ row[linkField]
});
}
else
2017-10-20 17:09:06 +00:00
Vn.Node.hide (this.$.testLink);
2016-10-04 15:27:49 +00:00
// Loading the default form
2016-10-04 15:27:49 +00:00
this._onFormChange ();
}
,loadMenu: function ()
{
2017-05-09 12:03:06 +00:00
var sql = 'CALL formList ()';
2016-10-04 15:27:49 +00:00
this._conn.execQuery (sql, this._onMenuLoad.bind (this));
}
2016-10-04 15:27:49 +00:00
,_onMenuLoad: function (resultSet)
{
// Retrieving menu sections
2017-04-08 11:42:27 +00:00
var row;
var res = resultSet.fetchResult ();
var sectionMap = {};
if (res)
2017-04-08 11:42:27 +00:00
for (var i = 0; row = res.fetchObject (); i++)
{
2017-04-08 11:42:27 +00:00
var parent = row.parent;
if (!sectionMap[parent])
sectionMap[parent] = [];
sectionMap[parent].push (i);
}
2017-10-20 17:09: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];
for (var i = 0; i < sections.length; i++)
{
res.row = sections[i];
2017-04-21 10:53:15 +00:00
var row = res.getObject (res.row);
var li = this.createElement ('li');
ul.appendChild (li);
2017-04-08 11:42:27 +00:00
var text = this.createTextNode (_(row.description));
var a = this.createElement ('a');
2017-03-30 11:44:53 +00:00
a.className = 'clickable';
2017-04-08 11:42:27 +00:00
if (row.path)
{
2017-04-08 11:42:27 +00:00
a.href = this.hash.make ({'form': row.path});
2017-04-19 06:16:37 +00:00
this._menuOptions[row.path] = a;
}
a.appendChild (text);
li.appendChild (a);
2017-04-08 11:42:27 +00:00
var formId = row.id;
if (sectionMap[formId])
{
var submenu = this.createElement ('ul');
submenu.className = 'submenu';
li.appendChild (submenu);
li.addEventListener ('mouseover',
2015-12-02 17:26:58 +00:00
this._onLiMouseHover.bind (this, submenu, a));
li.addEventListener ('mouseout',
2015-12-02 17:26:58 +00:00
this._onLiMouseOut.bind (this));
this.createMenu (res, sectionMap, formId, submenu);
}
}
}
2015-12-02 17:26:58 +00:00
,_onLiMouseHover: function (submenu, parent)
{
2017-04-19 06:16:37 +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';
}
2015-12-02 17:26:58 +00:00
,_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
2017-10-22 00:25:57 +00:00
,onMenuButtonClick: function (event)
{
event.stopPropagation ();
this.showMenu ();
}
,onLeftPanelClick: function (event)
{
event.stopPropagation ();
}
2016-10-04 15:27:49 +00:00
,showMenu: function ()
{
this.showBackground ();
2017-10-20 17:09:06 +00:00
Vn.Node.addClass (this.$.leftPanel, 'show');
2017-04-19 06:16:37 +00:00
this._menuShown = true;
2016-10-04 15:27:49 +00:00
this.hideMenuCallback = this.hideMenu.bind (this);
this.doc.addEventListener ('click', this.hideMenuCallback);
2016-10-04 15:27:49 +00:00
}
,hideMenu: function ()
{
2017-04-19 06:16:37 +00:00
if (!this._menuShown)
2016-10-04 15:27:49 +00:00
return;
this.hideBackground ();
2017-10-20 17:09:06 +00:00
Vn.Node.removeClass (this.$.leftPanel, 'show');
2017-04-19 06:16:37 +00:00
this._menuShown = false;
2016-10-04 15:27:49 +00:00
this.doc.removeEventListener ('click', this.hideMenuCallback);
2016-10-04 15:27:49 +00:00
this.hideMenuCallback = null;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++ Navigation bar
,_onScroll: function ()
{
if (this._scrollTimeout === null)
this._scrollTimeout = setTimeout (
this._scrollTimeoutFunc.bind (this), 150);
}
,_scrollTimeoutFunc: function ()
{
if (!this._shown)
return;
2017-10-22 00:25:57 +00:00
var navbar = this.$.navbar;
2017-04-08 11:42:27 +00:00
var yOffset = window.pageYOffset;
2016-10-04 15:27:49 +00:00
var showNavbar = this._lastYOffset > yOffset || yOffset < navbar.offsetHeight;
if (showNavbar !== this._navbarVisible)
{
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
2017-10-22 00:25:57 +00:00
,onBackgroundClick: function () {}
2016-10-04 15:27:49 +00:00
,showBackground: function ()
{
2017-10-20 17:09:06 +00:00
Vn.Node.addClass (this.$.background, 'show');
2016-10-04 15:27:49 +00:00
}
,hideBackground: function ()
{
2017-10-20 17:09:06 +00:00
Vn.Node.removeClass (this.$.background, 'show');
2016-10-04 15:27:49 +00:00
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++ Spinner
,loaderPush: function ()
{
this.loadingCount++;
if (this.loadingCount == 1)
2017-10-20 17:09:06 +00:00
this.$.loader.start ();
2016-10-04 15:27:49 +00:00
}
,loaderPop: function ()
{
if (this.loadingCount == 0)
return;
this.loadingCount--;
if (this.loadingCount == 0)
2017-10-20 17:09:06 +00:00
this.$.loader.stop ();
2016-10-04 15:27:49 +00:00
}
2015-12-02 17:26:58 +00:00
//++++++++++++++++++++++++++++++++++++++++++++++++++++++ Forms
,_onFormChange: function ()
{
var formPath = this.formParam.value;
2017-04-05 14:06:07 +00:00
if (!formPath)
formPath = Vn.Config['default_form'];
this.openForm (formPath,
this._onFormLoad.bind (this));
}
,openForm: function (formPath, callback)
{
this.hideMenu ();
this.loaderPush ();
2015-09-16 16:11:15 +00:00
this.closeForm ();
2017-04-19 06:16:37 +00:00
this._requestedForm = formPath;
2017-04-19 06:16:37 +00:00
var newChoosedOption = this._menuOptions[formPath];
if (newChoosedOption)
{
Vn.Node.addClass (newChoosedOption, 'selected');
2017-04-19 06:16:37 +00:00
this._choosedOption = newChoosedOption;
}
2017-04-19 06:16:37 +00:00
var formInfo = this._forms[formPath];
if (!formInfo)
{
2016-09-26 09:28:47 +00:00
formInfo = new Module ('forms', formPath);
2017-04-19 06:16:37 +00:00
this._forms[formPath] = formInfo;
}
2017-03-23 16:20:51 +00:00
formInfo.load (callback);
}
,_onFormLoad: function (formInfo)
{
this.loaderPop ();
if (formInfo.error)
{
Htk.Toast.showError (_('Error loading form'));
return;
}
2017-04-19 06:16:37 +00:00
this._activeForm = new formInfo.klass ({
gui: this,
formInfo: formInfo
});
this._activeForm.open ();
}
,setForm: function (form)
{
2017-10-20 17:09:06 +00:00
Vn.Node.removeChilds (this.$.formHolder);
if (form)
{
2017-03-17 12:42:10 +00:00
var div = this.createElement('div');
div.appendChild (form);
2017-10-20 17:09:06 +00:00
this.$.formHolder.appendChild (div);
2017-03-22 16:57:21 +00:00
setTimeout (this._onSetFormTimeout.bind (this), 10);
}
}
,_onSetFormTimeout: function ()
{
2017-10-20 17:09:06 +00:00
Vn.Node.addClass (this.$.formHolder.firstChild, 'slide');
}
,setTitle: function (title)
{
2017-10-20 17:09:06 +00:00
Vn.Node.setChild (this.$.title, title);
}
,setActions: function (actions)
{
2017-10-20 17:09:06 +00:00
Vn.Node.setChild (this.$.actionBar, actions);
2015-09-16 16:11:15 +00:00
}
,closeForm: function ()
{
2017-04-19 06:16:37 +00:00
if (this._activeForm)
2015-09-16 16:11:15 +00:00
{
2017-04-19 06:16:37 +00:00
this._activeForm.formInfo.unload ();
this._activeForm.close ();
this._activeForm.unref ();
this._activeForm = null;
2015-09-16 16:11:15 +00:00
}
2017-04-19 06:16:37 +00:00
if (this._choosedOption)
{
2017-04-19 06:16:37 +00:00
Vn.Node.removeClass (this._choosedOption, 'selected');
this._choosedOption = null;
}
}
2017-03-23 16:20:51 +00:00
,openReport: function (reportName, params)
{
2017-03-23 16:20:51 +00:00
var hashParams = {
form: 'preview',
report: reportName
};
2017-04-08 11:42:27 +00:00
this.hash.params = Object.assign (hashParams, params);
}
2017-03-23 16:20:51 +00:00
2015-12-02 17:26:58 +00:00
//++++++++++++++++++++++++++++++++++++++++++++++++++++++ Supplant
2017-03-23 16:20:51 +00:00
2016-10-04 15:27:49 +00:00
,supplantUser: function (user, callback)
{
2016-10-04 15:27:49 +00:00
this._conn.supplantUser (user,
this._onUserSupplant.bind (this, callback));
}
2016-10-04 15:27:49 +00:00
,_onUserSupplant: function (callback, supplantOk)
{
2016-10-04 15:27:49 +00:00
if (!supplantOk)
return;
2016-10-04 15:27:49 +00:00
this.loadMenu ();
var sql = 'SELECT name FROM customer_user';
this._conn.execQuery (sql, this._onSupplantName.bind (this));
if (callback)
callback ();
}
2016-10-04 15:27:49 +00:00
,_onSupplantName: function (resultSet)
{
2016-10-04 15:27:49 +00:00
var userName = resultSet.fetchValue ();
2017-10-20 17:09:06 +00:00
Vn.Node.setText (this.$.supplanted, userName);
Vn.Node.show (this.$.supplant);
}
2016-10-04 15:27:49 +00:00
,onSupplantExitClick: function ()
{
2017-10-20 17:09:06 +00:00
Vn.Node.hide (this.$.supplant);
2016-10-04 15:27:49 +00:00
this._conn.supplantEnd ();
this.loadMenu ();
this._onFormChange ();
}
2015-12-02 17:26:58 +00:00
//++++++++++++++++++++++++++++++++++++++++++++++++++++++ Destroy
,_destroy: function ()
{
2015-09-16 16:11:15 +00:00
this.hide ();
this.parent ();
}
});