PicasaStore.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.data.PicasaStore"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.data.PicasaStore"] = true;
  8. dojo.provide("dojox.data.PicasaStore");
  9. dojo.require("dojo.io.script");
  10. dojo.require("dojo.data.util.simpleFetch");
  11. dojo.require("dojo.date.stamp");
  12. dojo.declare("dojox.data.PicasaStore", null, {
  13. constructor: function(/*Object*/args){
  14. // summary:
  15. // Initializer for the PicasaStore store.
  16. // description:
  17. // The PicasaStore is a Datastore interface to one of the basic services
  18. // of the Picasa service, the public photo feed. This does not provide
  19. // access to all the services of Picasa.
  20. // This store cannot do * and ? filtering as the picasa service
  21. // provides no interface for wildcards.
  22. if(args && args.label){
  23. this.label = args.label;
  24. }
  25. if(args && "urlPreventCache" in args){
  26. this.urlPreventCache = args.urlPreventCache?true:false;
  27. }
  28. if(args && "maxResults" in args){
  29. this.maxResults = parseInt(args.maxResults);
  30. if(!this.maxResults){
  31. this.maxResults = 20;
  32. }
  33. }
  34. },
  35. _picasaUrl: "http://picasaweb.google.com/data/feed/api/all",
  36. _storeRef: "_S",
  37. //label: string
  38. //The attribute to use from the picasa item as its label.
  39. label: "title",
  40. //urlPreventCache: boolean
  41. //Flag denoting if preventCache should be passed to dojo.io.script.
  42. urlPreventCache: false,
  43. //maxResults: Define out how many results to return for a fetch.
  44. maxResults: 20,
  45. _assertIsItem: function(/* item */ item){
  46. // summary:
  47. // This function tests whether the item passed in is indeed an item in the store.
  48. // item:
  49. // The item to test for being contained by the store.
  50. if(!this.isItem(item)){
  51. throw new Error("dojox.data.PicasaStore: a function was passed an item argument that was not an item");
  52. }
  53. },
  54. _assertIsAttribute: function(/* attribute-name-string */ attribute){
  55. // summary:
  56. // This function tests whether the item passed in is indeed a valid 'attribute' like type for the store.
  57. // attribute:
  58. // The attribute to test for being contained by the store.
  59. if(typeof attribute !== "string"){
  60. throw new Error("dojox.data.PicasaStore: a function was passed an attribute argument that was not an attribute name string");
  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. getValue: function(item, attribute, defaultValue){
  71. // summary:
  72. // See dojo.data.api.Read.getValue()
  73. var values = this.getValues(item, attribute);
  74. if(values && values.length > 0){
  75. return values[0];
  76. }
  77. return defaultValue;
  78. },
  79. getAttributes: function(item){
  80. // summary:
  81. // See dojo.data.api.Read.getAttributes()
  82. return ["id", "published", "updated", "category", "title$type", "title",
  83. "summary$type", "summary", "rights$type", "rights", "link", "author",
  84. "gphoto$id", "gphoto$name", "location", "imageUrlSmall", "imageUrlMedium",
  85. "imageUrl", "datePublished", "dateTaken","description"];
  86. },
  87. hasAttribute: function(item, attribute){
  88. // summary:
  89. // See dojo.data.api.Read.hasAttributes()
  90. if(this.getValue(item,attribute)){
  91. return true;
  92. }
  93. return false;
  94. },
  95. isItemLoaded: function(item){
  96. // summary:
  97. // See dojo.data.api.Read.isItemLoaded()
  98. return this.isItem(item);
  99. },
  100. loadItem: function(keywordArgs){
  101. // summary:
  102. // See dojo.data.api.Read.loadItem()
  103. },
  104. getLabel: function(item){
  105. // summary:
  106. // See dojo.data.api.Read.getLabel()
  107. return this.getValue(item,this.label);
  108. },
  109. getLabelAttributes: function(item){
  110. // summary:
  111. // See dojo.data.api.Read.getLabelAttributes()
  112. return [this.label];
  113. },
  114. containsValue: function(item, attribute, value){
  115. // summary:
  116. // See dojo.data.api.Read.containsValue()
  117. var values = this.getValues(item,attribute);
  118. for(var i = 0; i < values.length; i++){
  119. if(values[i] === value){
  120. return true;
  121. }
  122. }
  123. return false;
  124. },
  125. getValues: function(item, attribute){
  126. // summary:
  127. // See dojo.data.api.Read.getValue()
  128. this._assertIsItem(item);
  129. this._assertIsAttribute(attribute);
  130. if(attribute === "title"){
  131. return [this._unescapeHtml(item.title)];
  132. }else if(attribute === "author"){
  133. return [this._unescapeHtml(item.author[0].name)];
  134. }else if(attribute === "datePublished"){
  135. return [dojo.date.stamp.fromISOString(item.published)];
  136. }else if(attribute === "dateTaken"){
  137. return [dojo.date.stamp.fromISOString(item.published)];
  138. }else if(attribute === "updated"){
  139. return [dojo.date.stamp.fromISOString(item.updated)];
  140. }else if(attribute === "imageUrlSmall"){
  141. return [item.media.thumbnail[1].url];
  142. }else if(attribute === "imageUrl"){
  143. return [item.content$src];
  144. }else if(attribute === "imageUrlMedium"){
  145. return [item.media.thumbnail[2].url];
  146. }else if(attribute === "link"){
  147. return [item.link[1]];
  148. }else if(attribute === "tags"){
  149. return item.tags.split(" ");
  150. }else if(attribute === "description"){
  151. return [this._unescapeHtml(item.summary)];
  152. }
  153. return [];
  154. },
  155. isItem: function(item){
  156. // summary:
  157. // See dojo.data.api.Read.isItem()
  158. if(item && item[this._storeRef] === this){
  159. return true;
  160. }
  161. return false;
  162. },
  163. close: function(request){
  164. // summary:
  165. // See dojo.data.api.Read.close()
  166. },
  167. _fetchItems: function(request, fetchHandler, errorHandler){
  168. // summary:
  169. // Fetch picasa items that match to a query
  170. // request:
  171. // A request object
  172. // fetchHandler:
  173. // A function to call for fetched items
  174. // errorHandler:
  175. // A function to call on error
  176. if(!request.query){
  177. request.query={};
  178. }
  179. //Build up the content to send the request for.
  180. var content = {alt: "jsonm", pp: "1", psc: "G"};
  181. content['start-index'] = "1";
  182. if(request.query.start){
  183. content['start-index'] = request.query.start;
  184. }
  185. if(request.query.tags){
  186. content.q = request.query.tags;
  187. }
  188. if(request.query.userid){
  189. content.uname = request.query.userid;
  190. }
  191. if(request.query.userids){
  192. content.ids = request.query.userids;
  193. }
  194. if(request.query.lang){
  195. content.hl = request.query.lang;
  196. }
  197. content['max-results'] = this.maxResults;
  198. //Linking this up to Picasa is a JOY!
  199. var self = this;
  200. var handle = null;
  201. var myHandler = function(data){
  202. if(handle !== null){
  203. dojo.disconnect(handle);
  204. }
  205. //Process the items...
  206. fetchHandler(self._processPicasaData(data), request);
  207. };
  208. var getArgs = {
  209. url: this._picasaUrl,
  210. preventCache: this.urlPreventCache,
  211. content: content,
  212. callbackParamName: 'callback',
  213. handle: myHandler
  214. };
  215. var deferred = dojo.io.script.get(getArgs);
  216. deferred.addErrback(function(error){
  217. dojo.disconnect(handle);
  218. errorHandler(error, request);
  219. });
  220. },
  221. _processPicasaData: function(data){
  222. var items = [];
  223. if(data.feed){
  224. items = data.feed.entry;
  225. //Add on the store ref so that isItem can work.
  226. for(var i = 0; i < items.length; i++){
  227. var item = items[i];
  228. item[this._storeRef] = this;
  229. }
  230. }
  231. return items;
  232. },
  233. _unescapeHtml: function(str){
  234. // summary: Utility function to un-escape XML special characters in an HTML string.
  235. // description: Utility function to un-escape XML special characters in an HTML string.
  236. // str: String.
  237. // The string to un-escape
  238. // returns: HTML String converted back to the normal text (unescaped) characters (<,>,&, ", etc,).
  239. //
  240. //TODO: Check to see if theres already compatible escape() in dojo.string or dojo.html
  241. if(str){
  242. str = str.replace(/&amp;/gm, "&").replace(/&lt;/gm, "<").replace(/&gt;/gm, ">").replace(/&quot;/gm, "\"");
  243. str = str.replace(/&#39;/gm, "'");
  244. }
  245. return str;
  246. }
  247. });
  248. dojo.extend(dojox.data.PicasaStore,dojo.data.util.simpleFetch);
  249. }