Debian Codegraph @

dojox/data/PicasaStore.js

raw symbols SCIP
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(/*Object*/args){
6 // summary:
7 // Initializer for the PicasaStore store.
8 // description:
9 // The PicasaStore is a Datastore interface to one of the basic services
10 // of the Picasa service, the public photo feed. This does not provide
11 // access to all the services of Picasa.
12 // This store cannot do * and ? filtering as the picasa service
13 // provides no interface for wildcards.
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 // label: string
33 // The attribute to use from the picasa item as its label.
34 label: "title",
35
36 // urlPreventCache: boolean
37 // Flag denoting if preventCache should be passed to io.script.
38 urlPreventCache: false,
39
40 // maxResults: Number
41 // Define out how many results to return for a fetch.
42 maxResults: 20,
43
44 _assertIsItem: function(/* item */ item){
45 // summary:
46 // This function tests whether the item passed in is indeed an item in the store.
47 // item:
48 // The item to test for being contained by the store.
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-name-string */ attribute){
55 // summary:
56 // This function tests whether the item passed in is indeed a valid 'attribute' like type for the store.
57 // attribute:
58 // The attribute to test for being contained by the store.
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 // summary:
66 // See dojo/data/api/Read.getFeatures()
67 return {
68 'dojo.data.api.Read': true
69 };
70 },
71
72 getValue: function(item, attribute, defaultValue){
73 // summary:
74 // See dojo/data/api/Read.getValue()
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 // summary:
84 // See dojo/data/api/Read.getAttributes()
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 // summary:
93 // See dojo/data/api/Read.hasAttributes()
94 if(this.getValue(item,attribute)){
95 return true;
96 }
97 return false;
98 },
99
100 isItemLoaded: function(item){
101 // summary:
102 // See dojo/data/api/Read.isItemLoaded()
103 return this.isItem(item);
104 },
105
106 loadItem: function(keywordArgs){
107 // summary:
108 // See dojo/data/api/Read.loadItem()
109 },
110
111 getLabel: function(item){
112 // summary:
113 // See dojo/data/api/Read.getLabel()
114 return this.getValue(item,this.label);
115 },
116
117 getLabelAttributes: function(item){
118 // summary:
119 // See dojo/data/api/Read.getLabelAttributes()
120 return [this.label];
121 },
122
123 containsValue: function(item, attribute, value){
124 // summary:
125 // See dojo/data/api/Read.containsValue()
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 // summary:
137 // See dojo/data/api/Read.getValue()
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 // summary:
169 // See dojo/data/api/Read.isItem()
170 if(item && item[this._storeRef] === this){
171 return true;
172 }
173 return false;
174 },
175
176 close: function(request){
177 // summary:
178 // See dojo/data/api/Read.close()
179 },
180
181 _fetchItems: function(request, fetchHandler, errorHandler){
182 // summary:
183 // Fetch picasa items that match to a query
184 // request:
185 // A request object
186 // fetchHandler:
187 // A function to call for fetched items
188 // errorHandler:
189 // A function to call on error
190
191 if(!request.query){
192 request.query={};
193 }
194
195 //Build up the content to send the request for.
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 //Linking this up to Picasa is a JOY!
217 var self = this;
218 var handle = null;
219 var myHandler = function(data){
220 if(handle !== null){
221 connect.disconnect(handle);
222 }
223
224 //Process the items...
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 //Add on the store ref so that isItem can work.
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 // summary:
257 // Utility function to un-escape XML special characters in an HTML string.
258 // description:
259 // Utility function to un-escape XML special characters in an HTML string.
260 // str: String
261 // The string to un-escape
262 // returns:
263 // HTML String converted back to the normal text (unescaped) characters (<,>,&, ", etc,).
264
265 // TODO: Check to see if theres already compatible escape() in dojo.string or dojo.html
266 if(str){
267 str = str.replace(/&amp;/gm, "&").replace(/&lt;/gm, "<").replace(/&gt;/gm, ">").replace(/&quot;/gm, "\"");
268 str = str.replace(/&#39;/gm, "'");
269 }
270 return str;
271 }
272 });
273 lang.extend(PicasaStore, simpleFetch);
274
275 return PicasaStore;
276
277 });