FlickrBadge.js 2.7 KB

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