forked from verdnatura/hedera-web
188 lines
3.9 KiB
JavaScript
Executable File
188 lines
3.9 KiB
JavaScript
Executable File
/**
|
|
* The main namespace.
|
|
**/
|
|
var Vn =
|
|
{
|
|
Config: {}
|
|
,jsIncludes: {}
|
|
,cssIncludes: {}
|
|
,xmlIncludes: {}
|
|
,customTags: {}
|
|
,head: document.getElementsByTagName ('head')[0]
|
|
,isMobileCached: null
|
|
|
|
/**
|
|
* Includes a new CSS stylesheet in the current document, if the stylesheet
|
|
* its already included, does nothing.
|
|
*
|
|
* @param {string} fileName The stylesheet file name
|
|
**/
|
|
,includeCss: function (fileName)
|
|
{
|
|
var cssData = this.cssIncludes[fileName];
|
|
|
|
if (!cssData)
|
|
{
|
|
var link = document.createElement ('link');
|
|
link.rel = 'stylesheet';
|
|
link.type = 'text/css';
|
|
link.href = fileName +'?'+ Vn.Cookie.get ('hedera_version');
|
|
this.head.appendChild (link);
|
|
|
|
this.cssIncludes[fileName] =
|
|
{
|
|
included: true
|
|
,link: link
|
|
};
|
|
}
|
|
else if (!cssData.included)
|
|
{
|
|
cssData.link.disabled = false;
|
|
cssData.included = true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Excludes a CSS stylesheet from the current document.
|
|
*
|
|
* @param {string} fileName The stylesheet file name
|
|
**/
|
|
,excludeCss: function (fileName)
|
|
{
|
|
var cssData = this.cssIncludes[fileName];
|
|
|
|
if (cssData && cssData.included)
|
|
{
|
|
cssData.link.disabled = true;
|
|
cssData.included = false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Includes a new Javascript in the current document, if the script
|
|
* its already included, does nothing.
|
|
*
|
|
* @param {string} fileName The script file name
|
|
* @param {Function} callback The function to call when script is
|
|
* downloaded and included
|
|
**/
|
|
,includeJs: function (fileName, callback, skipVersion)
|
|
{
|
|
var includeData = this.jsIncludes[fileName];
|
|
|
|
if (includeData === undefined)
|
|
{
|
|
var src = fileName;
|
|
|
|
if (!skipVersion)
|
|
src = src +'?'+ Vn.Cookie.get ('hedera_version');
|
|
|
|
var script = document.createElement ('script');
|
|
script.type = 'text/javascript';
|
|
script.src = src;
|
|
|
|
includeData = {
|
|
script: script
|
|
,callbacks: []
|
|
,loaded: false
|
|
};
|
|
|
|
if (callback)
|
|
includeData.callbacks.push (callback);
|
|
|
|
script.onload =
|
|
this.jsLoaded.bind (this, includeData, true);
|
|
script.onerror =
|
|
this.jsLoaded.bind (this, includeData, false);
|
|
script.onreadystatechange =
|
|
this.jsStateChanged.bind (this, includeData);
|
|
|
|
this.jsIncludes[fileName] = includeData;
|
|
|
|
this.head.appendChild (script);
|
|
}
|
|
else if (callback)
|
|
{
|
|
if (includeData.loaded)
|
|
callback ();
|
|
else
|
|
includeData.callbacks.push (callback);
|
|
}
|
|
}
|
|
|
|
,jsStateChanged: function (includeData)
|
|
{
|
|
console.log ('js: '+ includeData.script.readyState);
|
|
|
|
if (includeData.script.readyState == 'complete')
|
|
this.jsLoaded (includeData, true);
|
|
}
|
|
|
|
,jsLoaded: function (includeData, success)
|
|
{
|
|
if (includeData.loaded)
|
|
return;
|
|
|
|
for (var i = 0; i < includeData.callbacks.length; i++)
|
|
includeData.callbacks[i] (success);
|
|
|
|
includeData.loaded = true;
|
|
includeData.callbacks = null;
|
|
}
|
|
|
|
,loadXml: function (path, callback)
|
|
{
|
|
var includeData = this.xmlIncludes[path];
|
|
|
|
if (!includeData)
|
|
{
|
|
var request = new XMLHttpRequest ();
|
|
request.onreadystatechange =
|
|
this.onXmlReady.bind (this, request, path, callback);
|
|
request.open ('get', path +'?'+ Vn.Cookie.get ('hedera_version'), true);
|
|
request.send ();
|
|
}
|
|
else if (callback)
|
|
callback (true);
|
|
}
|
|
|
|
,onXmlReady: function (request, path, callback)
|
|
{
|
|
if (request.readyState != 4)
|
|
return;
|
|
|
|
if (request.status == 200)
|
|
this.xmlIncludes[path] = request.responseXML;
|
|
|
|
if (callback)
|
|
callback (request.status == 200);
|
|
}
|
|
|
|
,getXml: function (path)
|
|
{
|
|
return this.xmlIncludes[path];
|
|
}
|
|
|
|
/**
|
|
* Checks if user is using a mobile browser.
|
|
*
|
|
* return {boolean} %true if is mobile, %false otherwise.
|
|
**/
|
|
,isMobile: function ()
|
|
{
|
|
if (this.isMobileCached === null)
|
|
{
|
|
var regExp = /(Android|webOS|iPhone|iPad|iPod|BlackBerry|Windows Phone)/i;
|
|
this.isMobileCached = navigator.userAgent.match (regExp);
|
|
}
|
|
|
|
return this.isMobileCached;
|
|
}
|
|
|
|
,get: function (id)
|
|
{
|
|
return document.getElementById (id);
|
|
}
|
|
};
|
|
|