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.widget.ContentPane");
12
dojo.require("dojo.widget.*");
13
dojo.require("dojo.io.*");
14
dojo.require("dojo.widget.HtmlWidget");
15
dojo.require("dojo.string");
16
dojo.require("dojo.string.extras");
17
dojo.require("dojo.html.style");
18
dojo.widget.defineWidget("dojo.widget.ContentPane", dojo.widget.HtmlWidget, function () {
19
	this._styleNodes = [];
20
	this._onLoadStack = [];
21
	this._onUnloadStack = [];
22
	this._callOnUnload = false;
23
	this._ioBindObj;
24
	this.scriptScope;
25
	this.bindArgs = {};
26
}, {isContainer:true, adjustPaths:true, href:"", extractContent:true, parseContent:true, cacheContent:true, preload:false, refreshOnShow:false, handler:"", executeScripts:false, scriptSeparation:true, loadingMessage:"Loading...", isLoaded:false, postCreate:function (args, frag, parentComp) {
27
	if (this.handler !== "") {
28
		this.setHandler(this.handler);
29
	}
30
	if (this.isShowing() || this.preload) {
31
		this.loadContents();
32
	}
33
}, show:function () {
34
	if (this.refreshOnShow) {
35
		this.refresh();
36
	} else {
37
		this.loadContents();
38
	}
39
	dojo.widget.ContentPane.superclass.show.call(this);
40
}, refresh:function () {
41
	this.isLoaded = false;
42
	this.loadContents();
43
}, loadContents:function () {
44
	if (this.isLoaded) {
45
		return;
46
	}
47
	if (dojo.lang.isFunction(this.handler)) {
48
		this._runHandler();
49
	} else {
50
		if (this.href != "") {
51
			this._downloadExternalContent(this.href, this.cacheContent && !this.refreshOnShow);
52
		}
53
	}
54
}, setUrl:function (url) {
55
	this.href = url;
56
	this.isLoaded = false;
57
	if (this.preload || this.isShowing()) {
58
		this.loadContents();
59
	}
60
}, abort:function () {
61
	var bind = this._ioBindObj;
62
	if (!bind || !bind.abort) {
63
		return;
64
	}
65
	bind.abort();
66
	delete this._ioBindObj;
67
}, _downloadExternalContent:function (url, useCache) {
68
	this.abort();
69
	this._handleDefaults(this.loadingMessage, "onDownloadStart");
70
	var self = this;
71
	this._ioBindObj = dojo.io.bind(this._cacheSetting({url:url, mimetype:"text/html", handler:function (type, data, xhr) {
72
		delete self._ioBindObj;
73
		if (type == "load") {
74
			self.onDownloadEnd.call(self, url, data);
75
		} else {
76
			var e = {responseText:xhr.responseText, status:xhr.status, statusText:xhr.statusText, responseHeaders:xhr.getAllResponseHeaders(), text:"Error loading '" + url + "' (" + xhr.status + " " + xhr.statusText + ")"};
77
			self._handleDefaults.call(self, e, "onDownloadError");
78
			self.onLoad();
79
		}
80
	}}, useCache));
81
}, _cacheSetting:function (bindObj, useCache) {
82
	for (var x in this.bindArgs) {
83
		if (dojo.lang.isUndefined(bindObj[x])) {
84
			bindObj[x] = this.bindArgs[x];
85
		}
86
	}
87
	if (dojo.lang.isUndefined(bindObj.useCache)) {
88
		bindObj.useCache = useCache;
89
	}
90
	if (dojo.lang.isUndefined(bindObj.preventCache)) {
91
		bindObj.preventCache = !useCache;
92
	}
93
	if (dojo.lang.isUndefined(bindObj.mimetype)) {
94
		bindObj.mimetype = "text/html";
95
	}
96
	return bindObj;
97
}, onLoad:function (e) {
98
	this._runStack("_onLoadStack");
99
	this.isLoaded = true;
100
}, onUnLoad:function (e) {
101
	dojo.deprecated(this.widgetType + ".onUnLoad, use .onUnload (lowercased load)", 0.5);
102
}, onUnload:function (e) {
103
	this._runStack("_onUnloadStack");
104
	delete this.scriptScope;
105
	if (this.onUnLoad !== dojo.widget.ContentPane.prototype.onUnLoad) {
106
		this.onUnLoad.apply(this, arguments);
107
	}
108
}, _runStack:function (stName) {
109
	var st = this[stName];
110
	var err = "";
111
	var scope = this.scriptScope || window;
112
	for (var i = 0; i < st.length; i++) {
113
		try {
114
			st[i].call(scope);
115
		}
116
		catch (e) {
117
			err += "\n" + st[i] + " failed: " + e.description;
118
		}
119
	}
120
	this[stName] = [];
121
	if (err.length) {
122
		var name = (stName == "_onLoadStack") ? "addOnLoad" : "addOnUnLoad";
123
		this._handleDefaults(name + " failure\n " + err, "onExecError", "debug");
124
	}
125
}, addOnLoad:function (obj, func) {
126
	this._pushOnStack(this._onLoadStack, obj, func);
127
}, addOnUnload:function (obj, func) {
128
	this._pushOnStack(this._onUnloadStack, obj, func);
129
}, addOnUnLoad:function () {
130
	dojo.deprecated(this.widgetType + ".addOnUnLoad, use addOnUnload instead. (lowercased Load)", 0.5);
131
	this.addOnUnload.apply(this, arguments);
132
}, _pushOnStack:function (stack, obj, func) {
133
	if (typeof func == "undefined") {
134
		stack.push(obj);
135
	} else {
136
		stack.push(function () {
137
			obj[func]();
138
		});
139
	}
140
}, destroy:function () {
141
	this.onUnload();
142
	dojo.widget.ContentPane.superclass.destroy.call(this);
143
}, onExecError:function (e) {
144
}, onContentError:function (e) {
145
}, onDownloadError:function (e) {
146
}, onDownloadStart:function (e) {
147
}, onDownloadEnd:function (url, data) {
148
	data = this.splitAndFixPaths(data, url);
149
	this.setContent(data);
150
}, _handleDefaults:function (e, handler, messType) {
151
	if (!handler) {
152
		handler = "onContentError";
153
	}
154
	if (dojo.lang.isString(e)) {
155
		e = {text:e};
156
	}
157
	if (!e.text) {
158
		e.text = e.toString();
159
	}
160
	e.toString = function () {
161
		return this.text;
162
	};
163
	if (typeof e.returnValue != "boolean") {
164
		e.returnValue = true;
165
	}
166
	if (typeof e.preventDefault != "function") {
167
		e.preventDefault = function () {
168
			this.returnValue = false;
169
		};
170
	}
171
	this[handler](e);
172
	if (e.returnValue) {
173
		switch (messType) {
174
		  case true:
175
		  case "alert":
176
			alert(e.toString());
177
			break;
178
		  case "debug":
179
			dojo.debug(e.toString());
180
			break;
181
		  default:
182
			if (this._callOnUnload) {
183
				this.onUnload();
184
			}
185
			this._callOnUnload = false;
186
			if (arguments.callee._loopStop) {
187
				dojo.debug(e.toString());
188
			} else {
189
				arguments.callee._loopStop = true;
190
				this._setContent(e.toString());
191
			}
192
		}
193
	}
194
	arguments.callee._loopStop = false;
195
}, splitAndFixPaths:function (s, url) {
196
	var titles = [], scripts = [], tmp = [];
197
	var match = [], requires = [], attr = [], styles = [];
198
	var str = "", path = "", fix = "", tagFix = "", tag = "", origPath = "";
199
	if (!url) {
200
		url = "./";
201
	}
202
	if (s) {
203
		var regex = /<title[^>]*>([\s\S]*?)<\/title>/i;
204
		while (match = regex.exec(s)) {
205
			titles.push(match[1]);
206
			s = s.substring(0, match.index) + s.substr(match.index + match[0].length);
207
		}
208
		if (this.adjustPaths) {
209
			var regexFindTag = /<[a-z][a-z0-9]*[^>]*\s(?:(?:src|href|style)=[^>])+[^>]*>/i;
210
			var regexFindAttr = /\s(src|href|style)=(['"]?)([\w()\[\]\/.,\\'"-:;#=&?\s@]+?)\2/i;
211
			var regexProtocols = /^(?:[#]|(?:(?:https?|ftps?|file|javascript|mailto|news):))/;
212
			while (tag = regexFindTag.exec(s)) {
213
				str += s.substring(0, tag.index);
214
				s = s.substring((tag.index + tag[0].length), s.length);
215
				tag = tag[0];
216
				tagFix = "";
217
				while (attr = regexFindAttr.exec(tag)) {
218
					path = "";
219
					origPath = attr[3];
220
					switch (attr[1].toLowerCase()) {
221
					  case "src":
222
					  case "href":
223
						if (regexProtocols.exec(origPath)) {
224
							path = origPath;
225
						} else {
226
							path = (new dojo.uri.Uri(url, origPath).toString());
227
						}
228
						break;
229
					  case "style":
230
						path = dojo.html.fixPathsInCssText(origPath, url);
231
						break;
232
					  default:
233
						path = origPath;
234
					}
235
					fix = " " + attr[1] + "=" + attr[2] + path + attr[2];
236
					tagFix += tag.substring(0, attr.index) + fix;
237
					tag = tag.substring((attr.index + attr[0].length), tag.length);
238
				}
239
				str += tagFix + tag;
240
			}
241
			s = str + s;
242
		}
243
		regex = /(?:<(style)[^>]*>([\s\S]*?)<\/style>|<link ([^>]*rel=['"]?stylesheet['"]?[^>]*)>)/i;
244
		while (match = regex.exec(s)) {
245
			if (match[1] && match[1].toLowerCase() == "style") {
246
				styles.push(dojo.html.fixPathsInCssText(match[2], url));
247
			} else {
248
				if (attr = match[3].match(/href=(['"]?)([^'">]*)\1/i)) {
249
					styles.push({path:attr[2]});
250
				}
251
			}
252
			s = s.substring(0, match.index) + s.substr(match.index + match[0].length);
253
		}
254
		var regex = /<script([^>]*)>([\s\S]*?)<\/script>/i;
255
		var regexSrc = /src=(['"]?)([^"']*)\1/i;
256
		var regexDojoJs = /.*(\bdojo\b\.js(?:\.uncompressed\.js)?)$/;
257
		var regexInvalid = /(?:var )?\bdjConfig\b(?:[\s]*=[\s]*\{[^}]+\}|\.[\w]*[\s]*=[\s]*[^;\n]*)?;?|dojo\.hostenv\.writeIncludes\(\s*\);?/g;
258
		var regexRequires = /dojo\.(?:(?:require(?:After)?(?:If)?)|(?:widget\.(?:manager\.)?registerWidgetPackage)|(?:(?:hostenv\.)?setModulePrefix|registerModulePath)|defineNamespace)\((['"]).*?\1\)\s*;?/;
259
		while (match = regex.exec(s)) {
260
			if (this.executeScripts && match[1]) {
261
				if (attr = regexSrc.exec(match[1])) {
262
					if (regexDojoJs.exec(attr[2])) {
263
						dojo.debug("Security note! inhibit:" + attr[2] + " from  being loaded again.");
264
					} else {
265
						scripts.push({path:attr[2]});
266
					}
267
				}
268
			}
269
			if (match[2]) {
270
				var sc = match[2].replace(regexInvalid, "");
271
				if (!sc) {
272
					continue;
273
				}
274
				while (tmp = regexRequires.exec(sc)) {
275
					requires.push(tmp[0]);
276
					sc = sc.substring(0, tmp.index) + sc.substr(tmp.index + tmp[0].length);
277
				}
278
				if (this.executeScripts) {
279
					scripts.push(sc);
280
				}
281
			}
282
			s = s.substr(0, match.index) + s.substr(match.index + match[0].length);
283
		}
284
		if (this.extractContent) {
285
			match = s.match(/<body[^>]*>\s*([\s\S]+)\s*<\/body>/im);
286
			if (match) {
287
				s = match[1];
288
			}
289
		}
290
		if (this.executeScripts && this.scriptSeparation) {
291
			var regex = /(<[a-zA-Z][a-zA-Z0-9]*\s[^>]*?\S=)((['"])[^>]*scriptScope[^>]*>)/;
292
			var regexAttr = /([\s'";:\(])scriptScope(.*)/;
293
			str = "";
294
			while (tag = regex.exec(s)) {
295
				tmp = ((tag[3] == "'") ? "\"" : "'");
296
				fix = "";
297
				str += s.substring(0, tag.index) + tag[1];
298
				while (attr = regexAttr.exec(tag[2])) {
299
					tag[2] = tag[2].substring(0, attr.index) + attr[1] + "dojo.widget.byId(" + tmp + this.widgetId + tmp + ").scriptScope" + attr[2];
300
				}
301
				str += tag[2];
302
				s = s.substr(tag.index + tag[0].length);
303
			}
304
			s = str + s;
305
		}
306
	}
307
	return {"xml":s, "styles":styles, "titles":titles, "requires":requires, "scripts":scripts, "url":url};
308
}, _setContent:function (cont) {
309
	this.destroyChildren();
310
	for (var i = 0; i < this._styleNodes.length; i++) {
311
		if (this._styleNodes[i] && this._styleNodes[i].parentNode) {
312
			this._styleNodes[i].parentNode.removeChild(this._styleNodes[i]);
313
		}
314
	}
315
	this._styleNodes = [];
316
	try {
317
		var node = this.containerNode || this.domNode;
318
		while (node.firstChild) {
319
			dojo.html.destroyNode(node.firstChild);
320
		}
321
		if (typeof cont != "string") {
322
			node.appendChild(cont);
323
		} else {
324
			node.innerHTML = cont;
325
		}
326
	}
327
	catch (e) {
328
		e.text = "Couldn't load content:" + e.description;
329
		this._handleDefaults(e, "onContentError");
330
	}
331
}, setContent:function (data) {
332
	this.abort();
333
	if (this._callOnUnload) {
334
		this.onUnload();
335
	}
336
	this._callOnUnload = true;
337
	if (!data || dojo.html.isNode(data)) {
338
		this._setContent(data);
339
		this.onResized();
340
		this.onLoad();
341
	} else {
342
		if (typeof data.xml != "string") {
343
			this.href = "";
344
			data = this.splitAndFixPaths(data);
345
		}
346
		this._setContent(data.xml);
347
		for (var i = 0; i < data.styles.length; i++) {
348
			if (data.styles[i].path) {
349
				this._styleNodes.push(dojo.html.insertCssFile(data.styles[i].path, dojo.doc(), false, true));
350
			} else {
351
				this._styleNodes.push(dojo.html.insertCssText(data.styles[i]));
352
			}
353
		}
354
		if (this.parseContent) {
355
			for (var i = 0; i < data.requires.length; i++) {
356
				try {
357
					eval(data.requires[i]);
358
				}
359
				catch (e) {
360
					e.text = "ContentPane: error in package loading calls, " + (e.description || e);
361
					this._handleDefaults(e, "onContentError", "debug");
362
				}
363
			}
364
		}
365
		var _self = this;
366
		function asyncParse() {
367
			if (_self.executeScripts) {
368
				_self._executeScripts(data.scripts);
369
			}
370
			if (_self.parseContent) {
371
				var node = _self.containerNode || _self.domNode;
372
				var parser = new dojo.xml.Parse();
373
				var frag = parser.parseElement(node, null, true);
374
				dojo.widget.getParser().createSubComponents(frag, _self);
375
			}
376
			_self.onResized();
377
			_self.onLoad();
378
		}
379
		if (dojo.hostenv.isXDomain && data.requires.length) {
380
			dojo.addOnLoad(asyncParse);
381
		} else {
382
			asyncParse();
383
		}
384
	}
385
}, setHandler:function (handler) {
386
	var fcn = dojo.lang.isFunction(handler) ? handler : window[handler];
387
	if (!dojo.lang.isFunction(fcn)) {
388
		this._handleDefaults("Unable to set handler, '" + handler + "' not a function.", "onExecError", true);
389
		return;
390
	}
391
	this.handler = function () {
392
		return fcn.apply(this, arguments);
393
	};
394
}, _runHandler:function () {
395
	var ret = true;
396
	if (dojo.lang.isFunction(this.handler)) {
397
		this.handler(this, this.domNode);
398
		ret = false;
399
	}
400
	this.onLoad();
401
	return ret;
402
}, _executeScripts:function (scripts) {
403
	var self = this;
404
	var tmp = "", code = "";
405
	for (var i = 0; i < scripts.length; i++) {
406
		if (scripts[i].path) {
407
			dojo.io.bind(this._cacheSetting({"url":scripts[i].path, "load":function (type, scriptStr) {
408
				dojo.lang.hitch(self, tmp = ";" + scriptStr);
409
			}, "error":function (type, error) {
410
				error.text = type + " downloading remote script";
411
				self._handleDefaults.call(self, error, "onExecError", "debug");
412
			}, "mimetype":"text/plain", "sync":true}, this.cacheContent));
413
			code += tmp;
414
		} else {
415
			code += scripts[i];
416
		}
417
	}
418
	try {
419
		if (this.scriptSeparation) {
420
			delete this.scriptScope;
421
			this.scriptScope = new (new Function("_container_", code + "; return this;"))(self);
422
		} else {
423
			var djg = dojo.global();
424
			if (djg.execScript) {
425
				djg.execScript(code);
426
			} else {
427
				var djd = dojo.doc();
428
				var sc = djd.createElement("script");
429
				sc.appendChild(djd.createTextNode(code));
430
				(this.containerNode || this.domNode).appendChild(sc);
431
			}
432
		}
433
	}
434
	catch (e) {
435
		e.text = "Error running scripts from content:\n" + e.description;
436
		this._handleDefaults(e, "onExecError", "debug");
437
	}
438
}});
439