3b38bdf8e7baed588b2db9865f693abf67468632.svn-base 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. if (!dojo._hasResource["dojo.io.iframe"]) { // _hasResource checks added by
  2. // build. Do not use _hasResource
  3. // directly in your code.
  4. dojo._hasResource["dojo.io.iframe"] = true;
  5. dojo.provide("dojo.io.iframe");
  6. dojo.io.iframe = {
  7. create : function(/* String */fname, /* String */onloadstr, /* String? */
  8. uri) {
  9. // summary:
  10. // Creates a hidden iframe in the page. Used mostly for IO
  11. // transports. You do not need to call this to start a
  12. // dojo.io.iframe request. Just call send().
  13. // fname: String
  14. // The name of the iframe. Used for the name attribute on the
  15. // iframe.
  16. // onloadstr: String
  17. // A string of JavaScript that will be executed when the content
  18. // in the iframe loads.
  19. // uri: String
  20. // The value of the src attribute on the iframe element. If a
  21. // value is not given, then dojo/resources/blank.html will be
  22. // used.
  23. if (window[fname]) {
  24. return window[fname];
  25. }
  26. if (window.frames[fname]) {
  27. return window.frames[fname];
  28. }
  29. var cframe = null;
  30. var turi = uri;
  31. if (!turi) {
  32. if (djConfig["useXDomain"] && !djConfig["dojoBlankHtmlUrl"]) {
  33. console
  34. .debug("dojo.io.iframe.create: When using cross-domain Dojo builds,"
  35. + " please save dojo/resources/blank.html to your domain and set djConfig.dojoBlankHtmlUrl"
  36. + " to the path on your domain to blank.html");
  37. }
  38. turi = (djConfig["dojoBlankHtmlUrl"] || dojo.moduleUrl("dojo",
  39. "resources/blank.html"));
  40. }
  41. var ifrstr = dojo.isIE ? '<iframe name="' + fname + '" src="'
  42. + turi + '" onload="' + onloadstr + '">' : 'iframe';
  43. cframe = dojo.doc.createElement(ifrstr);
  44. with (cframe) {
  45. name = fname;
  46. setAttribute("name", fname);
  47. id = fname;
  48. }
  49. dojo.body().appendChild(cframe);
  50. window[fname] = cframe;
  51. with (cframe.style) {
  52. if (dojo.isSafari < 3) {
  53. // We can't change the src in Safari 2.0.3 if absolute
  54. // position. Bizarro.
  55. position = "absolute";
  56. }
  57. left = top = "1px";
  58. height = width = "1px";
  59. visibility = "hidden";
  60. }
  61. if (!dojo.isIE) {
  62. this.setSrc(cframe, turi, true);
  63. cframe.onload = new Function(onloadstr);
  64. }
  65. return cframe;
  66. },
  67. setSrc : function(/* DOMNode */iframe, /* String */src, /* Boolean */
  68. replace) {
  69. // summary:
  70. // Sets the URL that is loaded in an IFrame. The replace parameter
  71. // indicates whether location.replace() should be used when
  72. // changing the location of the iframe.
  73. try {
  74. if (!replace) {
  75. if (dojo.isSafari) {
  76. iframe.location = src;
  77. } else {
  78. frames[iframe.name].location = src;
  79. }
  80. } else {
  81. // Fun with DOM 0 incompatibilities!
  82. var idoc;
  83. if (dojo.isIE || dojo.isSafari > 2) {
  84. idoc = iframe.contentWindow.document;
  85. } else if (dojo.isSafari) {
  86. idoc = iframe.document;
  87. } else { // if(d.isMozilla){
  88. idoc = iframe.contentWindow;
  89. }
  90. // For Safari (at least 2.0.3) and Opera, if the iframe
  91. // has just been created but it doesn't have content
  92. // yet, then iframe.document may be null. In that case,
  93. // use iframe.location and return.
  94. if (!idoc) {
  95. iframe.location = src;
  96. return;
  97. } else {
  98. idoc.location.replace(src);
  99. }
  100. }
  101. } catch (e) {
  102. console.debug("dojo.io.iframe.setSrc: ", e);
  103. }
  104. },
  105. doc : function(/* DOMNode */iframeNode) {
  106. // summary: Returns the document object associated with the iframe
  107. // DOM Node argument.
  108. var doc = iframeNode.contentDocument
  109. || // W3
  110. ((iframeNode.contentWindow) && (iframeNode.contentWindow.document))
  111. || // IE
  112. ((iframeNode.name) && (document.frames[iframeNode.name]) && (document.frames[iframeNode.name].document))
  113. || null;
  114. return doc;
  115. },
  116. /*
  117. * ===== dojo.io.iframe.__ioArgs = function(kwArgs){ // summary: // All
  118. * the properties described in the dojo.__ioArgs type, apply // to this
  119. * type. The following additional properties are allowed // for
  120. * dojo.io.iframe.send(): // method: String? // The HTTP method to use.
  121. * "GET" or "POST" are the only supported // values. It will try to read
  122. * the value from the form node's // method, then try this argument. If
  123. * neither one exists, then it // defaults to POST. // handleAs: String? //
  124. * Specifies what format the result data should be given to the //
  125. * load/handle callback. Valid values are: text, html, javascript, //
  126. * json. IMPORTANT: For all values EXCEPT html, The server // response
  127. * should be an HTML file with a textarea element. The // response data
  128. * should be inside the textarea element. Using an // HTML document the
  129. * only reliable, cross-browser way this // transport can know when the
  130. * response has loaded. For the html // handleAs value, just return a
  131. * normal HTML document. NOTE: xml // or any other XML type is NOT
  132. * supported by this transport. // content: Object? // If "form" is one
  133. * of the other args properties, then the content // object properties
  134. * become hidden form form elements. For // instance, a content object
  135. * of {name1 : "value1"} is converted // to a hidden form element with a
  136. * name of "name1" and a value of // "value1". If there is not a "form"
  137. * property, then the content // object is converted into a
  138. * name=value&name=value string, by // using dojo.objectToQuery(). }
  139. * =====
  140. */
  141. send : function(/* dojo.io.iframe.__ioArgs */args) {
  142. // summary: function that sends the request to the server.
  143. // This transport can only process one send() request at a time, so
  144. // if send() is called
  145. // multiple times, it will queue up the calls and only process one
  146. // at a time.
  147. if (!this["_frame"]) {
  148. this._frame = this.create(this._iframeName,
  149. "dojo.io.iframe._iframeOnload();");
  150. }
  151. // Set up the deferred.
  152. var dfd = dojo._ioSetArgs(args, function(/* Deferred */dfd) {
  153. // summary: canceller function for dojo._ioSetArgs call.
  154. dfd.canceled = true;
  155. dfd.ioArgs._callNext();
  156. }, function(/* Deferred */dfd) {
  157. // summary: okHandler function for dojo._ioSetArgs call.
  158. var value = null;
  159. try {
  160. var ioArgs = dfd.ioArgs;
  161. var dii = dojo.io.iframe;
  162. var ifd = dii.doc(dii._frame);
  163. var handleAs = ioArgs.handleAs;
  164. // Assign correct value based on handleAs value.
  165. value = ifd; // html
  166. if (handleAs != "html") {
  167. value = ifd.getElementsByTagName("textarea")[0].value; // text
  168. if (handleAs == "json") {
  169. value = dojo.fromJson(value); // json
  170. } else if (handleAs == "javascript") {
  171. value = dojo.eval(value); // javascript
  172. }
  173. }
  174. } catch (e) {
  175. value = e;
  176. } finally {
  177. ioArgs._callNext();
  178. }
  179. return value;
  180. }, function(/* Error */error, /* Deferred */dfd) {
  181. // summary: errHandler function for dojo._ioSetArgs
  182. // call.
  183. dfd.ioArgs._hasError = true;
  184. dfd.ioArgs._callNext();
  185. return error;
  186. });
  187. // Set up a function that will fire the next iframe request. Make
  188. // sure it only
  189. // happens once per deferred.
  190. dfd.ioArgs._callNext = function() {
  191. if (!this["_calledNext"]) {
  192. this._calledNext = true;
  193. dojo.io.iframe._currentDfd = null;
  194. dojo.io.iframe._fireNextRequest();
  195. }
  196. }
  197. this._dfdQueue.push(dfd);
  198. this._fireNextRequest();
  199. // Add it the IO watch queue, to get things like timeout support.
  200. dojo._ioWatch(dfd, function(/* Deferred */dfd) {
  201. // validCheck
  202. return !dfd.ioArgs["_hasError"];
  203. }, function(dfd) {
  204. // ioCheck
  205. return (!!dfd.ioArgs["_finished"]);
  206. }, function(dfd) {
  207. // resHandle
  208. if (dfd.ioArgs._finished) {
  209. dfd.callback(dfd);
  210. } else {
  211. dfd
  212. .errback(new Error("Invalid dojo.io.iframe request state"));
  213. }
  214. });
  215. return dfd;
  216. },
  217. _currentDfd : null,
  218. _dfdQueue : [],
  219. _iframeName : "dojoIoIframe",
  220. _fireNextRequest : function() {
  221. // summary: Internal method used to fire the next request in the
  222. // bind queue.
  223. try {
  224. if ((this._currentDfd) || (this._dfdQueue.length == 0)) {
  225. return;
  226. }
  227. var dfd = this._currentDfd = this._dfdQueue.shift();
  228. var ioArgs = dfd.ioArgs;
  229. var args = ioArgs.args;
  230. ioArgs._contentToClean = [];
  231. var fn = args["form"];
  232. var content = args["content"] || {};
  233. if (fn) {
  234. if (content) {
  235. // if we have things in content, we need to add them to
  236. // the form
  237. // before submission
  238. for (var x in content) {
  239. if (!fn[x]) {
  240. var tn;
  241. if (dojo.isIE) {
  242. tn = dojo.doc
  243. .createElement("<input type='hidden' name='"
  244. + x + "'>");
  245. } else {
  246. tn = dojo.doc.createElement("input");
  247. tn.type = "hidden";
  248. tn.name = x;
  249. }
  250. tn.value = content[x];
  251. fn.appendChild(tn);
  252. ioArgs._contentToClean.push(x);
  253. } else {
  254. fn[x].value = content[x];
  255. }
  256. }
  257. }
  258. // IE requires going through getAttributeNode instead of
  259. // just getAttribute in some form cases,
  260. // so use it for all. See #2844
  261. var actnNode = fn.getAttributeNode("action");
  262. var mthdNode = fn.getAttributeNode("method");
  263. var trgtNode = fn.getAttributeNode("target");
  264. if (args["url"]) {
  265. ioArgs._originalAction = actnNode
  266. ? actnNode.value
  267. : null;
  268. if (actnNode) {
  269. actnNode.value = args.url;
  270. } else {
  271. fn.setAttribute("action", args.url);
  272. }
  273. }
  274. if (!mthdNode || !mthdNode.value) {
  275. if (mthdNode) {
  276. mthdNode.value = (args["method"])
  277. ? args["method"]
  278. : "post";
  279. } else {
  280. fn.setAttribute("method", (args["method"])
  281. ? args["method"]
  282. : "post");
  283. }
  284. }
  285. ioArgs._originalTarget = trgtNode ? trgtNode.value : null;
  286. if (trgtNode) {
  287. trgtNode.value = this._iframeName;
  288. } else {
  289. fn.setAttribute("target", this._iframeName);
  290. }
  291. fn.target = this._iframeName;
  292. fn.submit();
  293. } else {
  294. // otherwise we post a GET string by changing URL location
  295. // for the
  296. // iframe
  297. var tmpUrl = args.url
  298. + (args.url.indexOf("?") > -1 ? "&" : "?")
  299. + ioArgs.query;
  300. this.setSrc(this._frame, tmpUrl, true);
  301. }
  302. } catch (e) {
  303. dfd.errback(e);
  304. }
  305. },
  306. _iframeOnload : function() {
  307. var dfd = this._currentDfd;
  308. if (!dfd) {
  309. this._fireNextRequest();
  310. return;
  311. }
  312. var ioArgs = dfd.ioArgs;
  313. var args = ioArgs.args;
  314. var fNode = args.form;
  315. if (fNode) {
  316. // remove all the hidden content inputs
  317. var toClean = ioArgs._contentToClean;
  318. for (var i = 0; i < toClean.length; i++) {
  319. var key = toClean[i];
  320. if (dojo.isSafari < 3) {
  321. // In Safari (at least 2.0.3), can't use form[key]
  322. // syntax to find the node,
  323. // for nodes that were dynamically added.
  324. for (var j = 0; j < fNode.childNodes.length; j++) {
  325. var chNode = fNode.childNodes[j];
  326. if (chNode.name == key) {
  327. dojo._destroyElement(chNode);
  328. break;
  329. }
  330. }
  331. } else {
  332. dojo._destroyElement(fNode[key]);
  333. fNode[key] = null;
  334. }
  335. }
  336. // restore original action + target
  337. if (ioArgs["_originalAction"]) {
  338. fNode.setAttribute("action", ioArgs._originalAction);
  339. }
  340. if (ioArgs["_originalTarget"]) {
  341. fNode.setAttribute("target", ioArgs._originalTarget);
  342. fNode.target = ioArgs._originalTarget;
  343. }
  344. }
  345. ioArgs._finished = true;
  346. }
  347. }
  348. }