| 2150 |
mathias |
1 |
if(!dojo._hasResource["dojox.wire.ml.util"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
2 |
dojo._hasResource["dojox.wire.ml.util"] = true;
|
|
|
3 |
dojo.provide("dojox.wire.ml.util");
|
|
|
4 |
|
|
|
5 |
dojo.require("dojox.data.dom");
|
|
|
6 |
dojo.require("dojox.wire.Wire");
|
|
|
7 |
|
|
|
8 |
dojox.wire.ml._getValue = function(/*String*/source, /*Array*/args){
|
|
|
9 |
// summary:
|
|
|
10 |
// Return a value
|
|
|
11 |
// description:
|
|
|
12 |
// This method obtains an object by an ID of a widget or an DOM
|
|
|
13 |
// element.
|
|
|
14 |
// If 'source' specifies a dotted notation to its property, a Wire is
|
|
|
15 |
// used to get the object property.
|
|
|
16 |
// If 'source' starts with "arguments", 'args' is used as a root
|
|
|
17 |
// object for the Wire.
|
|
|
18 |
// source:
|
|
|
19 |
// A string to specify an object and its property
|
|
|
20 |
// args:
|
|
|
21 |
// An optional arguments array
|
|
|
22 |
// returns:
|
|
|
23 |
// A value
|
|
|
24 |
if(!source){
|
|
|
25 |
return undefined; //undefined
|
|
|
26 |
}
|
|
|
27 |
var property = undefined;
|
|
|
28 |
if(args && source.length >= 9 && source.substring(0, 9) == "arguments"){
|
|
|
29 |
property = source.substring(9);
|
|
|
30 |
return new dojox.wire.Wire({property: property}).getValue(args);
|
|
|
31 |
}
|
|
|
32 |
var i = source.indexOf('.');
|
|
|
33 |
if(i >= 0){
|
|
|
34 |
property = source.substring(i + 1);
|
|
|
35 |
source = source.substring(0, i);
|
|
|
36 |
}
|
|
|
37 |
var object = (dijit.byId(source) || dojo.byId(source) || dojo.getObject(source));
|
|
|
38 |
if(!object){
|
|
|
39 |
return undefined; //undefined
|
|
|
40 |
}
|
|
|
41 |
if(!property){
|
|
|
42 |
return object; //Object
|
|
|
43 |
}else{
|
|
|
44 |
return new dojox.wire.Wire({object: object, property: property}).getValue(); //anything
|
|
|
45 |
}
|
|
|
46 |
};
|
|
|
47 |
|
|
|
48 |
dojox.wire.ml._setValue = function(/*String*/target, /*anything*/value){
|
|
|
49 |
// summary:
|
|
|
50 |
// Store a value
|
|
|
51 |
// description:
|
|
|
52 |
// This method stores a value by an ID of a widget or an DOM
|
|
|
53 |
// element with a dotted notation to its property, using a Wire.
|
|
|
54 |
// target:
|
|
|
55 |
// A string to specify an object and its property
|
|
|
56 |
// value:
|
|
|
57 |
// A value
|
|
|
58 |
if(!target){
|
|
|
59 |
return; //undefined
|
|
|
60 |
}
|
|
|
61 |
var i = target.indexOf('.');
|
|
|
62 |
if(i < 0){
|
|
|
63 |
return; //undefined
|
|
|
64 |
}
|
|
|
65 |
var object = this._getValue(target.substring(0, i));
|
|
|
66 |
if(!object){
|
|
|
67 |
return; //undefined
|
|
|
68 |
}
|
|
|
69 |
var property = target.substring(i + 1);
|
|
|
70 |
new dojox.wire.Wire({object: object, property: property}).setValue(value);
|
|
|
71 |
};
|
|
|
72 |
|
|
|
73 |
dojo.declare("dojox.wire.ml.XmlElement", null, {
|
|
|
74 |
// summary:
|
|
|
75 |
// An object wrapping an XML element
|
|
|
76 |
// description:
|
|
|
77 |
// This class represents an XML element.
|
|
|
78 |
|
|
|
79 |
constructor: function(/*Element||String*/element){
|
|
|
80 |
// summary:
|
|
|
81 |
// Initialize with an XML element or a tag name
|
|
|
82 |
// element:
|
|
|
83 |
// An XML element or a tag name
|
|
|
84 |
if(dojo.isString(element)){
|
|
|
85 |
element = this._getDocument().createElement(element);
|
|
|
86 |
}
|
|
|
87 |
this.element = element;
|
|
|
88 |
},
|
|
|
89 |
getPropertyValue: function(/*String*/property){
|
|
|
90 |
// summary:
|
|
|
91 |
// Return a property value
|
|
|
92 |
// description:
|
|
|
93 |
// If 'property' starts with '@', the attribute value is returned.
|
|
|
94 |
// If 'property' specifies "text()", the value of the first child
|
|
|
95 |
// text is returned.
|
|
|
96 |
// Otherwise, child elements of the tag name specified with
|
|
|
97 |
// 'property' are returned.
|
|
|
98 |
// property:
|
|
|
99 |
// A property name
|
|
|
100 |
// returns:
|
|
|
101 |
// A property value
|
|
|
102 |
var value = undefined;
|
|
|
103 |
if(!this.element){
|
|
|
104 |
return value; //undefined
|
|
|
105 |
}
|
|
|
106 |
if(!property){
|
|
|
107 |
return value; //undefined
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
if(property.charAt(0) == '@'){
|
|
|
111 |
var attribute = property.substring(1);
|
|
|
112 |
value = this.element.getAttribute(attribute);
|
|
|
113 |
}else if(property == "text()"){
|
|
|
114 |
var text = this.element.firstChild;
|
|
|
115 |
if(text){
|
|
|
116 |
value = text.nodeValue;
|
|
|
117 |
}
|
|
|
118 |
}else{ // child elements
|
|
|
119 |
var elements = [];
|
|
|
120 |
for(var i = 0; i < this.element.childNodes.length; i++){
|
|
|
121 |
var child = this.element.childNodes[i];
|
|
|
122 |
if(child.nodeType === 1 /* ELEMENT_NODE */ && child.nodeName == property){
|
|
|
123 |
elements.push(new dojox.wire.ml.XmlElement(child));
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
if(elements.length > 0){
|
|
|
127 |
if(elements.length === 1){
|
|
|
128 |
value = elements[0];
|
|
|
129 |
}else{
|
|
|
130 |
value = elements;
|
|
|
131 |
}
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
return value; //String||Array||XmlElement
|
|
|
135 |
},
|
|
|
136 |
|
|
|
137 |
setPropertyValue: function(/*String*/property, /*String||Array||XmlElement*/value){
|
|
|
138 |
// summary:
|
|
|
139 |
// Store a property value
|
|
|
140 |
// description:
|
|
|
141 |
// If 'property' starts with '@', 'value' is set to the attribute.
|
|
|
142 |
// If 'property' specifies "text()", 'value' is set as the first
|
|
|
143 |
// child text.
|
|
|
144 |
// If 'value' is a string, a child element of the tag name
|
|
|
145 |
// specified with 'property' is created and 'value' is set as
|
|
|
146 |
// the first child text of the child element.
|
|
|
147 |
// Otherwise, 'value' is set to as child elements.
|
|
|
148 |
// property:
|
|
|
149 |
// A property name
|
|
|
150 |
// value:
|
|
|
151 |
// A property value
|
|
|
152 |
if(!this.element){
|
|
|
153 |
return; //undefined
|
|
|
154 |
}
|
|
|
155 |
if(!property){
|
|
|
156 |
return; //undefined
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
if(property.charAt(0) == '@'){
|
|
|
160 |
var attribute = property.substring(1);
|
|
|
161 |
if(value){
|
|
|
162 |
this.element.setAttribute(attribute, value);
|
|
|
163 |
}else{
|
|
|
164 |
this.element.removeAttribute(attribute);
|
|
|
165 |
}
|
|
|
166 |
}else if(property == "text()"){
|
|
|
167 |
while(this.element.firstChild){
|
|
|
168 |
this.element.removeChild(this.element.firstChild);
|
|
|
169 |
}
|
|
|
170 |
if(value){
|
|
|
171 |
var text = this._getDocument().createTextNode(value);
|
|
|
172 |
this.element.appendChild(text);
|
|
|
173 |
}
|
|
|
174 |
}else{ // child elements
|
|
|
175 |
var nextChild = null;
|
|
|
176 |
for(var i = this.element.childNodes.length - 1; i >= 0; i--){
|
|
|
177 |
var child = this.element.childNodes[i];
|
|
|
178 |
if(child.nodeType === 1 /* ELEMENT_NODE */ && child.nodeName == property){
|
|
|
179 |
if(!nextChild){
|
|
|
180 |
nextChild = child.nextSibling;
|
|
|
181 |
}
|
|
|
182 |
this.element.removeChild(child);
|
|
|
183 |
}
|
|
|
184 |
}
|
|
|
185 |
if(value){
|
|
|
186 |
if(dojo.isArray(value)){
|
|
|
187 |
for(var i in value){
|
|
|
188 |
var e = value[i];
|
|
|
189 |
if(e.element){
|
|
|
190 |
this.element.insertBefore(e.element, nextChild);
|
|
|
191 |
}
|
|
|
192 |
}
|
|
|
193 |
}else if(value instanceof dojox.wire.ml.XmlElement){
|
|
|
194 |
if(value.element){
|
|
|
195 |
this.element.insertBefore(value.element, nextChild);
|
|
|
196 |
}
|
|
|
197 |
}else{ // assume string
|
|
|
198 |
var child = this._getDocument().createElement(property);
|
|
|
199 |
var text = this._getDocument().createTextNode(value);
|
|
|
200 |
child.appendChild(text);
|
|
|
201 |
this.element.insertBefore(child, nextChild);
|
|
|
202 |
}
|
|
|
203 |
}
|
|
|
204 |
}
|
|
|
205 |
},
|
|
|
206 |
|
|
|
207 |
toString: function(){
|
|
|
208 |
// summary:
|
|
|
209 |
// Return a value of the first text child of the element
|
|
|
210 |
// description:
|
|
|
211 |
// A value of the first text child of the element is returned.
|
|
|
212 |
// returns:
|
|
|
213 |
// A value of the first text child of the element
|
|
|
214 |
var s = "";
|
|
|
215 |
if(this.element){
|
|
|
216 |
var text = this.element.firstChild;
|
|
|
217 |
if(text){
|
|
|
218 |
s = text.nodeValue;
|
|
|
219 |
}
|
|
|
220 |
}
|
|
|
221 |
return s; //String
|
|
|
222 |
},
|
|
|
223 |
|
|
|
224 |
toObject: function(){
|
|
|
225 |
// summary:
|
|
|
226 |
// Return an object representation of the element
|
|
|
227 |
// description:
|
|
|
228 |
// An object with properties for child elements, attributes and
|
|
|
229 |
// text is returned.
|
|
|
230 |
// returns:
|
|
|
231 |
// An object representation of the element
|
|
|
232 |
if(!this.element){
|
|
|
233 |
return null; //null
|
|
|
234 |
}
|
|
|
235 |
var text = "";
|
|
|
236 |
var obj = {};
|
|
|
237 |
var elements = 0;
|
|
|
238 |
for(var i = 0; i < this.element.childNodes.length; i++){
|
|
|
239 |
var child = this.element.childNodes[i];
|
|
|
240 |
if(child.nodeType === 1 /* ELEMENT_NODE */){
|
|
|
241 |
elements++;
|
|
|
242 |
var o = new dojox.wire.ml.XmlElement(child).toObject();
|
|
|
243 |
var name = child.nodeName;
|
|
|
244 |
var p = obj[name];
|
|
|
245 |
if(!p){
|
|
|
246 |
obj[name] = o;
|
|
|
247 |
}else if(dojo.isArray(p)){
|
|
|
248 |
p.push(o);
|
|
|
249 |
}else{
|
|
|
250 |
obj[name] = [p, o]; // make them array
|
|
|
251 |
}
|
|
|
252 |
}else if(child.nodeType === 3 /* TEXT_NODE */ ||
|
|
|
253 |
child.nodeType === 4 /* CDATA_SECTION_NODE */){
|
|
|
254 |
text += child.nodeValue;
|
|
|
255 |
}
|
|
|
256 |
}
|
|
|
257 |
var attributes = 0;
|
|
|
258 |
if(this.element.nodeType === 1 /* ELEMENT_NODE */){
|
|
|
259 |
attributes = this.element.attributes.length;
|
|
|
260 |
for(var i = 0; i < attributes; i++){
|
|
|
261 |
var attr = this.element.attributes[i];
|
|
|
262 |
obj["@" + attr.nodeName] = attr.nodeValue;
|
|
|
263 |
}
|
|
|
264 |
}
|
|
|
265 |
if(elements === 0){
|
|
|
266 |
if(attributes === 0){
|
|
|
267 |
// text only
|
|
|
268 |
return text; //String
|
|
|
269 |
}
|
|
|
270 |
// text with attributes
|
|
|
271 |
obj["text()"] = text;
|
|
|
272 |
}
|
|
|
273 |
// else ignore text
|
|
|
274 |
return obj; //Object
|
|
|
275 |
},
|
|
|
276 |
|
|
|
277 |
_getDocument: function(){
|
|
|
278 |
// summary:
|
|
|
279 |
// Return a DOM document
|
|
|
280 |
// description:
|
|
|
281 |
// If 'element' is specified, a DOM document of the element is
|
|
|
282 |
// returned.
|
|
|
283 |
// Otherwise, a DOM document is created.
|
|
|
284 |
// returns:
|
|
|
285 |
// A DOM document
|
|
|
286 |
if(this.element){
|
|
|
287 |
return (this.element.nodeType == 9 /* DOCUMENT_NODE */ ?
|
|
|
288 |
this.element : this.element.ownerDocument); //Document
|
|
|
289 |
}else{
|
|
|
290 |
return dojox.data.dom.createDocument(); //Document
|
|
|
291 |
}
|
|
|
292 |
}
|
|
|
293 |
});
|
|
|
294 |
|
|
|
295 |
}
|