This repository has been archived by the owner on Jul 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.js
194 lines (176 loc) · 4.16 KB
/
loader.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
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
192
193
194
/*!
* loader.js
*
* Copyright 2011 PixelGrid, Inc.
* Licensed under the MIT License:
* http://www.opensource.org/licenses/mit-license.php
*
* @version 0.1.2 (2011/05/04)
*/
(function(window, undefined) {
var document = window.document;
var head = document.getElementsByTagName('head')[0];
var isIE = Boolean(document.all);
var cache = {};
var uid = 0;
/* find basePaths */
var firstCSSLink = (function(){
var links = document.getElementsByTagName('link');
for(var i=0, l=links.length, link; link=links[i]; i++){
if(link.rel && (link.rel.toLowerCase() === 'stylesheet')){
return link;
}
}
})();
var loaderScript = (function(){
var scripts = document.getElementsByTagName('script');
return scripts[scripts.length-1];
})();
var basePath_css = firstCSSLink ? firstCSSLink.href.replace(/[^\/]+$/, '') : '';
var basePath_js = loaderScript.src.replace(/[^\/]+$/, '');
/* utils */
function isRelativePath(str){
if(str.indexOf('/') === 0){ return false; }
if(str.indexOf('http') === 0){ return false; }
return true;
}
function normalizePath(path, type){
if(isRelativePath(path)){
if(type==='js'){ return basePath_js + path; }
if(type==='css'){ return basePath_css + path; }
}
return path;
}
function publishUniqueFnName(){
return '__loader_ready_' + uid++;
}
/* css loader */
function loadCss(path, media){
if(cache[path]){
return;
}
var link = document.createElement('link');
media = media || 'all';
link.type = "text/css";
link.rel = "stylesheet";
link.href = path;
link.media = media;
head.appendChild(link);
cache[path] = true;
}
/**
* loader
*/
window.loader = (function(){
var loader = {};
var _chainLoader;
function _createChainLoader(){
if(_chainLoader){
return;
}
var callback = function(){
_chainLoader = null; // gc the last chainLoader for next
};
if(isIE){
_chainLoader = new ChainLoaderIE(callback);
}else{
_chainLoader = new ChainLoader(callback);
}
}
loader.script = function(src, ignoreBathPath){
if(!ignoreBathPath){
src = normalizePath(src, 'js');
}
_createChainLoader();
_chainLoader.add(src);
return loader;
};
loader.css = function(href, media, ignoreBathPath){
if(!ignoreBathPath){
href = normalizePath(href, 'css');
}
loadCss(href, media);
return loader;
};
loader.ready = function(fn){
_chainLoader.ready(fn);
_chainLoader = null;
return loader;
};
return loader;
})();
/**
* ChainLoader
* for non IE browsers
*/
var ChainLoader = function(callback){
this._callback = callback;
};
ChainLoader.prototype = {
add: function(src){
if(cache[src]){
return;
}
cache[src] = true;
document.write('<script src="' + src + '"></script>');
},
ready: function(fn){
this._callback();
var fnName = publishUniqueFnName();
window[fnName] = function(){
fn();
};
document.write('<script>' + fnName + '()</script>');
}
};
/**
* ChainLoaderIE
* IE script loading explained here.
* http://msdn.microsoft.com/en-us/library/hh180173(v=vs.85).aspx
*/
var ChainLoaderIE = function(callback){
this._callback = callback;
this._loadHandlerFnName = publishUniqueFnName();
var self = this;
/* define proxy fn as global */
window[this._loadHandlerFnName] = function(script){
if(self._isScriptComplete(script)){
self._increaseLoadCount();
script.onreadystatechange = null; // yepnope says that this prevents IE momory leak
}
};
};
ChainLoaderIE.prototype = {
_loaded: 0,
_total: 0,
_fire: function(){
if(this._callback){
this._callback();
}
if(this._readyFn){
this._readyFn();
}
},
_increaseLoadCount: function(){
this._loaded++;
if(this._total === this._loaded){
this._fire();
}
},
_isScriptComplete: function(script){
return (script.readyState === 'complete');
},
add: function(src){
if(cache[src]){
return;
}
cache[src] = true;
this._total++;
var onreadyAttr = 'onreadystatechange="' + this._loadHandlerFnName + '(this)"';
document.write('<script src="' + src + '" ' + onreadyAttr + '></script>');
},
ready: function(callback){
this._readyFn = callback;
}
};
})(window);