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.DomWidget");
12
dojo.require("dojo.event.*");
13
dojo.require("dojo.widget.Widget");
14
dojo.require("dojo.dom");
15
dojo.require("dojo.html.style");
16
dojo.require("dojo.xml.Parse");
17
dojo.require("dojo.uri.*");
18
dojo.require("dojo.lang.func");
19
dojo.require("dojo.lang.extras");
20
dojo.widget._cssFiles = {};
21
dojo.widget._cssStrings = {};
22
dojo.widget._templateCache = {};
23
dojo.widget.defaultStrings = {dojoRoot:dojo.hostenv.getBaseScriptUri(), dojoWidgetModuleUri:dojo.uri.moduleUri("dojo.widget"), baseScriptUri:dojo.hostenv.getBaseScriptUri()};
24
dojo.widget.fillFromTemplateCache = function (obj, templatePath, templateString, avoidCache) {
25
	var tpath = templatePath || obj.templatePath;
26
	var tmplts = dojo.widget._templateCache;
27
	if (!tpath && !obj["widgetType"]) {
28
		do {
29
			var dummyName = "__dummyTemplate__" + dojo.widget._templateCache.dummyCount++;
30
		} while (tmplts[dummyName]);
31
		obj.widgetType = dummyName;
32
	}
33
	var wt = tpath ? tpath.toString() : obj.widgetType;
34
	var ts = tmplts[wt];
35
	if (!ts) {
36
		tmplts[wt] = {"string":null, "node":null};
37
		if (avoidCache) {
38
			ts = {};
39
		} else {
40
			ts = tmplts[wt];
41
		}
42
	}
43
	if ((!obj.templateString) && (!avoidCache)) {
44
		obj.templateString = templateString || ts["string"];
45
	}
46
	if (obj.templateString) {
47
		obj.templateString = this._sanitizeTemplateString(obj.templateString);
48
	}
49
	if ((!obj.templateNode) && (!avoidCache)) {
50
		obj.templateNode = ts["node"];
51
	}
52
	if ((!obj.templateNode) && (!obj.templateString) && (tpath)) {
53
		var tstring = this._sanitizeTemplateString(dojo.hostenv.getText(tpath));
54
		obj.templateString = tstring;
55
		if (!avoidCache) {
56
			tmplts[wt]["string"] = tstring;
57
		}
58
	}
59
	if ((!ts["string"]) && (!avoidCache)) {
60
		ts.string = obj.templateString;
61
	}
62
};
63
dojo.widget._sanitizeTemplateString = function (tString) {
64
	if (tString) {
65
		tString = tString.replace(/^\s*<\?xml(\s)+version=[\'\"](\d)*.(\d)*[\'\"](\s)*\?>/im, "");
66
		var matches = tString.match(/<body[^>]*>\s*([\s\S]+)\s*<\/body>/im);
67
		if (matches) {
68
			tString = matches[1];
69
		}
70
	} else {
71
		tString = "";
72
	}
73
	return tString;
74
};
75
dojo.widget._templateCache.dummyCount = 0;
76
dojo.widget.attachProperties = ["dojoAttachPoint", "id"];
77
dojo.widget.eventAttachProperty = "dojoAttachEvent";
78
dojo.widget.onBuildProperty = "dojoOnBuild";
79
dojo.widget.waiNames = ["waiRole", "waiState"];
80
dojo.widget.wai = {waiRole:{name:"waiRole", "namespace":"http://www.w3.org/TR/xhtml2", alias:"x2", prefix:"wairole:"}, waiState:{name:"waiState", "namespace":"http://www.w3.org/2005/07/aaa", alias:"aaa", prefix:""}, setAttr:function (node, ns, attr, value) {
81
	if (dojo.render.html.ie) {
82
		node.setAttribute(this[ns].alias + ":" + attr, this[ns].prefix + value);
83
	} else {
84
		node.setAttributeNS(this[ns]["namespace"], attr, this[ns].prefix + value);
85
	}
86
}, getAttr:function (node, ns, attr) {
87
	if (dojo.render.html.ie) {
88
		return node.getAttribute(this[ns].alias + ":" + attr);
89
	} else {
90
		return node.getAttributeNS(this[ns]["namespace"], attr);
91
	}
92
}, removeAttr:function (node, ns, attr) {
93
	var success = true;
94
	if (dojo.render.html.ie) {
95
		success = node.removeAttribute(this[ns].alias + ":" + attr);
96
	} else {
97
		node.removeAttributeNS(this[ns]["namespace"], attr);
98
	}
99
	return success;
100
}};
101
dojo.widget.attachTemplateNodes = function (rootNode, targetObj, events) {
102
	var elementNodeType = dojo.dom.ELEMENT_NODE;
103
	function trim(str) {
104
		return str.replace(/^\s+|\s+$/g, "");
105
	}
106
	if (!rootNode) {
107
		rootNode = targetObj.domNode;
108
	}
109
	if (rootNode.nodeType != elementNodeType) {
110
		return;
111
	}
112
	var nodes = rootNode.all || rootNode.getElementsByTagName("*");
113
	var _this = targetObj;
114
	for (var x = -1; x < nodes.length; x++) {
115
		var baseNode = (x == -1) ? rootNode : nodes[x];
116
		var attachPoint = [];
117
		if (!targetObj.widgetsInTemplate || !baseNode.getAttribute("dojoType")) {
118
			for (var y = 0; y < this.attachProperties.length; y++) {
119
				var tmpAttachPoint = baseNode.getAttribute(this.attachProperties[y]);
120
				if (tmpAttachPoint) {
121
					attachPoint = tmpAttachPoint.split(";");
122
					for (var z = 0; z < attachPoint.length; z++) {
123
						if (dojo.lang.isArray(targetObj[attachPoint[z]])) {
124
							targetObj[attachPoint[z]].push(baseNode);
125
						} else {
126
							targetObj[attachPoint[z]] = baseNode;
127
						}
128
					}
129
					break;
130
				}
131
			}
132
			var attachEvent = baseNode.getAttribute(this.eventAttachProperty);
133
			if (attachEvent) {
134
				var evts = attachEvent.split(";");
135
				for (var y = 0; y < evts.length; y++) {
136
					if ((!evts[y]) || (!evts[y].length)) {
137
						continue;
138
					}
139
					var thisFunc = null;
140
					var tevt = trim(evts[y]);
141
					if (evts[y].indexOf(":") >= 0) {
142
						var funcNameArr = tevt.split(":");
143
						tevt = trim(funcNameArr[0]);
144
						thisFunc = trim(funcNameArr[1]);
145
					}
146
					if (!thisFunc) {
147
						thisFunc = tevt;
148
					}
149
					var tf = function () {
150
						var ntf = new String(thisFunc);
151
						return function (evt) {
152
							if (_this[ntf]) {
153
								_this[ntf](dojo.event.browser.fixEvent(evt, this));
154
							}
155
						};
156
					}();
157
					dojo.event.browser.addListener(baseNode, tevt, tf, false, true);
158
				}
159
			}
160
			for (var y = 0; y < events.length; y++) {
161
				var evtVal = baseNode.getAttribute(events[y]);
162
				if ((evtVal) && (evtVal.length)) {
163
					var thisFunc = null;
164
					var domEvt = events[y].substr(4);
165
					thisFunc = trim(evtVal);
166
					var funcs = [thisFunc];
167
					if (thisFunc.indexOf(";") >= 0) {
168
						funcs = dojo.lang.map(thisFunc.split(";"), trim);
169
					}
170
					for (var z = 0; z < funcs.length; z++) {
171
						if (!funcs[z].length) {
172
							continue;
173
						}
174
						var tf = function () {
175
							var ntf = new String(funcs[z]);
176
							return function (evt) {
177
								if (_this[ntf]) {
178
									_this[ntf](dojo.event.browser.fixEvent(evt, this));
179
								}
180
							};
181
						}();
182
						dojo.event.browser.addListener(baseNode, domEvt, tf, false, true);
183
					}
184
				}
185
			}
186
		}
187
		var tmpltPoint = baseNode.getAttribute(this.templateProperty);
188
		if (tmpltPoint) {
189
			targetObj[tmpltPoint] = baseNode;
190
		}
191
		dojo.lang.forEach(dojo.widget.waiNames, function (name) {
192
			var wai = dojo.widget.wai[name];
193
			var val = baseNode.getAttribute(wai.name);
194
			if (val) {
195
				if (val.indexOf("-") == -1) {
196
					dojo.widget.wai.setAttr(baseNode, wai.name, "role", val);
197
				} else {
198
					var statePair = val.split("-");
199
					dojo.widget.wai.setAttr(baseNode, wai.name, statePair[0], statePair[1]);
200
				}
201
			}
202
		}, this);
203
		var onBuild = baseNode.getAttribute(this.onBuildProperty);
204
		if (onBuild) {
205
			eval("var node = baseNode; var widget = targetObj; " + onBuild);
206
		}
207
	}
208
};
209
dojo.widget.getDojoEventsFromStr = function (str) {
210
	var re = /(dojoOn([a-z]+)(\s?))=/gi;
211
	var evts = str ? str.match(re) || [] : [];
212
	var ret = [];
213
	var lem = {};
214
	for (var x = 0; x < evts.length; x++) {
215
		if (evts[x].length < 1) {
216
			continue;
217
		}
218
		var cm = evts[x].replace(/\s/, "");
219
		cm = (cm.slice(0, cm.length - 1));
220
		if (!lem[cm]) {
221
			lem[cm] = true;
222
			ret.push(cm);
223
		}
224
	}
225
	return ret;
226
};
227
dojo.declare("dojo.widget.DomWidget", dojo.widget.Widget, function () {
228
	if ((arguments.length > 0) && (typeof arguments[0] == "object")) {
229
		this.create(arguments[0]);
230
	}
231
}, {templateNode:null, templateString:null, templateCssString:null, preventClobber:false, domNode:null, containerNode:null, widgetsInTemplate:false, addChild:function (widget, overrideContainerNode, pos, ref, insertIndex) {
232
	if (!this.isContainer) {
233
		dojo.debug("dojo.widget.DomWidget.addChild() attempted on non-container widget");
234
		return null;
235
	} else {
236
		if (insertIndex == undefined) {
237
			insertIndex = this.children.length;
238
		}
239
		this.addWidgetAsDirectChild(widget, overrideContainerNode, pos, ref, insertIndex);
240
		this.registerChild(widget, insertIndex);
241
	}
242
	return widget;
243
}, addWidgetAsDirectChild:function (widget, overrideContainerNode, pos, ref, insertIndex) {
244
	if ((!this.containerNode) && (!overrideContainerNode)) {
245
		this.containerNode = this.domNode;
246
	}
247
	var cn = (overrideContainerNode) ? overrideContainerNode : this.containerNode;
248
	if (!pos) {
249
		pos = "after";
250
	}
251
	if (!ref) {
252
		if (!cn) {
253
			cn = dojo.body();
254
		}
255
		ref = cn.lastChild;
256
	}
257
	if (!insertIndex) {
258
		insertIndex = 0;
259
	}
260
	widget.domNode.setAttribute("dojoinsertionindex", insertIndex);
261
	if (!ref) {
262
		cn.appendChild(widget.domNode);
263
	} else {
264
		if (pos == "insertAtIndex") {
265
			dojo.dom.insertAtIndex(widget.domNode, ref.parentNode, insertIndex);
266
		} else {
267
			if ((pos == "after") && (ref === cn.lastChild)) {
268
				cn.appendChild(widget.domNode);
269
			} else {
270
				dojo.dom.insertAtPosition(widget.domNode, cn, pos);
271
			}
272
		}
273
	}
274
}, registerChild:function (widget, insertionIndex) {
275
	widget.dojoInsertionIndex = insertionIndex;
276
	var idx = -1;
277
	for (var i = 0; i < this.children.length; i++) {
278
		if (this.children[i].dojoInsertionIndex <= insertionIndex) {
279
			idx = i;
280
		}
281
	}
282
	this.children.splice(idx + 1, 0, widget);
283
	widget.parent = this;
284
	widget.addedTo(this, idx + 1);
285
	delete dojo.widget.manager.topWidgets[widget.widgetId];
286
}, removeChild:function (widget) {
287
	dojo.dom.removeNode(widget.domNode);
288
	return dojo.widget.DomWidget.superclass.removeChild.call(this, widget);
289
}, getFragNodeRef:function (frag) {
290
	if (!frag) {
291
		return null;
292
	}
293
	if (!frag[this.getNamespacedType()]) {
294
		dojo.raise("Error: no frag for widget type " + this.getNamespacedType() + ", id " + this.widgetId + " (maybe a widget has set it's type incorrectly)");
295
	}
296
	return frag[this.getNamespacedType()]["nodeRef"];
297
}, postInitialize:function (args, frag, parentComp) {
298
	var sourceNodeRef = this.getFragNodeRef(frag);
299
	if (parentComp && (parentComp.snarfChildDomOutput || !sourceNodeRef)) {
300
		parentComp.addWidgetAsDirectChild(this, "", "insertAtIndex", "", args["dojoinsertionindex"], sourceNodeRef);
301
	} else {
302
		if (sourceNodeRef) {
303
			if (this.domNode && (this.domNode !== sourceNodeRef)) {
304
				this._sourceNodeRef = dojo.dom.replaceNode(sourceNodeRef, this.domNode);
305
			}
306
		}
307
	}
308
	if (parentComp) {
309
		parentComp.registerChild(this, args.dojoinsertionindex);
310
	} else {
311
		dojo.widget.manager.topWidgets[this.widgetId] = this;
312
	}
313
	if (this.widgetsInTemplate) {
314
		var parser = new dojo.xml.Parse();
315
		var subContainerNode;
316
		var subnodes = this.domNode.getElementsByTagName("*");
317
		for (var i = 0; i < subnodes.length; i++) {
318
			if (subnodes[i].getAttribute("dojoAttachPoint") == "subContainerWidget") {
319
				subContainerNode = subnodes[i];
320
			}
321
			if (subnodes[i].getAttribute("dojoType")) {
322
				subnodes[i].setAttribute("isSubWidget", true);
323
			}
324
		}
325
		if (this.isContainer && !this.containerNode) {
326
			if (subContainerNode) {
327
				var src = this.getFragNodeRef(frag);
328
				if (src) {
329
					dojo.dom.moveChildren(src, subContainerNode);
330
					frag["dojoDontFollow"] = true;
331
				}
332
			} else {
333
				dojo.debug("No subContainerWidget node can be found in template file for widget " + this);
334
			}
335
		}
336
		var templatefrag = parser.parseElement(this.domNode, null, true);
337
		dojo.widget.getParser().createSubComponents(templatefrag, this);
338
		var subwidgets = [];
339
		var stack = [this];
340
		var w;
341
		while ((w = stack.pop())) {
342
			for (var i = 0; i < w.children.length; i++) {
343
				var cwidget = w.children[i];
344
				if (cwidget._processedSubWidgets || !cwidget.extraArgs["issubwidget"]) {
345
					continue;
346
				}
347
				subwidgets.push(cwidget);
348
				if (cwidget.isContainer) {
349
					stack.push(cwidget);
350
				}
351
			}
352
		}
353
		for (var i = 0; i < subwidgets.length; i++) {
354
			var widget = subwidgets[i];
355
			if (widget._processedSubWidgets) {
356
				dojo.debug("This should not happen: widget._processedSubWidgets is already true!");
357
				return;
358
			}
359
			widget._processedSubWidgets = true;
360
			if (widget.extraArgs["dojoattachevent"]) {
361
				var evts = widget.extraArgs["dojoattachevent"].split(";");
362
				for (var j = 0; j < evts.length; j++) {
363
					var thisFunc = null;
364
					var tevt = dojo.string.trim(evts[j]);
365
					if (tevt.indexOf(":") >= 0) {
366
						var funcNameArr = tevt.split(":");
367
						tevt = dojo.string.trim(funcNameArr[0]);
368
						thisFunc = dojo.string.trim(funcNameArr[1]);
369
					}
370
					if (!thisFunc) {
371
						thisFunc = tevt;
372
					}
373
					if (dojo.lang.isFunction(widget[tevt])) {
374
						dojo.event.kwConnect({srcObj:widget, srcFunc:tevt, targetObj:this, targetFunc:thisFunc});
375
					} else {
376
						alert(tevt + " is not a function in widget " + widget);
377
					}
378
				}
379
			}
380
			if (widget.extraArgs["dojoattachpoint"]) {
381
				this[widget.extraArgs["dojoattachpoint"]] = widget;
382
			}
383
		}
384
	}
385
	if (this.isContainer && !frag["dojoDontFollow"]) {
386
		dojo.widget.getParser().createSubComponents(frag, this);
387
	}
388
}, buildRendering:function (args, frag) {
389
	var ts = dojo.widget._templateCache[this.widgetType];
390
	if (args["templatecsspath"]) {
391
		args["templateCssPath"] = args["templatecsspath"];
392
	}
393
	var cpath = args["templateCssPath"] || this.templateCssPath;
394
	if (cpath && !dojo.widget._cssFiles[cpath.toString()]) {
395
		if ((!this.templateCssString) && (cpath)) {
396
			this.templateCssString = dojo.hostenv.getText(cpath);
397
			this.templateCssPath = null;
398
		}
399
		dojo.widget._cssFiles[cpath.toString()] = true;
400
	}
401
	if ((this["templateCssString"]) && (!dojo.widget._cssStrings[this.templateCssString])) {
402
		dojo.html.insertCssText(this.templateCssString, null, cpath);
403
		dojo.widget._cssStrings[this.templateCssString] = true;
404
	}
405
	if ((!this.preventClobber) && ((this.templatePath) || (this.templateNode) || ((this["templateString"]) && (this.templateString.length)) || ((typeof ts != "undefined") && ((ts["string"]) || (ts["node"]))))) {
406
		this.buildFromTemplate(args, frag);
407
	} else {
408
		this.domNode = this.getFragNodeRef(frag);
409
	}
410
	this.fillInTemplate(args, frag);
411
}, buildFromTemplate:function (args, frag) {
412
	var avoidCache = false;
413
	if (args["templatepath"]) {
414
		args["templatePath"] = args["templatepath"];
415
	}
416
	dojo.widget.fillFromTemplateCache(this, args["templatePath"], null, avoidCache);
417
	var ts = dojo.widget._templateCache[this.templatePath ? this.templatePath.toString() : this.widgetType];
418
	if ((ts) && (!avoidCache)) {
419
		if (!this.templateString.length) {
420
			this.templateString = ts["string"];
421
		}
422
		if (!this.templateNode) {
423
			this.templateNode = ts["node"];
424
		}
425
	}
426
	var matches = false;
427
	var node = null;
428
	var tstr = this.templateString;
429
	if ((!this.templateNode) && (this.templateString)) {
430
		matches = this.templateString.match(/\$\{([^\}]+)\}/g);
431
		if (matches) {
432
			var hash = this.strings || {};
433
			for (var key in dojo.widget.defaultStrings) {
434
				if (dojo.lang.isUndefined(hash[key])) {
435
					hash[key] = dojo.widget.defaultStrings[key];
436
				}
437
			}
438
			for (var i = 0; i < matches.length; i++) {
439
				var key = matches[i];
440
				key = key.substring(2, key.length - 1);
441
				var kval = (key.substring(0, 5) == "this.") ? dojo.lang.getObjPathValue(key.substring(5), this) : hash[key];
442
				var value;
443
				if ((kval) || (dojo.lang.isString(kval))) {
444
					value = new String((dojo.lang.isFunction(kval)) ? kval.call(this, key, this.templateString) : kval);
445
					while (value.indexOf("\"") > -1) {
446
						value = value.replace("\"", "&quot;");
447
					}
448
					tstr = tstr.replace(matches[i], value);
449
				}
450
			}
451
		} else {
452
			this.templateNode = this.createNodesFromText(this.templateString, true)[0];
453
			if (!avoidCache) {
454
				ts.node = this.templateNode;
455
			}
456
		}
457
	}
458
	if ((!this.templateNode) && (!matches)) {
459
		dojo.debug("DomWidget.buildFromTemplate: could not create template");
460
		return false;
461
	} else {
462
		if (!matches) {
463
			node = this.templateNode.cloneNode(true);
464
			if (!node) {
465
				return false;
466
			}
467
		} else {
468
			node = this.createNodesFromText(tstr, true)[0];
469
		}
470
	}
471
	this.domNode = node;
472
	this.attachTemplateNodes();
473
	if (this.isContainer && this.containerNode) {
474
		var src = this.getFragNodeRef(frag);
475
		if (src) {
476
			dojo.dom.moveChildren(src, this.containerNode);
477
		}
478
	}
479
}, attachTemplateNodes:function (baseNode, targetObj) {
480
	if (!baseNode) {
481
		baseNode = this.domNode;
482
	}
483
	if (!targetObj) {
484
		targetObj = this;
485
	}
486
	return dojo.widget.attachTemplateNodes(baseNode, targetObj, dojo.widget.getDojoEventsFromStr(this.templateString));
487
}, fillInTemplate:function () {
488
}, destroyRendering:function () {
489
	try {
490
		dojo.dom.destroyNode(this.domNode);
491
		delete this.domNode;
492
	}
493
	catch (e) {
494
	}
495
	if (this._sourceNodeRef) {
496
		try {
497
			dojo.dom.destroyNode(this._sourceNodeRef);
498
		}
499
		catch (e) {
500
		}
501
	}
502
}, createNodesFromText:function () {
503
	dojo.unimplemented("dojo.widget.DomWidget.createNodesFromText");
504
}});
505