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