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