source: Dev/branches/rest-dojo-ui/client/dojox/data/PicasaStore.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 7.5 KB
Line 
1define(["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
4var 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:  Define out how many results to return for a fetch.
41        maxResults: 20,
42
43        _assertIsItem: function(/* item */ item){
44                //      summary:
45                //      This function tests whether the item passed in is indeed an item in the store.
46                //      item:
47                //              The item to test for being contained by the store.
48                if(!this.isItem(item)){
49                        throw new Error("dojox.data.PicasaStore: a function was passed an item argument that was not an item");
50                }
51        },
52
53        _assertIsAttribute: function(/* attribute-name-string */ attribute){
54                //      summary:
55                //              This function tests whether the item passed in is indeed a valid 'attribute' like type for the store.
56                //      attribute:
57                //              The attribute to test for being contained by the store.
58                if(typeof attribute !== "string"){
59                        throw new Error("dojox.data.PicasaStore: a function was passed an attribute argument that was not an attribute name string");
60                }
61        },
62
63        getFeatures: function(){
64                //      summary:
65                //      See dojo.data.api.Read.getFeatures()
66                return {
67                        'dojo.data.api.Read': true
68                };
69        },
70
71        getValue: function(item, attribute, defaultValue){
72                //      summary:
73                //      See dojo.data.api.Read.getValue()
74                var values = this.getValues(item, attribute);
75                if(values && values.length > 0){
76                        return values[0];
77                }
78                return defaultValue;
79        },
80
81        getAttributes: function(item){
82                //      summary:
83                //      See dojo.data.api.Read.getAttributes()
84                 return ["id", "published", "updated", "category", "title$type", "title",
85                         "summary$type", "summary", "rights$type", "rights", "link", "author",
86                         "gphoto$id", "gphoto$name", "location", "imageUrlSmall", "imageUrlMedium",
87                         "imageUrl", "datePublished", "dateTaken","description"];
88        },
89
90        hasAttribute: function(item, attribute){
91                //      summary:
92                //      See dojo.data.api.Read.hasAttributes()
93                if(this.getValue(item,attribute)){
94                        return true;
95                }
96                return false;
97        },
98
99        isItemLoaded: function(item){
100                 //     summary:
101                 //      See dojo.data.api.Read.isItemLoaded()
102                 return this.isItem(item);
103        },
104
105        loadItem: function(keywordArgs){
106                //      summary:
107                //      See dojo.data.api.Read.loadItem()
108        },
109
110        getLabel: function(item){
111                //      summary:
112                //      See dojo.data.api.Read.getLabel()
113                return this.getValue(item,this.label);
114        },
115       
116        getLabelAttributes: function(item){
117                //      summary:
118                //      See dojo.data.api.Read.getLabelAttributes()
119                return [this.label];
120        },
121
122        containsValue: function(item, attribute, value){
123                //      summary:
124                //      See dojo.data.api.Read.containsValue()
125                var values = this.getValues(item,attribute);
126                for(var i = 0; i < values.length; i++){
127                        if(values[i] === value){
128                                return true;
129                        }
130                }
131                return false;
132        },
133
134        getValues: function(item, attribute){
135                //      summary:
136                //      See dojo.data.api.Read.getValue()
137
138                this._assertIsItem(item);
139                this._assertIsAttribute(attribute);
140                if(attribute === "title"){
141                        return [this._unescapeHtml(item.title)];
142                }else if(attribute === "author"){
143                        return [this._unescapeHtml(item.author[0].name)];
144                }else if(attribute === "datePublished"){
145                        return [dateAtamp.fromISOString(item.published)];
146                }else if(attribute === "dateTaken"){
147                        return [dateStamp.fromISOString(item.published)];
148                }else if(attribute === "updated"){
149                        return [dateStamp.fromISOString(item.updated)];
150                }else if(attribute === "imageUrlSmall"){
151                        return [item.media.thumbnail[1].url];
152                }else if(attribute === "imageUrl"){
153                        return [item.content$src];
154                }else if(attribute === "imageUrlMedium"){
155                        return [item.media.thumbnail[2].url];
156                }else if(attribute === "link"){
157                        return [item.link[1]];
158                }else if(attribute === "tags"){
159                        return item.tags.split(" ");
160                }else if(attribute === "description"){
161                        return [this._unescapeHtml(item.summary)];
162                }
163                return [];
164        },
165
166        isItem: function(item){
167                //      summary:
168                //      See dojo.data.api.Read.isItem()
169                if(item && item[this._storeRef] === this){
170                        return true;
171                }
172                return false;
173        },
174       
175        close: function(request){
176                //      summary:
177                //      See dojo.data.api.Read.close()
178        },
179
180        _fetchItems: function(request, fetchHandler, errorHandler){
181                //      summary:
182                //              Fetch picasa items that match to a query
183                //      request:
184                //              A request object
185                //      fetchHandler:
186                //              A function to call for fetched items
187                //      errorHandler:
188                //              A function to call on error
189
190                if(!request.query){
191                        request.query={};
192                }
193
194                //Build up the content to send the request for.
195                var content = {alt: "jsonm", pp: "1", psc: "G"};
196
197                content['start-index'] = "1";
198                if(request.query.start){
199                        content['start-index'] = request.query.start;
200                }
201                if(request.query.tags){
202                        content.q = request.query.tags;
203                }
204                if(request.query.userid){
205                        content.uname = request.query.userid;
206                }
207                if(request.query.userids){
208                        content.ids = request.query.userids;
209                }
210                if(request.query.lang){
211                        content.hl = request.query.lang;
212                }
213                content['max-results'] = this.maxResults;
214
215                //Linking this up to Picasa is a JOY!
216                var self = this;
217                var handle = null;
218                var myHandler = function(data){
219                        if(handle !== null){
220                                connect.disconnect(handle);
221                        }
222
223                        //Process the items...
224                        fetchHandler(self._processPicasaData(data), request);
225                };
226                var getArgs = {
227                        url: this._picasaUrl,
228                        preventCache: this.urlPreventCache,
229                        content: content,
230                        callbackParamName: 'callback',
231                        handle: myHandler
232                };
233                var deferred = scriptIO.get(getArgs);
234               
235                deferred.addErrback(function(error){
236                        connect.disconnect(handle);
237                        errorHandler(error, request);
238                });
239        },
240
241        _processPicasaData: function(data){
242                var items = [];
243                if(data.feed){
244                        items = data.feed.entry;
245                        //Add on the store ref so that isItem can work.
246                        for(var i = 0; i < items.length; i++){
247                                var item = items[i];
248                                item[this._storeRef] = this;
249                        }
250                }
251                return items;
252        },
253
254        _unescapeHtml: function(str){
255                // summary: Utility function to un-escape XML special characters in an HTML string.
256                // description: Utility function to un-escape XML special characters in an HTML string.
257                // str: String.
258                //   The string to un-escape
259                // returns: HTML String converted back to the normal text (unescaped) characters (<,>,&, ", etc,).
260                //
261                //TODO: Check to see if theres already compatible escape() in dojo.string or dojo.html
262                if(str){
263                        str = str.replace(/&amp;/gm, "&").replace(/&lt;/gm, "<").replace(/&gt;/gm, ">").replace(/&quot;/gm, "\"");
264                        str = str.replace(/&#39;/gm, "'");
265                }
266                return str;
267        }
268});
269lang.extend(PicasaStore, simpleFetch);
270
271return PicasaStore;
272
273});
Note: See TracBrowser for help on using the repository browser.