root / trunk / web / dojo / dojox / data / GoogleSearchStore.js @ 13
History | View | Annotate | Download (7.66 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.data.GoogleSearchStore"]){ |
||
9 | dojo._hasResource["dojox.data.GoogleSearchStore"]=true; |
||
10 | dojo.provide("dojox.data.GoogleSearchStore");
|
||
11 | dojo.provide("dojox.data.GoogleWebSearchStore");
|
||
12 | dojo.provide("dojox.data.GoogleBlogSearchStore");
|
||
13 | dojo.provide("dojox.data.GoogleLocalSearchStore");
|
||
14 | dojo.provide("dojox.data.GoogleVideoSearchStore");
|
||
15 | dojo.provide("dojox.data.GoogleNewsSearchStore");
|
||
16 | dojo.provide("dojox.data.GoogleBookSearchStore");
|
||
17 | dojo.provide("dojox.data.GoogleImageSearchStore");
|
||
18 | dojo.require("dojo.io.script");
|
||
19 | dojo.experimental("dojox.data.GoogleSearchStore");
|
||
20 | dojo.declare("dojox.data.GoogleSearchStore",null,{constructor:function(_1){ |
||
21 | if(_1){
|
||
22 | if(_1.label){
|
||
23 | this.label=_1.label;
|
||
24 | } |
||
25 | if(_1.key){
|
||
26 | this._key=_1.key;
|
||
27 | } |
||
28 | if(_1.lang){
|
||
29 | this._lang=_1.lang;
|
||
30 | } |
||
31 | if("urlPreventCache" in _1){ |
||
32 | this.urlPreventCache=_1.urlPreventCache?true:false; |
||
33 | } |
||
34 | } |
||
35 | this._id=dojox.data.GoogleSearchStore.prototype._id++;
|
||
36 | },_id:0,_requestCount:0,_googleUrl:"http://ajax.googleapis.com/ajax/services/search/",_storeRef:"_S",_attributes:["unescapedUrl","url","visibleUrl","cacheUrl","title","titleNoFormatting","content","estimatedResultCount"],_aggregatedAttributes:{estimatedResultCount:"cursor.estimatedResultCount"},label:"titleNoFormatting",_type:"web",urlPreventCache:true,_queryAttrs:{text:"q"},_assertIsItem:function(_2){ |
||
37 | if(!this.isItem(_2)){ |
||
38 | throw new Error("dojox.data.GoogleSearchStore: a function was passed an item argument that was not an item"); |
||
39 | } |
||
40 | },_assertIsAttribute:function(_3){ |
||
41 | if(typeof _3!=="string"){ |
||
42 | throw new Error("dojox.data.GoogleSearchStore: a function was passed an attribute argument that was not an attribute name string"); |
||
43 | } |
||
44 | },getFeatures:function(){ |
||
45 | return {"dojo.data.api.Read":true}; |
||
46 | },getValue:function(_4,_5,_6){ |
||
47 | var _7=this.getValues(_4,_5); |
||
48 | if(_7&&_7.length>0){ |
||
49 | return _7[0]; |
||
50 | } |
||
51 | return _6;
|
||
52 | },getAttributes:function(_8){ |
||
53 | return this._attributes; |
||
54 | },hasAttribute:function(_9,_a){ |
||
55 | if(this.getValue(_9,_a)){ |
||
56 | return true; |
||
57 | } |
||
58 | return false; |
||
59 | },isItemLoaded:function(_b){ |
||
60 | return this.isItem(_b); |
||
61 | },loadItem:function(_c){ |
||
62 | },getLabel:function(_d){ |
||
63 | return this.getValue(_d,this.label); |
||
64 | },getLabelAttributes:function(_e){ |
||
65 | return [this.label]; |
||
66 | },containsValue:function(_f,_10,_11){ |
||
67 | var _12=this.getValues(_f,_10); |
||
68 | for(var i=0;i<_12.length;i++){ |
||
69 | if(_12[i]===_11){
|
||
70 | return true; |
||
71 | } |
||
72 | } |
||
73 | return false; |
||
74 | },getValues:function(_13,_14){ |
||
75 | this._assertIsItem(_13);
|
||
76 | this._assertIsAttribute(_14);
|
||
77 | var val=_13[_14];
|
||
78 | if(dojo.isArray(val)){
|
||
79 | return val;
|
||
80 | }else{
|
||
81 | if(val!==undefined){ |
||
82 | return [val];
|
||
83 | }else{
|
||
84 | return [];
|
||
85 | } |
||
86 | } |
||
87 | },isItem:function(_15){ |
||
88 | if(_15&&_15[this._storeRef]===this){ |
||
89 | return true; |
||
90 | } |
||
91 | return false; |
||
92 | },close:function(_16){ |
||
93 | },_format:function(_17,_18){ |
||
94 | return _17;
|
||
95 | },fetch:function(_19){ |
||
96 | _19=_19||{}; |
||
97 | var _1a=_19.scope||dojo.global;
|
||
98 | if(!_19.query){
|
||
99 | if(_19.onError){
|
||
100 | _19.onError.call(_1a,new Error(this.declaredClass+": A query must be specified.")); |
||
101 | return;
|
||
102 | } |
||
103 | } |
||
104 | var _1b={};
|
||
105 | for(var _1c in this._queryAttrs){ |
||
106 | _1b[_1c]=_19.query[_1c]; |
||
107 | } |
||
108 | _19={query:_1b,onComplete:_19.onComplete,onError:_19.onError,onItem:_19.onItem,onBegin:_19.onBegin,start:_19.start,count:_19.count}; |
||
109 | var _1d=8; |
||
110 | var _1e="GoogleSearchStoreCallback_"+this._id+"_"+(++this._requestCount); |
||
111 | var _1f=this._createContent(_1b,_1e,_19); |
||
112 | var _20;
|
||
113 | if(typeof (_19.start)==="undefined"||_19.start===null){ |
||
114 | _19.start=0;
|
||
115 | } |
||
116 | if(!_19.count){
|
||
117 | _19.count=_1d; |
||
118 | } |
||
119 | _20={start:_19.start-_19.start%_1d};
|
||
120 | var _21=this; |
||
121 | var _22=this._googleUrl+this._type; |
||
122 | var _23={url:_22,preventCache:this.urlPreventCache,content:_1f}; |
||
123 | var _24=[];
|
||
124 | var _25=0; |
||
125 | var _26=false; |
||
126 | var _27=_19.start-1; |
||
127 | var _28=0; |
||
128 | var _29=[];
|
||
129 | function _2a(req){ |
||
130 | _28++; |
||
131 | _23.content.context=_23.content.start=req.start; |
||
132 | var _2b=dojo.io.script.get(_23);
|
||
133 | _29.push(_2b.ioArgs.id); |
||
134 | _2b.addErrback(function(_2c){
|
||
135 | if(_19.onError){
|
||
136 | _19.onError.call(_1a,_2c,_19); |
||
137 | } |
||
138 | }); |
||
139 | }; |
||
140 | var _2d=function(_2e,_2f){ |
||
141 | if(_29.length>0){ |
||
142 | dojo.query("#"+_29.splice(0,1)).forEach(dojo.destroy); |
||
143 | } |
||
144 | if(_26){
|
||
145 | return;
|
||
146 | } |
||
147 | var _30=_21._getItems(_2f);
|
||
148 | var _31=_2f?_2f["cursor"]:null; |
||
149 | if(_30){
|
||
150 | for(var i=0;i<_30.length&&i+_2e<_19.count+_19.start;i++){ |
||
151 | _21._processItem(_30[i],_2f); |
||
152 | _24[i+_2e]=_30[i]; |
||
153 | } |
||
154 | _25++; |
||
155 | if(_25==1){ |
||
156 | var _32=_31?_31.pages:null; |
||
157 | var _33=_32?Number(_32[_32.length-1].start):0; |
||
158 | if(_19.onBegin){
|
||
159 | var est=_31?_31.estimatedResultCount:_30.length;
|
||
160 | var _34=est?Math.min(est,_33+_30.length):_33+_30.length;
|
||
161 | _19.onBegin.call(_1a,_34,_19); |
||
162 | } |
||
163 | var _35=(_19.start-_19.start%_1d)+_1d;
|
||
164 | var _36=1; |
||
165 | while(_32){
|
||
166 | if(!_32[_36]||Number(_32[_36].start)>=_19.start+_19.count){
|
||
167 | break;
|
||
168 | } |
||
169 | if(Number(_32[_36].start)>=_35){
|
||
170 | _2a({start:_32[_36].start});
|
||
171 | } |
||
172 | _36++; |
||
173 | } |
||
174 | } |
||
175 | if(_19.onItem&&_24[_27+1]){ |
||
176 | do{
|
||
177 | _27++; |
||
178 | _19.onItem.call(_1a,_24[_27],_19); |
||
179 | }while(_24[_27+1]&&_27<_19.start+_19.count); |
||
180 | } |
||
181 | if(_25==_28){
|
||
182 | _26=true;
|
||
183 | dojo.global[_1e]=null;
|
||
184 | if(_19.onItem){
|
||
185 | _19.onComplete.call(_1a,null,_19);
|
||
186 | }else{
|
||
187 | _24=_24.slice(_19.start,_19.start+_19.count); |
||
188 | _19.onComplete.call(_1a,_24,_19); |
||
189 | } |
||
190 | } |
||
191 | } |
||
192 | }; |
||
193 | var _37=[];
|
||
194 | var _38=_20.start-1; |
||
195 | dojo.global[_1e]=function(_39,_3a,_3b,_3c){
|
||
196 | try{
|
||
197 | if(_3b!=200){ |
||
198 | if(_19.onError){
|
||
199 | _19.onError.call(_1a,new Error("Response from Google was: "+_3b),_19); |
||
200 | } |
||
201 | dojo.global[_1e]=function(){
|
||
202 | }; |
||
203 | return;
|
||
204 | } |
||
205 | if(_39==_38+1){ |
||
206 | _2d(Number(_39),_3a); |
||
207 | _38+=_1d; |
||
208 | if(_37.length>0){ |
||
209 | _37.sort(_21._getSort()); |
||
210 | while(_37.length>0&&_37[0].start==_38+1){ |
||
211 | _2d(Number(_37[0].start),_37[0].data); |
||
212 | _37.splice(0,1); |
||
213 | _38+=_1d; |
||
214 | } |
||
215 | } |
||
216 | }else{
|
||
217 | _37.push({start:_39,data:_3a}); |
||
218 | } |
||
219 | } |
||
220 | catch(e){
|
||
221 | _19.onError.call(_1a,e,_19); |
||
222 | } |
||
223 | }; |
||
224 | _2a(_20); |
||
225 | },_getSort:function(){ |
||
226 | return function(a,b){ |
||
227 | if(a.start<b.start){
|
||
228 | return -1; |
||
229 | } |
||
230 | if(b.start<a.start){
|
||
231 | return 1; |
||
232 | } |
||
233 | return 0; |
||
234 | }; |
||
235 | },_processItem:function(_3d,_3e){ |
||
236 | _3d[this._storeRef]=this; |
||
237 | for(var _3f in this._aggregatedAttributes){ |
||
238 | _3d[_3f]=dojo.getObject(this._aggregatedAttributes[_3f],false,_3e); |
||
239 | } |
||
240 | },_getItems:function(_40){ |
||
241 | return _40["results"]||_40; |
||
242 | },_createContent:function(_41,_42,_43){ |
||
243 | var _44={v:"1.0",rsz:"large",callback:_42,key:this._key,hl:this._lang}; |
||
244 | for(var _45 in this._queryAttrs){ |
||
245 | _44[this._queryAttrs[_45]]=_41[_45];
|
||
246 | } |
||
247 | return _44;
|
||
248 | }}); |
||
249 | dojo.declare("dojox.data.GoogleWebSearchStore",dojox.data.GoogleSearchStore,{});
|
||
250 | dojo.declare("dojox.data.GoogleBlogSearchStore",dojox.data.GoogleSearchStore,{_type:"blogs",_attributes:["blogUrl","postUrl","title","titleNoFormatting","content","author","publishedDate"],_aggregatedAttributes:{}}); |
||
251 | dojo.declare("dojox.data.GoogleLocalSearchStore",dojox.data.GoogleSearchStore,{_type:"local",_attributes:["title","titleNoFormatting","url","lat","lng","streetAddress","city","region","country","phoneNumbers","ddUrl","ddUrlToHere","ddUrlFromHere","staticMapUrl","viewport"],_aggregatedAttributes:{viewport:"viewport"},_queryAttrs:{text:"q",centerLatLong:"sll",searchSpan:"sspn"}}); |
||
252 | dojo.declare("dojox.data.GoogleVideoSearchStore",dojox.data.GoogleSearchStore,{_type:"video",_attributes:["title","titleNoFormatting","content","url","published","publisher","duration","tbWidth","tbHeight","tbUrl","playUrl"],_aggregatedAttributes:{}}); |
||
253 | dojo.declare("dojox.data.GoogleNewsSearchStore",dojox.data.GoogleSearchStore,{_type:"news",_attributes:["title","titleNoFormatting","content","url","unescapedUrl","publisher","clusterUrl","location","publishedDate","relatedStories"],_aggregatedAttributes:{}}); |
||
254 | dojo.declare("dojox.data.GoogleBookSearchStore",dojox.data.GoogleSearchStore,{_type:"books",_attributes:["title","titleNoFormatting","authors","url","unescapedUrl","bookId","pageCount","publishedYear"],_aggregatedAttributes:{}}); |
||
255 | dojo.declare("dojox.data.GoogleImageSearchStore",dojox.data.GoogleSearchStore,{_type:"images",_attributes:["title","titleNoFormatting","visibleUrl","url","unescapedUrl","originalContextUrl","width","height","tbWidth","tbHeight","tbUrl","content","contentNoFormatting"],_aggregatedAttributes:{}}); |
||
256 | } |