/src/main/resources/org/apache/struts2/static/get/get.js

http://struts2yuiplugin.googlecode.com/ · JavaScript · 724 lines · 270 code · 76 blank · 378 comment · 66 complexity · 158b905b602116717045dcc20682ec8c MD5 · raw file

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