Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

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