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