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");
5 dojo.require("dojo.data.util.simpleFetch");
6 dojo.require("dojo.io.script");
7 dojo.require("dojo.date.stamp");
9 dojo.declare("dojox.data.PicasaStore", null, {
10 constructor: function(/*Object*/args){
12 // Initializer for the PicasaStore store.
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;
24 _picasaUrl: "http://picasaweb.google.com/data/feed/api/all",
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.PicasaStore: 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.PicasaStore: 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 ["id", "published", "updated", "category", "title$type", "title", "summary$type", "summary", "rights$type", "rights", "link", "author", "gphoto$id", "gphoto$name", "location"];
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[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)];
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 picasa 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 = {alt: "jsonm", pp: "1", psc: "G"};
178 content['start-index'] = "1";
179 if(request.query.start){
180 content['start-index'] = request.query.start;
182 if(request.query.tags){
183 content.q = request.query.tags;
185 if(request.query.userid){
186 content.uname = request.query.userid;
188 if(request.query.userids){
189 content.ids = request.query.userids;
191 if(request.query.lang){
192 content.hl = request.query.lang;
195 content['max-results'] = request.count;
197 content['max-results'] = "20";
200 //Linking this up to Picasa is a JOY!
203 var myHandler = function(data){
205 dojo.disconnect(handle);
208 //Process the items...
209 fetchHandler(self._processPicasaData(data), request);
212 url: this._picasaUrl,
213 // preventCache: true,
215 callbackParamName: 'callback',
218 var deferred = dojo.io.script.get(getArgs);
220 deferred.addErrback(function(error){
221 dojo.disconnect(handle);
222 errorHandler(error, request);
226 _processPicasaData: function(data){
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++){
233 item[this._storeRef] = this;
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.
243 // The string to un-escape
244 // returns: HTML String converted back to the normal text (unescaped) characters (<,>,&, ", etc,).
246 //TODO: Check to see if theres already compatible escape() in dojo.string or dojo.html
247 str = str.replace(/&/gm, "&").replace(/</gm, "<").replace(/>/gm, ">").replace(/"/gm, "\"");
248 str = str.replace(/'/gm, "'");
252 dojo.extend(dojox.data.PicasaStore,dojo.data.util.simpleFetch);