2
Copyright (c) 2008, Yahoo! Inc. All rights reserved.
3
Code licensed under the BSD License:
4
http://developer.yahoo.net/yui/license.txt
8
YUI.add("get", function(Y) {
14
* Provides a mechanism to fetch remote resources and
15
* insert them into a document.
21
* Fetches and inserts one or more script or link nodes into the document
28
* hash of queues to manage multiple requests
35
* queue index used to generate transaction ids
43
* node index used to generate unique node ids
55
* interal property used to prevent multiple simultaneous purge
65
* Generates an HTML element, this is not appended to a document
67
* @param type {string} the type of element
68
* @param attr {string} the attributes
69
* @param win {Window} optional window to create the element in
70
* @return {HTMLElement} the generated node
73
var _node = function(type, attr, win) {
74
var w = win || Y.config.win, d=w.document, n=d.createElement(type);
77
if (attr[i] && Y.Object.owns(attr, i)) {
78
n.setAttribute(i, attr[i]);
86
* Generates a link node
88
* @param url {string} the url for the css file
89
* @param win {Window} optional window to create the node in
90
* @return {HTMLElement} the generated node
93
var _linkNode = function(url, win, charset) {
94
var c = charset || "utf-8";
95
return _node("link", {
96
"id": "yui__dyn_" + (nidx++),
105
* Generates a script node
106
* @method _scriptNode
107
* @param url {string} the url for the script file
108
* @param win {Window} optional window to create the node in
109
* @return {HTMLElement} the generated node
112
var _scriptNode = function(url, win, charset) {
113
var c = charset || "utf-8";
114
return _node("script", {
115
"id": "yui__dyn_" + (nidx++),
116
"type": "text/javascript",
123
* Removes the nodes for the specified queue
127
var _purge = function(tId) {
130
var n=q.nodes, l=n.length, d=q.win.document,
131
h=d.getElementsByTagName("head")[0];
133
if (q.insertBefore) {
134
var s = _get(q.insertBefore, tId);
140
for (var i=0; i<l; i=i+1) {
148
* Returns the data payload for callback functions
149
* @method _returnData
152
var _returnData = function(q, msg) {
166
* The request failed, execute fail handler with whatever
167
* was accomplished. There isn't a failure case at the
168
* moment unless you count aborted transactions
170
* @param id {string} the id of the request
173
var _fail = function(id, msg) {
175
Y.log("get failure: " + msg, "warn", "get");
182
// execute failure callback
184
var sc=q.context || q;
185
q.onFailure.call(sc, _returnData(q, msg));
189
var _get = function(nId, tId) {
191
n = (L.isString(nId)) ? q.win.document.getElementById(nId) : nId;
193
_fail(tId, "target node not found: " + nId);
201
* The request is complete, so executing the requester's callback
203
* @param id {string} the id of the request
206
var _finish = function(id) {
207
Y.log("Finishing transaction " + id, "info", "get");
215
var msg = "transaction " + id + " was aborted";
220
// execute success callback
222
var sc=q.context || q;
223
q.onSuccess.call(sc, _returnData(q));
230
* @param id {string} the id of the request
233
var _timeout = function(id) {
234
Y.log("Timeout " + id, "info", "get");
237
var sc=q.context || q;
238
q.onTimeout.call(sc, _returnData(q));
243
* Loads the next item for a given request
245
* @param id {string} the id of the request
246
* @param loaded {string} the url that was just loaded, if any
249
var _next = function(id, loaded) {
250
Y.log("_next: " + id + ", loaded: " + loaded, "info", "get");
255
// Y.log('cancel timer');
260
var msg = "transaction " + id + " was aborted";
271
// This is the first pass: make sure the url is an array
272
q.url = (L.isString(q.url)) ? [q.url] : q.url;
274
q.varName = (L.isString(q.varName)) ? [q.varName] : q.varName;
278
var w=q.win, d=w.document, h=d.getElementsByTagName("head")[0], n;
280
if (q.url.length === 0) {
287
// if the url is undefined, this is probably a trailing comma problem in IE
290
Y.log('skipping empty url');
294
Y.log("attempting to load " + url, "info", "get");
297
// Y.log('create timer');
298
q.timer = L.later(q.timeout, q, _timeout, id);
301
if (q.type === "script") {
302
n = _scriptNode(url, w, q.charset);
304
n = _linkNode(url, w, q.charset);
307
// track this node's load progress
308
_track(q.type, n, id, url, w, q.url.length);
310
// add the node to the queue so we can return it to the user supplied callback
313
// add it to the head or insert it before 'insertBefore'
314
if (q.insertBefore) {
315
var s = _get(q.insertBefore, id);
317
s.parentNode.insertBefore(n, s);
323
Y.log("Appending node: " + url, "info", "get");
325
// FireFox does not support the onload event for link nodes, so there is
326
// no way to make the css requests synchronous. This means that the css
327
// rules in multiple files could be applied out of order in this browser
328
// if a later request returns before an earlier one. Safari too.
329
if ((ua.webkit || ua.gecko) && q.type === "css") {
335
* Removes processed queues and corresponding nodes
339
var _autoPurge = function() {
346
for (var i in queues) {
347
if (queues.hasOwnProperty(i)) {
349
if (q.autopurge && q.finished) {
360
* Saves the state for the request and begins loading
363
* @param type {string} the type of node to insert
364
* @param url {string} the url to load
365
* @param opts the hash of options for this request
368
var _queue = function(type, url, opts) {
370
var id = "q" + (qidx++);
373
if (qidx % Y.Get.PURGE_THRESH === 0) {
377
queues[id] = Y.merge(opts, {
386
q.win = q.win || Y.config.win;
387
q.context = q.context || q;
388
q.autopurge = ("autopurge" in q) ? q.autopurge :
389
(type === "script") ? true : false;
391
L.later(0, q, _next, id);
399
* Detects when a node has been loaded. In the case of
400
* script nodes, this does not guarantee that contained
401
* script is ready to use.
403
* @param type {string} the type of node to track
404
* @param n {HTMLElement} the node to track
405
* @param id {string} the id of the request
406
* @param url {string} the url that is being loaded
407
* @param win {Window} the targeted window
408
* @param qlength the number of remaining items in the queue,
410
* @param trackfn {Function} function to execute when finished
411
* the default is _next
414
var _track = function(type, n, id, url, win, qlength, trackfn) {
415
var f = trackfn || _next;
417
// IE supports the readystatechange event for script and css nodes
418
// Opera only for script nodes. Opera support onload for script
419
// nodes, but this doesn't fire when there is a load failure.
420
// The onreadystatechange appears to be a better way to respond
421
// to both success and failure.
423
n.onreadystatechange = function() {
424
var rs = this.readyState;
425
if ("loaded" === rs || "complete" === rs) {
426
Y.log(id + " onreadstatechange " + url, "info", "get");
427
n.onreadystatechange = null;
432
// webkit prior to 3.x is no longer supported
433
} else if (ua.webkit) {
435
if (type === "script") {
436
// Safari 3.x supports the load event for script nodes (DOM2)
437
n.addEventListener("load", function() {
438
Y.log(id + " DOM2 onload " + url, "info", "get");
443
// FireFox and Opera support onload (but not DOM2 in FF) handlers for
444
// script nodes. Opera, but not FF, supports the onload event for link
448
n.onload = function() {
449
Y.log(id + " onload " + url, "info", "get");
453
n.onerror = function(e) {
454
_fail(id, e + ": " + url);
462
* The number of request required before an automatic purge.
463
* property PURGE_THRESH
471
* Called by the the helper for detecting script load in Safari
474
* @param id {string} the transaction id
477
_finalize: function(id) {
478
Y.log(id + " finalized ", "info", "get");
479
L.later(0, null, _finish, id);
483
* Abort a transaction
486
* @param o {string|object} Either the tId or the object returned from
490
var id = (L.isString(o)) ? o : o.tId;
493
Y.log("Aborting " + id, "info", "get");
499
* Fetches and inserts one or more script nodes into the head
500
* of the current document or the document in a specified window.
504
* @param url {string|string[]} the url or urls to the script(s)
505
* @param opts {object} Options:
509
* callback to execute when the script(s) are finished loading
510
* The callback receives an object back with the following
514
* <dd>the window the script(s) were inserted into</dd>
516
* <dd>the data object passed in when the request was made</dd>
518
* <dd>An array containing references to the nodes that were
521
* <dd>A function that, when executed, will remove the nodes
522
* that were inserted</dd>
528
* callback to execute when a timeout occurs.
529
* The callback receives an object back with the following
533
* <dd>the window the script(s) were inserted into</dd>
535
* <dd>the data object passed in when the request was made</dd>
537
* <dd>An array containing references to the nodes that were
540
* <dd>A function that, when executed, will remove the nodes
541
* that were inserted</dd>
547
* callback to execute when the script load operation fails
548
* The callback receives an object back with the following
552
* <dd>the window the script(s) were inserted into</dd>
554
* <dd>the data object passed in when the request was made</dd>
556
* <dd>An array containing references to the nodes that were
557
* inserted successfully</dd>
559
* <dd>A function that, when executed, will remove any nodes
560
* that were inserted</dd>
565
* <dd>the execution context for the callbacks</dd>
567
* <dd>a window other than the one the utility occupies</dd>
570
* setting to true will let the utilities cleanup routine purge
571
* the script once loaded
575
* data that is supplied to the callback when the script(s) are
578
* <dt>insertBefore</dt>
579
* <dd>node or node id that will become the new node's nextSibling</dd>
582
* <dd>Node charset, default utf-8</dd>
584
* <dd>Number of milliseconds to wait before aborting and firing the timeout event</dd>
586
* Y.Get.script(
587
* ["http://yui.yahooapis.com/2.5.2/build/yahoo/yahoo-min.js",
588
* "http://yui.yahooapis.com/2.5.2/build/event/event-min.js"], {
589
* onSuccess: function(o) {
590
* this.log("won't cause error because Y is the context");
591
* Y.log(o.data); // foo
592
* Y.log(o.nodes.length === 2) // true
593
* // o.purge(); // optionally remove the script nodes immediately
594
* },
595
* onFailure: function(o) {
596
* Y.log("transaction failed");
597
* },
598
* onTimeout: function(o) {
599
* Y.log("transaction timed out");
600
* },
601
* data: "foo",
602
* timeout: 10000, // 10 second timeout
603
* context: Y, // make the YUI instance
604
* // win: otherframe // target another window/frame
605
* autopurge: true // allow the utility to choose when to remove the nodes
606
* });
608
* @return {tId: string} an object containing info about the transaction
610
script: function(url, opts) {
611
return _queue("script", url, opts);
615
* Fetches and inserts one or more css link nodes into the
616
* head of the current document or the document in a specified
620
* @param url {string} the url or urls to the css file(s)
621
* @param opts Options:
625
* callback to execute when the css file(s) are finished loading
626
* The callback receives an object back with the following
629
* <dd>the window the link nodes(s) were inserted into</dd>
631
* <dd>the data object passed in when the request was made</dd>
633
* <dd>An array containing references to the nodes that were
636
* <dd>A function that, when executed, will remove the nodes
637
* that were inserted</dd>
642
* <dd>the execution context for the callbacks</dd>
644
* <dd>a window other than the one the utility occupies</dd>
647
* data that is supplied to the callbacks when the nodes(s) are
650
* <dt>insertBefore</dt>
651
* <dd>node or node id that will become the new node's nextSibling</dd>
653
* <dd>Node charset, default utf-8</dd>
656
* Y.Get.css("http://yui.yahooapis.com/2.3.1/build/menu/assets/skins/sam/menu.css");
659
* Y.Get.css(
660
* ["http://yui.yahooapis.com/2.3.1/build/menu/assets/skins/sam/menu.css",
661
* "http://yui.yahooapis.com/2.3.1/build/logger/assets/skins/sam/logger.css"], {
662
* insertBefore: 'custom-styles' // nodes will be inserted before the specified node
663
* });
665
* @return {tId: string} an object containing info about the transaction
667
css: function(url, opts) {
668
return _queue("css", url, opts);