Debian Codegraph @

dojox/wire/ml/util.js

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