iframe.js 12 KB

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