root / trunk / web / dojo / dojox / data / AtomReadStore.js @ 13
History | View | Annotate | Download (7.27 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.AtomReadStore"]){ |
9 |
dojo._hasResource["dojox.data.AtomReadStore"]=true; |
10 |
dojo.provide("dojox.data.AtomReadStore");
|
11 |
dojo.require("dojo.data.util.simpleFetch");
|
12 |
dojo.require("dojo.data.util.filter");
|
13 |
dojo.require("dojo.date.stamp");
|
14 |
dojo.experimental("dojox.data.AtomReadStore");
|
15 |
dojo.declare("dojox.data.AtomReadStore",null,{constructor:function(_1){ |
16 |
if(_1){
|
17 |
this.url=_1.url;
|
18 |
this.rewriteUrl=_1.rewriteUrl;
|
19 |
this.label=_1.label||this.label; |
20 |
this.sendQuery=(_1.sendQuery||_1.sendquery||this.sendQuery); |
21 |
this.unescapeHTML=_1.unescapeHTML;
|
22 |
if("urlPreventCache" in _1){ |
23 |
this.urlPreventCache=_1.urlPreventCache?true:false; |
24 |
} |
25 |
} |
26 |
if(!this.url){ |
27 |
throw new Error("AtomReadStore: a URL must be specified when creating the data store"); |
28 |
} |
29 |
},url:"",label:"title",sendQuery:false,unescapeHTML:false,urlPreventCache:false,getValue:function(_2,_3,_4){ |
30 |
this._assertIsItem(_2);
|
31 |
this._assertIsAttribute(_3);
|
32 |
this._initItem(_2);
|
33 |
_3=_3.toLowerCase(); |
34 |
if(!_2._attribs[_3]&&!_2._parsed){
|
35 |
this._parseItem(_2);
|
36 |
_2._parsed=true;
|
37 |
} |
38 |
var _5=_2._attribs[_3];
|
39 |
if(!_5&&_3=="summary"){ |
40 |
var _6=this.getValue(_2,"content"); |
41 |
var _7=new RegExp("/(<([^>]+)>)/g","i"); |
42 |
var _8=_6.text.replace(_7,""); |
43 |
_5={text:_8.substring(0,Math.min(400,_8.length)),type:"text"}; |
44 |
_2._attribs[_3]=_5; |
45 |
} |
46 |
if(_5&&this.unescapeHTML){ |
47 |
if((_3=="content"||_3=="summary"||_3=="subtitle")&&!_2["_"+_3+"Escaped"]){ |
48 |
_5.text=this._unescapeHTML(_5.text);
|
49 |
_2["_"+_3+"Escaped"]=true; |
50 |
} |
51 |
} |
52 |
return _5?dojo.isArray(_5)?_5[0]:_5:_4; |
53 |
},getValues:function(_9,_a){ |
54 |
this._assertIsItem(_9);
|
55 |
this._assertIsAttribute(_a);
|
56 |
this._initItem(_9);
|
57 |
_a=_a.toLowerCase(); |
58 |
if(!_9._attribs[_a]){
|
59 |
this._parseItem(_9);
|
60 |
} |
61 |
var _b=_9._attribs[_a];
|
62 |
return _b?((_b.length!==undefined&&typeof (_b)!=="string")?_b:[_b]):undefined; |
63 |
},getAttributes:function(_c){ |
64 |
this._assertIsItem(_c);
|
65 |
if(!_c._attribs){
|
66 |
this._initItem(_c);
|
67 |
this._parseItem(_c);
|
68 |
} |
69 |
var _d=[];
|
70 |
for(var x in _c._attribs){ |
71 |
_d.push(x); |
72 |
} |
73 |
return _d;
|
74 |
},hasAttribute:function(_e,_f){ |
75 |
return (this.getValue(_e,_f)!==undefined); |
76 |
},containsValue:function(_10,_11,_12){ |
77 |
var _13=this.getValues(_10,_11); |
78 |
for(var i=0;i<_13.length;i++){ |
79 |
if((typeof _12==="string")){ |
80 |
if(_13[i].toString&&_13[i].toString()===_12){
|
81 |
return true; |
82 |
} |
83 |
}else{
|
84 |
if(_13[i]===_12){
|
85 |
return true; |
86 |
} |
87 |
} |
88 |
} |
89 |
return false; |
90 |
},isItem:function(_14){ |
91 |
if(_14&&_14.element&&_14.store&&_14.store===this){ |
92 |
return true; |
93 |
} |
94 |
return false; |
95 |
},isItemLoaded:function(_15){ |
96 |
return this.isItem(_15); |
97 |
},loadItem:function(_16){ |
98 |
},getFeatures:function(){ |
99 |
var _17={"dojo.data.api.Read":true}; |
100 |
return _17;
|
101 |
},getLabel:function(_18){ |
102 |
if((this.label!=="")&&this.isItem(_18)){ |
103 |
var _19=this.getValue(_18,this.label); |
104 |
if(_19&&_19.text){
|
105 |
return _19.text;
|
106 |
}else{
|
107 |
if(_19){
|
108 |
return _19.toString();
|
109 |
}else{
|
110 |
return undefined; |
111 |
} |
112 |
} |
113 |
} |
114 |
return undefined; |
115 |
},getLabelAttributes:function(_1a){ |
116 |
if(this.label!==""){ |
117 |
return [this.label]; |
118 |
} |
119 |
return null; |
120 |
},getFeedValue:function(_1b,_1c){ |
121 |
var _1d=this.getFeedValues(_1b,_1c); |
122 |
if(dojo.isArray(_1d)){
|
123 |
return _1d[0]; |
124 |
} |
125 |
return _1d;
|
126 |
},getFeedValues:function(_1e,_1f){ |
127 |
if(!this.doc){ |
128 |
return _1f;
|
129 |
} |
130 |
if(!this._feedMetaData){ |
131 |
this._feedMetaData={element:this.doc.getElementsByTagName("feed")[0],store:this,_attribs:{}}; |
132 |
this._parseItem(this._feedMetaData); |
133 |
} |
134 |
return this._feedMetaData._attribs[_1e]||_1f; |
135 |
},_initItem:function(_20){ |
136 |
if(!_20._attribs){
|
137 |
_20._attribs={}; |
138 |
} |
139 |
},_fetchItems:function(_21,_22,_23){ |
140 |
var url=this._getFetchUrl(_21); |
141 |
if(!url){
|
142 |
_23(new Error("No URL specified.")); |
143 |
return;
|
144 |
} |
145 |
var _24=(!this.sendQuery?_21:null); |
146 |
var _25=this; |
147 |
var _26=function(_27){ |
148 |
_25.doc=_27; |
149 |
var _28=_25._getItems(_27,_24);
|
150 |
var _29=_21.query;
|
151 |
if(_29){
|
152 |
if(_29.id){
|
153 |
_28=dojo.filter(_28,function(_2a){
|
154 |
return (_25.getValue(_2a,"id")==_29.id); |
155 |
}); |
156 |
}else{
|
157 |
if(_29.category){
|
158 |
_28=dojo.filter(_28,function(_2b){
|
159 |
var _2c=_25.getValues(_2b,"category"); |
160 |
if(!_2c){
|
161 |
return false; |
162 |
} |
163 |
return dojo.some(_2c,"return item.term=='"+_29.category+"'"); |
164 |
}); |
165 |
} |
166 |
} |
167 |
} |
168 |
if(_28&&_28.length>0){ |
169 |
_22(_28,_21); |
170 |
}else{
|
171 |
_22([],_21); |
172 |
} |
173 |
}; |
174 |
if(this.doc){ |
175 |
_26(this.doc);
|
176 |
}else{
|
177 |
var _2d={url:url,handleAs:"xml",preventCache:this.urlPreventCache}; |
178 |
var _2e=dojo.xhrGet(_2d);
|
179 |
_2e.addCallback(_26); |
180 |
_2e.addErrback(function(_2f){
|
181 |
_23(_2f,_21); |
182 |
}); |
183 |
} |
184 |
},_getFetchUrl:function(_30){ |
185 |
if(!this.sendQuery){ |
186 |
return this.url; |
187 |
} |
188 |
var _31=_30.query;
|
189 |
if(!_31){
|
190 |
return this.url; |
191 |
} |
192 |
if(dojo.isString(_31)){
|
193 |
return this.url+_31; |
194 |
} |
195 |
var _32=""; |
196 |
for(var _33 in _31){ |
197 |
var _34=_31[_33];
|
198 |
if(_34){
|
199 |
if(_32){
|
200 |
_32+="&";
|
201 |
} |
202 |
_32+=(_33+"="+_34);
|
203 |
} |
204 |
} |
205 |
if(!_32){
|
206 |
return this.url; |
207 |
} |
208 |
var _35=this.url; |
209 |
if(_35.indexOf("?")<0){ |
210 |
_35+="?";
|
211 |
}else{
|
212 |
_35+="&";
|
213 |
} |
214 |
return _35+_32;
|
215 |
},_getItems:function(_36,_37){ |
216 |
if(this._items){ |
217 |
return this._items; |
218 |
} |
219 |
var _38=[];
|
220 |
var _39=[];
|
221 |
if(_36.childNodes.length<1){ |
222 |
this._items=_38;
|
223 |
return _38;
|
224 |
} |
225 |
var _3a=dojo.filter(_36.childNodes,"return item.tagName && item.tagName.toLowerCase() == 'feed'"); |
226 |
var _3b=_37.query;
|
227 |
if(!_3a||_3a.length!=1){ |
228 |
return _38;
|
229 |
} |
230 |
_39=dojo.filter(_3a[0].childNodes,"return item.tagName && item.tagName.toLowerCase() == 'entry'"); |
231 |
if(_37.onBegin){
|
232 |
_37.onBegin(_39.length,this.sendQuery?_37:{});
|
233 |
} |
234 |
for(var i=0;i<_39.length;i++){ |
235 |
var _3c=_39[i];
|
236 |
if(_3c.nodeType!=1){ |
237 |
continue;
|
238 |
} |
239 |
_38.push(this._getItem(_3c));
|
240 |
} |
241 |
this._items=_38;
|
242 |
return _38;
|
243 |
},close:function(_3d){ |
244 |
},_getItem:function(_3e){ |
245 |
return {element:_3e,store:this}; |
246 |
},_parseItem:function(_3f){ |
247 |
var _40=_3f._attribs;
|
248 |
var _41=this; |
249 |
var _42,_43;
|
250 |
function _44(_45){ |
251 |
var txt=_45.textContent||_45.innerHTML||_45.innerXML;
|
252 |
if(!txt&&_45.childNodes[0]){ |
253 |
var _46=_45.childNodes[0]; |
254 |
if(_46&&(_46.nodeType==3||_46.nodeType==4)){ |
255 |
txt=_45.childNodes[0].nodeValue;
|
256 |
} |
257 |
} |
258 |
return txt;
|
259 |
}; |
260 |
function _47(_48){ |
261 |
return {text:_44(_48),type:_48.getAttribute("type")}; |
262 |
}; |
263 |
dojo.forEach(_3f.element.childNodes,function(_49){
|
264 |
var _4a=_49.tagName?_49.tagName.toLowerCase():""; |
265 |
switch(_4a){
|
266 |
case "title": |
267 |
_40[_4a]={text:_44(_49),type:_49.getAttribute("type")}; |
268 |
break;
|
269 |
case "subtitle": |
270 |
case "summary": |
271 |
case "content": |
272 |
_40[_4a]=_47(_49); |
273 |
break;
|
274 |
case "author": |
275 |
var _4b,_4c;
|
276 |
dojo.forEach(_49.childNodes,function(_4d){
|
277 |
if(!_4d.tagName){
|
278 |
return;
|
279 |
} |
280 |
switch(_4d.tagName.toLowerCase()){
|
281 |
case "name": |
282 |
_4b=_4d; |
283 |
break;
|
284 |
case "uri": |
285 |
_4c=_4d; |
286 |
break;
|
287 |
} |
288 |
}); |
289 |
var _4e={};
|
290 |
if(_4b&&_4b.length==1){ |
291 |
_4e.name=_44(_4b[0]);
|
292 |
} |
293 |
if(_4c&&_4c.length==1){ |
294 |
_4e.uri=_44(_4c[0]);
|
295 |
} |
296 |
_40[_4a]=_4e; |
297 |
break;
|
298 |
case "id": |
299 |
_40[_4a]=_44(_49); |
300 |
break;
|
301 |
case "updated": |
302 |
_40[_4a]=dojo.date.stamp.fromISOString(_44(_49)); |
303 |
break;
|
304 |
case "published": |
305 |
_40[_4a]=dojo.date.stamp.fromISOString(_44(_49)); |
306 |
break;
|
307 |
case "category": |
308 |
if(!_40[_4a]){
|
309 |
_40[_4a]=[]; |
310 |
} |
311 |
_40[_4a].push({scheme:_49.getAttribute("scheme"),term:_49.getAttribute("term")}); |
312 |
break;
|
313 |
case "link": |
314 |
if(!_40[_4a]){
|
315 |
_40[_4a]=[]; |
316 |
} |
317 |
var _4f={rel:_49.getAttribute("rel"),href:_49.getAttribute("href"),type:_49.getAttribute("type")}; |
318 |
_40[_4a].push(_4f); |
319 |
if(_4f.rel=="alternate"){ |
320 |
_40["alternate"]=_4f;
|
321 |
} |
322 |
break;
|
323 |
default:
|
324 |
break;
|
325 |
} |
326 |
}); |
327 |
},_unescapeHTML:function(_50){ |
328 |
_50=_50.replace(/’/m,"'").replace(/″/m,"\"").replace(/</m,">").replace(/>/m,"<").replace(/&/m,"&"); |
329 |
return _50;
|
330 |
},_assertIsItem:function(_51){ |
331 |
if(!this.isItem(_51)){ |
332 |
throw new Error("dojox.data.AtomReadStore: Invalid item argument."); |
333 |
} |
334 |
},_assertIsAttribute:function(_52){ |
335 |
if(typeof _52!=="string"){ |
336 |
throw new Error("dojox.data.AtomReadStore: Invalid attribute argument."); |
337 |
} |
338 |
}}); |
339 |
dojo.extend(dojox.data.AtomReadStore,dojo.data.util.simpleFetch); |
340 |
} |