PicasaStore.js 7.3 KB

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