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.ScriptSrcIO");
12
dojo.require("dojo.io.BrowserIO");
13
dojo.require("dojo.undo.browser");
14
dojo.io.ScriptSrcTransport = new function () {
15
	this.preventCache = false;
16
	this.maxUrlLength = 1000;
17
	this.inFlightTimer = null;
18
	this.DsrStatusCodes = {Continue:100, Ok:200, Error:500};
19
	this.startWatchingInFlight = function () {
20
		if (!this.inFlightTimer) {
21
			this.inFlightTimer = setInterval("dojo.io.ScriptSrcTransport.watchInFlight();", 100);
22
		}
23
	};
24
	this.watchInFlight = function () {
25
		var totalCount = 0;
26
		var doneCount = 0;
27
		for (var param in this._state) {
28
			totalCount++;
29
			var currentState = this._state[param];
30
			if (currentState.isDone) {
31
				doneCount++;
32
				delete this._state[param];
33
			} else {
34
				if (!currentState.isFinishing) {
35
					var listener = currentState.kwArgs;
36
					try {
37
						if (currentState.checkString && eval("typeof(" + currentState.checkString + ") != 'undefined'")) {
38
							currentState.isFinishing = true;
39
							this._finish(currentState, "load");
40
							doneCount++;
41
							delete this._state[param];
42
						} else {
43
							if (listener.timeoutSeconds && listener.timeout) {
44
								if (currentState.startTime + (listener.timeoutSeconds * 1000) < (new Date()).getTime()) {
45
									currentState.isFinishing = true;
46
									this._finish(currentState, "timeout");
47
									doneCount++;
48
									delete this._state[param];
49
								}
50
							} else {
51
								if (!listener.timeoutSeconds) {
52
									doneCount++;
53
								}
54
							}
55
						}
56
					}
57
					catch (e) {
58
						currentState.isFinishing = true;
59
						this._finish(currentState, "error", {status:this.DsrStatusCodes.Error, response:e});
60
					}
61
				}
62
			}
63
		}
64
		if (doneCount >= totalCount) {
65
			clearInterval(this.inFlightTimer);
66
			this.inFlightTimer = null;
67
		}
68
	};
69
	this.canHandle = function (kwArgs) {
70
		return dojo.lang.inArray(["text/javascript", "text/json", "application/json"], (kwArgs["mimetype"].toLowerCase())) && (kwArgs["method"].toLowerCase() == "get") && !(kwArgs["formNode"] && dojo.io.formHasFile(kwArgs["formNode"])) && (!kwArgs["sync"] || kwArgs["sync"] == false) && !kwArgs["file"] && !kwArgs["multipart"];
71
	};
72
	this.removeScripts = function () {
73
		var scripts = document.getElementsByTagName("script");
74
		for (var i = 0; scripts && i < scripts.length; i++) {
75
			var scriptTag = scripts[i];
76
			if (scriptTag.className == "ScriptSrcTransport") {
77
				var parent = scriptTag.parentNode;
78
				parent.removeChild(scriptTag);
79
				i--;
80
			}
81
		}
82
	};
83
	this.bind = function (kwArgs) {
84
		var url = kwArgs.url;
85
		var query = "";
86
		if (kwArgs["formNode"]) {
87
			var ta = kwArgs.formNode.getAttribute("action");
88
			if ((ta) && (!kwArgs["url"])) {
89
				url = ta;
90
			}
91
			var tp = kwArgs.formNode.getAttribute("method");
92
			if ((tp) && (!kwArgs["method"])) {
93
				kwArgs.method = tp;
94
			}
95
			query += dojo.io.encodeForm(kwArgs.formNode, kwArgs.encoding, kwArgs["formFilter"]);
96
		}
97
		if (url.indexOf("#") > -1) {
98
			dojo.debug("Warning: dojo.io.bind: stripping hash values from url:", url);
99
			url = url.split("#")[0];
100
		}
101
		var urlParts = url.split("?");
102
		if (urlParts && urlParts.length == 2) {
103
			url = urlParts[0];
104
			query += (query ? "&" : "") + urlParts[1];
105
		}
106
		if (kwArgs["backButton"] || kwArgs["back"] || kwArgs["changeUrl"]) {
107
			dojo.undo.browser.addToHistory(kwArgs);
108
		}
109
		var id = kwArgs["apiId"] ? kwArgs["apiId"] : "id" + this._counter++;
110
		var content = kwArgs["content"];
111
		var jsonpName = kwArgs.jsonParamName;
112
		if (kwArgs.sendTransport || jsonpName) {
113
			if (!content) {
114
				content = {};
115
			}
116
			if (kwArgs.sendTransport) {
117
				content["dojo.transport"] = "scriptsrc";
118
			}
119
			if (jsonpName) {
120
				content[jsonpName] = "dojo.io.ScriptSrcTransport._state." + id + ".jsonpCall";
121
			}
122
		}
123
		if (kwArgs.postContent) {
124
			query = kwArgs.postContent;
125
		} else {
126
			if (content) {
127
				query += ((query) ? "&" : "") + dojo.io.argsFromMap(content, kwArgs.encoding, jsonpName);
128
			}
129
		}
130
		if (kwArgs["apiId"]) {
131
			kwArgs["useRequestId"] = true;
132
		}
133
		var state = {"id":id, "idParam":"_dsrid=" + id, "url":url, "query":query, "kwArgs":kwArgs, "startTime":(new Date()).getTime(), "isFinishing":false};
134
		if (!url) {
135
			this._finish(state, "error", {status:this.DsrStatusCodes.Error, statusText:"url.none"});
136
			return;
137
		}
138
		if (content && content[jsonpName]) {
139
			state.jsonp = content[jsonpName];
140
			state.jsonpCall = function (data) {
141
				if (data["Error"] || data["error"]) {
142
					if (dojo["json"] && dojo["json"]["serialize"]) {
143
						dojo.debug(dojo.json.serialize(data));
144
					}
145
					dojo.io.ScriptSrcTransport._finish(this, "error", data);
146
				} else {
147
					dojo.io.ScriptSrcTransport._finish(this, "load", data);
148
				}
149
			};
150
		}
151
		if (kwArgs["useRequestId"] || kwArgs["checkString"] || state["jsonp"]) {
152
			this._state[id] = state;
153
		}
154
		if (kwArgs["checkString"]) {
155
			state.checkString = kwArgs["checkString"];
156
		}
157
		state.constantParams = (kwArgs["constantParams"] == null ? "" : kwArgs["constantParams"]);
158
		if (kwArgs["preventCache"] || (this.preventCache == true && kwArgs["preventCache"] != false)) {
159
			state.nocacheParam = "dojo.preventCache=" + new Date().valueOf();
160
		} else {
161
			state.nocacheParam = "";
162
		}
163
		var urlLength = state.url.length + state.query.length + state.constantParams.length + state.nocacheParam.length + this._extraPaddingLength;
164
		if (kwArgs["useRequestId"]) {
165
			urlLength += state.idParam.length;
166
		}
167
		if (!kwArgs["checkString"] && kwArgs["useRequestId"] && !state["jsonp"] && !kwArgs["forceSingleRequest"] && urlLength > this.maxUrlLength) {
168
			if (url > this.maxUrlLength) {
169
				this._finish(state, "error", {status:this.DsrStatusCodes.Error, statusText:"url.tooBig"});
170
				return;
171
			} else {
172
				this._multiAttach(state, 1);
173
			}
174
		} else {
175
			var queryParams = [state.constantParams, state.nocacheParam, state.query];
176
			if (kwArgs["useRequestId"] && !state["jsonp"]) {
177
				queryParams.unshift(state.idParam);
178
			}
179
			var finalUrl = this._buildUrl(state.url, queryParams);
180
			state.finalUrl = finalUrl;
181
			this._attach(state.id, finalUrl);
182
		}
183
		this.startWatchingInFlight();
184
	};
185
	this._counter = 1;
186
	this._state = {};
187
	this._extraPaddingLength = 16;
188
	this._buildUrl = function (url, nameValueArray) {
189
		var finalUrl = url;
190
		var joiner = "?";
191
		for (var i = 0; i < nameValueArray.length; i++) {
192
			if (nameValueArray[i]) {
193
				finalUrl += joiner + nameValueArray[i];
194
				joiner = "&";
195
			}
196
		}
197
		return finalUrl;
198
	};
199
	this._attach = function (id, url) {
200
		var element = document.createElement("script");
201
		element.type = "text/javascript";
202
		element.src = url;
203
		element.id = id;
204
		element.className = "ScriptSrcTransport";
205
		document.getElementsByTagName("head")[0].appendChild(element);
206
	};
207
	this._multiAttach = function (state, part) {
208
		if (state.query == null) {
209
			this._finish(state, "error", {status:this.DsrStatusCodes.Error, statusText:"query.null"});
210
			return;
211
		}
212
		if (!state.constantParams) {
213
			state.constantParams = "";
214
		}
215
		var queryMax = this.maxUrlLength - state.idParam.length - state.constantParams.length - state.url.length - state.nocacheParam.length - this._extraPaddingLength;
216
		var isDone = state.query.length < queryMax;
217
		var currentQuery;
218
		if (isDone) {
219
			currentQuery = state.query;
220
			state.query = null;
221
		} else {
222
			var ampEnd = state.query.lastIndexOf("&", queryMax - 1);
223
			var eqEnd = state.query.lastIndexOf("=", queryMax - 1);
224
			if (ampEnd > eqEnd || eqEnd == queryMax - 1) {
225
				currentQuery = state.query.substring(0, ampEnd);
226
				state.query = state.query.substring(ampEnd + 1, state.query.length);
227
			} else {
228
				currentQuery = state.query.substring(0, queryMax);
229
				var queryName = currentQuery.substring((ampEnd == -1 ? 0 : ampEnd + 1), eqEnd);
230
				state.query = queryName + "=" + state.query.substring(queryMax, state.query.length);
231
			}
232
		}
233
		var queryParams = [currentQuery, state.idParam, state.constantParams, state.nocacheParam];
234
		if (!isDone) {
235
			queryParams.push("_part=" + part);
236
		}
237
		var url = this._buildUrl(state.url, queryParams);
238
		this._attach(state.id + "_" + part, url);
239
	};
240
	this._finish = function (state, callback, event) {
241
		if (callback != "partOk" && !state.kwArgs[callback] && !state.kwArgs["handle"]) {
242
			if (callback == "error") {
243
				state.isDone = true;
244
				throw event;
245
			}
246
		} else {
247
			switch (callback) {
248
			  case "load":
249
				var response = event ? event.response : null;
250
				if (!response) {
251
					response = event;
252
				}
253
				state.kwArgs[(typeof state.kwArgs.load == "function") ? "load" : "handle"]("load", response, event, state.kwArgs);
254
				state.isDone = true;
255
				break;
256
			  case "partOk":
257
				var part = parseInt(event.response.part, 10) + 1;
258
				if (event.response.constantParams) {
259
					state.constantParams = event.response.constantParams;
260
				}
261
				this._multiAttach(state, part);
262
				state.isDone = false;
263
				break;
264
			  case "error":
265
				state.kwArgs[(typeof state.kwArgs.error == "function") ? "error" : "handle"]("error", event.response, event, state.kwArgs);
266
				state.isDone = true;
267
				break;
268
			  default:
269
				state.kwArgs[(typeof state.kwArgs[callback] == "function") ? callback : "handle"](callback, event, event, state.kwArgs);
270
				state.isDone = true;
271
			}
272
		}
273
	};
274
	dojo.io.transports.addTransport("ScriptSrcTransport");
275
};
276
window.onscriptload = function (event) {
277
	var state = null;
278
	var transport = dojo.io.ScriptSrcTransport;
279
	if (transport._state[event.id]) {
280
		state = transport._state[event.id];
281
	} else {
282
		var tempState;
283
		for (var param in transport._state) {
284
			tempState = transport._state[param];
285
			if (tempState.finalUrl && tempState.finalUrl == event.id) {
286
				state = tempState;
287
				break;
288
			}
289
		}
290
		if (state == null) {
291
			var scripts = document.getElementsByTagName("script");
292
			for (var i = 0; scripts && i < scripts.length; i++) {
293
				var scriptTag = scripts[i];
294
				if (scriptTag.getAttribute("class") == "ScriptSrcTransport" && scriptTag.src == event.id) {
295
					state = transport._state[scriptTag.id];
296
					break;
297
				}
298
			}
299
		}
300
		if (state == null) {
301
			throw "No matching state for onscriptload event.id: " + event.id;
302
		}
303
	}
304
	var callbackName = "error";
305
	switch (event.status) {
306
	  case dojo.io.ScriptSrcTransport.DsrStatusCodes.Continue:
307
		callbackName = "partOk";
308
		break;
309
	  case dojo.io.ScriptSrcTransport.DsrStatusCodes.Ok:
310
		callbackName = "load";
311
		break;
312
	}
313
	transport._finish(state, callbackName, event);
314
};
315