root / trunk / web / dojo / dojox / data / OpenSearchStore.js @ 12
History | View | Annotate | Download (6.2 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.OpenSearchStore"]){ |
9 |
dojo._hasResource["dojox.data.OpenSearchStore"]=true; |
10 |
dojo.provide("dojox.data.OpenSearchStore");
|
11 |
dojo.require("dojo.data.util.simpleFetch");
|
12 |
dojo.require("dojox.xml.DomParser");
|
13 |
dojo.require("dojox.xml.parser");
|
14 |
dojo.experimental("dojox.data.OpenSearchStore");
|
15 |
dojo.declare("dojox.data.OpenSearchStore",null,{constructor:function(_1){ |
16 |
if(_1){
|
17 |
this.label=_1.label;
|
18 |
this.url=_1.url;
|
19 |
this.itemPath=_1.itemPath;
|
20 |
if("urlPreventCache" in _1){ |
21 |
this.urlPreventCache=_1.urlPreventCache?true:false; |
22 |
} |
23 |
} |
24 |
var _2=dojo.xhrGet({url:this.url,handleAs:"xml",sync:true,preventCache:this.urlPreventCache}); |
25 |
_2.addCallback(this,"_processOsdd"); |
26 |
_2.addErrback(function(){
|
27 |
throw new Error("Unable to load OpenSearch Description document from ".args.url); |
28 |
}); |
29 |
},url:"",itemPath:"",_storeRef:"_S",urlElement:null,iframeElement:null,urlPreventCache:true,ATOM_CONTENT_TYPE:3,ATOM_CONTENT_TYPE_STRING:"atom",RSS_CONTENT_TYPE:2,RSS_CONTENT_TYPE_STRING:"rss",XML_CONTENT_TYPE:1,XML_CONTENT_TYPE_STRING:"xml",_assertIsItem:function(_3){ |
30 |
if(!this.isItem(_3)){ |
31 |
throw new Error("dojox.data.OpenSearchStore: a function was passed an item argument that was not an item"); |
32 |
} |
33 |
},_assertIsAttribute:function(_4){ |
34 |
if(typeof _4!=="string"){ |
35 |
throw new Error("dojox.data.OpenSearchStore: a function was passed an attribute argument that was not an attribute name string"); |
36 |
} |
37 |
},getFeatures:function(){ |
38 |
return {"dojo.data.api.Read":true}; |
39 |
},getValue:function(_5,_6,_7){ |
40 |
var _8=this.getValues(_5,_6); |
41 |
if(_8){
|
42 |
return _8[0]; |
43 |
} |
44 |
return _7;
|
45 |
},getAttributes:function(_9){ |
46 |
return ["content"]; |
47 |
},hasAttribute:function(_a,_b){ |
48 |
if(this.getValue(_a,_b)){ |
49 |
return true; |
50 |
} |
51 |
return false; |
52 |
},isItemLoaded:function(_c){ |
53 |
return this.isItem(_c); |
54 |
},loadItem:function(_d){ |
55 |
},getLabel:function(_e){ |
56 |
return undefined; |
57 |
},getLabelAttributes:function(_f){ |
58 |
return null; |
59 |
},containsValue:function(_10,_11,_12){ |
60 |
var _13=this.getValues(_10,_11); |
61 |
for(var i=0;i<_13.length;i++){ |
62 |
if(_13[i]===_12){
|
63 |
return true; |
64 |
} |
65 |
} |
66 |
return false; |
67 |
},getValues:function(_14,_15){ |
68 |
this._assertIsItem(_14);
|
69 |
this._assertIsAttribute(_15);
|
70 |
var _16=this.processItem(_14,_15); |
71 |
if(_16){
|
72 |
return [_16];
|
73 |
} |
74 |
return undefined; |
75 |
},isItem:function(_17){ |
76 |
if(_17&&_17[this._storeRef]===this){ |
77 |
return true; |
78 |
} |
79 |
return false; |
80 |
},close:function(_18){ |
81 |
},process:function(_19){ |
82 |
return this["_processOSD"+this.contentType](_19); |
83 |
},processItem:function(_1a,_1b){ |
84 |
return this["_processItem"+this.contentType](_1a.node,_1b); |
85 |
},_createSearchUrl:function(_1c){ |
86 |
var _1d=this.urlElement.attributes.getNamedItem("template").nodeValue; |
87 |
var _1e=this.urlElement.attributes; |
88 |
var _1f=_1d.indexOf("{searchTerms}"); |
89 |
_1d=_1d.substring(0,_1f)+_1c.query.searchTerms+_1d.substring(_1f+13); |
90 |
dojo.forEach([{"name":"count","test":_1c.count,"def":"10"},{"name":"startIndex","test":_1c.start,"def":this.urlElement.attributes.getNamedItem("indexOffset")?this.urlElement.attributes.getNamedItem("indexOffset").nodeValue:0},{"name":"startPage","test":_1c.startPage,"def":this.urlElement.attributes.getNamedItem("pageOffset")?this.urlElement.attributes.getNamedItem("pageOffset").nodeValue:0},{"name":"language","test":_1c.language,"def":"*"},{"name":"inputEncoding","test":_1c.inputEncoding,"def":"UTF-8"},{"name":"outputEncoding","test":_1c.outputEncoding,"def":"UTF-8"}],function(_20){ |
91 |
_1d=_1d.replace("{"+_20.name+"}",_20.test||_20.def); |
92 |
_1d=_1d.replace("{"+_20.name+"?}",_20.test||_20.def); |
93 |
}); |
94 |
return _1d;
|
95 |
},_fetchItems:function(_21,_22,_23){ |
96 |
if(!_21.query){
|
97 |
_21.query={}; |
98 |
} |
99 |
var _24=this; |
100 |
var url=this._createSearchUrl(_21); |
101 |
var _25={url:url,preventCache:this.urlPreventCache}; |
102 |
var xhr=dojo.xhrGet(_25);
|
103 |
xhr.addErrback(function(_26){
|
104 |
_23(_26,_21); |
105 |
}); |
106 |
xhr.addCallback(function(_27){
|
107 |
var _28=[];
|
108 |
if(_27){
|
109 |
_28=_24.process(_27); |
110 |
for(var i=0;i<_28.length;i++){ |
111 |
_28[i]={node:_28[i]};
|
112 |
_28[i][_24._storeRef]=_24; |
113 |
} |
114 |
} |
115 |
_22(_28,_21); |
116 |
}); |
117 |
},_processOSDxml:function(_29){ |
118 |
var div=dojo.doc.createElement("div"); |
119 |
div.innerHTML=_29; |
120 |
return dojo.query(this.itemPath,div); |
121 |
},_processItemxml:function(_2a,_2b){ |
122 |
if(_2b==="content"){ |
123 |
return _2a.innerHTML;
|
124 |
} |
125 |
return undefined; |
126 |
},_processOSDatom:function(_2c){ |
127 |
return this._processOSDfeed(_2c,"entry"); |
128 |
},_processItematom:function(_2d,_2e){ |
129 |
return this._processItemfeed(_2d,_2e,"content"); |
130 |
},_processOSDrss:function(_2f){ |
131 |
return this._processOSDfeed(_2f,"item"); |
132 |
},_processItemrss:function(_30,_31){ |
133 |
return this._processItemfeed(_30,_31,"description"); |
134 |
},_processOSDfeed:function(_32,_33){ |
135 |
_32=dojox.xml.parser.parse(_32); |
136 |
var _34=[];
|
137 |
var _35=_32.getElementsByTagName(_33);
|
138 |
for(var i=0;i<_35.length;i++){ |
139 |
_34.push(_35.item(i)); |
140 |
} |
141 |
return _34;
|
142 |
},_processItemfeed:function(_36,_37,_38){ |
143 |
if(_37==="content"){ |
144 |
var _39=_36.getElementsByTagName(_38).item(0); |
145 |
return this._getNodeXml(_39,true); |
146 |
} |
147 |
return undefined; |
148 |
},_getNodeXml:function(_3a,_3b){ |
149 |
var i;
|
150 |
switch(_3a.nodeType){
|
151 |
case 1: |
152 |
var xml=[];
|
153 |
if(!_3b){
|
154 |
xml.push("<"+_3a.tagName);
|
155 |
var _3c;
|
156 |
for(i=0;i<_3a.attributes.length;i++){ |
157 |
_3c=_3a.attributes.item(i); |
158 |
xml.push(" "+_3c.nodeName+"=\""+_3c.nodeValue+"\""); |
159 |
} |
160 |
xml.push(">");
|
161 |
} |
162 |
for(i=0;i<_3a.childNodes.length;i++){ |
163 |
xml.push(this._getNodeXml(_3a.childNodes.item(i)));
|
164 |
} |
165 |
if(!_3b){
|
166 |
xml.push("</"+_3a.tagName+">\n"); |
167 |
} |
168 |
return xml.join(""); |
169 |
case 3: |
170 |
case 4: |
171 |
return _3a.nodeValue;
|
172 |
} |
173 |
return undefined; |
174 |
},_processOsdd:function(doc){ |
175 |
var _3d=doc.getElementsByTagName("Url"); |
176 |
var _3e=[];
|
177 |
var _3f;
|
178 |
var i;
|
179 |
for(i=0;i<_3d.length;i++){ |
180 |
_3f=_3d[i].attributes.getNamedItem("type").nodeValue;
|
181 |
switch(_3f){
|
182 |
case "application/rss+xml": |
183 |
_3e[i]=this.RSS_CONTENT_TYPE;
|
184 |
break;
|
185 |
case "application/atom+xml": |
186 |
_3e[i]=this.ATOM_CONTENT_TYPE;
|
187 |
break;
|
188 |
default:
|
189 |
_3e[i]=this.XML_CONTENT_TYPE;
|
190 |
break;
|
191 |
} |
192 |
} |
193 |
var _40=0; |
194 |
var _41=_3e[0]; |
195 |
for(i=1;i<_3d.length;i++){ |
196 |
if(_3e[i]>_41){
|
197 |
_40=i; |
198 |
_41=_3e[i]; |
199 |
} |
200 |
} |
201 |
var _42=_3d[_40].nodeName.toLowerCase();
|
202 |
if(_42=="url"){ |
203 |
var _43=_3d[_40].attributes;
|
204 |
this.urlElement=_3d[_40];
|
205 |
switch(_3e[_40]){
|
206 |
case this.ATOM_CONTENT_TYPE: |
207 |
this.contentType=this.ATOM_CONTENT_TYPE_STRING; |
208 |
break;
|
209 |
case this.RSS_CONTENT_TYPE: |
210 |
this.contentType=this.RSS_CONTENT_TYPE_STRING; |
211 |
break;
|
212 |
case this.XML_CONTENT_TYPE: |
213 |
this.contentType=this.XML_CONTENT_TYPE_STRING; |
214 |
break;
|
215 |
} |
216 |
} |
217 |
}}); |
218 |
dojo.extend(dojox.data.OpenSearchStore,dojo.data.util.simpleFetch); |
219 |
} |