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.common");
14
dojo.require("dojo.string");
15
dojo.require("dojo.lang.extras");
16
dojo.io.transports = [];
17
dojo.io.hdlrFuncNames = ["load", "error", "timeout"];
18
dojo.io.Request = function (url, mimetype, transport, changeUrl) {
19
	if ((arguments.length == 1) && (arguments[0].constructor == Object)) {
20
		this.fromKwArgs(arguments[0]);
21
	} else {
22
		this.url = url;
23
		if (mimetype) {
24
			this.mimetype = mimetype;
25
		}
26
		if (transport) {
27
			this.transport = transport;
28
		}
29
		if (arguments.length >= 4) {
30
			this.changeUrl = changeUrl;
31
		}
32
	}
33
};
1422 alexandre_ 34
dojo.lang.extend(dojo.io.Request, {url:"", mimetype:"text/plain", method:"GET", content:undefined, transport:undefined, changeUrl:undefined, formNode:undefined, sync:false, bindSuccess:false, useCache:false, preventCache:false, jsonFilter:function (value) {
35
	if ((this.mimetype == "text/json-comment-filtered") || (this.mimetype == "application/json-comment-filtered")) {
36
		var cStartIdx = value.indexOf("/*");
37
		var cEndIdx = value.lastIndexOf("*/");
38
		if ((cStartIdx == -1) || (cEndIdx == -1)) {
39
			dojo.debug("your JSON wasn't comment filtered!");
40
			return "";
41
		}
42
		return value.substring(cStartIdx + 2, cEndIdx);
43
	}
44
	dojo.debug("please consider using a mimetype of text/json-comment-filtered to avoid potential security issues with JSON endpoints");
45
	return value;
46
}, load:function (type, data, transportImplementation, kwArgs) {
1318 alexandre_ 47
}, error:function (type, error, transportImplementation, kwArgs) {
48
}, timeout:function (type, empty, transportImplementation, kwArgs) {
49
}, handle:function (type, data, transportImplementation, kwArgs) {
50
}, timeoutSeconds:0, abort:function () {
51
}, fromKwArgs:function (kwArgs) {
52
	if (kwArgs["url"]) {
53
		kwArgs.url = kwArgs.url.toString();
54
	}
55
	if (kwArgs["formNode"]) {
56
		kwArgs.formNode = dojo.byId(kwArgs.formNode);
57
	}
58
	if (!kwArgs["method"] && kwArgs["formNode"] && kwArgs["formNode"].method) {
59
		kwArgs.method = kwArgs["formNode"].method;
60
	}
61
	if (!kwArgs["handle"] && kwArgs["handler"]) {
62
		kwArgs.handle = kwArgs.handler;
63
	}
64
	if (!kwArgs["load"] && kwArgs["loaded"]) {
65
		kwArgs.load = kwArgs.loaded;
66
	}
67
	if (!kwArgs["changeUrl"] && kwArgs["changeURL"]) {
68
		kwArgs.changeUrl = kwArgs.changeURL;
69
	}
70
	kwArgs.encoding = dojo.lang.firstValued(kwArgs["encoding"], djConfig["bindEncoding"], "");
71
	kwArgs.sendTransport = dojo.lang.firstValued(kwArgs["sendTransport"], djConfig["ioSendTransport"], false);
72
	var isFunction = dojo.lang.isFunction;
73
	for (var x = 0; x < dojo.io.hdlrFuncNames.length; x++) {
74
		var fn = dojo.io.hdlrFuncNames[x];
75
		if (kwArgs[fn] && isFunction(kwArgs[fn])) {
76
			continue;
77
		}
78
		if (kwArgs["handle"] && isFunction(kwArgs["handle"])) {
79
			kwArgs[fn] = kwArgs.handle;
80
		}
81
	}
82
	dojo.lang.mixin(this, kwArgs);
83
}});
84
dojo.io.Error = function (msg, type, num) {
85
	this.message = msg;
86
	this.type = type || "unknown";
87
	this.number = num || 0;
88
};
89
dojo.io.transports.addTransport = function (name) {
90
	this.push(name);
91
	this[name] = dojo.io[name];
92
};
93
dojo.io.bind = function (request) {
94
	if (!(request instanceof dojo.io.Request)) {
95
		try {
96
			request = new dojo.io.Request(request);
97
		}
98
		catch (e) {
99
			dojo.debug(e);
100
		}
101
	}
102
	var tsName = "";
103
	if (request["transport"]) {
104
		tsName = request["transport"];
105
		if (!this[tsName]) {
106
			dojo.io.sendBindError(request, "No dojo.io.bind() transport with name '" + request["transport"] + "'.");
107
			return request;
108
		}
109
		if (!this[tsName].canHandle(request)) {
110
			dojo.io.sendBindError(request, "dojo.io.bind() transport with name '" + request["transport"] + "' cannot handle this type of request.");
111
			return request;
112
		}
113
	} else {
114
		for (var x = 0; x < dojo.io.transports.length; x++) {
115
			var tmp = dojo.io.transports[x];
116
			if ((this[tmp]) && (this[tmp].canHandle(request))) {
117
				tsName = tmp;
118
				break;
119
			}
120
		}
121
		if (tsName == "") {
122
			dojo.io.sendBindError(request, "None of the loaded transports for dojo.io.bind()" + " can handle the request.");
123
			return request;
124
		}
125
	}
126
	this[tsName].bind(request);
127
	request.bindSuccess = true;
128
	return request;
129
};
130
dojo.io.sendBindError = function (request, message) {
131
	if ((typeof request.error == "function" || typeof request.handle == "function") && (typeof setTimeout == "function" || typeof setTimeout == "object")) {
132
		var errorObject = new dojo.io.Error(message);
133
		setTimeout(function () {
134
			request[(typeof request.error == "function") ? "error" : "handle"]("error", errorObject, null, request);
135
		}, 50);
136
	} else {
137
		dojo.raise(message);
138
	}
139
};
140
dojo.io.queueBind = function (request) {
141
	if (!(request instanceof dojo.io.Request)) {
142
		try {
143
			request = new dojo.io.Request(request);
144
		}
145
		catch (e) {
146
			dojo.debug(e);
147
		}
148
	}
149
	var oldLoad = request.load;
150
	request.load = function () {
151
		dojo.io._queueBindInFlight = false;
152
		var ret = oldLoad.apply(this, arguments);
153
		dojo.io._dispatchNextQueueBind();
154
		return ret;
155
	};
156
	var oldErr = request.error;
157
	request.error = function () {
158
		dojo.io._queueBindInFlight = false;
159
		var ret = oldErr.apply(this, arguments);
160
		dojo.io._dispatchNextQueueBind();
161
		return ret;
162
	};
163
	dojo.io._bindQueue.push(request);
164
	dojo.io._dispatchNextQueueBind();
165
	return request;
166
};
167
dojo.io._dispatchNextQueueBind = function () {
168
	if (!dojo.io._queueBindInFlight) {
169
		dojo.io._queueBindInFlight = true;
170
		if (dojo.io._bindQueue.length > 0) {
171
			dojo.io.bind(dojo.io._bindQueue.shift());
172
		} else {
173
			dojo.io._queueBindInFlight = false;
174
		}
175
	}
176
};
177
dojo.io._bindQueue = [];
178
dojo.io._queueBindInFlight = false;
179
dojo.io.argsFromMap = function (map, encoding, last) {
180
	var enc = /utf/i.test(encoding || "") ? encodeURIComponent : dojo.string.encodeAscii;
181
	var mapped = [];
182
	var control = new Object();
183
	for (var name in map) {
184
		var domap = function (elt) {
185
			var val = enc(name) + "=" + enc(elt);
186
			mapped[(last == name) ? "push" : "unshift"](val);
187
		};
188
		if (!control[name]) {
189
			var value = map[name];
190
			if (dojo.lang.isArray(value)) {
191
				dojo.lang.forEach(value, domap);
192
			} else {
193
				domap(value);
194
			}
195
		}
196
	}
197
	return mapped.join("&");
198
};
199
dojo.io.setIFrameSrc = function (iframe, src, replace) {
200
	try {
201
		var r = dojo.render.html;
202
		if (!replace) {
203
			if (r.safari) {
204
				iframe.location = src;
205
			} else {
206
				frames[iframe.name].location = src;
207
			}
208
		} else {
209
			var idoc;
210
			if (r.ie) {
211
				idoc = iframe.contentWindow.document;
212
			} else {
213
				if (r.safari) {
214
					idoc = iframe.document;
215
				} else {
216
					idoc = iframe.contentWindow;
217
				}
218
			}
219
			if (!idoc) {
220
				iframe.location = src;
221
				return;
222
			} else {
223
				idoc.location.replace(src);
224
			}
225
		}
226
	}
227
	catch (e) {
228
		dojo.debug(e);
229
		dojo.debug("setIFrameSrc: " + e);
230
	}
231
};
232