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