Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2150 mathias 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");
4
 
5
dojo.require("dojo.data.util.simpleFetch");
6
dojo.require("dojo.io.script");
7
dojo.require("dojo.date.stamp");
8
 
9
dojo.declare("dojox.data.FlickrStore", null, {
10
	constructor: function(/*Object*/args){
11
		//	summary:
12
		//		Initializer for the FlickrStore store.
13
		//	description:
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;
21
		}
22
	},
23
 
24
	_flickrUrl: "http://api.flickr.com/services/feeds/photos_public.gne",
25
 
26
	_storeRef: "_S",
27
 
28
	label: "title",
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){
59
		//	summary:
60
		//      See dojo.data.api.Read.getValue()
61
		var values = this.getValues(item, attribute);
62
		if(values){
63
			return values[0];
64
		}
65
		return undefined;
66
	},
67
 
68
	getAttributes: function(item){
69
		//	summary:
70
		//      See dojo.data.api.Read.getAttributes()
71
		return ["title", "description", "author", "datePublished", "dateTaken", "imageUrl", "imageUrlSmall", "imageUrlMedium", "tags", "link"];
72
	},
73
 
74
	hasAttribute: function(item, attribute){
75
		//	summary:
76
		//      See dojo.data.api.Read.hasAttributes()
77
		if(this.getValue(item,attribute)){
78
			return true;
79
		}
80
		return false;
81
	},
82
 
83
	isItemLoaded: function(item){
84
		 //	summary:
85
		 //      See dojo.data.api.Read.isItemLoaded()
86
		 return this.isItem(item);
87
	},
88
 
89
	loadItem: function(keywordArgs){
90
		//	summary:
91
		//      See dojo.data.api.Read.loadItem()
92
	},
93
 
94
	getLabel: function(item){
95
		//	summary:
96
		//      See dojo.data.api.Read.getLabel()
97
		return this.getValue(item,this.label);
98
	},
99
 
100
	getLabelAttributes: function(item){
101
		//	summary:
102
		//      See dojo.data.api.Read.getLabelAttributes()
103
		return [this.label];
104
	},
105
 
106
	containsValue: function(item, attribute, value){
107
		//	summary:
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){
112
				return true;
113
			}
114
		}
115
		return false;
116
	},
117
 
118
	getValues: function(item, attribute){
119
		//	summary:
120
		//      See dojo.data.api.Read.getValue()
121
 
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"){
139
			return [item.link];
140
		}else if(attribute === "tags"){
141
			return item.tags.split(" ");
142
		}else if(attribute === "description"){
143
			return [this._unescapeHtml(item.description)];
144
		}
145
		return undefined;
146
	},
147
 
148
	isItem: function(item){
149
		//	summary:
150
		//      See dojo.data.api.Read.isItem()
151
		if(item && item[this._storeRef] === this){
152
			return true;
153
		}
154
		return false;
155
	},
156
 
157
	close: function(request){
158
		//	summary:
159
		//      See dojo.data.api.Read.close()
160
	},
161
 
162
	_fetchItems: function(request, fetchHandler, errorHandler){
163
		//	summary:
164
		//		Fetch flickr items that match to a query
165
		//	request:
166
		//		A request object
167
		//	fetchHandler:
168
		//		A function to call for fetched items
169
		//	errorHandler:
170
		//		A function to call on error
171
 
172
		if(!request.query){
173
			request.query={};
174
		}
175
 
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;
180
		}
181
		if (request.query.tagmode) {
182
			content.tagmode = request.query.tagmode;
183
		}
184
		if (request.query.userid) {
185
			content.id = request.query.userid;
186
		}
187
		if (request.query.userids) {
188
			content.ids = request.query.userids;
189
		}
190
		if (request.query.lang) {
191
			content.lang = request.query.lang;
192
		}
193
 
194
		//Linking this up to Flickr is a PAIN!
195
		var self = this;
196
		var handle = null;
197
		var getArgs = {
198
			url: this._flickrUrl,
199
			preventCache: true,
200
			content: content
201
		};
202
		var myHandler = function(data){
203
			if(handle !== null){
204
				dojo.disconnect(handle);
205
			}
206
 
207
			//Process the items...
208
			fetchHandler(self._processFlickrData(data), request);
209
		};
210
        handle = dojo.connect("jsonFlickrFeed", myHandler);
211
		var deferred = dojo.io.script.get(getArgs);
212
 
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);
218
		});
219
	},
220
 
221
	_processFlickrData: function(data){
222
		 var items = [];
223
		 if(data.items){
224
			 items = data.items;
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;
229
			 }
230
		 }
231
		 return items;
232
	},
233
 
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.
237
		//
238
		// str: String.
239
		//   The string to un-escape
240
		// returns: HTML String converted back to the normal text (unescaped) characters (<,>,&, ", etc,).
241
		//
242
		//TODO: Check to see if theres already compatible escape() in dojo.string or dojo.html
243
		str = str.replace(/&amp;/gm, "&").replace(/&lt;/gm, "<").replace(/&gt;/gm, ">").replace(/&quot;/gm, "\"");
244
		str = str.replace(/&#39;/gm, "'");
245
		return str;
246
	}
247
});
248
dojo.extend(dojox.data.FlickrStore,dojo.data.util.simpleFetch);
249
 
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){};
254
}
255
 
256
 
257
}