forked from verdnatura/hedera-web
542 lines
10 KiB
JavaScript
Executable File
542 lines
10 KiB
JavaScript
Executable File
|
|
Vn.includeCss ('js/hedera/gui.css');
|
|
Vn.resource ('js/hedera/gui.xml');
|
|
Vn.define (function () {
|
|
|
|
Vn.Gui = new Class
|
|
({
|
|
Extends: Htk.Widget,
|
|
Properties:
|
|
{
|
|
conn:
|
|
{
|
|
set: function (x)
|
|
{
|
|
this.link ({_conn: x},
|
|
{
|
|
'error': this.onConnError
|
|
,'loading-changed': this.onConnLoading
|
|
});
|
|
|
|
var sql = 'SELECT default_form, image_dir, image_host FROM config;'
|
|
+'SELECT production_domain, test_domain FROM config;'
|
|
+'SELECT name FROM customer_account;'
|
|
+'CALL form_list ();';
|
|
this.conn.execQuery (sql, this.onMainQueryDone.bind (this));
|
|
},
|
|
get: function ()
|
|
{
|
|
return this._conn;
|
|
}
|
|
}
|
|
}
|
|
|
|
,forms: {}
|
|
,activeForm: null
|
|
,activeCss: null
|
|
,requestedForm: null
|
|
,menuShown: false
|
|
,menuOptions: {}
|
|
,choosedOption: null
|
|
|
|
,initialize: function (props)
|
|
{
|
|
this.parent (props);
|
|
this.builderInit ('js/hedera/gui.xml');
|
|
|
|
this.loadingCount = 0;
|
|
this.formHolder = this.$('form-holder');
|
|
|
|
this.hash = Vn.Hash;
|
|
this.hashParam = new Vn.HashParam ({key: 'form'});
|
|
this.hashParam.on ('changed', this._onFormChange, this);
|
|
|
|
this.$('background').onclick = function () {};
|
|
|
|
this.$('menu-button').addEventListener ('click', function (event)
|
|
{
|
|
event.stopPropagation ();
|
|
this.showMenu ();
|
|
}.bind (this));
|
|
|
|
this.$('left-panel').addEventListener ('click', function (event)
|
|
{
|
|
event.stopPropagation ();
|
|
});
|
|
|
|
if (!Vn.Cookie.check ('hedera_cookies'))
|
|
{
|
|
Vn.Cookie.set ('hedera_cookies', true);
|
|
Htk.Toast.showWarning (_('By using this site you accept cookies'));
|
|
}
|
|
}
|
|
|
|
,show: function ()
|
|
{
|
|
document.body.appendChild (this.node);
|
|
}
|
|
|
|
,hide: function ()
|
|
{
|
|
this.closeForm ();
|
|
Vn.Node.remove (this.node);
|
|
}
|
|
|
|
,onLogoutClick: function ()
|
|
{
|
|
this.conn.close (this.onConnClose.bind (this));
|
|
}
|
|
|
|
,onConnClose: function ()
|
|
{
|
|
this.signalEmit ('logout');
|
|
}
|
|
|
|
,showBackground: function ()
|
|
{
|
|
Vn.Node.show (this.$('background'));
|
|
}
|
|
|
|
,hideBackground: function ()
|
|
{
|
|
Vn.Node.hide (this.$('background'));
|
|
}
|
|
|
|
,showMenu: function ()
|
|
{
|
|
this.showBackground ();
|
|
Vn.Node.addClass (this.$('left-panel'), 'show');
|
|
this.menuShown = true;
|
|
|
|
this.hideMenuCallback = this.hideMenu.bind (this);
|
|
document.addEventListener ('click', this.hideMenuCallback);
|
|
}
|
|
|
|
,hideMenu: function ()
|
|
{
|
|
if (!this.menuShown)
|
|
return;
|
|
|
|
this.hideBackground ();
|
|
Vn.Node.removeClass (this.$('left-panel'), 'show');
|
|
this.menuShown = false;
|
|
|
|
document.removeEventListener ('click', this.hideMenuCallback);
|
|
this.hideMenuCallback = null;
|
|
}
|
|
|
|
,onMainQueryDone: function (resultSet)
|
|
{
|
|
// 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
|
|
|
|
var res = resultSet.fetchResult ();
|
|
|
|
if (res.next () && res.get ('test_domain'))
|
|
{
|
|
if (location.host != res.get ('production_domain'))
|
|
{
|
|
var linkText = 'ReturnToOldWebsite';
|
|
var linkField = 'production_domain';
|
|
}
|
|
else
|
|
{
|
|
var linkText = 'TestTheNewWebsite';
|
|
var linkField = 'test_domain';
|
|
}
|
|
|
|
Vn.Node.setText (this.$('test-link'), _(linkText));
|
|
this.$('test-link').href = '//'+ res.get (linkField);
|
|
}
|
|
else
|
|
Vn.Node.hide (this.$('test-link'));
|
|
|
|
// Retrieving the user name
|
|
|
|
var userName = resultSet.fetchValue ();
|
|
|
|
if (userName)
|
|
{
|
|
var span = this.$('user-name');
|
|
span.appendChild (document.createTextNode (userName));
|
|
}
|
|
|
|
// Retrieving menu sections
|
|
|
|
var res = resultSet.fetchResult ();
|
|
var sectionMap = {};
|
|
|
|
if (res)
|
|
for (var i = 0; res.next (); i++)
|
|
{
|
|
var parent = res.get ('parent');
|
|
|
|
if (!sectionMap[parent])
|
|
sectionMap[parent] = [];
|
|
|
|
sectionMap[parent].push (i);
|
|
}
|
|
|
|
this.createMenu (res, sectionMap, null, this.$('main-menu'));
|
|
|
|
// Loading the default form
|
|
|
|
this._onFormChange ();
|
|
}
|
|
|
|
,notifyError: function (error)
|
|
{
|
|
if (error instanceof Error)
|
|
{
|
|
var httpRequest = new Vn.HttpRequest ()
|
|
httpRequest.add
|
|
({
|
|
'file': error.fileName
|
|
,'line': error.lineNumber
|
|
,'message': error.message
|
|
,'stack': error.stack
|
|
});
|
|
httpRequest.send ('log.php');
|
|
}
|
|
}
|
|
|
|
,errorHandler: function (error)
|
|
{
|
|
if (error instanceof Vn.Error)
|
|
switch (error.domain)
|
|
{
|
|
case 'Auth':
|
|
Htk.Toast.showError (_('You\'ve been too idle'));
|
|
this.signalEmit ('logout');
|
|
break;
|
|
case 'Version':
|
|
this.newVersion (error);
|
|
break;
|
|
case 'User':
|
|
Htk.Toast.showError (error.message);
|
|
break;
|
|
default:
|
|
console.error (error.message);
|
|
Htk.Toast.showError (_('There was an internal error'));
|
|
}
|
|
else
|
|
{
|
|
console.error (error);
|
|
Htk.Toast.showError (_('There was an internal error'));
|
|
this.notifyError (error);
|
|
}
|
|
}
|
|
|
|
,onConnError: function (conn, error)
|
|
{
|
|
this.errorHandler (error);
|
|
}
|
|
|
|
,onConnLoading: function (conn, isLoading)
|
|
{
|
|
if (isLoading)
|
|
this.loaderPush ();
|
|
else
|
|
this.loaderPop ();
|
|
}
|
|
|
|
,loaderPush: function ()
|
|
{
|
|
this.loadingCount++;
|
|
|
|
if (this.loadingCount == 1)
|
|
this.$('loader').start ();
|
|
}
|
|
|
|
,loaderPop: function ()
|
|
{
|
|
if (this.loadingCount == 0)
|
|
return;
|
|
|
|
this.loadingCount--;
|
|
|
|
if (this.loadingCount == 0)
|
|
this.$('loader').stop ();
|
|
}
|
|
|
|
,newVersion: function (error)
|
|
{
|
|
if (this.newVersionBlock || this.skipVersion)
|
|
return;
|
|
|
|
this.newVersionBlock = true;
|
|
|
|
var reload;
|
|
var message = _('New version available') +"\n\n"+ error.message;
|
|
|
|
if (error.code == 'criticalVersion')
|
|
{
|
|
alert (message)
|
|
reload = true;
|
|
}
|
|
else
|
|
{
|
|
reload = confirm (message);
|
|
this.skipVersion = true;
|
|
}
|
|
|
|
if (reload)
|
|
location.reload ();
|
|
|
|
this.newVersionBlock = false;
|
|
}
|
|
|
|
,createMenu: function (res, sectionMap, parent, ul)
|
|
{
|
|
var sections = sectionMap[parent];
|
|
|
|
for (var i = 0; i < sections.length; i++)
|
|
{
|
|
res.row = sections[i];
|
|
|
|
var li = document.createElement ('li');
|
|
ul.appendChild (li);
|
|
|
|
var a = document.createElement ('a');
|
|
a.href = Vn.Hash.make ({'form': res.get ('path')});
|
|
this.menuOptions[res.get ('path')] = a;
|
|
li.appendChild (a);
|
|
|
|
var text = document.createTextNode (_(res.get ('description')));
|
|
a.appendChild (text);
|
|
|
|
var formId = res.get ('id');
|
|
|
|
if (sectionMap[formId])
|
|
{
|
|
var submenu = document.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)
|
|
{
|
|
if (this.menuShown)
|
|
return;
|
|
|
|
this.hideSubmenu ();
|
|
this.activeSubmenu = submenu;
|
|
|
|
var rect = parent.getBoundingClientRect ();
|
|
Vn.Node.addClass (submenu, 'show');
|
|
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, 'show');
|
|
submenu.style.left = 'initial';
|
|
submenu.style.top = 'initial';
|
|
clearTimeout (this.timeout);
|
|
this.activeSubmenu = null;
|
|
this.timeout = 0;
|
|
}
|
|
}
|
|
|
|
,_onFormChange: function ()
|
|
{
|
|
var formPath = this.hashParam.value;
|
|
|
|
if (!formPath)
|
|
formPath = Vn.Config['default_form'];
|
|
|
|
this.openForm (formPath,
|
|
this._onFormLoad.bind (this));
|
|
}
|
|
|
|
,openForm: function (formPath, callback)
|
|
{
|
|
this.hideMenu ();
|
|
this.loaderPush ();
|
|
|
|
this.closeForm ();
|
|
this.requestedForm = formPath;
|
|
|
|
var newChoosedOption = this.menuOptions[formPath];
|
|
|
|
if (newChoosedOption)
|
|
{
|
|
Vn.Node.addClass (newChoosedOption, 'selected');
|
|
this.choosedOption = newChoosedOption;
|
|
}
|
|
|
|
this.activeCss = 'forms/'+ formPath +'/style.css';
|
|
Vn.includeCss (this.activeCss);
|
|
|
|
var formInfo = this.forms[formPath];
|
|
|
|
if (!formInfo)
|
|
{
|
|
formInfo = new Vn.Module ('forms', formPath);
|
|
this.forms[formPath] = formInfo;
|
|
}
|
|
|
|
formInfo.addCallback (callback);
|
|
}
|
|
|
|
,_onFormLoad: function (formInfo)
|
|
{
|
|
this.loaderPop ();
|
|
|
|
if (formInfo.error)
|
|
{
|
|
Htk.Toast.showError (_('Error loading form'));
|
|
return;
|
|
}
|
|
|
|
this.activeForm = new formInfo.klass (this, formInfo);
|
|
this.activeForm.open ();
|
|
this.activeForm.activate ();
|
|
}
|
|
|
|
,setTitle: function (title)
|
|
{
|
|
Vn.Node.removeChilds (this.$('title'));
|
|
|
|
if (title)
|
|
this.$('title').appendChild (title);
|
|
}
|
|
|
|
,setActions: function (actions)
|
|
{
|
|
Vn.Node.removeChilds (this.$('action-bar'));
|
|
|
|
if (actions)
|
|
this.$('action-bar').appendChild (actions);
|
|
}
|
|
|
|
,closeForm: function ()
|
|
{
|
|
if (this.activeForm)
|
|
{
|
|
this.activeForm.deactivate ();
|
|
this.activeForm.close ();
|
|
this.activeForm.unref ();
|
|
this.activeForm = null;
|
|
}
|
|
|
|
if (this.activeCss)
|
|
{
|
|
Vn.excludeCss (this.activeCss);
|
|
this.activeCss = null;
|
|
}
|
|
|
|
if (this.choosedOption)
|
|
{
|
|
Vn.Node.removeClass (this.choosedOption, 'selected');
|
|
this.choosedOption = null;
|
|
}
|
|
}
|
|
|
|
,openReport: function (reportName, batch)
|
|
{
|
|
this.loaderPush ();
|
|
|
|
var module = new Vn.Module ('reports', reportName);
|
|
module.addCallback (this._onReportLoad.bind (this, batch));
|
|
}
|
|
|
|
,_onReportLoad: function (batch, module)
|
|
{
|
|
this.loaderPop ();
|
|
|
|
if (module.error)
|
|
{
|
|
Htk.Toast.showError (_('Error loading report'));
|
|
return;
|
|
}
|
|
|
|
var report = new module.klass (module, this);
|
|
report.open (batch);
|
|
}
|
|
|
|
,supplantUser: function (userId, callback)
|
|
{
|
|
var batch = new Sql.Batch ();
|
|
batch.addValue ('user', userId);
|
|
|
|
var query = 'UPDATE user_session_view SET user_id = #user;'+
|
|
'SELECT Cliente FROM vn2008.Clientes WHERE Id_cliente = #user';
|
|
this.conn.execQuery (query,
|
|
this._onUserSupplant.bind (this, userId, callback), batch);
|
|
}
|
|
|
|
,_onUserSupplant: function (userId, callback, resultSet)
|
|
{
|
|
this._supplantClear ();
|
|
this._supplanted = userId;
|
|
|
|
resultSet.fetchResult ();
|
|
var userName = resultSet.fetchValue ();
|
|
|
|
Vn.Node.setText (this.$('supplanted'), userName);
|
|
Vn.Node.show (this.$('supplant'));
|
|
|
|
if (callback)
|
|
callback ();
|
|
}
|
|
|
|
,_supplantClear: function ()
|
|
{
|
|
if (this._supplanted)
|
|
{
|
|
Vn.Node.hide (this.$('supplant'));
|
|
this._supplanted = null;
|
|
}
|
|
}
|
|
|
|
,onSupplantExitClick: function ()
|
|
{
|
|
var query = 'UPDATE user_session_view SET user_id = account.user_get_id ()'
|
|
this.conn.execQuery (query, this.supplantExit.bind (this));
|
|
}
|
|
|
|
,supplantExit: function ()
|
|
{
|
|
this._supplantClear ();
|
|
this._onFormChange ();
|
|
}
|
|
|
|
,_destroy: function ()
|
|
{
|
|
this.hide ();
|
|
this.hashParam.unref ();
|
|
this.parent ();
|
|
}
|
|
});
|
|
|
|
});
|
|
|