FlickrStore.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. if (!dojo._hasResource["dojox.data.FlickrStore"]) { // _hasResource checks added
  2. // by build. Do not use
  3. // _hasResource directly in
  4. // your code.
  5. dojo._hasResource["dojox.data.FlickrStore"] = true;
  6. dojo.provide("dojox.data.FlickrStore");
  7. dojo.require("dojo.data.util.simpleFetch");
  8. dojo.require("dojo.io.script");
  9. dojo.require("dojo.date.stamp");
  10. dojo.declare("dojox.data.FlickrStore", null, {
  11. constructor : function(/* Object */args) {
  12. // summary:
  13. // Initializer for the FlickrStore store.
  14. // description:
  15. // The FlickrStore is a Datastore interface to one of the basic
  16. // services
  17. // of the Flickr service, the public photo feed. This does not
  18. // provide
  19. // access to all the services of Flickr.
  20. // This store cannot do * and ? filtering as the flickr service
  21. // provides no interface for wildcards.
  22. if (args && args.label) {
  23. this.label = args.label;
  24. }
  25. },
  26. _flickrUrl : "http://api.flickr.com/services/feeds/photos_public.gne",
  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.FlickrStore: 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.FlickrStore: 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 ["title", "description", "author", "datePublished",
  69. "dateTaken", "imageUrl", "imageUrlSmall", "imageUrlMedium",
  70. "tags", "link"];
  71. },
  72. hasAttribute : function(item, attribute) {
  73. // summary:
  74. // See dojo.data.api.Read.hasAttributes()
  75. if (this.getValue(item, attribute)) {
  76. return true;
  77. }
  78. return false;
  79. },
  80. isItemLoaded : function(item) {
  81. // summary:
  82. // See dojo.data.api.Read.isItemLoaded()
  83. return this.isItem(item);
  84. },
  85. loadItem : function(keywordArgs) {
  86. // summary:
  87. // See dojo.data.api.Read.loadItem()
  88. },
  89. getLabel : function(item) {
  90. // summary:
  91. // See dojo.data.api.Read.getLabel()
  92. return this.getValue(item, this.label);
  93. },
  94. getLabelAttributes : function(item) {
  95. // summary:
  96. // See dojo.data.api.Read.getLabelAttributes()
  97. return [this.label];
  98. },
  99. containsValue : function(item, attribute, value) {
  100. // summary:
  101. // See dojo.data.api.Read.containsValue()
  102. var values = this.getValues(item, attribute);
  103. for (var i = 0; i < values.length; i++) {
  104. if (values[i] === value) {
  105. return true;
  106. }
  107. }
  108. return false;
  109. },
  110. getValues : function(item, attribute) {
  111. // summary:
  112. // See dojo.data.api.Read.getValue()
  113. this._assertIsItem(item);
  114. this._assertIsAttribute(attribute);
  115. if (attribute === "title") {
  116. return [this._unescapeHtml(item.title)];
  117. } else if (attribute === "author") {
  118. return [this._unescapeHtml(item.author)];
  119. } else if (attribute === "datePublished") {
  120. return [dojo.date.stamp.fromISOString(item.published)];
  121. } else if (attribute === "dateTaken") {
  122. return [dojo.date.stamp.fromISOString(item.date_taken)];
  123. } else if (attribute === "imageUrlSmall") {
  124. return [item.media.m.replace(/_m\./, "_s.")];
  125. } else if (attribute === "imageUrl") {
  126. return [item.media.m.replace(/_m\./, ".")];
  127. } else if (attribute === "imageUrlMedium") {
  128. return [item.media.m];
  129. } else if (attribute === "link") {
  130. return [item.link];
  131. } else if (attribute === "tags") {
  132. return item.tags.split(" ");
  133. } else if (attribute === "description") {
  134. return [this._unescapeHtml(item.description)];
  135. }
  136. return undefined;
  137. },
  138. isItem : function(item) {
  139. // summary:
  140. // See dojo.data.api.Read.isItem()
  141. if (item && item[this._storeRef] === this) {
  142. return true;
  143. }
  144. return false;
  145. },
  146. close : function(request) {
  147. // summary:
  148. // See dojo.data.api.Read.close()
  149. },
  150. _fetchItems : function(request, fetchHandler, errorHandler) {
  151. // summary:
  152. // Fetch flickr items that match to a query
  153. // request:
  154. // A request object
  155. // fetchHandler:
  156. // A function to call for fetched items
  157. // errorHandler:
  158. // A function to call on error
  159. if (!request.query) {
  160. request.query = {};
  161. }
  162. // Build up the content to send the request for.
  163. var content = {
  164. format : "json",
  165. tagmode : "any"
  166. };
  167. if (request.query.tags) {
  168. content.tags = request.query.tags;
  169. }
  170. if (request.query.tagmode) {
  171. content.tagmode = request.query.tagmode;
  172. }
  173. if (request.query.userid) {
  174. content.id = request.query.userid;
  175. }
  176. if (request.query.userids) {
  177. content.ids = request.query.userids;
  178. }
  179. if (request.query.lang) {
  180. content.lang = request.query.lang;
  181. }
  182. // Linking this up to Flickr is a PAIN!
  183. var self = this;
  184. var handle = null;
  185. var getArgs = {
  186. url : this._flickrUrl,
  187. preventCache : true,
  188. content : content
  189. };
  190. var myHandler = function(data) {
  191. if (handle !== null) {
  192. dojo.disconnect(handle);
  193. }
  194. // Process the items...
  195. fetchHandler(self._processFlickrData(data), request);
  196. };
  197. handle = dojo.connect("jsonFlickrFeed", myHandler);
  198. var deferred = dojo.io.script.get(getArgs);
  199. // We only set up the errback, because the callback isn't ever
  200. // really used because we have
  201. // to link to the jsonFlickrFeed function....
  202. deferred.addErrback(function(error) {
  203. dojo.disconnect(handle);
  204. errorHandler(error, request);
  205. });
  206. },
  207. _processFlickrData : function(data) {
  208. var items = [];
  209. if (data.items) {
  210. items = data.items;
  211. // Add on the store ref so that isItem can work.
  212. for (var i = 0; i < data.items.length; i++) {
  213. var item = data.items[i];
  214. item[this._storeRef] = this;
  215. }
  216. }
  217. return items;
  218. },
  219. _unescapeHtml : function(str) {
  220. // summary: Utility function to un-escape XML special characters in
  221. // an HTML string.
  222. // description: Utility function to un-escape XML special characters
  223. // in an HTML string.
  224. //
  225. // str: String.
  226. // The string to un-escape
  227. // returns: HTML String converted back to the normal text
  228. // (unescaped) characters (<,>,&, ", etc,).
  229. //
  230. // TODO: Check to see if theres already compatible escape() in
  231. // dojo.string or dojo.html
  232. str = str.replace(/&amp;/gm, "&").replace(/&lt;/gm, "<").replace(
  233. /&gt;/gm, ">").replace(/&quot;/gm, "\"");
  234. str = str.replace(/&#39;/gm, "'");
  235. return str;
  236. }
  237. });
  238. dojo.extend(dojox.data.FlickrStore, dojo.data.util.simpleFetch);
  239. // We have to define this because of how the Flickr API works.
  240. // This somewhat stinks, but what can you do?
  241. if (!jsonFlickrFeed) {
  242. var jsonFlickrFeed = function(data) {
  243. };
  244. }
  245. }