]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojox/data/PicasaStore.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dojox / data / PicasaStore.js
1 if(!dojo._hasResource["dojox.data.PicasaStore"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.data.PicasaStore"] = true;
3 dojo.provide("dojox.data.PicasaStore");
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.PicasaStore", null, {
10         constructor: function(/*Object*/args){
11                 //      summary:
12                 //              Initializer for the PicasaStore store.  
13                 //      description:
14                 //              The PicasaStore is a Datastore interface to one of the basic services
15                 //              of the Picasa service, the public photo feed.  This does not provide
16                 //              access to all the services of Picasa.
17                 //              This store cannot do * and ? filtering as the picasa service 
18                 //              provides no interface for wildcards.
19                 if(args && args.label){
20                         this.label = args.label;
21                 }
22         },
23
24         _picasaUrl: "http://picasaweb.google.com/data/feed/api/all",
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.PicasaStore: 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.PicasaStore: 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 ["id", "published", "updated", "category", "title$type", "title", "summary$type", "summary", "rights$type", "rights", "link", "author", "gphoto$id", "gphoto$name", "location"]; 
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[0].name)];
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.thumbnail[1].url];
134                 }else if(attribute === "imageUrl"){
135                         return [item.content$src];
136                 }else if(attribute === "imageUrlMedium"){
137                         return [item.media.thumbnail[2].url];
138                 }else if(attribute === "link"){
139                         return [item.link[1]];
140                 }else if(attribute === "tags"){
141                         return item.tags.split(" ");
142                 }else if(attribute === "description"){
143                         return [this._unescapeHtml(item.summary)];
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 picasa 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 = {alt: "jsonm", pp: "1", psc: "G"};
178                 content['start-index'] = "1";
179                 if(request.query.start){
180                         content['start-index'] = request.query.start;
181                 }
182                 if(request.query.tags){
183                         content.q = request.query.tags;
184                 }
185                 if(request.query.userid){
186                         content.uname = request.query.userid;
187                 }
188                 if(request.query.userids){
189                         content.ids = request.query.userids;
190                 }
191                 if(request.query.lang){
192                         content.hl = request.query.lang;
193                 }
194                 if(request.count){
195                         content['max-results'] = request.count;
196                 }else{
197                         content['max-results'] = "20";
198                 }
199
200                 //Linking this up to Picasa is a JOY!
201                 var self = this;
202                 var handle = null;
203                 var myHandler = function(data){
204                         if(handle !== null){
205                                 dojo.disconnect(handle);
206                         }
207
208                         //Process the items...
209                         fetchHandler(self._processPicasaData(data), request);
210                 };
211                 var getArgs = {
212                         url: this._picasaUrl,
213                         // preventCache: true,
214                         content: content,
215                         callbackParamName: 'callback',
216                         handle: myHandler
217                 };
218                 var deferred = dojo.io.script.get(getArgs);
219                 
220                 deferred.addErrback(function(error){
221                         dojo.disconnect(handle);
222                         errorHandler(error, request);
223                 });
224         },
225
226         _processPicasaData: function(data){
227                 var items = [];
228                 if(data.feed){
229                         items = data.feed.entry;
230                         //Add on the store ref so that isItem can work.
231                         for(var i = 0; i < items.length; i++){
232                                 var item = items[i];
233                                 item[this._storeRef] = this;
234                         }
235                 }
236                 return items;
237         },
238
239         _unescapeHtml: function(str){
240                 // summary: Utility function to un-escape XML special characters in an HTML string.
241                 // description: Utility function to un-escape XML special characters in an HTML string.
242                 // str: String.
243                 //   The string to un-escape
244                 // returns: HTML String converted back to the normal text (unescaped) characters (<,>,&, ", etc,).
245                 //
246                 //TODO: Check to see if theres already compatible escape() in dojo.string or dojo.html
247                 str = str.replace(/&amp;/gm, "&").replace(/&lt;/gm, "<").replace(/&gt;/gm, ">").replace(/&quot;/gm, "\"");
248                 str = str.replace(/&#39;/gm, "'"); 
249                 return str;
250         }
251 });
252 dojo.extend(dojox.data.PicasaStore,dojo.data.util.simpleFetch);                                                                           
253
254 }