Subversion Repositories Applications.papyrus

Rev

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