Subversion Repositories Applications.papyrus

Rev

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