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
* The request failed, execute fail handler with whatever
124
* was accomplished. There isn't a failure case at the
125
* moment unless you count aborted transactions
127
* @param id {string} the id of the request
130
var _fail = function(id, msg) {
132
Y.log("get failure: " + msg, "warn", "get");
139
// execute failure callback
141
var sc=q.context || q;
142
q.onFailure.call(sc, _returnData(q, msg));
146
var _get = function(nId, tId) {
148
n = (L.isString(nId)) ? q.win.document.getElementById(nId) : nId;
150
_fail(tId, "target node not found: " + nId);
157
* Removes the nodes for the specified queue
161
var _purge = function(tId) {
164
var n=q.nodes, l=n.length, d=q.win.document,
165
h=d.getElementsByTagName("head")[0];
167
if (q.insertBefore) {
168
var s = _get(q.insertBefore, tId);
174
for (var i=0; i<l; i=i+1) {
182
* Returns the data payload for callback functions
183
* @method _returnData
186
var _returnData = function(q, msg) {
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) {
286
Y.log("attempting to load " + url, "info", "get");
289
// Y.log('create timer');
290
q.timer = L.later(q.timeout, q, _timeout, id);
293
if (q.type === "script") {
294
n = _scriptNode(url, w, q.charset);
296
n = _linkNode(url, w, q.charset);
299
// track this node's load progress
300
_track(q.type, n, id, url, w, q.url.length);
302
// add the node to the queue so we can return it to the user supplied callback
305
// add it to the head or insert it before 'insertBefore'
306
if (q.insertBefore) {
307
var s = _get(q.insertBefore, id);
309
s.parentNode.insertBefore(n, s);
315
Y.log("Appending node: " + url, "info", "get");
317
// FireFox does not support the onload event for link nodes, so there is
318
// no way to make the css requests synchronous. This means that the css
319
// rules in multiple files could be applied out of order in this browser
320
// if a later request returns before an earlier one. Safari too.
321
if ((ua.webkit || ua.gecko) && q.type === "css") {
327
* Removes processed queues and corresponding nodes
331
var _autoPurge = function() {
338
for (var i in queues) {
339
if (queues.hasOwnProperty(i)) {
341
if (q.autopurge && q.finished) {
352
* Saves the state for the request and begins loading
355
* @param type {string} the type of node to insert
356
* @param url {string} the url to load
357
* @param opts the hash of options for this request
360
var _queue = function(type, url, opts) {
362
var id = "q" + (qidx++);
365
if (qidx % Y.Get.PURGE_THRESH === 0) {
369
queues[id] = Y.merge(opts, {
378
q.win = q.win || Y.config.win;
379
q.context = q.context || q;
380
q.autopurge = ("autopurge" in q) ? q.autopurge :
381
(type === "script") ? true : false;
383
L.later(0, q, _next, id);
391
* Detects when a node has been loaded. In the case of
392
* script nodes, this does not guarantee that contained
393
* script is ready to use.
395
* @param type {string} the type of node to track
396
* @param n {HTMLElement} the node to track
397
* @param id {string} the id of the request
398
* @param url {string} the url that is being loaded
399
* @param win {Window} the targeted window
400
* @param qlength the number of remaining items in the queue,
402
* @param trackfn {Function} function to execute when finished
403
* the default is _next
406
var _track = function(type, n, id, url, win, qlength, trackfn) {
407
var f = trackfn || _next;
409
// IE supports the readystatechange event for script and css nodes
410
// Opera only for script nodes. Opera support onload for script
411
// nodes, but this doesn't fire when their is a load failure.
412
// The onreadystatechange appears to be a better way to respond
413
// to both success and failure.
415
n.onreadystatechange = function() {
416
var rs = this.readyState;
417
if ("loaded" === rs || "complete" === rs) {
418
Y.log(id + " onreadstatechange " + url, "info", "get");
423
// webkit prior to 3.x is no longer supported
424
} else if (ua.webkit) {
426
if (type === "script") {
427
// Safari 3.x supports the load event for script nodes (DOM2)
428
n.addEventListener("load", function() {
429
Y.log(id + " DOM2 onload " + url, "info", "get");
434
// FireFox and Opera support onload (but not DOM2 in FF) handlers for
435
// script nodes. Opera, but not FF, supports the onload event for link
439
n.onload = function() {
440
Y.log(id + " onload " + url, "info", "get");
444
n.onerror = function(e) {
445
_fail(id, e + ": " + url);
453
* The number of request required before an automatic purge.
454
* property PURGE_THRESH
462
* Called by the the helper for detecting script load in Safari
465
* @param id {string} the transaction id
468
_finalize: function(id) {
469
Y.log(id + " finalized ", "info", "get");
470
L.later(0, null, _finish, id);
474
* Abort a transaction
477
* @param o {string|object} Either the tId or the object returned from
481
var id = (L.isString(o)) ? o : o.tId;
484
Y.log("Aborting " + id, "info", "get");
490
* Fetches and inserts one or more script nodes into the head
491
* of the current document or the document in a specified window.
495
* @param url {string|string[]} the url or urls to the script(s)
496
* @param opts {object} Options:
500
* callback to execute when the script(s) are finished loading
501
* The callback receives an object back with the following
505
* <dd>the window the script(s) were inserted into</dd>
507
* <dd>the data object passed in when the request was made</dd>
509
* <dd>An array containing references to the nodes that were
512
* <dd>A function that, when executed, will remove the nodes
513
* that were inserted</dd>
519
* callback to execute when a timeout occurs.
520
* The callback receives an object back with the following
524
* <dd>the window the script(s) were inserted into</dd>
526
* <dd>the data object passed in when the request was made</dd>
528
* <dd>An array containing references to the nodes that were
531
* <dd>A function that, when executed, will remove the nodes
532
* that were inserted</dd>
538
* callback to execute when the script load operation fails
539
* The callback receives an object back with the following
543
* <dd>the window the script(s) were inserted into</dd>
545
* <dd>the data object passed in when the request was made</dd>
547
* <dd>An array containing references to the nodes that were
548
* inserted successfully</dd>
550
* <dd>A function that, when executed, will remove any nodes
551
* that were inserted</dd>
556
* <dd>the execution context for the callbacks</dd>
558
* <dd>a window other than the one the utility occupies</dd>
561
* setting to true will let the utilities cleanup routine purge
562
* the script once loaded
566
* data that is supplied to the callback when the script(s) are
569
* <dt>insertBefore</dt>
570
* <dd>node or node id that will become the new node's nextSibling</dd>
573
* <dd>Node charset, default utf-8</dd>
575
* <dd>Number of milliseconds to wait before aborting and firing the timeout event</dd>
577
* Y.Get.script(
578
* ["http://yui.yahooapis.com/2.5.2/build/yahoo/yahoo-min.js",
579
* "http://yui.yahooapis.com/2.5.2/build/event/event-min.js"], {
580
* onSuccess: function(o) {
581
* this.log("won't cause error because Y is the context");
582
* Y.log(o.data); // foo
583
* Y.log(o.nodes.length === 2) // true
584
* // o.purge(); // optionally remove the script nodes immediately
585
* },
586
* onFailure: function(o) {
587
* Y.log("transaction failed");
588
* },
589
* onTimeout: function(o) {
590
* Y.log("transaction timed out");
591
* },
592
* data: "foo",
593
* timeout: 10000, // 10 second timeout
594
* context: Y, // make the YUI instance
595
* // win: otherframe // target another window/frame
596
* autopurge: true // allow the utility to choose when to remove the nodes
597
* });
599
* @return {tId: string} an object containing info about the transaction
601
script: function(url, opts) {
602
return _queue("script", url, opts);
606
* Fetches and inserts one or more css link nodes into the
607
* head of the current document or the document in a specified
611
* @param url {string} the url or urls to the css file(s)
612
* @param opts Options:
616
* callback to execute when the css file(s) are finished loading
617
* The callback receives an object back with the following
620
* <dd>the window the link nodes(s) were inserted into</dd>
622
* <dd>the data object passed in when the request was made</dd>
624
* <dd>An array containing references to the nodes that were
627
* <dd>A function that, when executed, will remove the nodes
628
* that were inserted</dd>
633
* <dd>the execution context for the callbacks</dd>
635
* <dd>a window other than the one the utility occupies</dd>
638
* data that is supplied to the callbacks when the nodes(s) are
641
* <dt>insertBefore</dt>
642
* <dd>node or node id that will become the new node's nextSibling</dd>
644
* <dd>Node charset, default utf-8</dd>
647
* Y.Get.css("http://yui.yahooapis.com/2.3.1/build/menu/assets/skins/sam/menu.css");
650
* Y.Get.css(
651
* ["http://yui.yahooapis.com/2.3.1/build/menu/assets/skins/sam/menu.css",
652
* "http://yui.yahooapis.com/2.3.1/build/logger/assets/skins/sam/logger.css"], {
653
* insertBefore: 'custom-styles' // nodes will be inserted before the specified node
654
* });
656
* @return {tId: string} an object containing info about the transaction
658
css: function(url, opts) {
659
return _queue("css", url, opts);