root / trunk / web / dojo / dojox / data / OpmlStore.js @ 12
History | View | Annotate | Download (7.3 KB)
1 |
/*
|
---|---|
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.data.OpmlStore"]){ |
9 |
dojo._hasResource["dojox.data.OpmlStore"]=true; |
10 |
dojo.provide("dojox.data.OpmlStore");
|
11 |
dojo.require("dojo.data.util.filter");
|
12 |
dojo.require("dojo.data.util.simpleFetch");
|
13 |
dojo.declare("dojox.data.OpmlStore",null,{constructor:function(_1){ |
14 |
this._xmlData=null; |
15 |
this._arrayOfTopLevelItems=[];
|
16 |
this._arrayOfAllItems=[];
|
17 |
this._metadataNodes=null; |
18 |
this._loadFinished=false; |
19 |
this.url=_1.url;
|
20 |
this._opmlData=_1.data;
|
21 |
if(_1.label){
|
22 |
this.label=_1.label;
|
23 |
} |
24 |
this._loadInProgress=false; |
25 |
this._queuedFetches=[];
|
26 |
this._identityMap={};
|
27 |
this._identCount=0; |
28 |
this._idProp="_I"; |
29 |
if(_1&&"urlPreventCache" in _1){ |
30 |
this.urlPreventCache=_1.urlPreventCache?true:false; |
31 |
} |
32 |
},label:"text",url:"",urlPreventCache:false,_assertIsItem:function(_2){ |
33 |
if(!this.isItem(_2)){ |
34 |
throw new Error("dojo.data.OpmlStore: a function was passed an item argument that was not an item"); |
35 |
} |
36 |
},_assertIsAttribute:function(_3){ |
37 |
if(!dojo.isString(_3)){
|
38 |
throw new Error("dojox.data.OpmlStore: a function was passed an attribute argument that was not an attribute object nor an attribute name string"); |
39 |
} |
40 |
},_removeChildNodesThatAreNotElementNodes:function(_4,_5){ |
41 |
var _6=_4.childNodes;
|
42 |
if(_6.length===0){ |
43 |
return;
|
44 |
} |
45 |
var _7=[];
|
46 |
var i,_8;
|
47 |
for(i=0;i<_6.length;++i){ |
48 |
_8=_6[i]; |
49 |
if(_8.nodeType!=1){ |
50 |
_7.push(_8); |
51 |
} |
52 |
} |
53 |
for(i=0;i<_7.length;++i){ |
54 |
_8=_7[i]; |
55 |
_4.removeChild(_8); |
56 |
} |
57 |
if(_5){
|
58 |
for(i=0;i<_6.length;++i){ |
59 |
_8=_6[i]; |
60 |
this._removeChildNodesThatAreNotElementNodes(_8,_5);
|
61 |
} |
62 |
} |
63 |
},_processRawXmlTree:function(_9){ |
64 |
this._loadFinished=true; |
65 |
this._xmlData=_9;
|
66 |
var _a=_9.getElementsByTagName("head"); |
67 |
var _b=_a[0]; |
68 |
if(_b){
|
69 |
this._removeChildNodesThatAreNotElementNodes(_b);
|
70 |
this._metadataNodes=_b.childNodes;
|
71 |
} |
72 |
var _c=_9.getElementsByTagName("body"); |
73 |
var _d=_c[0]; |
74 |
if(_d){
|
75 |
this._removeChildNodesThatAreNotElementNodes(_d,true); |
76 |
var _e=_c[0].childNodes; |
77 |
for(var i=0;i<_e.length;++i){ |
78 |
var _f=_e[i];
|
79 |
if(_f.tagName=="outline"){ |
80 |
this._identityMap[this._identCount]=_f; |
81 |
this._identCount++;
|
82 |
this._arrayOfTopLevelItems.push(_f);
|
83 |
this._arrayOfAllItems.push(_f);
|
84 |
this._checkChildNodes(_f);
|
85 |
} |
86 |
} |
87 |
} |
88 |
},_checkChildNodes:function(_10){ |
89 |
if(_10.firstChild){
|
90 |
for(var i=0;i<_10.childNodes.length;i++){ |
91 |
var _11=_10.childNodes[i];
|
92 |
if(_11.tagName=="outline"){ |
93 |
this._identityMap[this._identCount]=_11; |
94 |
this._identCount++;
|
95 |
this._arrayOfAllItems.push(_11);
|
96 |
this._checkChildNodes(_11);
|
97 |
} |
98 |
} |
99 |
} |
100 |
},_getItemsArray:function(_12){ |
101 |
if(_12&&_12.deep){
|
102 |
return this._arrayOfAllItems; |
103 |
} |
104 |
return this._arrayOfTopLevelItems; |
105 |
},getValue:function(_13,_14,_15){ |
106 |
this._assertIsItem(_13);
|
107 |
this._assertIsAttribute(_14);
|
108 |
if(_14=="children"){ |
109 |
return (_13.firstChild||_15);
|
110 |
}else{
|
111 |
var _16=_13.getAttribute(_14);
|
112 |
return (_16!==undefined)?_16:_15; |
113 |
} |
114 |
},getValues:function(_17,_18){ |
115 |
this._assertIsItem(_17);
|
116 |
this._assertIsAttribute(_18);
|
117 |
var _19=[];
|
118 |
if(_18=="children"){ |
119 |
for(var i=0;i<_17.childNodes.length;++i){ |
120 |
_19.push(_17.childNodes[i]); |
121 |
} |
122 |
}else{
|
123 |
if(_17.getAttribute(_18)!==null){ |
124 |
_19.push(_17.getAttribute(_18)); |
125 |
} |
126 |
} |
127 |
return _19;
|
128 |
},getAttributes:function(_1a){ |
129 |
this._assertIsItem(_1a);
|
130 |
var _1b=[];
|
131 |
var _1c=_1a;
|
132 |
var _1d=_1c.attributes;
|
133 |
for(var i=0;i<_1d.length;++i){ |
134 |
var _1e=_1d.item(i);
|
135 |
_1b.push(_1e.nodeName); |
136 |
} |
137 |
if(_1c.childNodes.length>0){ |
138 |
_1b.push("children");
|
139 |
} |
140 |
return _1b;
|
141 |
},hasAttribute:function(_1f,_20){ |
142 |
return (this.getValues(_1f,_20).length>0); |
143 |
},containsValue:function(_21,_22,_23){ |
144 |
var _24=undefined; |
145 |
if(typeof _23==="string"){ |
146 |
_24=dojo.data.util.filter.patternToRegExp(_23,false);
|
147 |
} |
148 |
return this._containsValue(_21,_22,_23,_24); |
149 |
},_containsValue:function(_25,_26,_27,_28){ |
150 |
var _29=this.getValues(_25,_26); |
151 |
for(var i=0;i<_29.length;++i){ |
152 |
var _2a=_29[i];
|
153 |
if(typeof _2a==="string"&&_28){ |
154 |
return (_2a.match(_28)!==null); |
155 |
}else{
|
156 |
if(_27===_2a){
|
157 |
return true; |
158 |
} |
159 |
} |
160 |
} |
161 |
return false; |
162 |
},isItem:function(_2b){ |
163 |
return (_2b&&_2b.nodeType==1&&_2b.tagName=="outline"&&_2b.ownerDocument===this._xmlData); |
164 |
},isItemLoaded:function(_2c){ |
165 |
return this.isItem(_2c); |
166 |
},loadItem:function(_2d){ |
167 |
},getLabel:function(_2e){ |
168 |
if(this.isItem(_2e)){ |
169 |
return this.getValue(_2e,this.label); |
170 |
} |
171 |
return undefined; |
172 |
},getLabelAttributes:function(_2f){ |
173 |
return [this.label]; |
174 |
},_fetchItems:function(_30,_31,_32){ |
175 |
var _33=this; |
176 |
var _34=function(_35,_36){ |
177 |
var _37=null; |
178 |
if(_35.query){
|
179 |
_37=[]; |
180 |
var _38=_35.queryOptions?_35.queryOptions.ignoreCase:false; |
181 |
var _39={};
|
182 |
for(var key in _35.query){ |
183 |
var _3a=_35.query[key];
|
184 |
if(typeof _3a==="string"){ |
185 |
_39[key]=dojo.data.util.filter.patternToRegExp(_3a,_38); |
186 |
} |
187 |
} |
188 |
for(var i=0;i<_36.length;++i){ |
189 |
var _3b=true; |
190 |
var _3c=_36[i];
|
191 |
for(var key in _35.query){ |
192 |
var _3a=_35.query[key];
|
193 |
if(!_33._containsValue(_3c,key,_3a,_39[key])){
|
194 |
_3b=false;
|
195 |
} |
196 |
} |
197 |
if(_3b){
|
198 |
_37.push(_3c); |
199 |
} |
200 |
} |
201 |
}else{
|
202 |
if(_36.length>0){ |
203 |
_37=_36.slice(0,_36.length);
|
204 |
} |
205 |
} |
206 |
_31(_37,_35); |
207 |
}; |
208 |
if(this._loadFinished){ |
209 |
_34(_30,this._getItemsArray(_30.queryOptions));
|
210 |
}else{
|
211 |
if(this._loadInProgress){ |
212 |
this._queuedFetches.push({args:_30,filter:_34}); |
213 |
}else{
|
214 |
if(this.url!==""){ |
215 |
this._loadInProgress=true; |
216 |
var _3d={url:_33.url,handleAs:"xml",preventCache:_33.urlPreventCache}; |
217 |
var _3e=dojo.xhrGet(_3d);
|
218 |
_3e.addCallback(function(_3f){
|
219 |
_33._processRawXmlTree(_3f); |
220 |
_34(_30,_33._getItemsArray(_30.queryOptions)); |
221 |
_33._handleQueuedFetches(); |
222 |
}); |
223 |
_3e.addErrback(function(_40){
|
224 |
throw _40;
|
225 |
}); |
226 |
}else{
|
227 |
if(this._opmlData){ |
228 |
this._processRawXmlTree(this._opmlData); |
229 |
this._opmlData=null; |
230 |
_34(_30,this._getItemsArray(_30.queryOptions));
|
231 |
}else{
|
232 |
throw new Error("dojox.data.OpmlStore: No OPML source data was provided as either URL or XML data input."); |
233 |
} |
234 |
} |
235 |
} |
236 |
} |
237 |
},getFeatures:function(){ |
238 |
var _41={"dojo.data.api.Read":true,"dojo.data.api.Identity":true}; |
239 |
return _41;
|
240 |
},getIdentity:function(_42){ |
241 |
if(this.isItem(_42)){ |
242 |
for(var i in this._identityMap){ |
243 |
if(this._identityMap[i]===_42){ |
244 |
return i;
|
245 |
} |
246 |
} |
247 |
} |
248 |
return null; |
249 |
},fetchItemByIdentity:function(_43){ |
250 |
if(!this._loadFinished){ |
251 |
var _44=this; |
252 |
if(this.url!==""){ |
253 |
if(this._loadInProgress){ |
254 |
this._queuedFetches.push({args:_43}); |
255 |
}else{
|
256 |
this._loadInProgress=true; |
257 |
var _45={url:_44.url,handleAs:"xml"}; |
258 |
var _46=dojo.xhrGet(_45);
|
259 |
_46.addCallback(function(_47){
|
260 |
var _48=_43.scope?_43.scope:dojo.global;
|
261 |
try{
|
262 |
_44._processRawXmlTree(_47); |
263 |
var _49=_44._identityMap[_43.identity];
|
264 |
if(!_44.isItem(_49)){
|
265 |
_49=null;
|
266 |
} |
267 |
if(_43.onItem){
|
268 |
_43.onItem.call(_48,_49); |
269 |
} |
270 |
_44._handleQueuedFetches(); |
271 |
} |
272 |
catch(error){
|
273 |
if(_43.onError){
|
274 |
_43.onError.call(_48,error); |
275 |
} |
276 |
} |
277 |
}); |
278 |
_46.addErrback(function(_4a){
|
279 |
this._loadInProgress=false; |
280 |
if(_43.onError){
|
281 |
var _4b=_43.scope?_43.scope:dojo.global;
|
282 |
_43.onError.call(_4b,_4a); |
283 |
} |
284 |
}); |
285 |
} |
286 |
}else{
|
287 |
if(this._opmlData){ |
288 |
this._processRawXmlTree(this._opmlData); |
289 |
this._opmlData=null; |
290 |
var _4c=this._identityMap[_43.identity]; |
291 |
if(!_44.isItem(_4c)){
|
292 |
_4c=null;
|
293 |
} |
294 |
if(_43.onItem){
|
295 |
var _4d=_43.scope?_43.scope:dojo.global;
|
296 |
_43.onItem.call(_4d,_4c); |
297 |
} |
298 |
} |
299 |
} |
300 |
}else{
|
301 |
var _4c=this._identityMap[_43.identity]; |
302 |
if(!this.isItem(_4c)){ |
303 |
_4c=null;
|
304 |
} |
305 |
if(_43.onItem){
|
306 |
var _4d=_43.scope?_43.scope:dojo.global;
|
307 |
_43.onItem.call(_4d,_4c); |
308 |
} |
309 |
} |
310 |
},getIdentityAttributes:function(_4e){ |
311 |
return null; |
312 |
},_handleQueuedFetches:function(){ |
313 |
if(this._queuedFetches.length>0){ |
314 |
for(var i=0;i<this._queuedFetches.length;i++){ |
315 |
var _4f=this._queuedFetches[i]; |
316 |
var _50=_4f.args;
|
317 |
var _51=_4f.filter;
|
318 |
if(_51){
|
319 |
_51(_50,this._getItemsArray(_50.queryOptions));
|
320 |
}else{
|
321 |
this.fetchItemByIdentity(_50);
|
322 |
} |
323 |
} |
324 |
this._queuedFetches=[];
|
325 |
} |
326 |
},close:function(_52){ |
327 |
}}); |
328 |
dojo.extend(dojox.data.OpmlStore,dojo.data.util.simpleFetch); |
329 |
} |