Uh oh!
There was an error while loading. Please reload this page.
forked from getify/LABjs
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfLAB.src.js
More file actions
Latest commit
191 lines (178 loc) · 7.3 KB
/
Copy pathfLAB.src.js
File metadata and controls
191 lines (178 loc) · 7.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// fLAB.js (file:// protocol adapter for LABjs 1.x only)
// v0.2 (c) Kyle Simpson
// MIT License
(function(global){
varorig_$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
varsUNDEF="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){returnoDOC.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);
functioncanonicalScriptURI(src,base_path){
if(typeofsrc!==sSTRING)src="";
if(typeofbase_path!==sSTRING)base_path="";
varret=(/^file\:\/\//.test(src) ? "" : base_path)+src;
return((/^file\:\/\//.test(ret) ? "" : (ret.charAt(0)==="/" ? DOCROOT : PAGEROOT))+ret);
}
functionscriptTagExists(uri){// checks if a script uri has ever been loaded into this page's DOM
vari=0,script;
while(script=docScripts[i++]){
if(typeofscript.src===sSTRING&&uri===canonicalScriptURI(script.src))returnbTRUE;
}
returnbFALSE;
}
functionengine(opts){
if(typeofopts===sUNDEF)opts=global_defs;
varready=bFALSE,
_which=opts.which,
_base_path=opts.base,
waitFunc=fNOOP,
scripts_loading=bFALSE,
publicAPI,
scripts={},
orig_engine=null
;
functioncreateScriptTag(scriptentry,src,type,charset){
if(append_to[scriptentry[sWHICH]][0]===null){// append_to object not yet ready
fSETTIMEOUT(arguments.callee,25);
return;
}
varscriptElem=oDOC.createElement(sSCRIPT),fSETATTRIBUTE=function(attr,val){scriptElem.setAttribute(attr,val);};
fSETATTRIBUTE("type",type);
if(typeofcharset===sSTRING)fSETATTRIBUTE("charset",charset);
fSETATTRIBUTE("src",src);
append_to[scriptentry[sWHICH]][0].appendChild(scriptElem);
}
functionloadScript(o){
if(typeofo.allowDup===sUNDEF)o.allowDup=opts.dupe;
varsrc=o.src,type=o.type,charset=o.charset,allowDup=o.allowDup,
src_uri=canonicalScriptURI(src,_base_path),scriptentry;
if(typeoftype!==sSTRING)type="text/javascript";
if(typeofcharset!==sSTRING)charset=null;
allowDup=!(!allowDup);
if(!allowDup&&
(
(typeofall_scripts[src_uri]!==sUNDEF&&all_scripts[src_uri]!==null)||
scriptTagExists(src_uri)
)
){
return;
}
if(typeofscripts[src_uri]===sUNDEF)scripts[src_uri]={};
scriptentry=scripts[src_uri];
if(typeofscriptentry[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);
}
functionserializeArgs(args){
varsargs=[],i;
for(i=0;i<args.length;i++){
if(fOBJTOSTRING.call(args[i])==="[object Array]")sargs=sargs.concat(serializeArgs(args[i]));
elsesargs[sargs.length]=args[i];
}
returnsargs;
}
publicAPI={
script:function(){
varargs=serializeArgs(arguments),use_engine,i;
for(i=0;i<args.length;i++){
if(typeofargs[i]===sSTRING)args[i]={src:canonicalScriptURI(args[i])};
elseif(typeofargs[i].src!==sUNDEF)args[i].src=canonicalScriptURI(args[i].src);
}
if(sync_script_loading){// handle without core LABjs since we're sync loading
use_engine=publicAPI;
for(i=0;i<args.length;i++)loadScript(args[i]);
}
else{// pass off to core LABjs to handle async loading
if(orig_engine===null)orig_engine=orig_$LAB.setOptions(opts.pubMap);
use_engine=orig_engine=orig_engine.script.apply(null,args);
}
returnuse_engine;
},
wait:function(func){
varuse_engine;
if(typeoffunc!==sFUNCTION)func=fNOOP;
if(sync_script_loading){// execute immediately since we're sync loading
use_engine=publicAPI;
fSETTIMEOUT(func,0);
}
else{// pass off to core LABjs to handle since we're async loading
if(orig_engine===null)orig_engine=orig_$LAB.setOptions(opts.pubMap);
use_engine=orig_engine=orig_engine.wait(func);
}
returnuse_engine;
}
};
publicAPI.block=publicAPI.wait;// alias "block" to "wait" -- "block" is now deprecated
returnpublicAPI;
}
functionprocessOpts(opts){
vark,newOpts={},
boolOpts={"AlwaysPreserveOrder":"preserve","AllowDuplicates":"dupe"},
allOpts={"AppendTo":"which","BasePath":"base"}
;
for(kinboolOpts)allOpts[k]=boolOpts[k];
for(kinallOpts){
if(allOpts.hasOwnProperty(k)&&typeofglobal_defs[allOpts[k]]!==sUNDEF)newOpts[allOpts[k]]=(opts&&typeofopts[k]!==sUNDEF) ? opts[k] : global_defs[allOpts[k]];
}
for(kinboolOpts){// normalize bool props to actual boolean values if not already
if(boolOpts.hasOwnProperty(k))newOpts[boolOpts[k]]=!(!newOpts[boolOpts[k]]);
}
newOpts.preload=newOpts.cache=newOpts.order=newOpts.xhr=bFALSE;// don't use any preloading techniques if working in file:///
newOpts.which=(newOpts.which===sHEAD||newOpts.which===sBODY) ? newOpts.which : sHEAD;
newOpts.pubMap={};
for(kinallOpts){
if(allOpts.hasOwnProperty(k))newOpts.pubMap[k]=newOpts[allOpts[k]];// create a hash of reverse mappings back to the public names, suitable to pass along to orig_$LAB.setOptions()
}
returnnewOpts;
}
global.$LAB={
setGlobalDefaults:function(gdefs){// intentionally does not return an "engine" instance -- must call as stand-alone function call on $LAB
global_defs=processOpts(gdefs);
},
setOptions:function(opts){// set options per chain
returnengine(processOpts(opts));
},
script:function(){// will load one or more scripts
returnengine().script.apply(null,arguments);
},
wait:function(){// will ensure that the chain's previous scripts are executed before execution of scripts in subsequent chain links
returnengine().wait.apply(null,arguments);
}
};
global.$LAB.block=global.$LAB.wait;// alias "block" to "wait" -- "block" is now deprecated
})(window);