Subversion Repositories Applications.papyrus

Rev

Rev 1422 | 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.BrowserIO");
14
dojo.require("dojo.io.common");
15
dojo.require("dojo.lang.array");
16
dojo.require("dojo.lang.func");
17
dojo.require("dojo.string.extras");
18
dojo.require("dojo.dom");
19
dojo.require("dojo.undo.browser");
20
if (!dj_undef("window")) {
21
	dojo.io.checkChildrenForFile = function (node) {
22
		var hasFile = false;
23
		var inputs = node.getElementsByTagName("input");
24
		dojo.lang.forEach(inputs, function (input) {
25
			if (hasFile) {
26
				return;
27
			}
28
			if (input.getAttribute("type") == "file") {
29
				hasFile = true;
30
			}
31
		});
32
		return hasFile;
33
	};
34
	dojo.io.formHasFile = function (formNode) {
35
		return dojo.io.checkChildrenForFile(formNode);
36
	};
37
	dojo.io.updateNode = function (node, urlOrArgs) {
38
		node = dojo.byId(node);
39
		var args = urlOrArgs;
40
		if (dojo.lang.isString(urlOrArgs)) {
41
			args = {url:urlOrArgs};
42
		}
43
		args.mimetype = "text/html";
44
		args.load = function (t, d, e) {
45
			while (node.firstChild) {
46
				dojo.dom.destroyNode(node.firstChild);
47
			}
48
			node.innerHTML = d;
49
		};
50
		dojo.io.bind(args);
51
	};
52
	dojo.io.formFilter = function (node) {
53
		var type = (node.type || "").toLowerCase();
54
		return !node.disabled && node.name && !dojo.lang.inArray(["file", "submit", "image", "reset", "button"], type);
55
	};
56
	dojo.io.encodeForm = function (formNode, encoding, formFilter) {
57
		if ((!formNode) || (!formNode.tagName) || (!formNode.tagName.toLowerCase() == "form")) {
58
			dojo.raise("Attempted to encode a non-form element.");
59
		}
60
		if (!formFilter) {
61
			formFilter = dojo.io.formFilter;
62
		}
63
		var enc = /utf/i.test(encoding || "") ? encodeURIComponent : dojo.string.encodeAscii;
64
		var values = [];
65
		for (var i = 0; i < formNode.elements.length; i++) {
66
			var elm = formNode.elements[i];
67
			if (!elm || elm.tagName.toLowerCase() == "fieldset" || !formFilter(elm)) {
68
				continue;
69
			}
70
			var name = enc(elm.name);
71
			var type = elm.type.toLowerCase();
72
			if (type == "select-multiple") {
73
				for (var j = 0; j < elm.options.length; j++) {
74
					if (elm.options[j].selected) {
75
						values.push(name + "=" + enc(elm.options[j].value));
76
					}
77
				}
78
			} else {
79
				if (dojo.lang.inArray(["radio", "checkbox"], type)) {
80
					if (elm.checked) {
81
						values.push(name + "=" + enc(elm.value));
82
					}
83
				} else {
84
					values.push(name + "=" + enc(elm.value));
85
				}
86
			}
87
		}
88
		var inputs = formNode.getElementsByTagName("input");
89
		for (var i = 0; i < inputs.length; i++) {
90
			var input = inputs[i];
91
			if (input.type.toLowerCase() == "image" && input.form == formNode && formFilter(input)) {
92
				var name = enc(input.name);
93
				values.push(name + "=" + enc(input.value));
94
				values.push(name + ".x=0");
95
				values.push(name + ".y=0");
96
			}
97
		}
98
		return values.join("&") + "&";
99
	};
100
	dojo.io.FormBind = function (args) {
101
		this.bindArgs = {};
102
		if (args && args.formNode) {
103
			this.init(args);
104
		} else {
105
			if (args) {
106
				this.init({formNode:args});
107
			}
108
		}
109
	};
110
	dojo.lang.extend(dojo.io.FormBind, {form:null, bindArgs:null, clickedButton:null, init:function (args) {
111
		var form = dojo.byId(args.formNode);
112
		if (!form || !form.tagName || form.tagName.toLowerCase() != "form") {
113
			throw new Error("FormBind: Couldn't apply, invalid form");
114
		} else {
115
			if (this.form == form) {
116
				return;
117
			} else {
118
				if (this.form) {
119
					throw new Error("FormBind: Already applied to a form");
120
				}
121
			}
122
		}
123
		dojo.lang.mixin(this.bindArgs, args);
124
		this.form = form;
125
		this.connect(form, "onsubmit", "submit");
126
		for (var i = 0; i < form.elements.length; i++) {
127
			var node = form.elements[i];
128
			if (node && node.type && dojo.lang.inArray(["submit", "button"], node.type.toLowerCase())) {
129
				this.connect(node, "onclick", "click");
130
			}
131
		}
132
		var inputs = form.getElementsByTagName("input");
133
		for (var i = 0; i < inputs.length; i++) {
134
			var input = inputs[i];
135
			if (input.type.toLowerCase() == "image" && input.form == form) {
136
				this.connect(input, "onclick", "click");
137
			}
138
		}
139
	}, onSubmit:function (form) {
140
		return true;
141
	}, submit:function (e) {
142
		e.preventDefault();
143
		if (this.onSubmit(this.form)) {
144
			dojo.io.bind(dojo.lang.mixin(this.bindArgs, {formFilter:dojo.lang.hitch(this, "formFilter")}));
145
		}
146
	}, click:function (e) {
147
		var node = e.currentTarget;
148
		if (node.disabled) {
149
			return;
150
		}
151
		this.clickedButton = node;
152
	}, formFilter:function (node) {
153
		var type = (node.type || "").toLowerCase();
154
		var accept = false;
155
		if (node.disabled || !node.name) {
156
			accept = false;
157
		} else {
158
			if (dojo.lang.inArray(["submit", "button", "image"], type)) {
159
				if (!this.clickedButton) {
160
					this.clickedButton = node;
161
				}
162
				accept = node == this.clickedButton;
163
			} else {
164
				accept = !dojo.lang.inArray(["file", "submit", "reset", "button"], type);
165
			}
166
		}
167
		return accept;
168
	}, connect:function (srcObj, srcFcn, targetFcn) {
169
		if (dojo.evalObjPath("dojo.event.connect")) {
170
			dojo.event.connect(srcObj, srcFcn, this, targetFcn);
171
		} else {
172
			var fcn = dojo.lang.hitch(this, targetFcn);
173
			srcObj[srcFcn] = function (e) {
174
				if (!e) {
175
					e = window.event;
176
				}
177
				if (!e.currentTarget) {
178
					e.currentTarget = e.srcElement;
179
				}
180
				if (!e.preventDefault) {
181
					e.preventDefault = function () {
182
						window.event.returnValue = false;
183
					};
184
				}
185
				fcn(e);
186
			};
187
		}
188
	}});
189
	dojo.io.XMLHTTPTransport = new function () {
190
		var _this = this;
191
		var _cache = {};
192
		this.useCache = false;
193
		this.preventCache = false;
194
		function getCacheKey(url, query, method) {
195
			return url + "|" + query + "|" + method.toLowerCase();
196
		}
197
		function addToCache(url, query, method, http) {
198
			_cache[getCacheKey(url, query, method)] = http;
199
		}
200
		function getFromCache(url, query, method) {
201
			return _cache[getCacheKey(url, query, method)];
202
		}
203
		this.clearCache = function () {
204
			_cache = {};
205
		};
206
		function doLoad(kwArgs, http, url, query, useCache) {
1422 alexandre_ 207
			if (((http.status >= 200) && (http.status < 300)) || (http.status == 304) || (http.status == 1223) || (location.protocol == "file:" && (http.status == 0 || http.status == undefined)) || (location.protocol == "chrome:" && (http.status == 0 || http.status == undefined))) {
1318 alexandre_ 208
				var ret;
209
				if (kwArgs.method.toLowerCase() == "head") {
210
					var headers = http.getAllResponseHeaders();
211
					ret = {};
212
					ret.toString = function () {
213
						return headers;
214
					};
215
					var values = headers.split(/[\r\n]+/g);
216
					for (var i = 0; i < values.length; i++) {
217
						var pair = values[i].match(/^([^:]+)\s*:\s*(.+)$/i);
218
						if (pair) {
219
							ret[pair[1]] = pair[2];
220
						}
221
					}
222
				} else {
223
					if (kwArgs.mimetype == "text/javascript") {
224
						try {
225
							ret = dj_eval(http.responseText);
226
						}
227
						catch (e) {
228
							dojo.debug(e);
229
							dojo.debug(http.responseText);
230
							ret = null;
231
						}
232
					} else {
1422 alexandre_ 233
						if (kwArgs.mimetype.substr(0, 9) == "text/json" || kwArgs.mimetype.substr(0, 16) == "application/json") {
1318 alexandre_ 234
							try {
1422 alexandre_ 235
								ret = dj_eval("(" + kwArgs.jsonFilter(http.responseText) + ")");
1318 alexandre_ 236
							}
237
							catch (e) {
238
								dojo.debug(e);
239
								dojo.debug(http.responseText);
240
								ret = false;
241
							}
242
						} else {
243
							if ((kwArgs.mimetype == "application/xml") || (kwArgs.mimetype == "text/xml")) {
244
								ret = http.responseXML;
245
								if (!ret || typeof ret == "string" || !http.getResponseHeader("Content-Type")) {
246
									ret = dojo.dom.createDocumentFromText(http.responseText);
247
								}
248
							} else {
249
								ret = http.responseText;
250
							}
251
						}
252
					}
253
				}
254
				if (useCache) {
255
					addToCache(url, query, kwArgs.method, http);
256
				}
257
				kwArgs[(typeof kwArgs.load == "function") ? "load" : "handle"]("load", ret, http, kwArgs);
258
			} else {
259
				var errObj = new dojo.io.Error("XMLHttpTransport Error: " + http.status + " " + http.statusText);
260
				kwArgs[(typeof kwArgs.error == "function") ? "error" : "handle"]("error", errObj, http, kwArgs);
261
			}
262
		}
263
		function setHeaders(http, kwArgs) {
264
			if (kwArgs["headers"]) {
265
				for (var header in kwArgs["headers"]) {
266
					if (header.toLowerCase() == "content-type" && !kwArgs["contentType"]) {
267
						kwArgs["contentType"] = kwArgs["headers"][header];
268
					} else {
269
						http.setRequestHeader(header, kwArgs["headers"][header]);
270
					}
271
				}
272
			}
273
		}
274
		this.inFlight = [];
275
		this.inFlightTimer = null;
276
		this.startWatchingInFlight = function () {
277
			if (!this.inFlightTimer) {
278
				this.inFlightTimer = setTimeout("dojo.io.XMLHTTPTransport.watchInFlight();", 10);
279
			}
280
		};
281
		this.watchInFlight = function () {
282
			var now = null;
283
			if (!dojo.hostenv._blockAsync && !_this._blockAsync) {
284
				for (var x = this.inFlight.length - 1; x >= 0; x--) {
285
					try {
286
						var tif = this.inFlight[x];
287
						if (!tif || tif.http._aborted || !tif.http.readyState) {
288
							this.inFlight.splice(x, 1);
289
							continue;
290
						}
291
						if (4 == tif.http.readyState) {
292
							this.inFlight.splice(x, 1);
293
							doLoad(tif.req, tif.http, tif.url, tif.query, tif.useCache);
294
						} else {
295
							if (tif.startTime) {
296
								if (!now) {
297
									now = (new Date()).getTime();
298
								}
299
								if (tif.startTime + (tif.req.timeoutSeconds * 1000) < now) {
300
									if (typeof tif.http.abort == "function") {
301
										tif.http.abort();
302
									}
303
									this.inFlight.splice(x, 1);
304
									tif.req[(typeof tif.req.timeout == "function") ? "timeout" : "handle"]("timeout", null, tif.http, tif.req);
305
								}
306
							}
307
						}
308
					}
309
					catch (e) {
310
						try {
311
							var errObj = new dojo.io.Error("XMLHttpTransport.watchInFlight Error: " + e);
312
							tif.req[(typeof tif.req.error == "function") ? "error" : "handle"]("error", errObj, tif.http, tif.req);
313
						}
314
						catch (e2) {
315
							dojo.debug("XMLHttpTransport error callback failed: " + e2);
316
						}
317
					}
318
				}
319
			}
320
			clearTimeout(this.inFlightTimer);
321
			if (this.inFlight.length == 0) {
322
				this.inFlightTimer = null;
323
				return;
324
			}
325
			this.inFlightTimer = setTimeout("dojo.io.XMLHTTPTransport.watchInFlight();", 10);
326
		};
327
		var hasXmlHttp = dojo.hostenv.getXmlhttpObject() ? true : false;
328
		this.canHandle = function (kwArgs) {
1422 alexandre_ 329
			var mlc = kwArgs["mimetype"].toLowerCase() || "";
330
			return hasXmlHttp && ((dojo.lang.inArray(["text/plain", "text/html", "application/xml", "text/xml", "text/javascript"], mlc)) || (mlc.substr(0, 9) == "text/json" || mlc.substr(0, 16) == "application/json")) && !(kwArgs["formNode"] && dojo.io.formHasFile(kwArgs["formNode"]));
1318 alexandre_ 331
		};
332
		this.multipartBoundary = "45309FFF-BD65-4d50-99C9-36986896A96F";
333
		this.bind = function (kwArgs) {
334
			if (!kwArgs["url"]) {
335
				if (!kwArgs["formNode"] && (kwArgs["backButton"] || kwArgs["back"] || kwArgs["changeUrl"] || kwArgs["watchForURL"]) && (!djConfig.preventBackButtonFix)) {
336
					dojo.deprecated("Using dojo.io.XMLHTTPTransport.bind() to add to browser history without doing an IO request", "Use dojo.undo.browser.addToHistory() instead.", "0.4");
337
					dojo.undo.browser.addToHistory(kwArgs);
338
					return true;
339
				}
340
			}
341
			var url = kwArgs.url;
342
			var query = "";
343
			if (kwArgs["formNode"]) {
344
				var ta = kwArgs.formNode.getAttribute("action");
345
				if ((ta) && (!kwArgs["url"])) {
346
					url = ta;
347
				}
348
				var tp = kwArgs.formNode.getAttribute("method");
349
				if ((tp) && (!kwArgs["method"])) {
350
					kwArgs.method = tp;
351
				}
352
				query += dojo.io.encodeForm(kwArgs.formNode, kwArgs.encoding, kwArgs["formFilter"]);
353
			}
354
			if (url.indexOf("#") > -1) {
355
				dojo.debug("Warning: dojo.io.bind: stripping hash values from url:", url);
356
				url = url.split("#")[0];
357
			}
358
			if (kwArgs["file"]) {
359
				kwArgs.method = "post";
360
			}
361
			if (!kwArgs["method"]) {
362
				kwArgs.method = "get";
363
			}
364
			if (kwArgs.method.toLowerCase() == "get") {
365
				kwArgs.multipart = false;
366
			} else {
367
				if (kwArgs["file"]) {
368
					kwArgs.multipart = true;
369
				} else {
370
					if (!kwArgs["multipart"]) {
371
						kwArgs.multipart = false;
372
					}
373
				}
374
			}
375
			if (kwArgs["backButton"] || kwArgs["back"] || kwArgs["changeUrl"]) {
376
				dojo.undo.browser.addToHistory(kwArgs);
377
			}
378
			var content = kwArgs["content"] || {};
379
			if (kwArgs.sendTransport) {
380
				content["dojo.transport"] = "xmlhttp";
381
			}
382
			do {
383
				if (kwArgs.postContent) {
384
					query = kwArgs.postContent;
385
					break;
386
				}
387
				if (content) {
388
					query += dojo.io.argsFromMap(content, kwArgs.encoding);
389
				}
390
				if (kwArgs.method.toLowerCase() == "get" || !kwArgs.multipart) {
391
					break;
392
				}
393
				var t = [];
394
				if (query.length) {
395
					var q = query.split("&");
396
					for (var i = 0; i < q.length; ++i) {
397
						if (q[i].length) {
398
							var p = q[i].split("=");
399
							t.push("--" + this.multipartBoundary, "Content-Disposition: form-data; name=\"" + p[0] + "\"", "", p[1]);
400
						}
401
					}
402
				}
403
				if (kwArgs.file) {
404
					if (dojo.lang.isArray(kwArgs.file)) {
405
						for (var i = 0; i < kwArgs.file.length; ++i) {
406
							var o = kwArgs.file[i];
407
							t.push("--" + this.multipartBoundary, "Content-Disposition: form-data; name=\"" + o.name + "\"; filename=\"" + ("fileName" in o ? o.fileName : o.name) + "\"", "Content-Type: " + ("contentType" in o ? o.contentType : "application/octet-stream"), "", o.content);
408
						}
409
					} else {
410
						var o = kwArgs.file;
411
						t.push("--" + this.multipartBoundary, "Content-Disposition: form-data; name=\"" + o.name + "\"; filename=\"" + ("fileName" in o ? o.fileName : o.name) + "\"", "Content-Type: " + ("contentType" in o ? o.contentType : "application/octet-stream"), "", o.content);
412
					}
413
				}
414
				if (t.length) {
415
					t.push("--" + this.multipartBoundary + "--", "");
416
					query = t.join("\r\n");
417
				}
418
			} while (false);
419
			var async = kwArgs["sync"] ? false : true;
420
			var preventCache = kwArgs["preventCache"] || (this.preventCache == true && kwArgs["preventCache"] != false);
421
			var useCache = kwArgs["useCache"] == true || (this.useCache == true && kwArgs["useCache"] != false);
422
			if (!preventCache && useCache) {
423
				var cachedHttp = getFromCache(url, query, kwArgs.method);
424
				if (cachedHttp) {
425
					doLoad(kwArgs, cachedHttp, url, query, false);
426
					return;
427
				}
428
			}
429
			var http = dojo.hostenv.getXmlhttpObject(kwArgs);
430
			var received = false;
431
			if (async) {
432
				var startTime = this.inFlight.push({"req":kwArgs, "http":http, "url":url, "query":query, "useCache":useCache, "startTime":kwArgs.timeoutSeconds ? (new Date()).getTime() : 0});
433
				this.startWatchingInFlight();
434
			} else {
435
				_this._blockAsync = true;
436
			}
437
			if (kwArgs.method.toLowerCase() == "post") {
438
				if (!kwArgs.user) {
439
					http.open("POST", url, async);
440
				} else {
441
					http.open("POST", url, async, kwArgs.user, kwArgs.password);
442
				}
443
				setHeaders(http, kwArgs);
444
				http.setRequestHeader("Content-Type", kwArgs.multipart ? ("multipart/form-data; boundary=" + this.multipartBoundary) : (kwArgs.contentType || "application/x-www-form-urlencoded"));
445
				try {
446
					http.send(query);
447
				}
448
				catch (e) {
449
					if (typeof http.abort == "function") {
450
						http.abort();
451
					}
452
					doLoad(kwArgs, {status:404}, url, query, useCache);
453
				}
454
			} else {
455
				var tmpUrl = url;
456
				if (query != "") {
457
					tmpUrl += (tmpUrl.indexOf("?") > -1 ? "&" : "?") + query;
458
				}
459
				if (preventCache) {
460
					tmpUrl += (dojo.string.endsWithAny(tmpUrl, "?", "&") ? "" : (tmpUrl.indexOf("?") > -1 ? "&" : "?")) + "dojo.preventCache=" + new Date().valueOf();
461
				}
462
				if (!kwArgs.user) {
463
					http.open(kwArgs.method.toUpperCase(), tmpUrl, async);
464
				} else {
465
					http.open(kwArgs.method.toUpperCase(), tmpUrl, async, kwArgs.user, kwArgs.password);
466
				}
467
				setHeaders(http, kwArgs);
468
				try {
469
					http.send(null);
470
				}
471
				catch (e) {
472
					if (typeof http.abort == "function") {
473
						http.abort();
474
					}
475
					doLoad(kwArgs, {status:404}, url, query, useCache);
476
				}
477
			}
478
			if (!async) {
479
				doLoad(kwArgs, http, url, query, useCache);
480
				_this._blockAsync = false;
481
			}
482
			kwArgs.abort = function () {
483
				try {
484
					http._aborted = true;
485
				}
486
				catch (e) {
487
				}
488
				return http.abort();
489
			};
490
			return;
491
		};
492
		dojo.io.transports.addTransport("XMLHTTPTransport");
493
	};
494
}
495