// fLAB.js (file:// protocol adapter for LABjs 1.x only) // v0.2 (c) Kyle Simpson // MIT License (function(global){ var orig_$LAB = global.$LAB, oDOC = global.document, oDOCLOC = oDOC.location, local_filesystem = (oDOCLOC.protocol === "file:") ; if (!orig_$LAB || !local_filesystem) return; // only adapt LABjs with fLABjs wrapper if LABjs exists and we're currently in local filesystem var sUNDEF = "undefined", // constants used for compression optimization sSTRING = "string", sHEAD = "head", sBODY = "body", sFUNCTION = "function", sSCRIPT = "script", sSRCURI = "srcuri", sDONE = "done", sWHICH = "which", bTRUE = true, bFALSE = false, fSETTIMEOUT = global.setTimeout, fGETELEMENTSBYTAGNAME = function(tn){return oDOC.getElementsByTagName(tn);}, fOBJTOSTRING = Object.prototype.toString, fNOOP = function(){}, append_to = {}, all_scripts = {}, PAGEROOT = /^[^?#]*\//.exec(oDOCLOC.href)[0], DOCROOT = /^file:\/\/(localhost)?(\/[a-z]:)?/i.exec(PAGEROOT)[0], docScripts = fGETELEMENTSBYTAGNAME(sSCRIPT), is_ie = !+"\v1", // feature detection based on Andrea Giammarchi's solution: http://webreflection.blogspot.com/2009/01/32-bytes-to-know-if-your-browser-is-ie.html sync_script_loading = is_ie, // only IE is currently known to do synchronous loading of file:// scripts, others require core LABjs async functionality global_defs = { dupe:bFALSE, // allow duplicate scripts? preserve:bFALSE, // preserve execution order of all loaded scripts (regardless of preloading) base:"", // base path to prepend to all non-absolute-path scripts which:sHEAD // which DOM object ("head" or "body") to append scripts to } ; append_to[sHEAD] = fGETELEMENTSBYTAGNAME(sHEAD); append_to[sBODY] = fGETELEMENTSBYTAGNAME(sBODY); function canonicalScriptURI(src,base_path) { if (typeof src !== sSTRING) src = ""; if (typeof base_path !== sSTRING) base_path = ""; var ret = (/^file\:\/\//.test(src) ? "" : base_path) + src; return ((/^file\:\/\//.test(ret) ? "" : (ret.charAt(0) === "/" ? DOCROOT : PAGEROOT)) + ret); } function scriptTagExists(uri) { // checks if a script uri has ever been loaded into this page's DOM var i = 0, script; while (script = docScripts[i++]) { if (typeof script.src === sSTRING && uri === canonicalScriptURI(script.src)) return bTRUE; } return bFALSE; } function engine(opts) { if (typeof opts === sUNDEF) opts = global_defs; var ready = bFALSE, _which = opts.which, _base_path = opts.base, waitFunc = fNOOP, scripts_loading = bFALSE, publicAPI, scripts = {}, orig_engine = null ; function createScriptTag(scriptentry,src,type,charset) { if (append_to[scriptentry[sWHICH]][0] === null) { // append_to object not yet ready fSETTIMEOUT(arguments.callee,25); return; } var scriptElem = oDOC.createElement(sSCRIPT), fSETATTRIBUTE = function(attr,val){scriptElem.setAttribute(attr,val);}; fSETATTRIBUTE("type",type); if (typeof charset === sSTRING) fSETATTRIBUTE("charset",charset); fSETATTRIBUTE("src",src); append_to[scriptentry[sWHICH]][0].appendChild(scriptElem); } function loadScript(o) { if (typeof o.allowDup === sUNDEF) o.allowDup = opts.dupe; var src = o.src, type = o.type, charset = o.charset, allowDup = o.allowDup, src_uri = canonicalScriptURI(src,_base_path), scriptentry; if (typeof type !== sSTRING) type = "text/javascript"; if (typeof charset !== sSTRING) charset = null; allowDup = !(!allowDup); if (!allowDup && ( (typeof all_scripts[src_uri] !== sUNDEF && all_scripts[src_uri] !== null) || scriptTagExists(src_uri) ) ) { return; } if (typeof scripts[src_uri] === sUNDEF) scripts[src_uri] = {}; scriptentry = scripts[src_uri]; if (typeof scriptentry[sWHICH] === sUNDEF) scriptentry[sWHICH] = _which; scriptentry[sDONE] = bFALSE; scriptentry[sSRCURI] = src_uri; scripts_loading = bTRUE; all_scripts[scriptentry[sSRCURI]] = bTRUE; createScriptTag(scriptentry,src_uri,type,charset); } function serializeArgs(args) { var sargs = [], i; for (i=0; i