FlickrBadge.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.image.FlickrBadge"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.image.FlickrBadge"] = true;
  8. dojo.provide("dojox.image.FlickrBadge");
  9. dojo.require("dojox.image.Badge");
  10. dojo.require("dojox.data.FlickrRestStore");
  11. dojo.declare("dojox.image.FlickrBadge", dojox.image.Badge, {
  12. children: "a.flickrImage",
  13. // userid: String
  14. // If you know your Flickr userid, you can set it to prevent a call to fetch the id
  15. userid: "",
  16. // username: String
  17. // Your Flickr username
  18. username: "",
  19. // setid: String
  20. // The id of the set to display
  21. setid: "",
  22. // tags: String|Array
  23. // A comma separated list of tags or an array of tags to grab from Flickr
  24. tags: "",
  25. // searchText: String
  26. // Free text search. Photos who's title, description, or tags contain the text will be displayed
  27. searchText: "",
  28. // target: String
  29. // Where to display the pictures when clicked on. Valid values are the same as the target attribute
  30. // of the A tag.
  31. target: "",
  32. apikey: "8c6803164dbc395fb7131c9d54843627",
  33. _store: null,
  34. postCreate: function(){
  35. if(this.username && !this.userid){
  36. var def = dojo.io.script.get({
  37. url: "http://www.flickr.com/services/rest/",
  38. preventCache: true,
  39. content: {
  40. format: "json",
  41. method: "flickr.people.findByUsername",
  42. api_key: this.apikey,
  43. username: this.username
  44. },
  45. callbackParamName: "jsoncallback"
  46. });
  47. def.addCallback(this, function(data){
  48. if(data.user && data.user.nsid){
  49. this.userid = data.user.nsid;
  50. if(!this._started){
  51. this.startup();
  52. }
  53. }
  54. });
  55. }
  56. },
  57. startup: function(){
  58. if(this._started){ return; }
  59. if(this.userid){
  60. var query = {
  61. userid: this.userid
  62. };
  63. if(this.setid){
  64. query["setid"] = this.setid;
  65. }
  66. if(this.tags){
  67. query.tags = this.tags;
  68. }
  69. if(this.searchText){
  70. query.text = this.searchText;
  71. }
  72. var args = arguments;
  73. this._store = new dojox.data.FlickrRestStore({ apikey: this.apikey });
  74. this._store.fetch({
  75. count: this.cols * this.rows,
  76. query: query,
  77. onComplete: dojo.hitch(this, function(items){
  78. dojo.forEach(items, function(item){
  79. var a = dojo.doc.createElement("a");
  80. dojo.addClass(a, "flickrImage");
  81. a.href = this._store.getValue(item, "link");
  82. if(this.target){
  83. a.target = this.target;
  84. }
  85. var img = dojo.doc.createElement("img");
  86. img.src = this._store.getValue(item, "imageUrlThumb");
  87. dojo.style(img, {
  88. width: "100%",
  89. height: "100%"
  90. });
  91. a.appendChild(img);
  92. this.domNode.appendChild(a);
  93. }, this);
  94. dojox.image.Badge.prototype.startup.call(this, args);
  95. })
  96. });
  97. }
  98. }
  99. });
  100. }