1 if(!dojo._hasResource["dojox.data.FlickrStore"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.data.FlickrStore"] = true;
3 dojo.provide("dojox.data.FlickrStore");
5 dojo.require("dojo.data.util.simpleFetch");
6 dojo.require("dojo.io.script");
7 dojo.require("dojo.date.stamp");
9 dojo.declare("dojox.data.FlickrStore", null, {
10 constructor: function(/*Object*/args){
12 // Initializer for the FlickrStore store.
14 // The FlickrStore is a Datastore interface to one of the basic services
15 // of the Flickr service, the public photo feed. This does not provide
16 // access to all the services of Flickr.
17 // This store cannot do * and ? filtering as the flickr service
18 // provides no interface for wildcards.
19 if(args && args.label){
20 this.label = args.label;
24 _flickrUrl: "http://api.flickr.com/services/feeds/photos_public.gne",
30 _assertIsItem: function(/* item */ item){
32 // This function tests whether the item passed in is indeed an item in the store.
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");
40 _assertIsAttribute: function(/* attribute-name-string */ attribute){
42 // This function tests whether the item passed in is indeed a valid 'attribute' like type for the store.
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");
50 getFeatures: function(){
52 // See dojo.data.api.Read.getFeatures()
54 'dojo.data.api.Read': true
58 getValue: function(item, attribute){
60 // See dojo.data.api.Read.getValue()
61 var values = this.getValues(item, attribute);
68 getAttributes: function(item){
70 // See dojo.data.api.Read.getAttributes()
71 return ["title", "description", "author", "datePublished", "dateTaken", "imageUrl", "imageUrlSmall", "imageUrlMedium", "tags", "link"];
74 hasAttribute: function(item, attribute){
76 // See dojo.data.api.Read.hasAttributes()
77 if(this.getValue(item,attribute)){
83 isItemLoaded: function(item){
85 // See dojo.data.api.Read.isItemLoaded()
86 return this.isItem(item);
89 loadItem: function(keywordArgs){
91 // See dojo.data.api.Read.loadItem()
94 getLabel: function(item){
96 // See dojo.data.api.Read.getLabel()
97 return this.getValue(item,this.label);
100 getLabelAttributes: function(item){
102 // See dojo.data.api.Read.getLabelAttributes()
106 containsValue: function(item, attribute, value){
108 // See dojo.data.api.Read.containsValue()
109 var values = this.getValues(item,attribute);
110 for(var i = 0; i < values.length; i++){
111 if(values[i] === value){
118 getValues: function(item, attribute){
120 // See dojo.data.api.Read.getValue()
122 this._assertIsItem(item);
123 this._assertIsAttribute(attribute);
124 if(attribute === "title"){
125 return [this._unescapeHtml(item.title)];
126 }else if(attribute === "author"){
127 return [this._unescapeHtml(item.author)];
128 }else if(attribute === "datePublished"){
129 return [dojo.date.stamp.fromISOString(item.published)];
130 }else if(attribute === "dateTaken"){
131 return [dojo.date.stamp.fromISOString(item.date_taken)];
132 }else if(attribute === "imageUrlSmall"){
133 return [item.media.m.replace(/_m\./, "_s.")];
134 }else if(attribute === "imageUrl"){
135 return [item.media.m.replace(/_m\./, ".")];
136 }else if(attribute === "imageUrlMedium"){
137 return [item.media.m];
138 }else if(attribute === "link"){
140 }else if(attribute === "tags"){
141 return item.tags.split(" ");
142 }else if(attribute === "description"){
143 return [this._unescapeHtml(item.description)];
148 isItem: function(item){
150 // See dojo.data.api.Read.isItem()
151 if(item && item[this._storeRef] === this){
157 close: function(request){
159 // See dojo.data.api.Read.close()
162 _fetchItems: function(request, fetchHandler, errorHandler){
164 // Fetch flickr items that match to a query
168 // A function to call for fetched items
170 // A function to call on error
176 //Build up the content to send the request for.
177 var content = {format: "json", tagmode:"any"};
178 if (request.query.tags) {
179 content.tags = request.query.tags;
181 if (request.query.tagmode) {
182 content.tagmode = request.query.tagmode;
184 if (request.query.userid) {
185 content.id = request.query.userid;
187 if (request.query.userids) {
188 content.ids = request.query.userids;
190 if (request.query.lang) {
191 content.lang = request.query.lang;
194 //Linking this up to Flickr is a PAIN!
198 url: this._flickrUrl,
202 var myHandler = function(data){
204 dojo.disconnect(handle);
207 //Process the items...
208 fetchHandler(self._processFlickrData(data), request);
210 handle = dojo.connect("jsonFlickrFeed", myHandler);
211 var deferred = dojo.io.script.get(getArgs);
213 //We only set up the errback, because the callback isn't ever really used because we have
214 //to link to the jsonFlickrFeed function....
215 deferred.addErrback(function(error){
216 dojo.disconnect(handle);
217 errorHandler(error, request);
221 _processFlickrData: function(data){
225 //Add on the store ref so that isItem can work.
226 for(var i = 0; i < data.items.length; i++){
227 var item = data.items[i];
228 item[this._storeRef] = this;
234 _unescapeHtml: function(str){
235 // summary: Utility function to un-escape XML special characters in an HTML string.
236 // description: Utility function to un-escape XML special characters in an HTML string.
239 // The string to un-escape
240 // returns: HTML String converted back to the normal text (unescaped) characters (<,>,&, ", etc,).
242 //TODO: Check to see if theres already compatible escape() in dojo.string or dojo.html
243 str = str.replace(/&/gm, "&").replace(/</gm, "<").replace(/>/gm, ">").replace(/"/gm, "\"");
244 str = str.replace(/'/gm, "'");
248 dojo.extend(dojox.data.FlickrStore,dojo.data.util.simpleFetch);
250 //We have to define this because of how the Flickr API works.
251 //This somewhat stinks, but what can you do?
252 if (!jsonFlickrFeed) {
253 var jsonFlickrFeed = function(data){};