Gallery.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.Gallery"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.image.Gallery"] = true;
  8. dojo.provide("dojox.image.Gallery");
  9. dojo.experimental("dojox.image.Gallery");
  10. //
  11. // dojox.image.Gallery courtesy Shane O Sullivan, licensed under a Dojo CLA
  12. //
  13. // For a sample usage, see http://www.skynet.ie/~sos/photos.php
  14. //
  15. // TODO: Make public, document params and privitize non-API conformant methods.
  16. // document topics.
  17. dojo.require("dojo.fx");
  18. dojo.require("dijit._Widget");
  19. dojo.require("dijit._Templated");
  20. dojo.require("dojox.image.ThumbnailPicker");
  21. dojo.require("dojox.image.SlideShow");
  22. dojo.declare("dojox.image.Gallery",
  23. [dijit._Widget, dijit._Templated],
  24. {
  25. // summary:
  26. // Gallery widget that wraps a dojox.image.ThumbnailPicker and dojox.image.SlideShow widget
  27. //
  28. // imageHeight: Number
  29. // Maximum height of an image in the SlideShow widget
  30. imageHeight: 375,
  31. // imageWidth: Number
  32. // Maximum width of an image in the SlideShow widget
  33. imageWidth: 500,
  34. // pageSize: Number
  35. // The number of records to retrieve from the data store per request.
  36. pageSize: dojox.image.SlideShow.prototype.pageSize,
  37. // autoLoad: Boolean
  38. // If true, images are loaded before the user views them. If false, an
  39. // image is loaded when the user displays it.
  40. autoLoad: true,
  41. // linkAttr: String
  42. // Defines the name of the attribute to request from the store to retrieve the
  43. // URL to link to from an image, if any.
  44. linkAttr: "link",
  45. // imageThumbAttr: String
  46. // Defines the name of the attribute to request from the store to retrieve the
  47. // URL to the thumbnail image.
  48. imageThumbAttr: "imageUrlThumb",
  49. // imageLargeAttr: String
  50. // Defines the name of the attribute to request from the store to retrieve the
  51. // URL to the image.
  52. imageLargeAttr: "imageUrl",
  53. // titleAttr: String
  54. // Defines the name of the attribute to request from the store to retrieve the
  55. // title of the picture, if any.
  56. titleAttr: "title",
  57. // slideshowInterval: Integer
  58. // Time, in seconds, between image changes in the slide show.
  59. slideshowInterval: 3,
  60. templateString: dojo.cache("dojox.image", "resources/Gallery.html", "<div dojoAttachPoint=\"outerNode\" class=\"imageGalleryWrapper\">\n\t<div dojoAttachPoint=\"thumbPickerNode\"></div>\n\t<div dojoAttachPoint=\"slideShowNode\"></div>\n</div>\n"),
  61. postCreate: function(){
  62. // summary:
  63. // Initializes the widget, creates the ThumbnailPicker and SlideShow widgets
  64. this.widgetid = this.id;
  65. this.inherited(arguments)
  66. this.thumbPicker = new dojox.image.ThumbnailPicker({
  67. linkAttr: this.linkAttr,
  68. imageLargeAttr: this.imageLargeAttr,
  69. imageThumbAttr: this.imageThumbAttr,
  70. titleAttr: this.titleAttr,
  71. useLoadNotifier: true,
  72. size: this.imageWidth
  73. }, this.thumbPickerNode);
  74. this.slideShow = new dojox.image.SlideShow({
  75. imageHeight: this.imageHeight,
  76. imageWidth: this.imageWidth,
  77. autoLoad: this.autoLoad,
  78. linkAttr: this.linkAttr,
  79. imageLargeAttr: this.imageLargeAttr,
  80. titleAttr: this.titleAttr,
  81. slideshowInterval: this.slideshowInterval,
  82. pageSize: this.pageSize
  83. }, this.slideShowNode);
  84. var _this = this;
  85. //When an image is shown in the Slideshow, make sure it is visible
  86. //in the ThumbnailPicker
  87. dojo.subscribe(this.slideShow.getShowTopicName(), function(packet){
  88. _this.thumbPicker._showThumbs(packet.index);
  89. });
  90. //When the user clicks a thumbnail, show that image
  91. dojo.subscribe(this.thumbPicker.getClickTopicName(), function(evt){
  92. _this.slideShow.showImage(evt.index);
  93. });
  94. //When the ThumbnailPicker moves to show a new set of pictures,
  95. //make the Slideshow start loading those pictures first.
  96. dojo.subscribe(this.thumbPicker.getShowTopicName(), function(evt){
  97. _this.slideShow.moveImageLoadingPointer(evt.index);
  98. });
  99. //When an image finished loading in the slideshow, update the loading
  100. //notification in the ThumbnailPicker
  101. dojo.subscribe(this.slideShow.getLoadTopicName(), function(index){
  102. _this.thumbPicker.markImageLoaded(index);
  103. });
  104. this._centerChildren();
  105. },
  106. setDataStore: function(dataStore, request, /*optional*/paramNames){
  107. // summary:
  108. // Sets the data store and request objects to read data from.
  109. // dataStore:
  110. // An implementation of the dojo.data.api.Read API. This accesses the image
  111. // data.
  112. // request:
  113. // An implementation of the dojo.data.api.Request API. This specifies the
  114. // query and paging information to be used by the data store
  115. // paramNames:
  116. // An object defining the names of the item attributes to fetch from the
  117. // data store. The four attributes allowed are 'linkAttr', 'imageLargeAttr',
  118. // 'imageThumbAttr' and 'titleAttr'
  119. this.thumbPicker.setDataStore(dataStore, request, paramNames);
  120. this.slideShow.setDataStore(dataStore, request, paramNames);
  121. },
  122. reset: function(){
  123. // summary:
  124. // Resets the widget to its initial state
  125. this.slideShow.reset();
  126. this.thumbPicker.reset();
  127. },
  128. showNextImage: function(inTimer){
  129. // summary:
  130. // Changes the image being displayed in the SlideShow to the next
  131. // image in the data store
  132. // inTimer: Boolean
  133. // If true, a slideshow is active, otherwise the slideshow is inactive.
  134. this.slideShow.showNextImage();
  135. },
  136. toggleSlideshow: function(){
  137. dojo.deprecated("dojox.widget.Gallery.toggleSlideshow is deprecated. Use toggleSlideShow instead.", "", "2.0");
  138. this.toggleSlideShow();
  139. },
  140. toggleSlideShow: function(){
  141. // summary:
  142. // Switches the slideshow mode on and off.
  143. this.slideShow.toggleSlideShow();
  144. },
  145. showImage: function(index, /*optional*/callback){
  146. // summary:
  147. // Shows the image at index 'idx'.
  148. // idx: Number
  149. // The position of the image in the data store to display
  150. // callback: Function
  151. // Optional callback function to call when the image has finished displaying.
  152. this.slideShow.showImage(index, callback);
  153. },
  154. resize: function(dim){
  155. this.thumbPicker.resize(dim);
  156. },
  157. _centerChildren: function() {
  158. // summary:
  159. // Ensures that the ThumbnailPicker and the SlideShow widgets
  160. // are centered.
  161. var thumbSize = dojo.marginBox(this.thumbPicker.outerNode);
  162. var slideSize = dojo.marginBox(this.slideShow.outerNode);
  163. var diff = (thumbSize.w - slideSize.w) / 2;
  164. if(diff > 0) {
  165. dojo.style(this.slideShow.outerNode, "marginLeft", diff + "px");
  166. } else if(diff < 0) {
  167. dojo.style(this.thumbPicker.outerNode, "marginLeft", (diff * -1) + "px");
  168. }
  169. }
  170. });
  171. }