hedera-web/web/js/vn/main.js

200 lines
4.1 KiB
JavaScript
Raw Normal View History

/**
* The main namespace.
**/
var Vn =
{
Config: {}
,jsIncludes: {}
,cssIncludes: {}
2015-03-06 23:33:54 +00:00
,xmlIncludes: {}
,customTags: {}
,head: document.getElementsByTagName ('head')[0]
2015-01-31 01:05:12 +00:00
,isMobileCached: null
2015-10-21 23:34:35 +00:00
,getVersion: function ()
{
if (this._version === undefined)
{
var re = /[; ]hedera_version=([^\\s;]*)/;
var sMatch = (' '+ document.cookie).match (re);
this._version = (sMatch) ? '?'+ unescape (sMatch[1]) : '';
}
return this._version;
}
/**
* 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)
{
2015-02-08 15:38:38 +00:00
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)
{
2015-02-08 15:38:38 +00:00
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 =
2015-02-17 11:48:53 +00:00
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)
{
2015-02-17 11:48:53 +00:00
console.log ('js: '+ includeData.script.readyState);
if (includeData.script.readyState == 'complete')
2015-02-17 11:48:53 +00:00
this.jsLoaded (includeData, true);
}
2015-02-17 11:48:53 +00:00
,jsLoaded: function (includeData, success)
{
if (includeData.loaded)
return;
for (var i = 0; i < includeData.callbacks.length; i++)
2015-02-17 11:48:53 +00:00
includeData.callbacks[i] (success);
includeData.loaded = true;
includeData.callbacks = null;
}
2015-03-06 23:33:54 +00:00
,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];
}
2015-02-08 15:38:38 +00:00
/**
* Checks if user is using a mobile browser.
*
* return {boolean} %true if is mobile, %false otherwise.
**/
2015-01-31 01:05:12 +00:00
,isMobile: function ()
{
if (this.isMobileCached === null)
2015-02-17 11:48:53 +00:00
{
var regExp = /(Android|webOS|iPhone|iPad|iPod|BlackBerry|Windows Phone)/i;
this.isMobileCached = navigator.userAgent.match (regExp);
}
2015-01-31 01:05:12 +00:00
return this.isMobileCached;
}
,get: function (id)
{
return document.getElementById (id);
}
};