Subversion Repositories Applications.papyrus

Rev

Rev 1318 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
1422 alexandre_ 11
 
12
 
1318 alexandre_ 13
dojo.provide("dojo.io.IframeIO");
14
dojo.require("dojo.io.BrowserIO");
15
dojo.require("dojo.uri.*");
16
dojo.io.createIFrame = function (fname, onloadstr, uri) {
17
	if (window[fname]) {
18
		return window[fname];
19
	}
20
	if (window.frames[fname]) {
21
		return window.frames[fname];
22
	}
23
	var r = dojo.render.html;
24
	var cframe = null;
25
	var turi = uri;
26
	if (!turi) {
27
		if (djConfig["useXDomain"] && !djConfig["dojoIframeHistoryUrl"]) {
28
			dojo.debug("dojo.io.createIFrame: When using cross-domain Dojo builds," + " please save iframe_history.html to your domain and set djConfig.dojoIframeHistoryUrl" + " to the path on your domain to iframe_history.html");
29
		}
30
		turi = (djConfig["dojoIframeHistoryUrl"] || dojo.uri.moduleUri("dojo", "../iframe_history.html")) + "#noInit=true";
31
	}
32
	var ifrstr = ((r.ie) && (dojo.render.os.win)) ? "<iframe name=\"" + fname + "\" src=\"" + turi + "\" onload=\"" + onloadstr + "\">" : "iframe";
33
	cframe = document.createElement(ifrstr);
34
	with (cframe) {
35
		name = fname;
36
		setAttribute("name", fname);
37
		id = fname;
38
	}
39
	dojo.body().appendChild(cframe);
40
	window[fname] = cframe;
41
	with (cframe.style) {
42
		if (!r.safari) {
43
			position = "absolute";
44
		}
45
		left = top = "0px";
46
		height = width = "1px";
47
		visibility = "hidden";
48
	}
49
	if (!r.ie) {
50
		dojo.io.setIFrameSrc(cframe, turi, true);
51
		cframe.onload = new Function(onloadstr);
52
	}
53
	return cframe;
54
};
55
dojo.io.IframeTransport = new function () {
56
	var _this = this;
57
	this.currentRequest = null;
58
	this.requestQueue = [];
59
	this.iframeName = "dojoIoIframe";
60
	this.fireNextRequest = function () {
61
		try {
62
			if ((this.currentRequest) || (this.requestQueue.length == 0)) {
63
				return;
64
			}
65
			var cr = this.currentRequest = this.requestQueue.shift();
66
			cr._contentToClean = [];
67
			var fn = cr["formNode"];
68
			var content = cr["content"] || {};
69
			if (cr.sendTransport) {
70
				content["dojo.transport"] = "iframe";
71
			}
72
			if (fn) {
73
				if (content) {
74
					for (var x in content) {
75
						if (!fn[x]) {
76
							var tn;
77
							if (dojo.render.html.ie) {
78
								tn = document.createElement("<input type='hidden' name='" + x + "' value='" + content[x] + "'>");
79
								fn.appendChild(tn);
80
							} else {
81
								tn = document.createElement("input");
82
								fn.appendChild(tn);
83
								tn.type = "hidden";
84
								tn.name = x;
85
								tn.value = content[x];
86
							}
87
							cr._contentToClean.push(x);
88
						} else {
89
							fn[x].value = content[x];
90
						}
91
					}
92
				}
93
				if (cr["url"]) {
94
					cr._originalAction = fn.getAttribute("action");
95
					fn.setAttribute("action", cr.url);
96
				}
97
				if (!fn.getAttribute("method")) {
98
					fn.setAttribute("method", (cr["method"]) ? cr["method"] : "post");
99
				}
100
				cr._originalTarget = fn.getAttribute("target");
101
				fn.setAttribute("target", this.iframeName);
102
				fn.target = this.iframeName;
103
				fn.submit();
104
			} else {
105
				var query = dojo.io.argsFromMap(this.currentRequest.content);
106
				var tmpUrl = cr.url + (cr.url.indexOf("?") > -1 ? "&" : "?") + query;
107
				dojo.io.setIFrameSrc(this.iframe, tmpUrl, true);
108
			}
109
		}
110
		catch (e) {
111
			this.iframeOnload(e);
112
		}
113
	};
114
	this.canHandle = function (kwArgs) {
115
		return ((dojo.lang.inArray(["text/plain", "text/html", "text/javascript", "text/json", "application/json"], kwArgs["mimetype"])) && (dojo.lang.inArray(["post", "get"], kwArgs["method"].toLowerCase())) && (!((kwArgs["sync"]) && (kwArgs["sync"] == true))));
116
	};
117
	this.bind = function (kwArgs) {
118
		if (!this["iframe"]) {
119
			this.setUpIframe();
120
		}
121
		this.requestQueue.push(kwArgs);
122
		this.fireNextRequest();
123
		return;
124
	};
125
	this.setUpIframe = function () {
126
		this.iframe = dojo.io.createIFrame(this.iframeName, "dojo.io.IframeTransport.iframeOnload();");
127
	};
128
	this.iframeOnload = function (errorObject) {
129
		if (!_this.currentRequest) {
130
			_this.fireNextRequest();
131
			return;
132
		}
133
		var req = _this.currentRequest;
134
		if (req.formNode) {
135
			var toClean = req._contentToClean;
136
			for (var i = 0; i < toClean.length; i++) {
137
				var key = toClean[i];
138
				if (dojo.render.html.safari) {
139
					var fNode = req.formNode;
140
					for (var j = 0; j < fNode.childNodes.length; j++) {
141
						var chNode = fNode.childNodes[j];
142
						if (chNode.name == key) {
143
							var pNode = chNode.parentNode;
144
							pNode.removeChild(chNode);
145
							break;
146
						}
147
					}
148
				} else {
149
					var input = req.formNode[key];
150
					req.formNode.removeChild(input);
151
					req.formNode[key] = null;
152
				}
153
			}
154
			if (req["_originalAction"]) {
155
				req.formNode.setAttribute("action", req._originalAction);
156
			}
157
			if (req["_originalTarget"]) {
158
				req.formNode.setAttribute("target", req._originalTarget);
159
				req.formNode.target = req._originalTarget;
160
			}
161
		}
162
		var contentDoc = function (iframe_el) {
163
			var doc = iframe_el.contentDocument || ((iframe_el.contentWindow) && (iframe_el.contentWindow.document)) || ((iframe_el.name) && (document.frames[iframe_el.name]) && (document.frames[iframe_el.name].document)) || null;
164
			return doc;
165
		};
166
		var value;
167
		var success = false;
168
		if (errorObject) {
169
			this._callError(req, "IframeTransport Request Error: " + errorObject);
170
		} else {
171
			var ifd = contentDoc(_this.iframe);
172
			try {
173
				var cmt = req.mimetype;
174
				if ((cmt == "text/javascript") || (cmt == "text/json") || (cmt == "application/json")) {
175
					var js = ifd.getElementsByTagName("textarea")[0].value;
176
					if (cmt == "text/json" || cmt == "application/json") {
177
						js = "(" + js + ")";
178
					}
179
					value = dj_eval(js);
180
				} else {
181
					if (cmt == "text/html") {
182
						value = ifd;
183
					} else {
184
						value = ifd.getElementsByTagName("textarea")[0].value;
185
					}
186
				}
187
				success = true;
188
			}
189
			catch (e) {
190
				this._callError(req, "IframeTransport Error: " + e);
191
			}
192
		}
193
		try {
194
			if (success && dojo.lang.isFunction(req["load"])) {
195
				req.load("load", value, req);
196
			}
197
		}
198
		catch (e) {
199
			throw e;
200
		}
201
		finally {
202
			_this.currentRequest = null;
203
			_this.fireNextRequest();
204
		}
205
	};
206
	this._callError = function (req, message) {
207
		var errObj = new dojo.io.Error(message);
208
		if (dojo.lang.isFunction(req["error"])) {
209
			req.error("error", errObj, req);
210
		}
211
	};
212
	dojo.io.transports.addTransport("IframeTransport");
213
};
214