root / trunk / web / dojo / dojox / data / FlickrRestStore.js @ 13
History | View | Annotate | Download (6.87 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.FlickrRestStore"]){ |
||
| 9 | dojo._hasResource["dojox.data.FlickrRestStore"]=true; |
||
| 10 | dojo.provide("dojox.data.FlickrRestStore");
|
||
| 11 | dojo.require("dojox.data.FlickrStore");
|
||
| 12 | dojo.declare("dojox.data.FlickrRestStore",dojox.data.FlickrStore,{constructor:function(_1){ |
||
| 13 | if(_1){
|
||
| 14 | if(_1.label){
|
||
| 15 | this.label=_1.label;
|
||
| 16 | } |
||
| 17 | if(_1.apikey){
|
||
| 18 | this._apikey=_1.apikey;
|
||
| 19 | } |
||
| 20 | } |
||
| 21 | this._cache=[];
|
||
| 22 | this._prevRequests={};
|
||
| 23 | this._handlers={};
|
||
| 24 | this._prevRequestRanges=[];
|
||
| 25 | this._maxPhotosPerUser={};
|
||
| 26 | this._id=dojox.data.FlickrRestStore.prototype._id++;
|
||
| 27 | },_id:0,_requestCount:0,_flickrRestUrl:"http://www.flickr.com/services/rest/",_apikey:null,_storeRef:"_S",_cache:null,_prevRequests:null,_handlers:null,_sortAttributes:{"date-posted":true,"date-taken":true,"interestingness":true},_fetchItems:function(_2,_3,_4){ |
||
| 28 | var _5={};
|
||
| 29 | if(!_2.query){
|
||
| 30 | _2.query=_5={};
|
||
| 31 | }else{
|
||
| 32 | dojo.mixin(_5,_2.query); |
||
| 33 | } |
||
| 34 | var _6=[];
|
||
| 35 | var _7=[];
|
||
| 36 | var _8={format:"json",method:"flickr.photos.search",api_key:this._apikey,extras:"owner_name,date_upload,date_taken"}; |
||
| 37 | var _9=false; |
||
| 38 | if(_5.userid){
|
||
| 39 | _9=true;
|
||
| 40 | _8.user_id=_2.query.userid; |
||
| 41 | _6.push("userid"+_2.query.userid);
|
||
| 42 | } |
||
| 43 | if(_5.groupid){
|
||
| 44 | _9=true;
|
||
| 45 | _8.group_id=_5.groupid; |
||
| 46 | _6.push("groupid"+_5.groupid);
|
||
| 47 | } |
||
| 48 | if(_5.apikey){
|
||
| 49 | _9=true;
|
||
| 50 | _8.api_key=_2.query.apikey; |
||
| 51 | _7.push("api"+_2.query.apikey);
|
||
| 52 | }else{
|
||
| 53 | if(_8.api_key){
|
||
| 54 | _9=true;
|
||
| 55 | _2.query.apikey=_8.api_key; |
||
| 56 | _7.push("api"+_8.api_key);
|
||
| 57 | }else{
|
||
| 58 | throw Error("dojox.data.FlickrRestStore: An API key must be specified."); |
||
| 59 | } |
||
| 60 | } |
||
| 61 | _2._curCount=_2.count; |
||
| 62 | if(_5.page){
|
||
| 63 | _8.page=_2.query.page; |
||
| 64 | _7.push("page"+_8.page);
|
||
| 65 | }else{
|
||
| 66 | if(("start" in _2)&&_2.start!==null){ |
||
| 67 | if(!_2.count){
|
||
| 68 | _2.count=20;
|
||
| 69 | } |
||
| 70 | var _a=_2.start%_2.count;
|
||
| 71 | var _b=_2.start,_c=_2.count;
|
||
| 72 | if(_a!==0){ |
||
| 73 | if(_b<_c/2){ |
||
| 74 | _c=_b+_c; |
||
| 75 | _b=0;
|
||
| 76 | }else{
|
||
| 77 | var _d=20,_e=2; |
||
| 78 | for(var i=_d;i>0;i--){ |
||
| 79 | if(_b%i===0&&(_b/i)>=_c){ |
||
| 80 | _e=i; |
||
| 81 | break;
|
||
| 82 | } |
||
| 83 | } |
||
| 84 | _c=_b/_e; |
||
| 85 | } |
||
| 86 | _2._realStart=_2.start; |
||
| 87 | _2._realCount=_2.count; |
||
| 88 | _2._curStart=_b; |
||
| 89 | _2._curCount=_c; |
||
| 90 | }else{
|
||
| 91 | _2._realStart=_2._realCount=null;
|
||
| 92 | _2._curStart=_2.start; |
||
| 93 | _2._curCount=_2.count; |
||
| 94 | } |
||
| 95 | _8.page=(_b/_c)+1;
|
||
| 96 | _7.push("page"+_8.page);
|
||
| 97 | } |
||
| 98 | } |
||
| 99 | if(_2._curCount){
|
||
| 100 | _8.per_page=_2._curCount; |
||
| 101 | _7.push("count"+_2._curCount);
|
||
| 102 | } |
||
| 103 | if(_5.lang){
|
||
| 104 | _8.lang=_2.query.lang; |
||
| 105 | _6.push("lang"+_2.lang);
|
||
| 106 | } |
||
| 107 | if(_5.setid){
|
||
| 108 | _8.method="flickr.photosets.getPhotos";
|
||
| 109 | _8.photoset_id=_2.query.setid; |
||
| 110 | _6.push("set"+_2.query.setid);
|
||
| 111 | } |
||
| 112 | if(_5.tags){
|
||
| 113 | if(_5.tags instanceof Array){ |
||
| 114 | _8.tags=_5.tags.join(",");
|
||
| 115 | }else{
|
||
| 116 | _8.tags=_5.tags; |
||
| 117 | } |
||
| 118 | _6.push("tags"+_8.tags);
|
||
| 119 | if(_5["tag_mode"]&&(_5.tag_mode.toLowerCase()==="any"||_5.tag_mode.toLowerCase()==="all")){ |
||
| 120 | _8.tag_mode=_5.tag_mode; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | if(_5.text){
|
||
| 124 | _8.text=_5.text; |
||
| 125 | _6.push("text:"+_5.text);
|
||
| 126 | } |
||
| 127 | if(_5.sort&&_5.sort.length>0){ |
||
| 128 | if(!_5.sort[0].attribute){ |
||
| 129 | _5.sort[0].attribute="date-posted"; |
||
| 130 | } |
||
| 131 | if(this._sortAttributes[_5.sort[0].attribute]){ |
||
| 132 | if(_5.sort[0].descending){ |
||
| 133 | _8.sort=_5.sort[0].attribute+"-desc"; |
||
| 134 | }else{
|
||
| 135 | _8.sort=_5.sort[0].attribute+"-asc"; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | }else{
|
||
| 139 | _8.sort="date-posted-asc";
|
||
| 140 | } |
||
| 141 | _6.push("sort:"+_8.sort);
|
||
| 142 | _6=_6.join(".");
|
||
| 143 | _7=_7.length>0?"."+_7.join("."):""; |
||
| 144 | var _f=_6+_7;
|
||
| 145 | _2={query:_5,count:_2._curCount,start:_2._curStart,_realCount:_2._realCount,_realStart:_2._realStart,onBegin:_2.onBegin,onComplete:_2.onComplete,onItem:_2.onItem};
|
||
| 146 | var _10={request:_2,fetchHandler:_3,errorHandler:_4}; |
||
| 147 | if(this._handlers[_f]){ |
||
| 148 | this._handlers[_f].push(_10);
|
||
| 149 | return;
|
||
| 150 | } |
||
| 151 | this._handlers[_f]=[_10];
|
||
| 152 | var _11=null; |
||
| 153 | var _12={url:this._flickrRestUrl,preventCache:this.urlPreventCache,content:_8,callbackParamName:"jsoncallback"}; |
||
| 154 | var _13=dojo.hitch(this,function(_14,_15,_16){ |
||
| 155 | var _17=_16.request.onBegin;
|
||
| 156 | _16.request.onBegin=null;
|
||
| 157 | var _18;
|
||
| 158 | var req=_16.request;
|
||
| 159 | if(("_realStart" in req)&&req._realStart!=null){ |
||
| 160 | req.start=req._realStart; |
||
| 161 | req.count=req._realCount; |
||
| 162 | req._realStart=req._realCount=null;
|
||
| 163 | } |
||
| 164 | if(_17){
|
||
| 165 | var _19=null; |
||
| 166 | if(_15){
|
||
| 167 | _19=(_15.photoset?_15.photoset:_15.photos); |
||
| 168 | } |
||
| 169 | if(_19&&("perpage" in _19)&&("pages" in _19)){ |
||
| 170 | if(_19.perpage*_19.pages<=_16.request.start+_16.request.count){
|
||
| 171 | _18=_16.request.start+_19.photo.length; |
||
| 172 | }else{
|
||
| 173 | _18=_19.perpage*_19.pages; |
||
| 174 | } |
||
| 175 | this._maxPhotosPerUser[_6]=_18;
|
||
| 176 | _17(_18,_16.request); |
||
| 177 | }else{
|
||
| 178 | if(this._maxPhotosPerUser[_6]){ |
||
| 179 | _17(this._maxPhotosPerUser[_6],_16.request);
|
||
| 180 | } |
||
| 181 | } |
||
| 182 | } |
||
| 183 | _16.fetchHandler(_14,_16.request); |
||
| 184 | if(_17){
|
||
| 185 | _16.request.onBegin=_17; |
||
| 186 | } |
||
| 187 | }); |
||
| 188 | var _1a=dojo.hitch(this,function(_1b){ |
||
| 189 | if(_1b.stat!="ok"){ |
||
| 190 | _4(null,_2);
|
||
| 191 | }else{
|
||
| 192 | var _1c=this._handlers[_f]; |
||
| 193 | if(!_1c){
|
||
| 194 | return;
|
||
| 195 | } |
||
| 196 | this._handlers[_f]=null; |
||
| 197 | this._prevRequests[_f]=_1b;
|
||
| 198 | var _1d=this._processFlickrData(_1b,_2,_6); |
||
| 199 | if(!this._prevRequestRanges[_6]){ |
||
| 200 | this._prevRequestRanges[_6]=[];
|
||
| 201 | } |
||
| 202 | this._prevRequestRanges[_6].push({start:_2.start,end:_2.start+(_1b.photoset?_1b.photoset.photo.length:_1b.photos.photo.length)}); |
||
| 203 | dojo.forEach(_1c,function(i){
|
||
| 204 | _13(_1d,_1b,i); |
||
| 205 | }); |
||
| 206 | } |
||
| 207 | }); |
||
| 208 | var _1e=this._prevRequests[_f]; |
||
| 209 | if(_1e){
|
||
| 210 | this._handlers[_f]=null; |
||
| 211 | _13(this._cache[_6],_1e,_10);
|
||
| 212 | return;
|
||
| 213 | }else{
|
||
| 214 | if(this._checkPrevRanges(_6,_2.start,_2.count)){ |
||
| 215 | this._handlers[_f]=null; |
||
| 216 | _13(this._cache[_6],null,_10); |
||
| 217 | return;
|
||
| 218 | } |
||
| 219 | } |
||
| 220 | var _1f=dojo.io.script.get(_12);
|
||
| 221 | _1f.addCallback(_1a); |
||
| 222 | _1f.addErrback(function(_20){
|
||
| 223 | dojo.disconnect(_11); |
||
| 224 | _4(_20,_2); |
||
| 225 | }); |
||
| 226 | },getAttributes:function(_21){ |
||
| 227 | return ["title","author","imageUrl","imageUrlSmall","imageUrlMedium","imageUrlThumb","imageUrlLarge","imageUrlOriginal","link","dateTaken","datePublished"]; |
||
| 228 | },getValues:function(_22,_23){ |
||
| 229 | this._assertIsItem(_22);
|
||
| 230 | this._assertIsAttribute(_23);
|
||
| 231 | switch(_23){
|
||
| 232 | case "title": |
||
| 233 | return [this._unescapeHtml(_22.title)]; |
||
| 234 | case "author": |
||
| 235 | return [_22.ownername];
|
||
| 236 | case "imageUrlSmall": |
||
| 237 | return [_22.media.s];
|
||
| 238 | case "imageUrl": |
||
| 239 | return [_22.media.l];
|
||
| 240 | case "imageUrlOriginal": |
||
| 241 | return [_22.media.o];
|
||
| 242 | case "imageUrlLarge": |
||
| 243 | return [_22.media.l];
|
||
| 244 | case "imageUrlMedium": |
||
| 245 | return [_22.media.m];
|
||
| 246 | case "imageUrlThumb": |
||
| 247 | return [_22.media.t];
|
||
| 248 | case "link": |
||
| 249 | return ["http://www.flickr.com/photos/"+_22.owner+"/"+_22.id]; |
||
| 250 | case "dateTaken": |
||
| 251 | return [_22.datetaken];
|
||
| 252 | case "datePublished": |
||
| 253 | return [_22.datepublished];
|
||
| 254 | default:
|
||
| 255 | return undefined; |
||
| 256 | } |
||
| 257 | },_processFlickrData:function(_24,_25,_26){ |
||
| 258 | if(_24.items){
|
||
| 259 | return dojox.data.FlickrStore.prototype._processFlickrData.apply(this,arguments); |
||
| 260 | } |
||
| 261 | var _27=["http://farm",null,".static.flickr.com/",null,"/",null,"_",null]; |
||
| 262 | var _28=[];
|
||
| 263 | var _29=(_24.photoset?_24.photoset:_24.photos);
|
||
| 264 | if(_24.stat=="ok"&&_29&&_29.photo){ |
||
| 265 | _28=_29.photo; |
||
| 266 | for(var i=0;i<_28.length;i++){ |
||
| 267 | var _2a=_28[i];
|
||
| 268 | _2a[this._storeRef]=this; |
||
| 269 | _27[1]=_2a.farm;
|
||
| 270 | _27[3]=_2a.server;
|
||
| 271 | _27[5]=_2a.id;
|
||
| 272 | _27[7]=_2a.secret;
|
||
| 273 | var _2b=_27.join(""); |
||
| 274 | _2a.media={s:_2b+"_s.jpg",m:_2b+"_m.jpg",l:_2b+".jpg",t:_2b+"_t.jpg",o:_2b+"_o.jpg"};
|
||
| 275 | if(!_2a.owner&&_24.photoset){
|
||
| 276 | _2a.owner=_24.photoset.owner; |
||
| 277 | } |
||
| 278 | } |
||
| 279 | } |
||
| 280 | var _2c=_25.start?_25.start:0; |
||
| 281 | var arr=this._cache[_26]; |
||
| 282 | if(!arr){
|
||
| 283 | this._cache[_26]=arr=[];
|
||
| 284 | } |
||
| 285 | dojo.forEach(_28,function(i,idx){
|
||
| 286 | arr[idx+_2c]=i; |
||
| 287 | }); |
||
| 288 | return arr;
|
||
| 289 | },_checkPrevRanges:function(_2d,_2e,_2f){ |
||
| 290 | var end=_2e+_2f;
|
||
| 291 | var arr=this._prevRequestRanges[_2d]; |
||
| 292 | return (!!arr)&&dojo.some(arr,function(_30){ |
||
| 293 | return ((_2e>=_30.start)&&(end<=_30.end));
|
||
| 294 | }); |
||
| 295 | }}); |
||
| 296 | } |