2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
window.addEventListener ('load',
|
|
|
|
function () { Vn.Web.initialize (); });
|
|
|
|
|
|
|
|
Vn.Web =
|
|
|
|
{
|
2015-01-31 01:05:12 +00:00
|
|
|
forms: {}
|
2015-01-23 13:09:30 +00:00
|
|
|
,activeForm: null
|
2015-02-08 15:38:38 +00:00
|
|
|
,activeCss: null
|
2015-01-23 13:09:30 +00:00
|
|
|
,requestedForm: null
|
2015-01-31 01:05:12 +00:00
|
|
|
,menuShown: false
|
|
|
|
,menuOptions: {}
|
|
|
|
,choosedOption: null
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
,initialize: function ()
|
|
|
|
{
|
|
|
|
this.loadingCount = 0;
|
2015-02-08 15:38:38 +00:00
|
|
|
this.loader = $('loader');
|
|
|
|
this.formHolder = $('form');
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
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-23 13:09:30 +00:00
|
|
|
});
|
2015-01-31 01:05:12 +00:00
|
|
|
this.hashLink.on ('changed', this.onFormChange, this);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
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-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-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;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
,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-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-31 01:05:12 +00:00
|
|
|
,onMainQueryDone: function (resultSet)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-01-31 01:05:12 +00:00
|
|
|
// Retrieving configuration parameters
|
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
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
|
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
var userName = resultSet.fetchValue ();
|
2015-01-31 01:05:12 +00:00
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
if (userName)
|
|
|
|
{
|
2015-02-08 15:38:38 +00:00
|
|
|
var span = $('user-name');
|
2015-01-23 13:09:30 +00:00
|
|
|
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 ();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
,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':
|
|
|
|
alert (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';
|
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
,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);
|
|
|
|
|
|
|
|
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;
|
2015-01-23 13:09:30 +00:00
|
|
|
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-23 13:09:30 +00:00
|
|
|
|
2015-01-31 01:05:12 +00:00
|
|
|
if (sectionMap[formId])
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
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);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
,onLiMouseHover: function (submenu, parent)
|
|
|
|
{
|
2015-02-08 15:38:38 +00:00
|
|
|
if (Vn.isMobile ())
|
|
|
|
return;
|
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
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';
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
,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-23 13:09:30 +00:00
|
|
|
{
|
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-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2015-02-17 11:48:53 +00:00
|
|
|
,openForm: function (formPath, callback)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-01-31 01:05:12 +00:00
|
|
|
if (Vn.isMobile ())
|
|
|
|
this.hideMenu ();
|
2015-01-23 13:09:30 +00:00
|
|
|
|
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-23 13:09:30 +00:00
|
|
|
|
2015-01-31 01:05:12 +00:00
|
|
|
if (!formInfo)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
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
|
2015-01-23 13:09:30 +00:00
|
|
|
,xml: null
|
2015-02-17 11:48:53 +00:00
|
|
|
,error: false
|
2015-01-31 01:05:12 +00:00
|
|
|
,ready: false
|
2015-01-23 13:09:30 +00:00
|
|
|
,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-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
var request = new XMLHttpRequest ();
|
|
|
|
request.onreadystatechange =
|
2015-01-31 01:05:12 +00:00
|
|
|
this.onFormUiReady.bind (this, formInfo, request);
|
|
|
|
request.open ('get', 'form.php?form='+ formPath, true);
|
2015-01-23 13:09:30 +00:00
|
|
|
request.send ();
|
|
|
|
|
2015-01-31 01:05:12 +00:00
|
|
|
this.forms[formPath] = formInfo;
|
2015-01-23 13:09:30 +00:00
|
|
|
this.loaderPush ();
|
|
|
|
}
|
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-23 13:09:30 +00:00
|
|
|
}
|
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-01-23 13:09:30 +00:00
|
|
|
|
2015-01-31 01:05:12 +00:00
|
|
|
,onFormUiReady: function (formInfo, request)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
|
|
|
if (request.readyState != 4)
|
|
|
|
return;
|
2015-02-17 11:48:53 +00:00
|
|
|
|
|
|
|
formInfo.uiReady = true;
|
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
if (request.status == 200)
|
2015-01-31 01:05:12 +00:00
|
|
|
formInfo.xml = request;
|
2015-02-17 11:48:53 +00:00
|
|
|
else
|
|
|
|
formInfo.error = true;
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-02-17 11:48:53 +00:00
|
|
|
this.onFormReady (formInfo);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
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))
|
2015-01-23 13:09:30 +00:00
|
|
|
return;
|
|
|
|
|
2015-01-31 01:05:12 +00:00
|
|
|
formInfo.ready = true;
|
2015-01-23 13:09:30 +00:00
|
|
|
|
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) {
|
|
|
|
console.error (e);
|
|
|
|
}
|
2015-01-31 01:05:12 +00:00
|
|
|
}
|
2015-02-17 11:48:53 +00:00
|
|
|
else
|
|
|
|
console.error (_('ErrorDownloadingForm'));
|
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 ();
|
|
|
|
}
|
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
,unload: function ()
|
|
|
|
{
|
2015-01-31 01:05:12 +00:00
|
|
|
this.hashLink.disconnect ('changed', this.onFormChange, this);
|
2015-01-23 13:09:30 +00:00
|
|
|
this.conn.disconnect ('error', this.onConnError, this);
|
|
|
|
this.conn.disconnect ('loading-changed', this.onConnLoading, this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|