| 1 |
define(["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/array", "dojo/data/util/simpleFetch", "dojo/io/script", |
| 2 |
"dojo/_base/connect", "dojo/date/stamp", "dojo/AdapterRegistry"], |
| 3 |
function(lang, declare, array, simpleFetch, scriptIO, connect, dateStamp, AdapterRegistry) { |
| 4 |
|
| 5 |
var FlickrStore = declare("dojox.data.FlickrStore", null, { |
| 6 |
constructor: function(args){ |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
if(args && args.label){ |
| 16 |
this.label = args.label; |
| 17 |
} |
| 18 |
if(args && "urlPreventCache" in args){ |
| 19 |
this.urlPreventCache = args.urlPreventCache?true:false; |
| 20 |
} |
| 21 |
}, |
| 22 |
|
| 23 |
_storeRef: "_S", |
| 24 |
|
| 25 |
label: "title", |
| 26 |
|
| 27 |
|
| 28 |
urlPreventCache: true, |
| 29 |
|
| 30 |
_assertIsItem: function( item){ |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
if(!this.isItem(item)){ |
| 36 |
throw new Error("dojox.data.FlickrStore: a function was passed an item argument that was not an item"); |
| 37 |
} |
| 38 |
}, |
| 39 |
|
| 40 |
_assertIsAttribute: function( attribute){ |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
if(typeof attribute !== "string"){ |
| 46 |
throw new Error("dojox.data.FlickrStore: a function was passed an attribute argument that was not an attribute name string"); |
| 47 |
} |
| 48 |
}, |
| 49 |
|
| 50 |
getFeatures: function(){ |
| 51 |
|
| 52 |
|
| 53 |
return { |
| 54 |
'dojo.data.api.Read': true |
| 55 |
}; |
| 56 |
}, |
| 57 |
|
| 58 |
getValue: function(item, attribute, defaultValue){ |
| 59 |
|
| 60 |
|
| 61 |
var values = this.getValues(item, attribute); |
| 62 |
if(values && values.length > 0){ |
| 63 |
return values[0]; |
| 64 |
} |
| 65 |
return defaultValue; |
| 66 |
}, |
| 67 |
|
| 68 |
getAttributes: function(item){ |
| 69 |
|
| 70 |
|
| 71 |
return [ |
| 72 |
"title", "description", "author", "datePublished", "dateTaken", |
| 73 |
"imageUrl", "imageUrlSmall", "imageUrlMedium", "tags", "link" |
| 74 |
]; |
| 75 |
}, |
| 76 |
|
| 77 |
hasAttribute: function(item, attribute){ |
| 78 |
|
| 79 |
|
| 80 |
var v = this.getValue(item,attribute); |
| 81 |
if(v || v === "" || v === false){ |
| 82 |
return true; |
| 83 |
} |
| 84 |
return false; |
| 85 |
}, |
| 86 |
|
| 87 |
isItemLoaded: function(item){ |
| 88 |
|
| 89 |
|
| 90 |
return this.isItem(item); |
| 91 |
}, |
| 92 |
|
| 93 |
loadItem: function(keywordArgs){ |
| 94 |
|
| 95 |
|
| 96 |
}, |
| 97 |
|
| 98 |
getLabel: function(item){ |
| 99 |
|
| 100 |
|
| 101 |
return this.getValue(item,this.label); |
| 102 |
}, |
| 103 |
|
| 104 |
getLabelAttributes: function(item){ |
| 105 |
|
| 106 |
|
| 107 |
return [this.label]; |
| 108 |
}, |
| 109 |
|
| 110 |
containsValue: function(item, attribute, value){ |
| 111 |
|
| 112 |
|
| 113 |
var values = this.getValues(item,attribute); |
| 114 |
for(var i = 0; i < values.length; i++){ |
| 115 |
if(values[i] === value){ |
| 116 |
return true; |
| 117 |
} |
| 118 |
} |
| 119 |
return false; |
| 120 |
}, |
| 121 |
|
| 122 |
getValues: function(item, attribute){ |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
this._assertIsItem(item); |
| 127 |
this._assertIsAttribute(attribute); |
| 128 |
var u = lang.hitch(this, "_unescapeHtml"); |
| 129 |
var s = lang.hitch(dateStamp, "fromISOString"); |
| 130 |
switch(attribute){ |
| 131 |
case "title": |
| 132 |
return [ u(item.title) ]; |
| 133 |
case "author": |
| 134 |
return [ u(item.author) ]; |
| 135 |
case "datePublished": |
| 136 |
return [ s(item.published) ]; |
| 137 |
case "dateTaken": |
| 138 |
return [ s(item.date_taken) ]; |
| 139 |
case "imageUrlSmall": |
| 140 |
return [ item.media.m.replace(/_m\./, "_s.") ]; |
| 141 |
case "imageUrl": |
| 142 |
return [ item.media.m.replace(/_m\./, ".") ]; |
| 143 |
case "imageUrlMedium": |
| 144 |
return [ item.media.m ]; |
| 145 |
case "link": |
| 146 |
return [ item.link ]; |
| 147 |
case "tags": |
| 148 |
return item.tags.split(" "); |
| 149 |
case "description": |
| 150 |
return [ u(item.description) ]; |
| 151 |
default: |
| 152 |
return []; |
| 153 |
} |
| 154 |
}, |
| 155 |
|
| 156 |
isItem: function(item){ |
| 157 |
|
| 158 |
|
| 159 |
if(item && item[this._storeRef] === this){ |
| 160 |
return true; |
| 161 |
} |
| 162 |
return false; |
| 163 |
}, |
| 164 |
|
| 165 |
close: function(request){ |
| 166 |
|
| 167 |
|
| 168 |
}, |
| 169 |
|
| 170 |
_fetchItems: function(request, fetchHandler, errorHandler){ |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
var rq = request.query = request.query || {}; |
| 181 |
|
| 182 |
|
| 183 |
var content = { |
| 184 |
format: "json", |
| 185 |
tagmode:"any" |
| 186 |
}; |
| 187 |
|
| 188 |
array.forEach( |
| 189 |
[ "tags", "tagmode", "lang", "id", "ids" ], |
| 190 |
function(i){ |
| 191 |
if(rq[i]){ content[i] = rq[i]; } |
| 192 |
} |
| 193 |
); |
| 194 |
|
| 195 |
content.id = rq.id || rq.userid || rq.groupid; |
| 196 |
|
| 197 |
if(rq.userids){ |
| 198 |
content.ids = rq.userids; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
var handle = null; |
| 203 |
var getArgs = { |
| 204 |
url: dojox.data.FlickrStore.urlRegistry.match(request), |
| 205 |
preventCache: this.urlPreventCache, |
| 206 |
content: content |
| 207 |
}; |
| 208 |
var myHandler = lang.hitch(this, function(data){ |
| 209 |
if(!!handle){ |
| 210 |
connect.disconnect(handle); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
fetchHandler(this._processFlickrData(data), request); |
| 215 |
}); |
| 216 |
handle = connect.connect("jsonFlickrFeed", myHandler); |
| 217 |
var deferred = scriptIO.get(getArgs); |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
deferred.addErrback(function(error){ |
| 222 |
connect.disconnect(handle); |
| 223 |
errorHandler(error, request); |
| 224 |
}); |
| 225 |
}, |
| 226 |
|
| 227 |
_processFlickrData: function(data){ |
| 228 |
var items = []; |
| 229 |
if(data.items){ |
| 230 |
items = data.items; |
| 231 |
|
| 232 |
for(var i = 0; i < data.items.length; i++){ |
| 233 |
var item = data.items[i]; |
| 234 |
item[this._storeRef] = this; |
| 235 |
} |
| 236 |
} |
| 237 |
return items; |
| 238 |
}, |
| 239 |
|
| 240 |
_unescapeHtml: function( str){ |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
return str.replace(/&/gm, "&"). |
| 254 |
replace(/</gm, "<"). |
| 255 |
replace(/>/gm, ">"). |
| 256 |
replace(/"/gm, "\""). |
| 257 |
replace(/'/gm, "'"); |
| 258 |
} |
| 259 |
}); |
| 260 |
|
| 261 |
lang.extend(FlickrStore, simpleFetch); |
| 262 |
|
| 263 |
var feedsUrl = "https://api.flickr.com/services/feeds/"; |
| 264 |
|
| 265 |
var reg = FlickrStore.urlRegistry = new AdapterRegistry(true); |
| 266 |
|
| 267 |
reg.register("group pool", |
| 268 |
function(request){ return !!request.query["groupid"]; }, |
| 269 |
feedsUrl+"groups_pool.gne" |
| 270 |
); |
| 271 |
|
| 272 |
reg.register("default", |
| 273 |
function(request){ return true; }, |
| 274 |
feedsUrl+"photos_public.gne" |
| 275 |
); |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
if(!jsonFlickrFeed){ |
| 280 |
var jsonFlickrFeed = function(data){}; |
| 281 |
} |
| 282 |
|
| 283 |
return FlickrStore; |
| 284 |
}); |