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(/*Object*/args){ |
---|
7 | // summary: |
---|
8 | // Initializer for the FlickrStore store. |
---|
9 | // description: |
---|
10 | // The FlickrStore is a Datastore interface to one of the basic services |
---|
11 | // of the Flickr service, the public photo feed. This does not provide |
---|
12 | // access to all the services of Flickr. |
---|
13 | // This store cannot do * and ? filtering as the flickr service |
---|
14 | // provides no interface for wildcards. |
---|
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 | //Flag to allor control of if cache prevention is enabled or not. |
---|
28 | urlPreventCache: true, |
---|
29 | |
---|
30 | _assertIsItem: function(/* item */ item){ |
---|
31 | // summary: |
---|
32 | // This function tests whether the item passed in is indeed an item in the store. |
---|
33 | // item: |
---|
34 | // The item to test for being contained by the store. |
---|
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-name-string */ attribute){ |
---|
41 | // summary: |
---|
42 | // This function tests whether the item passed in is indeed a valid 'attribute' like type for the store. |
---|
43 | // attribute: |
---|
44 | // The attribute to test for being contained by the store. |
---|
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 | // summary: |
---|
52 | // See dojo/data/api/Read.getFeatures() |
---|
53 | return { |
---|
54 | 'dojo.data.api.Read': true |
---|
55 | }; |
---|
56 | }, |
---|
57 | |
---|
58 | getValue: function(item, attribute, defaultValue){ |
---|
59 | // summary: |
---|
60 | // See dojo/data/api/Read.getValue() |
---|
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 | // summary: |
---|
70 | // See dojo/data/api/Read.getAttributes() |
---|
71 | return [ |
---|
72 | "title", "description", "author", "datePublished", "dateTaken", |
---|
73 | "imageUrl", "imageUrlSmall", "imageUrlMedium", "tags", "link" |
---|
74 | ]; |
---|
75 | }, |
---|
76 | |
---|
77 | hasAttribute: function(item, attribute){ |
---|
78 | // summary: |
---|
79 | // See dojo/data/api/Read.hasAttributes() |
---|
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 | // summary: |
---|
89 | // See dojo/data/api/Read.isItemLoaded() |
---|
90 | return this.isItem(item); |
---|
91 | }, |
---|
92 | |
---|
93 | loadItem: function(keywordArgs){ |
---|
94 | // summary: |
---|
95 | // See dojo/data/api/Read.loadItem() |
---|
96 | }, |
---|
97 | |
---|
98 | getLabel: function(item){ |
---|
99 | // summary: |
---|
100 | // See dojo/data/api/Read.getLabel() |
---|
101 | return this.getValue(item,this.label); |
---|
102 | }, |
---|
103 | |
---|
104 | getLabelAttributes: function(item){ |
---|
105 | // summary: |
---|
106 | // See dojo/data/api/Read.getLabelAttributes() |
---|
107 | return [this.label]; |
---|
108 | }, |
---|
109 | |
---|
110 | containsValue: function(item, attribute, value){ |
---|
111 | // summary: |
---|
112 | // See dojo/data/api/Read.containsValue() |
---|
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 | // summary: |
---|
124 | // See dojo/data/api/Read.getValue() |
---|
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 | // summary: |
---|
158 | // See dojo/data/api/Read.isItem() |
---|
159 | if(item && item[this._storeRef] === this){ |
---|
160 | return true; |
---|
161 | } |
---|
162 | return false; |
---|
163 | }, |
---|
164 | |
---|
165 | close: function(request){ |
---|
166 | // summary: |
---|
167 | // See dojo/data/api/Read.close() |
---|
168 | }, |
---|
169 | |
---|
170 | _fetchItems: function(request, fetchHandler, errorHandler){ |
---|
171 | // summary: |
---|
172 | // Fetch flickr items that match to a query |
---|
173 | // request: |
---|
174 | // A request object |
---|
175 | // fetchHandler: |
---|
176 | // A function to call for fetched items |
---|
177 | // errorHandler: |
---|
178 | // A function to call on error |
---|
179 | |
---|
180 | var rq = request.query = request.query || {}; |
---|
181 | |
---|
182 | //Build up the content to send the request for. |
---|
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 | //Linking this up to Flickr is a PAIN! |
---|
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 | //Process the items... |
---|
214 | fetchHandler(this._processFlickrData(data), request); |
---|
215 | }); |
---|
216 | handle = connect.connect("jsonFlickrFeed", myHandler); |
---|
217 | var deferred = scriptIO.get(getArgs); |
---|
218 | |
---|
219 | //We only set up the errback, because the callback isn't ever really used because we have |
---|
220 | //to link to the jsonFlickrFeed function.... |
---|
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 | //Add on the store ref so that isItem can work. |
---|
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(/*String*/ str){ |
---|
241 | // summary: |
---|
242 | // Utility function to un-escape XML special characters in an |
---|
243 | // HTML string. |
---|
244 | // str: String. |
---|
245 | // The string to un-escape |
---|
246 | // returns: |
---|
247 | // HTML String converted back to the normal text (unescaped) |
---|
248 | // characters (<,>,&, ", etc,). |
---|
249 | |
---|
250 | //TODO: |
---|
251 | // Check to see if theres already compatible escape() in |
---|
252 | // dojo.string or dojo.html |
---|
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 = "http://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 | //We have to define this because of how the Flickr API works. |
---|
278 | //This somewhat stinks, but what can you do? |
---|
279 | if(!jsonFlickrFeed){ |
---|
280 | var jsonFlickrFeed = function(data){}; |
---|
281 | } |
---|
282 | |
---|
283 | return FlickrStore; |
---|
284 | }); |
---|