0
1
Fork 0
hedera-web-mindshore/package/usr/share/hedera-web/pages/web/web.js

443 lines
9.1 KiB
JavaScript
Raw Normal View History

window.addEventListener ('load',
function () { Vn.Web.initialize (); });
Vn.Web =
{
2015-01-31 01:05:12 +00:00
forms: {}
,activeForm: null
2015-02-08 15:38:38 +00:00
,activeCss: null
,requestedForm: null
2015-01-31 01:05:12 +00:00
,menuShown: false
,menuOptions: {}
,choosedOption: null
,initialize: function ()
{
this.loadingCount = 0;
2015-02-08 15:38:38 +00:00
this.loader = $('loader');
2015-03-06 23:33:54 +00:00
this.formHolder = $('form-holder');
Vn.Hash.initialize ();
this.hash = Vn.Hash;
this.hashParam = new Vn.HashParam ({key: 'form'});
this.hashParam.on ('changed', this.onFormChange, this);
2015-01-31 01:05:12 +00:00
2015-02-18 16:11:25 +00:00
$('background').onclick = function () {};
2015-02-08 15:38:38 +00:00
$('menu-button').addEventListener ('click', function (event)
{
event.stopPropagation ();
if (!this.menuShown)
this.showMenu ();
else
this.hideMenu ();
}.bind (this));
2015-01-31 01:05:12 +00:00
2015-02-08 15:38:38 +00:00
$('menu-box').addEventListener ('click', function (event)
2015-01-31 01:05:12 +00:00
{
event.stopPropagation ();
});
2015-03-09 08:36:54 +00:00
if (!Vn.Cookie.check ('hedera_cookies'))
{
Vn.Cookie.set ('hedera_cookies', true);
(new Htk.Toast ()).showWarning (_('CookiesNotification'));
}
this.conn = new Db.Conn ();
this.conn.on ('error', this.onConnError, this);
this.conn.on ('loading-changed', this.onConnLoading, this);
// this.conn.open (null, null, null, this.connOpened.bind (this));
var sql = 'SELECT default_form, image_dir FROM config;'
+'SELECT production_domain, test_domain FROM config;'
+'SELECT name FROM customer_view;'
+'CALL form_list ();';
this.conn.execQuery (sql, this.onMainQueryDone.bind (this));
2015-01-31 01:05:12 +00:00
}
2015-02-08 15:38:38 +00:00
,showBackground: function ()
2015-01-31 01:05:12 +00:00
{
Vn.Node.show ($('background'));
2015-02-08 15:38:38 +00:00
}
2015-01-31 01:05:12 +00:00
2015-02-08 15:38:38 +00:00
,hideBackground: function ()
{
Vn.Node.hide ($('background'));
2015-01-31 01:05:12 +00:00
}
,showMenu: function ()
{
2015-02-08 15:38:38 +00:00
this.showBackground ();
Vn.Node.show ($('menu-box'));
2015-01-31 01:05:12 +00:00
this.menuShown = true;
this.hideMenuCallback = this.hideMenu.bind (this);
document.addEventListener ('click', this.hideMenuCallback);
}
,hideMenu: function ()
{
2015-02-08 15:38:38 +00:00
this.hideBackground ();
Vn.Node.hide ($('menu-box'));
2015-02-08 15:38:38 +00:00
$('menu-button').style.display = 'initial';
2015-01-31 01:05:12 +00:00
this.menuShown = false;
document.removeEventListener ('click', this.hideMenuCallback);
this.hideMenuCallback = null;
}
2015-01-31 01:05:12 +00:00
,onMainQueryDone: function (resultSet)
{
2015-01-31 01:05:12 +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
var res = resultSet.fetchResult ();
if (res.next () && res.get ('test_domain'))
{
if (location.host != res.get ('production_domain'))
{
Vn.Node.setText ($('test-link'), _('ReturnToOldWebsite'));
$('test-link').href = res.get ('production_domain');
}
else
$('test-link').href = res.get ('test_domain');
}
else
Vn.Node.hide ($('test-link'));
2015-01-31 01:05:12 +00:00
// Retrieving the user name
var userName = resultSet.fetchValue ();
2015-01-31 01:05:12 +00:00
if (userName)
{
2015-02-08 15:38:38 +00:00
var span = $('user-name');
span.appendChild (document.createTextNode (', '+ userName));
}
2015-01-31 01:05:12 +00:00
// 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);
}
2015-02-08 15:38:38 +00:00
this.createMenu (res, sectionMap, null, $('menu'));
2015-01-31 01:05:12 +00:00
// Loading the default form
this.onFormChange ();
}
,onConnError: function (conn, error)
{
switch (error.domain)
{
case 'Auth':
this.unload ();
location.assign ('?page=login#!error='+ error.code);
break;
case 'Version':
this.newVersion (error);
break;
case 'Conn':
2015-03-06 23:33:54 +00:00
(new Htk.Toast ()).showError (error.message);
break;
default:
console.warn (error.message);
}
}
2015-01-31 01:05:12 +00:00
,onConnLoading: function (conn, isLoading)
{
if (isLoading)
this.loaderPush ();
else
this.loaderPop ();
}
,loaderPush: function ()
{
this.loadingCount++;
if (this.loadingCount == 1)
this.loader.style.visibility = 'visible';
}
,loaderPop: function ()
{
2015-02-01 03:21:54 +00:00
if (this.loadingCount == 0)
return;
2015-01-31 01:05:12 +00:00
this.loadingCount--;
if (this.loadingCount == 0)
this.loader.style.visibility = 'hidden';
}
,newVersion: function (error)
{
if (this.skipVersion)
return;
this.skipVersion = true;
var reload;
var message = _('NewVersionAvailable') +"\n\n"+ error.message;
if (error.code == 'criticalVersion')
{
alert (message)
reload = true;
}
else
reload = confirm (message);
2015-03-06 23:33:54 +00:00
if (reload)
{
this.unload ();
location.reload ();
}
}
,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')});
2015-02-08 15:38:38 +00:00
this.menuOptions[res.get ('path')] = a;
li.appendChild (a);
var text = document.createTextNode (_(res.get ('description')));
a.appendChild (text);
2015-01-31 01:05:12 +00:00
var formId = res.get ('id');
2015-01-31 01:05:12 +00:00
if (sectionMap[formId])
{
2015-02-08 15:38:38 +00:00
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));
2015-02-01 03:21:54 +00:00
2015-01-31 01:05:12 +00:00
this.createMenu (res, sectionMap, formId, submenu);
}
}
}
,onLiMouseHover: function (submenu, parent)
{
2015-02-08 15:38:38 +00:00
if (Vn.isMobile ())
return;
this.hideSubmenu ();
this.activeSubmenu = submenu;
var rect = parent.getBoundingClientRect ();
submenu.style.display = 'inline';
2015-01-31 01:05:12 +00:00
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)
{
submenu.style.display = 'none';
clearTimeout (this.timeout);
this.activeSubmenu = null;
this.timeout = 0;
}
}
2015-01-31 01:05:12 +00:00
,onFormChange: function ()
{
var formPath = this.hashParam.value;
2015-02-17 11:48:53 +00:00
this.openForm (formPath ? formPath : Vn.Config['default_form'], null);
}
2015-02-17 11:48:53 +00:00
,openForm: function (formPath, callback)
{
2015-01-31 01:05:12 +00:00
if (Vn.isMobile ())
this.hideMenu ();
2015-03-06 23:33:54 +00:00
this.loaderPush ();
2015-01-31 01:05:12 +00:00
this.requestedForm = formPath;
2015-02-17 11:48:53 +00:00
2015-01-31 01:05:12 +00:00
var formInfo = this.forms[formPath];
2015-02-08 15:38:38 +00:00
var path = 'forms/'+ formPath;
if (this.activeForm)
{
this.activeForm.close ();
this.activeForm = null;
}
if (this.activeCss)
2015-02-17 11:48:53 +00:00
{
Vn.excludeCss (this.activeCss +'/style.css');
if (Vn.isMobile ())
Vn.excludeCss (this.activeCss +'/mobile.css');
}
2015-02-08 15:38:38 +00:00
2015-02-17 11:48:53 +00:00
this.activeCss = path;
Vn.includeCss (this.activeCss +'/style.css');
if (Vn.isMobile ())
Vn.includeCss (this.activeCss +'/mobile.css');
2015-01-31 01:05:12 +00:00
if (!formInfo)
{
2015-01-31 01:05:12 +00:00
var aux = formPath.split ('/');
var formName = aux[aux.length - 1];
var klass = 'Vn.'+ formName.charAt (0).toUpperCase ();
klass += formName.substr (1).replace (/\w\-\w/g, function (token)
{
2015-02-01 03:21:54 +00:00
return token.charAt (0) + token.charAt (2).toUpperCase ();
2015-01-31 01:05:12 +00:00
});
formInfo = {
path: formPath
,klass: klass
,localeReady: false
,jsReady: false
2015-02-17 11:48:53 +00:00
,uiReady: false
,error: false
2015-01-31 01:05:12 +00:00
,ready: false
,callbacks: []
};
Vn.Locale.load (path,
2015-01-31 01:05:12 +00:00
this.onFormLocaleReady.bind (this, formInfo));
Vn.includeJs (path +'/'+ formName +'.js',
this.onFormJsReady.bind (this, formInfo));
2015-03-06 23:33:54 +00:00
Vn.loadXml ('forms/'+ formPath +'/ui.xml',
this.onFormUiReady.bind (this, formInfo));
2015-01-31 01:05:12 +00:00
this.forms[formPath] = formInfo;
}
2015-01-31 01:05:12 +00:00
var newChoosedOption = this.menuOptions[formInfo.path];
if (newChoosedOption)
{
if (this.choosedOption)
this.choosedOption.className = null;
newChoosedOption.className = 'selected';
this.choosedOption = newChoosedOption;
}
if (callback)
2015-02-08 15:38:38 +00:00
formInfo.callbacks.push (callback);
if (formInfo.ready)
2015-02-17 11:48:53 +00:00
this.onFormReady (formInfo);
}
2015-01-31 01:05:12 +00:00
2015-02-17 11:48:53 +00:00
,onFormLocaleReady: function (formInfo, success)
2015-01-31 01:05:12 +00:00
{
formInfo.localeReady = true;
2015-02-17 11:48:53 +00:00
this.onFormReady (formInfo);
2015-01-31 01:05:12 +00:00
}
2015-02-17 11:48:53 +00:00
,onFormJsReady: function (formInfo, success)
2015-01-31 01:05:12 +00:00
{
formInfo.jsReady = true;
2015-02-17 11:48:53 +00:00
formInfo.error = !success;
this.onFormReady (formInfo);
2015-01-31 01:05:12 +00:00
}
2015-03-06 23:33:54 +00:00
,onFormUiReady: function (formInfo, success)
{
2015-02-17 11:48:53 +00:00
formInfo.uiReady = true;
2015-03-06 23:33:54 +00:00
formInfo.error = !success;
2015-02-17 11:48:53 +00:00
this.onFormReady (formInfo);
}
2015-02-17 11:48:53 +00:00
,onFormReady: function (formInfo)
2015-01-31 01:05:12 +00:00
{
2015-02-17 11:48:53 +00:00
if (!(formInfo.localeReady && formInfo.jsReady && formInfo.uiReady))
return;
2015-01-31 01:05:12 +00:00
formInfo.ready = true;
2015-02-17 11:48:53 +00:00
if (!formInfo.error)
{
if (formInfo.path == this.requestedForm)
try {
var klass = eval (formInfo.klass);
this.activeForm = new klass (this, formInfo);
this.activeForm.open ();
2015-02-17 11:48:53 +00:00
this.activeForm.activate ();
}
catch (e) {
2015-03-06 23:33:54 +00:00
formInfo.error = true;
2015-02-17 11:48:53 +00:00
console.error (e);
}
2015-01-31 01:05:12 +00:00
}
2015-02-17 11:48:53 +00:00
else
2015-03-06 23:33:54 +00:00
(new Htk.Toast ()).showError (_('ErrorLoadingForm'));
2015-02-08 15:38:38 +00:00
var callbacks = formInfo.callbacks;
formInfo.callbacks = [];
for (var i = 0; i < callbacks.length; i++)
callbacks[i] (this.activeForm);
this.loaderPop ();
}
,unload: function ()
{
this.hashParam.destroy ();
this.conn.disconnect ('error', this.onConnError, this);
this.conn.disconnect ('loading-changed', this.onConnLoading, this);
}
};