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.Parse");
12
dojo.require("dojo.widget.Manager");
13
dojo.require("dojo.dom");
14
dojo.widget.Parse = function (fragment) {
15
	this.propertySetsList = [];
16
	this.fragment = fragment;
17
	this.createComponents = function (frag, parentComp) {
18
		var comps = [];
19
		var built = false;
20
		try {
21
			if (frag && frag.tagName && (frag != frag.nodeRef)) {
22
				var djTags = dojo.widget.tags;
23
				var tna = String(frag.tagName).split(";");
24
				for (var x = 0; x < tna.length; x++) {
25
					var ltn = tna[x].replace(/^\s+|\s+$/g, "").toLowerCase();
26
					frag.tagName = ltn;
27
					var ret;
28
					if (djTags[ltn]) {
29
						built = true;
30
						ret = djTags[ltn](frag, this, parentComp, frag.index);
31
						comps.push(ret);
32
					} else {
33
						if (ltn.indexOf(":") == -1) {
34
							ltn = "dojo:" + ltn;
35
						}
36
						ret = dojo.widget.buildWidgetFromParseTree(ltn, frag, this, parentComp, frag.index);
37
						if (ret) {
38
							built = true;
39
							comps.push(ret);
40
						}
41
					}
42
				}
43
			}
44
		}
45
		catch (e) {
46
			dojo.debug("dojo.widget.Parse: error:", e);
47
		}
48
		if (!built) {
49
			comps = comps.concat(this.createSubComponents(frag, parentComp));
50
		}
51
		return comps;
52
	};
53
	this.createSubComponents = function (fragment, parentComp) {
54
		var frag, comps = [];
55
		for (var item in fragment) {
56
			frag = fragment[item];
57
			if (frag && typeof frag == "object" && (frag != fragment.nodeRef) && (frag != fragment.tagName) && (!dojo.dom.isNode(frag))) {
58
				comps = comps.concat(this.createComponents(frag, parentComp));
59
			}
60
		}
61
		return comps;
62
	};
63
	this.parsePropertySets = function (fragment) {
64
		return [];
65
	};
66
	this.parseProperties = function (fragment) {
67
		var properties = {};
68
		for (var item in fragment) {
69
			if ((fragment[item] == fragment.tagName) || (fragment[item] == fragment.nodeRef)) {
70
			} else {
71
				var frag = fragment[item];
72
				if (frag.tagName && dojo.widget.tags[frag.tagName.toLowerCase()]) {
73
				} else {
74
					if (frag[0] && frag[0].value != "" && frag[0].value != null) {
75
						try {
76
							if (item.toLowerCase() == "dataprovider") {
77
								var _this = this;
78
								this.getDataProvider(_this, frag[0].value);
79
								properties.dataProvider = this.dataProvider;
80
							}
81
							properties[item] = frag[0].value;
82
							var nestedProperties = this.parseProperties(frag);
83
							for (var property in nestedProperties) {
84
								properties[property] = nestedProperties[property];
85
							}
86
						}
87
						catch (e) {
88
							dojo.debug(e);
89
						}
90
					}
91
				}
92
				switch (item.toLowerCase()) {
93
				  case "checked":
94
				  case "disabled":
95
					if (typeof properties[item] != "boolean") {
96
						properties[item] = true;
97
					}
98
					break;
99
				}
100
			}
101
		}
102
		return properties;
103
	};
104
	this.getDataProvider = function (objRef, dataUrl) {
105
		dojo.io.bind({url:dataUrl, load:function (type, evaldObj) {
106
			if (type == "load") {
107
				objRef.dataProvider = evaldObj;
108
			}
109
		}, mimetype:"text/javascript", sync:true});
110
	};
111
	this.getPropertySetById = function (propertySetId) {
112
		for (var x = 0; x < this.propertySetsList.length; x++) {
113
			if (propertySetId == this.propertySetsList[x]["id"][0].value) {
114
				return this.propertySetsList[x];
115
			}
116
		}
117
		return "";
118
	};
119
	this.getPropertySetsByType = function (componentType) {
120
		var propertySets = [];
121
		for (var x = 0; x < this.propertySetsList.length; x++) {
122
			var cpl = this.propertySetsList[x];
123
			var cpcc = cpl.componentClass || cpl.componentType || null;
124
			var propertySetId = this.propertySetsList[x]["id"][0].value;
125
			if (cpcc && (propertySetId == cpcc[0].value)) {
126
				propertySets.push(cpl);
127
			}
128
		}
129
		return propertySets;
130
	};
131
	this.getPropertySets = function (fragment) {
132
		var ppl = "dojo:propertyproviderlist";
133
		var propertySets = [];
134
		var tagname = fragment.tagName;
135
		if (fragment[ppl]) {
136
			var propertyProviderIds = fragment[ppl].value.split(" ");
137
			for (var propertySetId in propertyProviderIds) {
138
				if ((propertySetId.indexOf("..") == -1) && (propertySetId.indexOf("://") == -1)) {
139
					var propertySet = this.getPropertySetById(propertySetId);
140
					if (propertySet != "") {
141
						propertySets.push(propertySet);
142
					}
143
				} else {
144
				}
145
			}
146
		}
147
		return this.getPropertySetsByType(tagname).concat(propertySets);
148
	};
149
	this.createComponentFromScript = function (nodeRef, componentName, properties, ns) {
150
		properties.fastMixIn = true;
151
		var ltn = (ns || "dojo") + ":" + componentName.toLowerCase();
152
		if (dojo.widget.tags[ltn]) {
153
			return [dojo.widget.tags[ltn](properties, this, null, null, properties)];
154
		}
155
		return [dojo.widget.buildWidgetFromParseTree(ltn, properties, this, null, null, properties)];
156
	};
157
};
158
dojo.widget._parser_collection = {"dojo":new dojo.widget.Parse()};
159
dojo.widget.getParser = function (name) {
160
	if (!name) {
161
		name = "dojo";
162
	}
163
	if (!this._parser_collection[name]) {
164
		this._parser_collection[name] = new dojo.widget.Parse();
165
	}
166
	return this._parser_collection[name];
167
};
168
dojo.widget.createWidget = function (name, props, refNode, position) {
169
	var isNode = false;
170
	var isNameStr = (typeof name == "string");
171
	if (isNameStr) {
172
		var pos = name.indexOf(":");
173
		var ns = (pos > -1) ? name.substring(0, pos) : "dojo";
174
		if (pos > -1) {
175
			name = name.substring(pos + 1);
176
		}
177
		var lowerCaseName = name.toLowerCase();
178
		var namespacedName = ns + ":" + lowerCaseName;
179
		isNode = (dojo.byId(name) && !dojo.widget.tags[namespacedName]);
180
	}
181
	if ((arguments.length == 1) && (isNode || !isNameStr)) {
182
		var xp = new dojo.xml.Parse();
183
		var tn = isNode ? dojo.byId(name) : name;
184
		return dojo.widget.getParser().createComponents(xp.parseElement(tn, null, true))[0];
185
	}
186
	function fromScript(placeKeeperNode, name, props, ns) {
187
		props[namespacedName] = {dojotype:[{value:lowerCaseName}], nodeRef:placeKeeperNode, fastMixIn:true};
188
		props.ns = ns;
189
		return dojo.widget.getParser().createComponentFromScript(placeKeeperNode, name, props, ns);
190
	}
191
	props = props || {};
192
	var notRef = false;
193
	var tn = null;
194
	var h = dojo.render.html.capable;
195
	if (h) {
196
		tn = document.createElement("span");
197
	}
198
	if (!refNode) {
199
		notRef = true;
200
		refNode = tn;
201
		if (h) {
202
			dojo.body().appendChild(refNode);
203
		}
204
	} else {
205
		if (position) {
206
			dojo.dom.insertAtPosition(tn, refNode, position);
207
		} else {
208
			tn = refNode;
209
		}
210
	}
211
	var widgetArray = fromScript(tn, name.toLowerCase(), props, ns);
212
	if ((!widgetArray) || (!widgetArray[0]) || (typeof widgetArray[0].widgetType == "undefined")) {
213
		throw new Error("createWidget: Creation of \"" + name + "\" widget failed.");
214
	}
215
	try {
216
		if (notRef && widgetArray[0].domNode.parentNode) {
217
			widgetArray[0].domNode.parentNode.removeChild(widgetArray[0].domNode);
218
		}
219
	}
220
	catch (e) {
221
		dojo.debug(e);
222
	}
223
	return widgetArray[0];
224
};
225