root / trunk / web / dojo / dojox / wire / ml / util.js @ 10
History | View | Annotate | Download (3.8 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.wire.ml.util"]){ |
||
9 | dojo._hasResource["dojox.wire.ml.util"]=true; |
||
10 | dojo.provide("dojox.wire.ml.util");
|
||
11 | dojo.require("dojox.xml.parser");
|
||
12 | dojo.require("dojox.wire.Wire");
|
||
13 | dojox.wire.ml._getValue=function(_1,_2){ |
||
14 | if(!_1){
|
||
15 | return undefined; |
||
16 | } |
||
17 | var _3=undefined; |
||
18 | if(_2&&_1.length>=9&&_1.substring(0,9)=="arguments"){ |
||
19 | _3=_1.substring(9);
|
||
20 | return new dojox.wire.Wire({property:_3}).getValue(_2); |
||
21 | } |
||
22 | var i=_1.indexOf("."); |
||
23 | if(i>=0){ |
||
24 | _3=_1.substring(i+1);
|
||
25 | _1=_1.substring(0,i);
|
||
26 | } |
||
27 | var _4=(dijit.byId(_1)||dojo.byId(_1)||dojo.getObject(_1));
|
||
28 | if(!_4){
|
||
29 | return undefined; |
||
30 | } |
||
31 | if(!_3){
|
||
32 | return _4;
|
||
33 | }else{
|
||
34 | return new dojox.wire.Wire({object:_4,property:_3}).getValue(); |
||
35 | } |
||
36 | }; |
||
37 | dojox.wire.ml._setValue=function(_5,_6){ |
||
38 | if(!_5){
|
||
39 | return;
|
||
40 | } |
||
41 | var i=_5.indexOf("."); |
||
42 | if(i<0){ |
||
43 | return;
|
||
44 | } |
||
45 | var _7=this._getValue(_5.substring(0,i)); |
||
46 | if(!_7){
|
||
47 | return;
|
||
48 | } |
||
49 | var _8=_5.substring(i+1); |
||
50 | var _9=new dojox.wire.Wire({object:_7,property:_8}).setValue(_6); |
||
51 | }; |
||
52 | dojo.declare("dojox.wire.ml.XmlElement",null,{constructor:function(_a){ |
||
53 | if(dojo.isString(_a)){
|
||
54 | _a=this._getDocument().createElement(_a);
|
||
55 | } |
||
56 | this.element=_a;
|
||
57 | },getPropertyValue:function(_b){ |
||
58 | var _c=undefined; |
||
59 | if(!this.element){ |
||
60 | return _c;
|
||
61 | } |
||
62 | if(!_b){
|
||
63 | return _c;
|
||
64 | } |
||
65 | if(_b.charAt(0)=="@"){ |
||
66 | var _d=_b.substring(1); |
||
67 | _c=this.element.getAttribute(_d);
|
||
68 | }else{
|
||
69 | if(_b=="text()"){ |
||
70 | var _e=this.element.firstChild; |
||
71 | if(_e){
|
||
72 | _c=_e.nodeValue; |
||
73 | } |
||
74 | }else{
|
||
75 | var _f=[];
|
||
76 | for(var i=0;i<this.element.childNodes.length;i++){ |
||
77 | var _10=this.element.childNodes[i]; |
||
78 | if(_10.nodeType===1&&_10.nodeName==_b){ |
||
79 | _f.push(new dojox.wire.ml.XmlElement(_10));
|
||
80 | } |
||
81 | } |
||
82 | if(_f.length>0){ |
||
83 | if(_f.length===1){ |
||
84 | _c=_f[0];
|
||
85 | }else{
|
||
86 | _c=_f; |
||
87 | } |
||
88 | } |
||
89 | } |
||
90 | } |
||
91 | return _c;
|
||
92 | },setPropertyValue:function(_11,_12){ |
||
93 | var i;
|
||
94 | var _13;
|
||
95 | if(!this.element){ |
||
96 | return;
|
||
97 | } |
||
98 | if(!_11){
|
||
99 | return;
|
||
100 | } |
||
101 | if(_11.charAt(0)=="@"){ |
||
102 | var _14=_11.substring(1); |
||
103 | if(_12){
|
||
104 | this.element.setAttribute(_14,_12);
|
||
105 | }else{
|
||
106 | this.element.removeAttribute(_14);
|
||
107 | } |
||
108 | }else{
|
||
109 | if(_11=="text()"){ |
||
110 | while(this.element.firstChild){ |
||
111 | this.element.removeChild(this.element.firstChild); |
||
112 | } |
||
113 | if(_12){
|
||
114 | _13=this._getDocument().createTextNode(_12);
|
||
115 | this.element.appendChild(_13);
|
||
116 | } |
||
117 | }else{
|
||
118 | var _15=null; |
||
119 | var _16;
|
||
120 | for(i=this.element.childNodes.length-1;i>=0;i--){ |
||
121 | _16=this.element.childNodes[i];
|
||
122 | if(_16.nodeType===1&&_16.nodeName==_11){ |
||
123 | if(!_15){
|
||
124 | _15=_16.nextSibling; |
||
125 | } |
||
126 | this.element.removeChild(_16);
|
||
127 | } |
||
128 | } |
||
129 | if(_12){
|
||
130 | if(dojo.isArray(_12)){
|
||
131 | for(i in _12){ |
||
132 | var e=_12[i];
|
||
133 | if(e.element){
|
||
134 | this.element.insertBefore(e.element,_15);
|
||
135 | } |
||
136 | } |
||
137 | }else{
|
||
138 | if(_12 instanceof dojox.wire.ml.XmlElement){ |
||
139 | if(_12.element){
|
||
140 | this.element.insertBefore(_12.element,_15);
|
||
141 | } |
||
142 | }else{
|
||
143 | _16=this._getDocument().createElement(_11);
|
||
144 | _13=this._getDocument().createTextNode(_12);
|
||
145 | _16.appendChild(_13); |
||
146 | this.element.insertBefore(_16,_15);
|
||
147 | } |
||
148 | } |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | },toString:function(){ |
||
153 | var s=""; |
||
154 | if(this.element){ |
||
155 | var _17=this.element.firstChild; |
||
156 | if(_17){
|
||
157 | s=_17.nodeValue; |
||
158 | } |
||
159 | } |
||
160 | return s;
|
||
161 | },toObject:function(){ |
||
162 | if(!this.element){ |
||
163 | return null; |
||
164 | } |
||
165 | var _18=""; |
||
166 | var obj={};
|
||
167 | var _19=0; |
||
168 | var i;
|
||
169 | for(i=0;i<this.element.childNodes.length;i++){ |
||
170 | var _1a=this.element.childNodes[i]; |
||
171 | if(_1a.nodeType===1){ |
||
172 | _19++; |
||
173 | var o=new dojox.wire.ml.XmlElement(_1a).toObject(); |
||
174 | var _1b=_1a.nodeName;
|
||
175 | var p=obj[_1b];
|
||
176 | if(!p){
|
||
177 | obj[_1b]=o; |
||
178 | }else{
|
||
179 | if(dojo.isArray(p)){
|
||
180 | p.push(o); |
||
181 | }else{
|
||
182 | obj[_1b]=[p,o]; |
||
183 | } |
||
184 | } |
||
185 | }else{
|
||
186 | if(_1a.nodeType===3||_1a.nodeType===4){ |
||
187 | _18+=_1a.nodeValue; |
||
188 | } |
||
189 | } |
||
190 | } |
||
191 | var _1c=0; |
||
192 | if(this.element.nodeType===1){ |
||
193 | _1c=this.element.attributes.length;
|
||
194 | for(i=0;i<_1c;i++){ |
||
195 | var _1d=this.element.attributes[i]; |
||
196 | obj["@"+_1d.nodeName]=_1d.nodeValue;
|
||
197 | } |
||
198 | } |
||
199 | if(_19===0){ |
||
200 | if(_1c===0){ |
||
201 | return _18;
|
||
202 | } |
||
203 | obj["text()"]=_18;
|
||
204 | } |
||
205 | return obj;
|
||
206 | },_getDocument:function(){ |
||
207 | if(this.element){ |
||
208 | return (this.element.nodeType==9?this.element:this.element.ownerDocument); |
||
209 | }else{
|
||
210 | return dojox.xml.parser.parse();
|
||
211 | } |
||
212 | }}); |
||
213 | } |