Subversion Repositories Applications.papyrus

Rev

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.hostenv.resetXd = function () {
12
	this.isXDomain = djConfig.useXDomain || false;
13
	this.xdTimer = 0;
14
	this.xdInFlight = {};
15
	this.xdOrderedReqs = [];
16
	this.xdDepMap = {};
17
	this.xdContents = [];
18
	this.xdDefList = [];
19
};
20
dojo.hostenv.resetXd();
21
dojo.hostenv.createXdPackage = function (contents, resourceName, resourcePath) {
22
	var deps = [];
23
	var depRegExp = /dojo.(require|requireIf|requireAll|provide|requireAfterIf|requireAfter|kwCompoundRequire|conditionalRequire|hostenv\.conditionalLoadModule|.hostenv\.loadModule|hostenv\.moduleLoaded)\(([\w\W]*?)\)/mg;
24
	var match;
25
	while ((match = depRegExp.exec(contents)) != null) {
26
		deps.push("\"" + match[1] + "\", " + match[2]);
27
	}
28
	var output = [];
29
	output.push("dojo.hostenv.packageLoaded({\n");
30
	if (deps.length > 0) {
31
		output.push("depends: [");
32
		for (var i = 0; i < deps.length; i++) {
33
			if (i > 0) {
34
				output.push(",\n");
35
			}
36
			output.push("[" + deps[i] + "]");
37
		}
38
		output.push("],");
39
	}
40
	output.push("\ndefinePackage: function(dojo){");
41
	output.push(contents);
42
	output.push("\n}, resourceName: '" + resourceName + "', resourcePath: '" + resourcePath + "'});");
43
	return output.join("");
44
};
45
dojo.hostenv.loadPath = function (relpath, module, cb) {
46
	var colonIndex = relpath.indexOf(":");
47
	var slashIndex = relpath.indexOf("/");
48
	var uri;
49
	var currentIsXDomain = false;
50
	if (colonIndex > 0 && colonIndex < slashIndex) {
51
		uri = relpath;
52
		this.isXDomain = currentIsXDomain = true;
53
	} else {
54
		uri = this.getBaseScriptUri() + relpath;
55
		colonIndex = uri.indexOf(":");
56
		slashIndex = uri.indexOf("/");
57
		if (colonIndex > 0 && colonIndex < slashIndex && (!location.host || uri.indexOf("http://" + location.host) != 0)) {
58
			this.isXDomain = currentIsXDomain = true;
59
		}
60
	}
61
	if (djConfig.cacheBust && dojo.render.html.capable) {
62
		uri += "?" + String(djConfig.cacheBust).replace(/\W+/g, "");
63
	}
64
	try {
65
		return ((!module || this.isXDomain) ? this.loadUri(uri, cb, currentIsXDomain, module) : this.loadUriAndCheck(uri, module, cb));
66
	}
67
	catch (e) {
68
		dojo.debug(e);
69
		return false;
70
	}
71
};
72
dojo.hostenv.loadUri = function (uri, cb, currentIsXDomain, module) {
73
	if (this.loadedUris[uri]) {
74
		return 1;
75
	}
76
	if (this.isXDomain) {
77
		if (uri.indexOf("__package__") != -1) {
78
			module += ".*";
79
		}
80
		this.xdOrderedReqs.push(module);
81
		if (currentIsXDomain) {
82
			this.xdInFlight[module] = true;
83
			this.inFlightCount++;
84
		}
85
		if (!this.xdTimer) {
86
			this.xdTimer = setInterval("dojo.hostenv.watchInFlightXDomain();", 100);
87
		}
88
		this.xdStartTime = (new Date()).getTime();
89
	}
90
	if (currentIsXDomain) {
91
		var lastIndex = uri.lastIndexOf(".");
92
		if (lastIndex <= 0) {
93
			lastIndex = uri.length - 1;
94
		}
95
		var xdUri = uri.substring(0, lastIndex) + ".xd";
96
		if (lastIndex != uri.length - 1) {
97
			xdUri += uri.substring(lastIndex, uri.length);
98
		}
99
		var element = document.createElement("script");
100
		element.type = "text/javascript";
101
		element.src = xdUri;
102
		if (!this.headElement) {
103
			this.headElement = document.getElementsByTagName("head")[0];
104
			if (!this.headElement) {
105
				this.headElement = document.getElementsByTagName("html")[0];
106
			}
107
		}
108
		this.headElement.appendChild(element);
109
	} else {
110
		var contents = this.getText(uri, null, true);
111
		if (contents == null) {
112
			return 0;
113
		}
114
		if (this.isXDomain && uri.indexOf("/nls/") == -1) {
115
			var pkg = this.createXdPackage(contents, module, uri);
116
			dj_eval(pkg);
117
		} else {
118
			if (cb) {
119
				contents = "(" + contents + ")";
120
			}
121
			var value = dj_eval(contents);
122
			if (cb) {
123
				cb(value);
124
			}
125
		}
126
	}
127
	this.loadedUris[uri] = true;
128
	return 1;
129
};
130
dojo.hostenv.packageLoaded = function (pkg) {
131
	var deps = pkg.depends;
132
	var requireList = null;
133
	var requireAfterList = null;
134
	var provideList = [];
135
	if (deps && deps.length > 0) {
136
		var dep = null;
137
		var insertHint = 0;
138
		var attachedPackage = false;
139
		for (var i = 0; i < deps.length; i++) {
140
			dep = deps[i];
141
			if (dep[0] == "provide" || dep[0] == "hostenv.moduleLoaded") {
142
				provideList.push(dep[1]);
143
			} else {
144
				if (!requireList) {
145
					requireList = [];
146
				}
147
				if (!requireAfterList) {
148
					requireAfterList = [];
149
				}
150
				var unpackedDeps = this.unpackXdDependency(dep);
151
				if (unpackedDeps.requires) {
152
					requireList = requireList.concat(unpackedDeps.requires);
153
				}
154
				if (unpackedDeps.requiresAfter) {
155
					requireAfterList = requireAfterList.concat(unpackedDeps.requiresAfter);
156
				}
157
			}
158
			var depType = dep[0];
159
			var objPath = depType.split(".");
160
			if (objPath.length == 2) {
161
				dojo[objPath[0]][objPath[1]].apply(dojo[objPath[0]], dep.slice(1));
162
			} else {
163
				dojo[depType].apply(dojo, dep.slice(1));
164
			}
165
		}
166
		var contentIndex = this.xdContents.push({content:pkg.definePackage, resourceName:pkg["resourceName"], resourcePath:pkg["resourcePath"], isDefined:false}) - 1;
167
		for (var i = 0; i < provideList.length; i++) {
168
			this.xdDepMap[provideList[i]] = {requires:requireList, requiresAfter:requireAfterList, contentIndex:contentIndex};
169
		}
170
		for (var i = 0; i < provideList.length; i++) {
171
			this.xdInFlight[provideList[i]] = false;
172
		}
173
	}
174
};
175
dojo.hostenv.xdLoadFlattenedBundle = function (moduleName, bundleName, locale, bundleData) {
176
	locale = locale || "root";
177
	var jsLoc = dojo.hostenv.normalizeLocale(locale).replace("-", "_");
178
	var bundlePackage = [moduleName, "nls", bundleName].join(".");
179
	var bundle = dojo.hostenv.startPackage(bundlePackage);
180
	bundle[jsLoc] = bundleData;
181
	var mapName = [moduleName, jsLoc, bundleName].join(".");
182
	var bundleMap = dojo.hostenv.xdBundleMap[mapName];
183
	if (bundleMap) {
184
		for (var param in bundleMap) {
185
			bundle[param] = bundleData;
186
		}
187
	}
188
};
189
dojo.hostenv.xdBundleMap = {};
190
dojo.xdRequireLocalization = function (moduleName, bundleName, locale, availableFlatLocales) {
191
	var locales = availableFlatLocales.split(",");
192
	var jsLoc = dojo.hostenv.normalizeLocale(locale);
193
	var bestLocale = "";
194
	for (var i = 0; i < locales.length; i++) {
195
		if (jsLoc.indexOf(locales[i]) == 0) {
196
			if (locales[i].length > bestLocale.length) {
197
				bestLocale = locales[i];
198
			}
199
		}
200
	}
201
	var fixedBestLocale = bestLocale.replace("-", "_");
202
	var bundlePackage = dojo.evalObjPath([moduleName, "nls", bundleName].join("."));
203
	if (bundlePackage && bundlePackage[fixedBestLocale]) {
204
		bundle[jsLoc.replace("-", "_")] = bundlePackage[fixedBestLocale];
205
	} else {
206
		var mapName = [moduleName, (fixedBestLocale || "root"), bundleName].join(".");
207
		var bundleMap = dojo.hostenv.xdBundleMap[mapName];
208
		if (!bundleMap) {
209
			bundleMap = dojo.hostenv.xdBundleMap[mapName] = {};
210
		}
211
		bundleMap[jsLoc.replace("-", "_")] = true;
212
		dojo.require(moduleName + ".nls" + (bestLocale ? "." + bestLocale : "") + "." + bundleName);
213
	}
214
};
215
(function () {
216
	var extra = djConfig.extraLocale;
217
	if (extra) {
218
		if (!extra instanceof Array) {
219
			extra = [extra];
220
		}
221
		dojo._xdReqLoc = dojo.xdRequireLocalization;
222
		dojo.xdRequireLocalization = function (m, b, locale, fLocales) {
223
			dojo._xdReqLoc(m, b, locale, fLocales);
224
			if (locale) {
225
				return;
226
			}
227
			for (var i = 0; i < extra.length; i++) {
228
				dojo._xdReqLoc(m, b, extra[i], fLocales);
229
			}
230
		};
231
	}
232
})();
233
dojo.hostenv.unpackXdDependency = function (dep) {
234
	var newDeps = null;
235
	var newAfterDeps = null;
236
	switch (dep[0]) {
237
	  case "requireIf":
238
	  case "requireAfterIf":
239
	  case "conditionalRequire":
240
		if ((dep[1] === true) || (dep[1] == "common") || (dep[1] && dojo.render[dep[1]].capable)) {
241
			newDeps = [{name:dep[2], content:null}];
242
		}
243
		break;
244
	  case "requireAll":
245
		dep.shift();
246
		newDeps = dep;
247
		dojo.hostenv.flattenRequireArray(newDeps);
248
		break;
249
	  case "kwCompoundRequire":
250
	  case "hostenv.conditionalLoadModule":
251
		var modMap = dep[1];
252
		var common = modMap["common"] || [];
253
		var newDeps = (modMap[dojo.hostenv.name_]) ? common.concat(modMap[dojo.hostenv.name_] || []) : common.concat(modMap["default"] || []);
254
		dojo.hostenv.flattenRequireArray(newDeps);
255
		break;
256
	  case "require":
257
	  case "requireAfter":
258
	  case "hostenv.loadModule":
259
		newDeps = [{name:dep[1], content:null}];
260
		break;
261
	}
262
	if (dep[0] == "requireAfterIf" || dep[0] == "requireIf") {
263
		newAfterDeps = newDeps;
264
		newDeps = null;
265
	}
266
	return {requires:newDeps, requiresAfter:newAfterDeps};
267
};
268
dojo.hostenv.xdWalkReqs = function () {
269
	var reqChain = null;
270
	var req;
271
	for (var i = 0; i < this.xdOrderedReqs.length; i++) {
272
		req = this.xdOrderedReqs[i];
273
		if (this.xdDepMap[req]) {
274
			reqChain = [req];
275
			reqChain[req] = true;
276
			this.xdEvalReqs(reqChain);
277
		}
278
	}
279
};
280
dojo.hostenv.xdEvalReqs = function (reqChain) {
281
	while (reqChain.length > 0) {
282
		var req = reqChain[reqChain.length - 1];
283
		var pkg = this.xdDepMap[req];
284
		if (pkg) {
285
			var reqs = pkg.requires;
286
			if (reqs && reqs.length > 0) {
287
				var nextReq;
288
				for (var i = 0; i < reqs.length; i++) {
289
					nextReq = reqs[i].name;
290
					if (nextReq && !reqChain[nextReq]) {
291
						reqChain.push(nextReq);
292
						reqChain[nextReq] = true;
293
						this.xdEvalReqs(reqChain);
294
					}
295
				}
296
			}
297
			var contents = this.xdContents[pkg.contentIndex];
298
			if (!contents.isDefined) {
299
				var content = contents.content;
300
				content["resourceName"] = contents["resourceName"];
301
				content["resourcePath"] = contents["resourcePath"];
302
				this.xdDefList.push(content);
303
				contents.isDefined = true;
304
			}
305
			this.xdDepMap[req] = null;
306
			var reqs = pkg.requiresAfter;
307
			if (reqs && reqs.length > 0) {
308
				var nextReq;
309
				for (var i = 0; i < reqs.length; i++) {
310
					nextReq = reqs[i].name;
311
					if (nextReq && !reqChain[nextReq]) {
312
						reqChain.push(nextReq);
313
						reqChain[nextReq] = true;
314
						this.xdEvalReqs(reqChain);
315
					}
316
				}
317
			}
318
		}
319
		reqChain.pop();
320
	}
321
};
322
dojo.hostenv.clearXdInterval = function () {
323
	clearInterval(this.xdTimer);
324
	this.xdTimer = 0;
325
};
326
dojo.hostenv.watchInFlightXDomain = function () {
327
	var waitInterval = (djConfig.xdWaitSeconds || 15) * 1000;
328
	if (this.xdStartTime + waitInterval < (new Date()).getTime()) {
329
		this.clearXdInterval();
330
		var noLoads = "";
331
		for (var param in this.xdInFlight) {
332
			if (this.xdInFlight[param]) {
333
				noLoads += param + " ";
334
			}
335
		}
336
		dojo.raise("Could not load cross-domain packages: " + noLoads);
337
	}
338
	for (var param in this.xdInFlight) {
339
		if (this.xdInFlight[param]) {
340
			return;
341
		}
342
	}
343
	this.clearXdInterval();
344
	this.xdWalkReqs();
345
	var defLength = this.xdDefList.length;
346
	for (var i = 0; i < defLength; i++) {
347
		var content = dojo.hostenv.xdDefList[i];
348
		if (djConfig["debugAtAllCosts"] && content["resourceName"]) {
349
			if (!this["xdDebugQueue"]) {
350
				this.xdDebugQueue = [];
351
			}
352
			this.xdDebugQueue.push({resourceName:content.resourceName, resourcePath:content.resourcePath});
353
		} else {
354
			content(dojo);
355
		}
356
	}
357
	for (var i = 0; i < this.xdContents.length; i++) {
358
		var current = this.xdContents[i];
359
		if (current.content && !current.isDefined) {
360
			current.content(dojo);
361
		}
362
	}
363
	this.resetXd();
364
	if (this["xdDebugQueue"] && this.xdDebugQueue.length > 0) {
365
		this.xdDebugFileLoaded();
366
	} else {
367
		this.xdNotifyLoaded();
368
	}
369
};
370
dojo.hostenv.xdNotifyLoaded = function () {
371
	this.inFlightCount = 0;
372
	this.callLoaded();
373
};
374
dojo.hostenv.flattenRequireArray = function (target) {
375
	if (target) {
376
		for (var i = 0; i < target.length; i++) {
377
			if (target[i] instanceof Array) {
378
				target[i] = {name:target[i][0], content:null};
379
			} else {
380
				target[i] = {name:target[i], content:null};
381
			}
382
		}
383
	}
384
};
385
dojo.hostenv.xdHasCalledPreload = false;
386
dojo.hostenv.xdRealCallLoaded = dojo.hostenv.callLoaded;
387
dojo.hostenv.callLoaded = function () {
388
	if (this.xdHasCalledPreload || dojo.hostenv.getModulePrefix("dojo") == "src" || !this.localesGenerated) {
389
		this.xdRealCallLoaded();
390
		this.xdHasCalledPreload = true;
391
	} else {
392
		if (this.localesGenerated) {
393
			this.registerNlsPrefix = function () {
394
				dojo.registerModulePath("nls", dojo.hostenv.getModulePrefix("dojo") + "/../nls");
395
			};
396
			this.preloadLocalizations();
397
		}
398
		this.xdHasCalledPreload = true;
399
	}
400
};
401