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

429 lines
8.7 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');
this.hash = new Vn.Hash ();
this.hashLink = new Vn.HashLink
({
hash: this.hash
2015-01-31 01:05:12 +00:00
,key: 'form'
});
2015-01-31 01:05:12 +00:00
this.hashLink.on ('changed', this.onFormChange, this);
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));
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'));
}
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
{
2015-02-08 15:38:38 +00:00
$('background').style.display = 'block';
}
2015-01-31 01:05:12 +00:00
2015-02-08 15:38:38 +00:00
,hideBackground: function ()
{
$('background').style.display = 'none';
2015-01-31 01:05:12 +00:00
}
,showMenu: function ()
{
2015-02-08 15:38:38 +00:00
this.showBackground ();
$('menu-box').style.display = 'block';
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 ();
$('menu-box').style.display = 'none';
$('menu-button').style.display = 'initial';
2015-01-31 01:05:12 +00:00
this.menuShown = false;
document.removeEventListener ('click', this.hideMenuCallback);
this.hideMenuCallback = null;
}
,connOpened: function (conn, success)
{
2015-01-31 01:05:12 +00:00
var sql = 'SELECT default_form, image_dir 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
,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);
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');
2015-01-31 01:05:12 +00:00
a.href = this.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 ()
{
2015-01-31 01:05:12 +00:00
var formPath = this.hashLink.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.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 ()
{
2015-01-31 01:05:12 +00:00
this.hashLink.disconnect ('changed', this.onFormChange, this);
this.conn.disconnect ('error', this.onConnError, this);
this.conn.disconnect ('loading-changed', this.onConnLoading, this);
}
};