]> ToastFreeware Gitweb - philipp/winterrodeln/wradmin.git/blob - wradmin/static/yui/get/get-debug.js
Rename public directory to static.
[philipp/winterrodeln/wradmin.git] / wradmin / static / yui / get / get-debug.js
1 /*
2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
5 version: 2.7.0
6 */
7 /**
8  * Provides a mechanism to fetch remote resources and
9  * insert them into a document
10  * @module get
11  * @requires yahoo
12  */
13
14 /**
15  * Fetches and inserts one or more script or link nodes into the document 
16  * @namespace YAHOO.util
17  * @class YAHOO.util.Get
18  */
19 YAHOO.util.Get = function() {
20
21     /**
22      * hash of queues to manage multiple requests
23      * @property queues
24      * @private
25      */
26     var queues={}, 
27         
28     /**
29      * queue index used to generate transaction ids
30      * @property qidx
31      * @type int
32      * @private
33      */
34         qidx=0, 
35         
36     /**
37      * node index used to generate unique node ids
38      * @property nidx
39      * @type int
40      * @private
41      */
42         nidx=0, 
43
44         // ridx=0,
45
46         // sandboxFrame=null,
47
48     /**
49      * interal property used to prevent multiple simultaneous purge 
50      * processes
51      * @property purging
52      * @type boolean
53      * @private
54      */
55         purging=false,
56
57         ua=YAHOO.env.ua, 
58         
59         lang=YAHOO.lang;
60     
61     /** 
62      * Generates an HTML element, this is not appended to a document
63      * @method _node
64      * @param type {string} the type of element
65      * @param attr {string} the attributes
66      * @param win {Window} optional window to create the element in
67      * @return {HTMLElement} the generated node
68      * @private
69      */
70     var _node = function(type, attr, win) {
71         var w = win || window, d=w.document, n=d.createElement(type);
72
73         for (var i in attr) {
74             if (attr[i] && YAHOO.lang.hasOwnProperty(attr, i)) {
75                 n.setAttribute(i, attr[i]);
76             }
77         }
78
79         return n;
80     };
81
82     /**
83      * Generates a link node
84      * @method _linkNode
85      * @param url {string} the url for the css file
86      * @param win {Window} optional window to create the node in
87      * @return {HTMLElement} the generated node
88      * @private
89      */
90     var _linkNode = function(url, win, charset) {
91         var c = charset || "utf-8";
92         return _node("link", {
93                 "id":      "yui__dyn_" + (nidx++),
94                 "type":    "text/css",
95                 "charset": c,
96                 "rel":     "stylesheet",
97                 "href":    url
98             }, win);
99     };
100
101     /**
102      * Generates a script node
103      * @method _scriptNode
104      * @param url {string} the url for the script file
105      * @param win {Window} optional window to create the node in
106      * @return {HTMLElement} the generated node
107      * @private
108      */
109     var _scriptNode = function(url, win, charset) {
110         var c = charset || "utf-8";
111         return _node("script", {
112                 "id":      "yui__dyn_" + (nidx++),
113                 "type":    "text/javascript",
114                 "charset": c,
115                 "src":     url
116             }, win);
117     };
118
119     /**
120      * Returns the data payload for callback functions
121      * @method _returnData
122      * @private
123      */
124     var _returnData = function(q, msg) {
125         return {
126                 tId: q.tId,
127                 win: q.win,
128                 data: q.data,
129                 nodes: q.nodes,
130                 msg: msg,
131                 purge: function() {
132                     _purge(this.tId);
133                 }
134             };
135     };
136
137     var _get = function(nId, tId) {
138         var q = queues[tId],
139             n = (lang.isString(nId)) ? q.win.document.getElementById(nId) : nId;
140         if (!n) {
141             _fail(tId, "target node not found: " + nId);
142         }
143
144         return n;
145     };
146
147     /*
148      * The request failed, execute fail handler with whatever
149      * was accomplished.  There isn't a failure case at the
150      * moment unless you count aborted transactions
151      * @method _fail
152      * @param id {string} the id of the request
153      * @private
154      */
155     var _fail = function(id, msg) {
156         YAHOO.log("get failure: " + msg, "warn", "Get");
157         var q = queues[id];
158         // execute failure callback
159         if (q.onFailure) {
160             var sc=q.scope || q.win;
161             q.onFailure.call(sc, _returnData(q, msg));
162         }
163     };
164
165     /**
166      * The request is complete, so executing the requester's callback
167      * @method _finish
168      * @param id {string} the id of the request
169      * @private
170      */
171     var _finish = function(id) {
172         YAHOO.log("Finishing transaction " + id);
173         var q = queues[id];
174         q.finished = true;
175
176         if (q.aborted) {
177             var msg = "transaction " + id + " was aborted";
178             _fail(id, msg);
179             return;
180         }
181
182         // execute success callback
183         if (q.onSuccess) {
184             var sc=q.scope || q.win;
185             q.onSuccess.call(sc, _returnData(q));
186         }
187     };
188
189     /**
190      * Timeout detected
191      * @method _timeout
192      * @param id {string} the id of the request
193      * @private
194      */
195     var _timeout = function(id) {
196         YAHOO.log("Timeout " + id, "info", "get");
197         var q = queues[id];
198         if (q.onTimeout) {
199             var sc=q.scope || q;
200             q.onTimeout.call(sc, _returnData(q));
201         }
202     };
203
204     /**
205      * Loads the next item for a given request
206      * @method _next
207      * @param id {string} the id of the request
208      * @param loaded {string} the url that was just loaded, if any
209      * @private
210      */
211     var _next = function(id, loaded) {
212         YAHOO.log("_next: " + id + ", loaded: " + loaded, "info", "Get");
213         var q = queues[id];
214
215         if (q.timer) {
216             // Y.log('cancel timer');
217             q.timer.cancel();
218         }
219
220         if (q.aborted) {
221             var msg = "transaction " + id + " was aborted";
222             _fail(id, msg);
223             return;
224         }
225
226         if (loaded) {
227             q.url.shift(); 
228             if (q.varName) {
229                 q.varName.shift(); 
230             }
231         } else {
232             // This is the first pass: make sure the url is an array
233             q.url = (lang.isString(q.url)) ? [q.url] : q.url;
234             if (q.varName) {
235                 q.varName = (lang.isString(q.varName)) ? [q.varName] : q.varName;
236             }
237         }
238
239         var w=q.win, d=w.document, h=d.getElementsByTagName("head")[0], n;
240
241         if (q.url.length === 0) {
242             // Safari 2.x workaround - There is no way to know when 
243             // a script is ready in versions of Safari prior to 3.x.
244             // Adding an extra node reduces the problem, but doesn't
245             // eliminate it completely because the browser executes
246             // them asynchronously. 
247             if (q.type === "script" && ua.webkit && ua.webkit < 420 && 
248                     !q.finalpass && !q.varName) {
249                 // Add another script node.  This does not guarantee that the
250                 // scripts will execute in order, but it does appear to fix the
251                 // problem on fast connections more effectively than using an
252                 // arbitrary timeout.  It is possible that the browser does
253                 // block subsequent script execution in this case for a limited
254                 // time.
255                 var extra = _scriptNode(null, q.win, q.charset);
256                 extra.innerHTML='YAHOO.util.Get._finalize("' + id + '");';
257                 q.nodes.push(extra); h.appendChild(extra);
258
259             } else {
260                 _finish(id);
261             }
262
263             return;
264         } 
265
266
267         var url = q.url[0];
268
269         // if the url is undefined, this is probably a trailing comma problem in IE
270         if (!url) {
271             q.url.shift(); 
272             YAHOO.log('skipping empty url');
273             return _next(id);
274         }
275
276         YAHOO.log("attempting to load " + url, "info", "Get");
277
278         if (q.timeout) {
279             // Y.log('create timer');
280             q.timer = lang.later(q.timeout, q, _timeout, id);
281         }
282
283         if (q.type === "script") {
284             n = _scriptNode(url, w, q.charset);
285         } else {
286             n = _linkNode(url, w, q.charset);
287         }
288
289         // track this node's load progress
290         _track(q.type, n, id, url, w, q.url.length);
291
292         // add the node to the queue so we can return it to the user supplied callback
293         q.nodes.push(n);
294
295         // add it to the head or insert it before 'insertBefore'
296         if (q.insertBefore) {
297             var s = _get(q.insertBefore, id);
298             if (s) {
299                 s.parentNode.insertBefore(n, s);
300             }
301         } else {
302             h.appendChild(n);
303         }
304         
305         YAHOO.log("Appending node: " + url, "info", "Get");
306
307         // FireFox does not support the onload event for link nodes, so there is
308         // no way to make the css requests synchronous. This means that the css 
309         // rules in multiple files could be applied out of order in this browser
310         // if a later request returns before an earlier one.  Safari too.
311         if ((ua.webkit || ua.gecko) && q.type === "css") {
312             _next(id, url);
313         }
314     };
315
316     /**
317      * Removes processed queues and corresponding nodes
318      * @method _autoPurge
319      * @private
320      */
321     var _autoPurge = function() {
322
323         if (purging) {
324             return;
325         }
326
327         purging = true;
328         for (var i in queues) {
329             var q = queues[i];
330             if (q.autopurge && q.finished) {
331                 _purge(q.tId);
332                 delete queues[i];
333             }
334         }
335
336         purging = false;
337     };
338
339     /**
340      * Removes the nodes for the specified queue
341      * @method _purge
342      * @private
343      */
344     var _purge = function(tId) {
345         var q=queues[tId];
346         if (q) {
347             var n=q.nodes, l=n.length, d=q.win.document, 
348                 h=d.getElementsByTagName("head")[0];
349
350             if (q.insertBefore) {
351                 var s = _get(q.insertBefore, tId);
352                 if (s) {
353                     h = s.parentNode;
354                 }
355             }
356
357             for (var i=0; i<l; i=i+1) {
358                 h.removeChild(n[i]);
359             }
360
361             q.nodes = [];
362         }
363     };
364
365     /**
366      * Saves the state for the request and begins loading
367      * the requested urls
368      * @method queue
369      * @param type {string} the type of node to insert
370      * @param url {string} the url to load
371      * @param opts the hash of options for this request
372      * @private
373      */
374     var _queue = function(type, url, opts) {
375
376         var id = "q" + (qidx++);
377         opts = opts || {};
378
379         if (qidx % YAHOO.util.Get.PURGE_THRESH === 0) {
380             _autoPurge();
381         }
382
383         queues[id] = lang.merge(opts, {
384             tId: id,
385             type: type,
386             url: url,
387             finished: false,
388             aborted: false,
389             nodes: []
390         });
391
392         var q = queues[id];
393         q.win = q.win || window;
394         q.scope = q.scope || q.win;
395         q.autopurge = ("autopurge" in q) ? q.autopurge : 
396                       (type === "script") ? true : false;
397
398         lang.later(0, q, _next, id);
399
400         return {
401             tId: id
402         };
403     };
404
405     /**
406      * Detects when a node has been loaded.  In the case of
407      * script nodes, this does not guarantee that contained
408      * script is ready to use.
409      * @method _track
410      * @param type {string} the type of node to track
411      * @param n {HTMLElement} the node to track
412      * @param id {string} the id of the request
413      * @param url {string} the url that is being loaded
414      * @param win {Window} the targeted window
415      * @param qlength the number of remaining items in the queue,
416      * including this one
417      * @param trackfn {Function} function to execute when finished
418      * the default is _next
419      * @private
420      */
421     var _track = function(type, n, id, url, win, qlength, trackfn) {
422         var f = trackfn || _next;
423
424         // IE supports the readystatechange event for script and css nodes
425         if (ua.ie) {
426             n.onreadystatechange = function() {
427                 var rs = this.readyState;
428                 if ("loaded" === rs || "complete" === rs) {
429                     YAHOO.log(id + " onload " + url, "info", "Get");
430                     n.onreadystatechange = null;
431                     f(id, url);
432                 }
433             };
434
435         // webkit prior to 3.x is problemmatic
436         } else if (ua.webkit) {
437
438             if (type === "script") {
439
440                 // Safari 3.x supports the load event for script nodes (DOM2)
441                 if (ua.webkit >= 420) {
442
443                     n.addEventListener("load", function() {
444                         YAHOO.log(id + " DOM2 onload " + url, "info", "Get");
445                         f(id, url);
446                     });
447
448                 // Nothing can be done with Safari < 3.x except to pause and hope
449                 // for the best, particularly after last script is inserted. The
450                 // scripts will always execute in the order they arrive, not
451                 // necessarily the order in which they were inserted.  To support
452                 // script nodes with complete reliability in these browsers, script
453                 // nodes either need to invoke a function in the window once they
454                 // are loaded or the implementer needs to provide a well-known
455                 // property that the utility can poll for.
456                 } else {
457                     // Poll for the existence of the named variable, if it
458                     // was supplied.
459                     var q = queues[id];
460                     if (q.varName) {
461                         var freq=YAHOO.util.Get.POLL_FREQ;
462                         YAHOO.log("Polling for " + q.varName[0]);
463                         q.maxattempts = YAHOO.util.Get.TIMEOUT/freq;
464                         q.attempts = 0;
465                         q._cache = q.varName[0].split(".");
466                         q.timer = lang.later(freq, q, function(o) {
467                             var a=this._cache, l=a.length, w=this.win, i;
468                             for (i=0; i<l; i=i+1) {
469                                 w = w[a[i]];
470                                 if (!w) {
471                                     // if we have exausted our attempts, give up
472                                     this.attempts++;
473                                     if (this.attempts++ > this.maxattempts) {
474                                         var msg = "Over retry limit, giving up";
475                                         q.timer.cancel();
476                                         _fail(id, msg);
477                                     } else {
478                                         YAHOO.log(a[i] + " failed, retrying");
479                                     }
480                                     return;
481                                 }
482                             }
483                             
484                             YAHOO.log("Safari poll complete");
485
486                             q.timer.cancel();
487                             f(id, url);
488
489                         }, null, true);
490                     } else {
491                         lang.later(YAHOO.util.Get.POLL_FREQ, null, f, [id, url]);
492                     }
493                 }
494             } 
495
496         // FireFox and Opera support onload (but not DOM2 in FF) handlers for
497         // script nodes.  Opera, but not FF, supports the onload event for link
498         // nodes.
499         } else { 
500             n.onload = function() {
501                 YAHOO.log(id + " onload " + url, "info", "Get");
502                 f(id, url);
503             };
504         }
505     };
506
507     return {
508
509         /**
510          * The default poll freqency in ms, when needed
511          * @property POLL_FREQ
512          * @static
513          * @type int
514          * @default 10
515          */
516         POLL_FREQ: 10,
517
518         /**
519          * The number of request required before an automatic purge.
520          * property PURGE_THRESH
521          * @static
522          * @type int
523          * @default 20
524          */
525         PURGE_THRESH: 20,
526
527         /**
528          * The length time to poll for varName when loading a script in
529          * Safari 2.x before the transaction fails.
530          * property TIMEOUT
531          * @static
532          * @type int
533          * @default 2000
534          */
535         TIMEOUT: 2000,
536         
537         /**
538          * Called by the the helper for detecting script load in Safari
539          * @method _finalize
540          * @param id {string} the transaction id
541          * @private
542          */
543         _finalize: function(id) {
544             YAHOO.log(id + " finalized ", "info", "Get");
545             lang.later(0, null, _finish, id);
546         },
547
548         /**
549          * Abort a transaction
550          * @method abort
551          * @param {string|object} either the tId or the object returned from
552          * script() or css()
553          */
554         abort: function(o) {
555             var id = (lang.isString(o)) ? o : o.tId;
556             var q = queues[id];
557             if (q) {
558                 YAHOO.log("Aborting " + id, "info", "Get");
559                 q.aborted = true;
560             }
561         }, 
562
563         /**
564          * Fetches and inserts one or more script nodes into the head
565          * of the current document or the document in a specified window.
566          *
567          * @method script
568          * @static
569          * @param url {string|string[]} the url or urls to the script(s)
570          * @param opts {object} Options: 
571          * <dl>
572          * <dt>onSuccess</dt>
573          * <dd>
574          * callback to execute when the script(s) are finished loading
575          * The callback receives an object back with the following
576          * data:
577          * <dl>
578          * <dt>win</dt>
579          * <dd>the window the script(s) were inserted into</dd>
580          * <dt>data</dt>
581          * <dd>the data object passed in when the request was made</dd>
582          * <dt>nodes</dt>
583          * <dd>An array containing references to the nodes that were
584          * inserted</dd>
585          * <dt>purge</dt>
586          * <dd>A function that, when executed, will remove the nodes
587          * that were inserted</dd>
588          * <dt>
589          * </dl>
590          * </dd>
591          * <dt>onFailure</dt>
592          * <dd>
593          * callback to execute when the script load operation fails
594          * The callback receives an object back with the following
595          * data:
596          * <dl>
597          * <dt>win</dt>
598          * <dd>the window the script(s) were inserted into</dd>
599          * <dt>data</dt>
600          * <dd>the data object passed in when the request was made</dd>
601          * <dt>nodes</dt>
602          * <dd>An array containing references to the nodes that were
603          * inserted successfully</dd>
604          * <dt>purge</dt>
605          * <dd>A function that, when executed, will remove any nodes
606          * that were inserted</dd>
607          * <dt>
608          * </dl>
609          * </dd>
610          * <dt>onTimeout</dt>
611          * <dd>
612          * callback to execute when a timeout occurs.
613          * The callback receives an object back with the following
614          * data:
615          * <dl>
616          * <dt>win</dt>
617          * <dd>the window the script(s) were inserted into</dd>
618          * <dt>data</dt>
619          * <dd>the data object passed in when the request was made</dd>
620          * <dt>nodes</dt>
621          * <dd>An array containing references to the nodes that were
622          * inserted</dd>
623          * <dt>purge</dt>
624          * <dd>A function that, when executed, will remove the nodes
625          * that were inserted</dd>
626          * <dt>
627          * </dl>
628          * </dd>
629          * <dt>scope</dt>
630          * <dd>the execution context for the callbacks</dd>
631          * <dt>win</dt>
632          * <dd>a window other than the one the utility occupies</dd>
633          * <dt>autopurge</dt>
634          * <dd>
635          * setting to true will let the utilities cleanup routine purge 
636          * the script once loaded
637          * </dd>
638          * <dt>data</dt>
639          * <dd>
640          * data that is supplied to the callback when the script(s) are
641          * loaded.
642          * </dd>
643          * <dt>varName</dt>
644          * <dd>
645          * variable that should be available when a script is finished
646          * loading.  Used to help Safari 2.x and below with script load 
647          * detection.  The type of this property should match what was
648          * passed into the url parameter: if loading a single url, a
649          * string can be supplied.  If loading multiple scripts, you
650          * must supply an array that contains the variable name for
651          * each script.
652          * </dd>
653          * <dt>insertBefore</dt>
654          * <dd>node or node id that will become the new node's nextSibling</dd>
655          * </dl>
656          * <dt>charset</dt>
657          * <dd>Node charset, default utf-8</dd>
658          * <dt>timeout</dt>
659          * <dd>Number of milliseconds to wait before aborting and firing the timeout event</dd>
660          * <pre>
661          * // assumes yahoo, dom, and event are already on the page
662          * &nbsp;&nbsp;YAHOO.util.Get.script(
663          * &nbsp;&nbsp;["http://yui.yahooapis.com/2.3.1/build/dragdrop/dragdrop-min.js",
664          * &nbsp;&nbsp;&nbsp;"http://yui.yahooapis.com/2.3.1/build/animation/animation-min.js"], &#123;
665          * &nbsp;&nbsp;&nbsp;&nbsp;onSuccess: function(o) &#123;
666          * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;YAHOO.log(o.data); // foo
667          * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new YAHOO.util.DDProxy("dd1"); // also new o.reference("dd1"); would work
668          * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.log("won't cause error because YAHOO is the scope");
669          * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.log(o.nodes.length === 2) // true
670          * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// o.purge(); // optionally remove the script nodes immediately
671          * &nbsp;&nbsp;&nbsp;&nbsp;&#125;,
672          * &nbsp;&nbsp;&nbsp;&nbsp;onFailure: function(o) &#123;
673          * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;YAHOO.log("transaction failed");
674          * &nbsp;&nbsp;&nbsp;&nbsp;&#125;,
675          * &nbsp;&nbsp;&nbsp;&nbsp;data: "foo",
676          * &nbsp;&nbsp;&nbsp;&nbsp;timeout: 10000, // 10 second timeout
677          * &nbsp;&nbsp;&nbsp;&nbsp;scope: YAHOO,
678          * &nbsp;&nbsp;&nbsp;&nbsp;// win: otherframe // target another window/frame
679          * &nbsp;&nbsp;&nbsp;&nbsp;autopurge: true // allow the utility to choose when to remove the nodes
680          * &nbsp;&nbsp;&#125;);
681          * </pre>
682          * @return {tId: string} an object containing info about the transaction
683          */
684         script: function(url, opts) { return _queue("script", url, opts); },
685
686         /**
687          * Fetches and inserts one or more css link nodes into the 
688          * head of the current document or the document in a specified
689          * window.
690          * @method css
691          * @static
692          * @param url {string} the url or urls to the css file(s)
693          * @param opts Options: 
694          * <dl>
695          * <dt>onSuccess</dt>
696          * <dd>
697          * callback to execute when the css file(s) are finished loading
698          * The callback receives an object back with the following
699          * data:
700          * <dl>win</dl>
701          * <dd>the window the link nodes(s) were inserted into</dd>
702          * <dt>data</dt>
703          * <dd>the data object passed in when the request was made</dd>
704          * <dt>nodes</dt>
705          * <dd>An array containing references to the nodes that were
706          * inserted</dd>
707          * <dt>purge</dt>
708          * <dd>A function that, when executed, will remove the nodes
709          * that were inserted</dd>
710          * <dt>
711          * </dl>
712          * </dd>
713          * <dt>scope</dt>
714          * <dd>the execution context for the callbacks</dd>
715          * <dt>win</dt>
716          * <dd>a window other than the one the utility occupies</dd>
717          * <dt>data</dt>
718          * <dd>
719          * data that is supplied to the callbacks when the nodes(s) are
720          * loaded.
721          * </dd>
722          * <dt>insertBefore</dt>
723          * <dd>node or node id that will become the new node's nextSibling</dd>
724          * <dt>charset</dt>
725          * <dd>Node charset, default utf-8</dd>
726          * </dl>
727          * <pre>
728          *      YAHOO.util.Get.css("http://yui.yahooapis.com/2.3.1/build/menu/assets/skins/sam/menu.css");
729          * </pre>
730          * <pre>
731          *      YAHOO.util.Get.css(["http://yui.yahooapis.com/2.3.1/build/menu/assets/skins/sam/menu.css",
732          *                          "http://yui.yahooapis.com/2.3.1/build/logger/assets/skins/sam/logger.css"]);
733          * </pre>
734          * @return {tId: string} an object containing info about the transaction
735          */
736         css: function(url, opts) {
737             return _queue("css", url, opts); 
738         }
739     };
740 }();
741
742 YAHOO.register("get", YAHOO.util.Get, {version: "2.7.0", build: "1799"});