[SPIP][PLUGINS] v3.0-->v3.2
[lhc/web/www.git] / www / plugins-dist / compresseur / lib / jQl / jQl.js
1 /**
2 * JQuery Loader
3 *
4 * Help to async load jQuery and supporting inline javascript with calls like
5 * $(function(){})
6 * or
7 * $(document).ready(function(){})
8 *
9 * Include it, then just call :
10 * jQl.loadjQ('//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
11 *
12 * You can also use it to load jQuery-dependent module in parallel,
13 * it will be queue and run after jQuery is loaded :
14 * jQl.loadjQ('//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
15 * jQl.loadjQdep('my.example.com/js/myplugin.jquery.js');
16 *
17 * If you use a defer inline script, you have to manually call boot() function :
18 *
19 * <script defer type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
20 * <script type="text/javascript">jQl.boot();</script>
21 *
22 *
23 *
24 * jQuery will be loaded without blocking browser rendering,
25 * and during this all inline calls to $(document).ready() and $.getScript()
26 * will be queued.
27 * As soon as jQuery is ready, all queued ready() and getScript() calls will be run
28 * respecting their initial order
29 *
30 * Be careful :
31 * At the moment, inline call and getScript executions are not waiting jQuery-dependent modules,
32 * but only jQuery core
33 * However, when jQuery is loaded, jQuery-dependent modules already loaded
34 * are run before inline scripts. But if some modules are longer to load and arrive
35 * after jQuery, they will be run after queued inline calls
36 *
37 * v 1.2.0
38 * (c) 2010-2016 Cedric Morin licence GPL
39 *
40 */
41 var jQl={
42 /**
43 * the inline calls queue
44 */
45 "q":[],
46
47 /**
48 * the jQuery dependencies xhr loaded queue
49 */
50 "dq":[],
51
52 /**
53 * the jQuery.getScript queue
54 */
55 "gs":[],
56
57 /**
58 * the ready function that collect calls and put it in the queue
59 */
60 "ready":function(f){
61 if(typeof f=='function'){
62 jQl.q.push(f);
63 }
64 // return jQl in order to support jQuery(document).ready()
65 return jQl;
66 },
67 "getScript":function(s,c){
68 jQl.gs.push([s,c]);
69 },
70
71 /**
72 * unqueue ready() function
73 * run all queues inline $.ready() calls
74 * in the right order and purge the queue
75 *
76 */
77 "unq":function(){
78 for(var i=0;i<jQl.q.length;i++)
79 jQl.q[i]();
80 jQl.q=[];},
81
82 /**
83 * unqueue getScript() function
84 * run all queues $.getScript calls
85 * in the right order and purge the queue
86 *
87 */
88 "ungs":function(){
89 for(var i=0;i<jQl.gs.length;i++)
90 jQuery.getScript(jQl.gs[i][0],jQl.gs[i][1]);
91 jQl.gs=[];
92 },
93
94 /**
95 * boot function
96 * call it after calling jQuery in order to wait it's loaded
97 * or use it in onload='' on script defer/async element
98 *
99 * @param function callback
100 * a callback to call after jQuery is loaded
101 *
102 */
103 "bId":null,
104 "boot":function(callback){
105 if(typeof window.jQuery.fn == 'undefined'){
106 if (!jQl.bId) {
107 jQl.bId = setInterval(function(){jQl.boot(callback)},25);
108 }
109 return;
110 }
111 if (jQl.bId) {
112 clearInterval(jQl.bId);
113 }
114 jQl.bId=0;
115 // OK, jQuery is loaded,
116 // we can load additional jQuery dependents modules
117 jQl.unqjQdep();
118
119 // then unqueue all getScript calls
120 jQl.ungs();
121
122 // and last unqueue all inline calls
123 // (when document is ready)
124 jQuery(jQl.unq);
125
126 // call the callback if provided
127 if(typeof callback=='function') callback();
128 },
129
130 "booted":function(){return jQl.bId===0},
131
132
133 /**
134 * load jQuery script asynchronously in all browsers
135 * by delayed dom injection
136 * @param string src
137 * jQuery url to use, can be a CDN hosted,
138 * or a compiled js including jQuery
139 * @param callback
140 */
141 "loadjQ":function(src,callback){
142 setTimeout(
143 function(){
144 var s=document.createElement('script');
145 s.src=src;
146 document.getElementsByTagName('head')[0].appendChild(s)
147 },
148 1);
149 jQl.boot(callback);
150 },
151
152
153 /**
154 *
155 *
156 *
157 * jQuery-dependant modules loading
158 * this section is not necessary in case of loading only one script
159 * including jQuery
160 * Can be removed to make the booter smaller
161 *
162 *
163 *
164 *
165 *
166 *
167 */
168
169 /**
170 * load a jQuery-dependent script
171 * parallel loading in most browsers by xhr loading and injection
172 * the jQ-dependant script is queued or run when loaded,
173 * depending of jQuery loading state
174 */
175 "loadjQdep":function(src){
176 jQl.loadxhr(src, jQl.qdep);
177 },
178
179 /**
180 * queue jQuery-dependent script if download finish before jQuery loading
181 * or run it directly if jQuery loaded, and previous loaded modules are run
182 * (preserve initial order)
183 *
184 * @param string txt
185 * the js script to inject inline in dom
186 * @param string src
187 * the source url of the script, not used here
188 */
189 "qdep":function(txt, src){
190 if (txt){
191 if (typeof window.jQuery.fn!=="undefined" && !jQl.dq.length){
192 jQl.rs(txt);
193 }
194 else {
195 jQl.dq.push(txt);
196 }
197 }
198 },
199
200 /**
201 * dequeue jQuery-dependent modules loaded before jQuery
202 * call once only
203 */
204 "unqjQdep":function(){
205 // security, should never happen
206 // so we keep setTimeout even if setInterval would be cleaner
207 if (typeof window.jQuery.fn=="undefined"){
208 setTimeout(jQl.unqjQdep, 50);
209 return;
210 }
211 for (var i=0;i<jQl.dq.length;i++) jQl.rs(jQl.dq[i]);
212 jQl.dq = [];
213 },
214
215
216 /**
217 * run a text script as inline js
218 * @param string txt
219 * js script
220 * @param string src
221 * original source of the script (not used here)
222 */
223 "rs":function(txt, src){
224 var se = document.createElement('script');
225 document.getElementsByTagName('head')[0].appendChild(se);
226 se.text = txt;
227 },
228
229 /**
230 * multi-browsers XHr loader,
231 * credits http://www.stevesouders.com/blog/2009/04/27/loading-scripts-without-blocking/
232 *
233 */
234 "loadxhr":function(src,callback) {
235 var xoe;
236 xoe = jQl.getxo();
237 xoe.onreadystatechange = function() {
238 if ( xoe.readyState != 4 || 200 != xoe.status ) return;
239 callback(xoe.responseText,src);
240 };
241 try {
242 xoe.open('GET', src, true);
243 xoe.send('');
244 }
245 catch(e) {
246 }
247 },
248
249 /**
250 * facilitie for XHr loader
251 * credits http://www.stevesouders.com/blog/2009/04/27/loading-scripts-without-blocking/
252 *
253 */
254 "getxo":function (){
255 var xhrObj = false;
256 try {
257 xhrObj = new XMLHttpRequest();
258 }
259 catch(e){
260 var progid = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
261 for ( var i=0; i < progid.length; ++i ) {
262 try {
263 xhrObj = new ActiveXObject(progid[i]);
264 }
265 catch(e){
266 continue;
267 }
268 break;
269 }
270 }
271 finally {
272 return xhrObj;
273 }
274 }
275 };
276
277
278 /**
279 *
280 * map $ and jQuery to the jQl.ready() function in order to catch all
281 * inline calls like :
282 * $(function(){...})
283 * jQuery(function(){...})
284 * $('document').ready(function(){...})
285 * jQuery('document').ready(function(){...})
286 *
287 * $.getScript() and jQuery.getScript() also catched and called
288 *
289 * only if jQuery is not already loaded
290 */
291 if (typeof window.jQuery=='undefined'){var $=jQl.ready,jQuery=$;$.getScript=jQl.getScript;}