forked from olachan/beehive.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
beehive.js
90 lines (83 loc) · 1.84 KB
/
beehive.js
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
/*!
* Beehive v0.1.0
*
* Copyright 2012, Simon Guo
* Email:[email protected]
* Date: 05/09/2012
*/
(function(){
var _head=document.head ||document.getElementsByTagName("head")[0]; //container
var _file={}, _module={};
_file.load=function(callback){
this.onload=this.onreadystatechange=function() {
if (!this.readyState || this.readyState==='loaded' || this.readyState==='complete') {
if(callback) {
callback(_module);
}
this.onload=this.onreadystatechange=null;
}
},
this.onerror=function(){
console.warn('model not found: '+(this.src||this.href));
this.onerror=null;
}
_head.appendChild(this);
}
_file.js=function(url){
var f=document.createElement('script');
f.type='text/javascript';
f.async='true';
f.src=url;
return f;
}
_file.css=function(url){
var f=document.createElement('link');
f.type='text/css';
f.rel='stylesheet';
f.href=url;
return f;
}
var _beehive=function(){
try{
var bees=arguments,
incubate=function(bee,callback){
var url=bee.split('?')[0],
t=url.toLowerCase().substring(url.lastIndexOf('.')+1);
_file.load.call(_file[t](url),callback);
};
_file.index=0;
_module= {
load:function(){
this[arguments[0]||"parallel"]();
},
//parallel load
parallel:function(){
for(var i=0;i<bees.length;i++){
if(typeof bees[i]==="function"){
bees[i]();
continue;
}
incubate(bees[i]);
}
},
//stackline load
stackline:function(){
if(typeof bees[_file.index]==="function"){
bees[_file.index]();
_file.index++;
if(_file.index==bees.length)return;
}
incubate(bees[_file.index],function(o){
_file.index++;
if(_file.index==bees.length)return;
o.stackline();
});
}
};
return _module;
}catch(e){
alert(e);
}
}
window.Beehive=_beehive;
})()