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