/*!
* Copyright (c) 2009 Francesco Mele jsbeans@francescomele.com
*
* This Software is licenced under the LGPL Licence (GNU Lesser General
* Public License).
* In addition to the LGPL Licence the Software is subject to the
* following conditions:
*
* i every modification must be public and comunicated to the Author
* ii every "jsbean" added to this library must be self consistent
* except for the dependence from jsbeans-x.x.x.js
* iii copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* A collection of utilities in Javascript Object fashion
* @module jsbeans
*/
/**
* Implementation of base Object used as namespace
* @class jsbeans
* @static
*/
jsbeans = {
/**
* Create a full qualified Object (namespace included) from a string
* @method define
* @param {String} pckName
* @static
*/
define: function(pckName) {
var node = window;
var v = pckName.split(".");
for (var i = 0; i < v.length; i++) {
var p = v[i];
if (node[p] == null) {
node[p] = new window.Object();
}
node = node[p];
}
// no need to return it
//return node;
},
/**
* @private
* */
_alreadyLoaded: new Array(),
/**
* Loads a 'jsbean' appending the <code>script</code> tag at the head of current page.<br/>
* An optional function, passed as second argument, may be invoked as callback after the jsbean has been loaded. It tries each 100 milliseconds for 5 seconds (configurable).
* @param jsbean {String} jsbean's name.
* @param [func] {Function} A callback fired after the jsbean has been loaded. Optional.
* @param [until] {Integer} Maximum number of seconds to try invoking the callback. Optional (default: 5).
* @static
* */
load: function(jsbean) {
var callback = arguments[1] || null;
var until = arguments[2] || 5;
if (!jsbeans._alreadyLoaded[jsbean]) {
var o = eval("jsbeans." + jsbean);
if (!o) {
var scripts = document.getElementsByTagName("script");
var _realLoad = function(m, jsbean) {
var root = src.substring(0, m.index);
var jsbeanSrc = root + "jsbeans/" + jsbean + ".js";
try {
var jsbeanScript = document.createElement("script");
jsbeanScript.src = jsbeanSrc;
document.getElementsByTagName("head")[0].appendChild(jsbeanScript);
// document.write("<scr"+"ipt type='text/javascript' src='" + jsbeanSrc + "'><!-- --></scr"+"ipt>");
}
catch (e) {
}
jsbeans._alreadyLoaded[jsbean] = true;
};
var re = /jsbeans\.js([\?\.]|$)/i;
var re2 = new RegExp("jsbeans-latest\.js([\?\.]|$)", "i");
for (var i = 0, script; script = scripts[i]; i++) {
var src = script.getAttribute("src");
if (src) {
var m = src.match(re);
if (m) {
_realLoad(m, jsbean);
break;
}
else {
m = src.match(re2);
if (m) {
_realLoad(m, jsbean);
break;
}
}
}
}
}
}
// fire the callback, if provided
if (callback != null) {
// from seconds to millis
until = until * 1000;
// interval
var each = 100;
var timer = 0;
var callbackID = setInterval(function() {
var b = eval("jsbeans." + jsbean);
if (typeof b != "undefined") {
try {
callback();
}
catch(e) {}
clearInterval(callbackID);
}
// update timer
else {
timer += each;
}
// clearInterval if timer exceeds the maximum allowed
if (timer >= until) {
clearInterval(callbackID);
}
}, each);
}
},
/**
* If you find namespace annoying and verbose, you can use this method to define an alternative to jsbeans global Object.<br/>
* Sample:<pre>
* // before
* jsbeans.string.trimLeft(" string to trim");
* // call alias
* jsbeans.alias("jsb");
* // after
* jsb.string.trimLeft(" string to trim");
* </pre>
* Note that if {@param} <code class="param">newBase</code> is null, empty or already defined this method will exit silently
* @method alias
* @param newBase {String} a global variable used as alias for jsbeans global Object
* */
alias: function(newBase) {
if (newBase == null
// trimmed
|| newBase.replace(/^\s\s*/, '').replace(/\s\s*$/, '') == ""
// already defined
|| typeof window[newBase] != "undefined") {
return;
}
eval(newBase + " = jsbeans;");
}
};