Subversion Repositories Applications.papyrus

Rev

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