ThumbnailPicker.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. // wrapped by build app
  2. define("dojox/image/ThumbnailPicker", ["dijit","dojo","dojox","dojo/require!dojox/fx/scroll,dojo/fx/easing,dojo/fx,dijit/_Widget,dijit/_Templated"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.image.ThumbnailPicker");
  4. dojo.experimental("dojox.image.ThumbnailPicker");
  5. //
  6. // dojox.image.ThumbnailPicker courtesy Shane O Sullivan, licensed under a Dojo CLA
  7. //
  8. // For a sample usage, see http://www.skynet.ie/~sos/photos.php
  9. //
  10. // document topics.
  11. dojo.require("dojox.fx.scroll"); // is optional, but don't want to dojo[require] it
  12. dojo.require("dojo.fx.easing");
  13. dojo.require("dojo.fx");
  14. dojo.require("dijit._Widget");
  15. dojo.require("dijit._Templated");
  16. // FIXME: use CSS for size, thumbHeight, and thumbWidth
  17. dojo.declare("dojox.image.ThumbnailPicker",
  18. [dijit._Widget, dijit._Templated],
  19. {
  20. // summary: A scrolling Thumbnail Picker widget
  21. //
  22. // imageStore: Object
  23. // A data store that implements the dojo.data Read API.
  24. imageStore: null,
  25. // request: Object
  26. // A dojo.data Read API Request object.
  27. request: null,
  28. // size: Number
  29. // Width or height in pixels, depending if horizontal or vertical.
  30. size: 500,
  31. // thumbHeight: Number
  32. // Default height of a thumbnail image
  33. thumbHeight: 75,
  34. // thumbWidth: Number
  35. // Default width of an image
  36. thumbWidth: 100,
  37. // useLoadNotifier: Boolean
  38. // Setting useLoadNotifier to true makes a colored DIV appear under each
  39. // thumbnail image, which is used to display the loading status of each
  40. // image in the data store.
  41. useLoadNotifier: false,
  42. // useHyperlink: boolean
  43. // Setting useHyperlink to true causes a click on a thumbnail to open a link.
  44. useHyperlink: false,
  45. // hyperlinkTarget: String
  46. // If hyperlinkTarget is set to "new", clicking on a thumb will open a new window
  47. // If it is set to anything else, clicking a thumbnail will open the url in the
  48. // current window.
  49. hyperlinkTarget: "new",
  50. // isClickable: Boolean
  51. // When set to true, the cursor over a thumbnail changes.
  52. isClickable: true,
  53. // isScrollable: Boolean
  54. // When true, uses smoothScroll to move between pages
  55. isScrollable: true,
  56. // isHorizontal: Boolean
  57. // If true, the thumbnails are displayed horizontally. Otherwise they are displayed
  58. // vertically
  59. isHorizontal: true,
  60. //autoLoad: Boolean
  61. autoLoad: true,
  62. // linkAttr: String
  63. // The attribute name for accessing the url from the data store
  64. linkAttr: "link",
  65. // imageThumbAttr: String
  66. // The attribute name for accessing the thumbnail image url from the data store
  67. imageThumbAttr: "imageUrlThumb",
  68. // imageLargeAttr: String
  69. // The attribute name for accessing the large image url from the data store
  70. imageLargeAttr: "imageUrl",
  71. // pageSize: Number
  72. // The number of images to request each time.
  73. pageSize: 20,
  74. // titleAttr: String
  75. // The attribute name for accessing the title from the data store
  76. titleAttr: "title",
  77. templateString: dojo.cache("dojox.image", "resources/ThumbnailPicker.html", "<div dojoAttachPoint=\"outerNode\" class=\"thumbOuter\">\n\t<div dojoAttachPoint=\"navPrev\" class=\"thumbNav thumbClickable\">\n\t <img src=\"\" dojoAttachPoint=\"navPrevImg\"/> \n\t</div>\n\t<div dojoAttachPoint=\"thumbScroller\" class=\"thumbScroller\">\n\t <div dojoAttachPoint=\"thumbsNode\" class=\"thumbWrapper\"></div>\n\t</div>\n\t<div dojoAttachPoint=\"navNext\" class=\"thumbNav thumbClickable\">\n\t <img src=\"\" dojoAttachPoint=\"navNextImg\"/> \n\t</div>\n</div>"),
  78. // thumbs: Array
  79. // Stores the image nodes for the thumbnails.
  80. _thumbs: [],
  81. // _thumbIndex: Number
  82. // The index of the first thumbnail shown
  83. _thumbIndex: 0,
  84. // _maxPhotos: Number
  85. // The total number of photos in the image store
  86. _maxPhotos: 0,
  87. // _loadedImages: Object
  88. // Stores the indices of images that have been marked as loaded using the
  89. // markImageLoaded function.
  90. _loadedImages: {},
  91. postCreate: function(){
  92. // summary:
  93. // Initializes styles and listeners
  94. this.widgetid = this.id;
  95. this.inherited(arguments);
  96. this.pageSize = Number(this.pageSize);
  97. this._scrollerSize = this.size - (51 * 2);
  98. var sizeProp = this._sizeProperty = this.isHorizontal ? "width" : "height";
  99. // FIXME: do this via css? calculate the correct width for the widget
  100. dojo.style(this.outerNode, "textAlign","center");
  101. dojo.style(this.outerNode, sizeProp, this.size+"px");
  102. dojo.style(this.thumbScroller, sizeProp, this._scrollerSize + "px");
  103. //If useHyperlink is true, then listen for a click on a thumbnail, and
  104. //open the link
  105. if(this.useHyperlink){
  106. dojo.subscribe(this.getClickTopicName(), this, function(packet){
  107. var index = packet.index;
  108. var url = this.imageStore.getValue(packet.data,this.linkAttr);
  109. //If the data item doesn't contain a URL, do nothing
  110. if(!url){return;}
  111. if(this.hyperlinkTarget == "new"){
  112. window.open(url);
  113. }else{
  114. window.location = url;
  115. }
  116. });
  117. }
  118. if(this.isClickable){
  119. dojo.addClass(this.thumbsNode, "thumbClickable");
  120. }
  121. this._totalSize = 0;
  122. this.init();
  123. },
  124. init: function(){
  125. // summary:
  126. // Creates DOM nodes for thumbnail images and initializes their listeners
  127. if(this.isInitialized) {return false;}
  128. var classExt = this.isHorizontal ? "Horiz" : "Vert";
  129. // FIXME: can we setup a listener around the whole element and determine based on e.target?
  130. dojo.addClass(this.navPrev, "prev" + classExt);
  131. dojo.addClass(this.navNext, "next" + classExt);
  132. dojo.addClass(this.thumbsNode, "thumb"+classExt);
  133. dojo.addClass(this.outerNode, "thumb"+classExt);
  134. dojo.attr(this.navNextImg, "src", this._blankGif);
  135. dojo.attr(this.navPrevImg, "src", this._blankGif);
  136. this.connect(this.navPrev, "onclick", "_prev");
  137. this.connect(this.navNext, "onclick", "_next");
  138. this.isInitialized = true;
  139. if(this.isHorizontal){
  140. this._offsetAttr = "offsetLeft";
  141. this._sizeAttr = "offsetWidth";
  142. this._scrollAttr = "scrollLeft";
  143. }else{
  144. this._offsetAttr = "offsetTop";
  145. this._sizeAttr = "offsetHeight";
  146. this._scrollAttr = "scrollTop";
  147. }
  148. this._updateNavControls();
  149. if(this.imageStore && this.request){this._loadNextPage();}
  150. return true;
  151. },
  152. getClickTopicName: function(){
  153. // summary:
  154. // Returns the name of the dojo topic that can be
  155. // subscribed to in order to receive notifications on
  156. // which thumbnail was selected.
  157. return (this.widgetId || this.id) + "/select"; // String
  158. },
  159. getShowTopicName: function(){
  160. // summary:
  161. // Returns the name of the dojo topic that can be
  162. // subscribed to in order to receive notifications on
  163. // which thumbnail is now visible
  164. return (this.widgetId || this.id) + "/show"; // String
  165. },
  166. setDataStore: function(dataStore, request, /*optional*/paramNames){
  167. // summary:
  168. // Sets the data store and request objects to read data from.
  169. // dataStore:
  170. // An implementation of the dojo.data.api.Read API. This accesses the image
  171. // data.
  172. // request:
  173. // An implementation of the dojo.data.api.Request API. This specifies the
  174. // query and paging information to be used by the data store
  175. // paramNames:
  176. // An object defining the names of the item attributes to fetch from the
  177. // data store. The four attributes allowed are 'linkAttr', 'imageLargeAttr',
  178. // 'imageThumbAttr' and 'titleAttr'
  179. this.reset();
  180. this.request = {
  181. query: {},
  182. start: request.start || 0,
  183. count: request.count || 10,
  184. onBegin: dojo.hitch(this, function(total){
  185. this._maxPhotos = total;
  186. })
  187. };
  188. if(request.query){ dojo.mixin(this.request.query, request.query);}
  189. if(paramNames){
  190. dojo.forEach(["imageThumbAttr", "imageLargeAttr", "linkAttr", "titleAttr"], function(attrName){
  191. if(paramNames[attrName]){ this[attrName] = paramNames[attrName]; }
  192. }, this);
  193. }
  194. this.request.start = 0;
  195. this.request.count = this.pageSize;
  196. this.imageStore = dataStore;
  197. this._loadInProgress = false;
  198. if(!this.init()){this._loadNextPage();}
  199. },
  200. reset: function(){
  201. // summary:
  202. // Resets the widget back to its original state.
  203. this._loadedImages = {};
  204. dojo.forEach(this._thumbs, function(img){
  205. if(img && img.parentNode){
  206. dojo.destroy(img);
  207. }
  208. });
  209. this._thumbs = [];
  210. this.isInitialized = false;
  211. this._noImages = true;
  212. },
  213. isVisible: function(index) {
  214. // summary:
  215. // Returns true if the image at the specified index is currently visible. False otherwise.
  216. var img = this._thumbs[index];
  217. if(!img){return false;}
  218. var pos = this.isHorizontal ? "offsetLeft" : "offsetTop";
  219. var size = this.isHorizontal ? "offsetWidth" : "offsetHeight";
  220. var scrollAttr = this.isHorizontal ? "scrollLeft" : "scrollTop";
  221. var offset = img[pos] - this.thumbsNode[pos];
  222. return (offset >= this.thumbScroller[scrollAttr]
  223. && offset + img[size] <= this.thumbScroller[scrollAttr] + this._scrollerSize);
  224. },
  225. resize: function(dim){
  226. var sizeParam = this.isHorizontal ? "w": "h";
  227. var total = 0;
  228. if(this._thumbs.length > 0 && dojo.marginBox(this._thumbs[0]).w == 0){
  229. // Skip the resize if the widget is not visible
  230. return;
  231. }
  232. // Calculate the complete size of the thumbnails
  233. dojo.forEach(this._thumbs, dojo.hitch(this, function(imgContainer){
  234. var mb = dojo.marginBox(imgContainer.firstChild);
  235. var size = mb[sizeParam];
  236. total += (Number(size) + 10);
  237. if(this.useLoadNotifier && mb.w > 0){
  238. dojo.style(imgContainer.lastChild, "width", (mb.w - 4) + "px");
  239. }
  240. dojo.style(imgContainer, "width", mb.w + "px");
  241. }));
  242. dojo.style(this.thumbsNode, this._sizeProperty, total + "px");
  243. this._updateNavControls();
  244. },
  245. _next: function() {
  246. // summary:
  247. // Displays the next page of images
  248. var pos = this.isHorizontal ? "offsetLeft" : "offsetTop";
  249. var size = this.isHorizontal ? "offsetWidth" : "offsetHeight";
  250. var baseOffset = this.thumbsNode[pos];
  251. var firstThumb = this._thumbs[this._thumbIndex];
  252. var origOffset = firstThumb[pos] - baseOffset;
  253. var index = -1, img;
  254. for(var i = this._thumbIndex + 1; i < this._thumbs.length; i++){
  255. img = this._thumbs[i];
  256. if(img[pos] - baseOffset + img[size] - origOffset > this._scrollerSize){
  257. this._showThumbs(i);
  258. return;
  259. }
  260. }
  261. },
  262. _prev: function(){
  263. // summary:
  264. // Displays the next page of images
  265. if(this.thumbScroller[this.isHorizontal ? "scrollLeft" : "scrollTop"] == 0){return;}
  266. var pos = this.isHorizontal ? "offsetLeft" : "offsetTop";
  267. var size = this.isHorizontal ? "offsetWidth" : "offsetHeight";
  268. var firstThumb = this._thumbs[this._thumbIndex];
  269. var origOffset = firstThumb[pos] - this.thumbsNode[pos];
  270. var index = -1, img;
  271. for(var i = this._thumbIndex - 1; i > -1; i--) {
  272. img = this._thumbs[i];
  273. if(origOffset - img[pos] > this._scrollerSize){
  274. this._showThumbs(i + 1);
  275. return;
  276. }
  277. }
  278. this._showThumbs(0);
  279. },
  280. _checkLoad: function(img, index){
  281. // summary:
  282. // Checks if an image is loaded.
  283. dojo.publish(this.getShowTopicName(), [{index:index}]);
  284. this._updateNavControls();
  285. this._loadingImages = {};
  286. this._thumbIndex = index;
  287. //If we have not already requested the data from the store, do so.
  288. if(this.thumbsNode.offsetWidth - img.offsetLeft < (this._scrollerSize * 2)){
  289. this._loadNextPage();
  290. }
  291. },
  292. _showThumbs: function(index){
  293. // summary:
  294. // Displays thumbnail images, starting at position 'index'
  295. // index: Number
  296. // The index of the first thumbnail
  297. //FIXME: When is this be called with an invalid index? Do we need this check at all?
  298. // if(typeof index != "number"){ index = this._thumbIndex; }
  299. index = Math.min(Math.max(index, 0), this._maxPhotos);
  300. if(index >= this._maxPhotos){ return; }
  301. var img = this._thumbs[index];
  302. if(!img){ return; }
  303. var left = img.offsetLeft - this.thumbsNode.offsetLeft;
  304. var top = img.offsetTop - this.thumbsNode.offsetTop;
  305. var offset = this.isHorizontal ? left : top;
  306. if( (offset >= this.thumbScroller[this._scrollAttr]) &&
  307. (offset + img[this._sizeAttr] <= this.thumbScroller[this._scrollAttr] + this._scrollerSize)
  308. ){
  309. // FIXME: WTF is this checking for?
  310. return;
  311. }
  312. if(this.isScrollable){
  313. var target = this.isHorizontal ? {x: left, y: 0} : { x:0, y:top};
  314. dojox.fx.smoothScroll({
  315. target: target,
  316. win: this.thumbScroller,
  317. duration:300,
  318. easing:dojo.fx.easing.easeOut,
  319. onEnd: dojo.hitch(this, "_checkLoad", img, index)
  320. }).play(10);
  321. }else{
  322. if(this.isHorizontal){
  323. this.thumbScroller.scrollLeft = left;
  324. }else{
  325. this.thumbScroller.scrollTop = top;
  326. }
  327. this._checkLoad(img, index);
  328. }
  329. },
  330. markImageLoaded: function(index){
  331. // summary:
  332. // Changes a visual cue to show the image is loaded
  333. // description:
  334. // If 'useLoadNotifier' is set to true, then a visual cue is
  335. // given to state whether the image is loaded or not. Calling this function
  336. // marks an image as loaded.
  337. var thumbNotifier = dojo.byId("loadingDiv_"+this.widgetid+"_"+index);
  338. if(thumbNotifier){this._setThumbClass(thumbNotifier, "thumbLoaded");}
  339. this._loadedImages[index] = true;
  340. },
  341. _setThumbClass: function(thumb, className){
  342. // summary:
  343. // Adds a CSS class to a thumbnail, only if 'autoLoad' is true
  344. // thumb: DomNode
  345. // The thumbnail DOM node to set the class on
  346. // className: String
  347. // The CSS class to add to the DOM node.
  348. if(!this.autoLoad){ return; }
  349. dojo.addClass(thumb, className);
  350. },
  351. _loadNextPage: function(){
  352. // summary:
  353. // Loads the next page of thumbnail images
  354. if(this._loadInProgress){return;}
  355. this._loadInProgress = true;
  356. var start = this.request.start + (this._noImages ? 0 : this.pageSize);
  357. var pos = start;
  358. while(pos < this._thumbs.length && this._thumbs[pos]){pos ++;}
  359. var store = this.imageStore;
  360. //Define the function to call when the items have been
  361. //returned from the data store.
  362. var complete = function(items, request){
  363. if(store != this.imageStore){
  364. // If the store has been changed, ignore this callback.
  365. return;
  366. }
  367. if(items && items.length){
  368. var itemCounter = 0;
  369. var loadNext = dojo.hitch(this, function(){
  370. if(itemCounter >= items.length){
  371. this._loadInProgress = false;
  372. return;
  373. }
  374. var counter = itemCounter++;
  375. this._loadImage(items[counter], pos + counter, loadNext);
  376. });
  377. loadNext();
  378. //Show or hide the navigation arrows on the thumbnails,
  379. //depending on whether or not the widget is at the start,
  380. //end, or middle of the list of images.
  381. this._updateNavControls();
  382. }else{
  383. this._loadInProgress = false;
  384. }
  385. };
  386. //Define the function to call if the store reports an error.
  387. var error = function(){
  388. this._loadInProgress = false;
  389. console.log("Error getting items");
  390. };
  391. this.request.onComplete = dojo.hitch(this, complete);
  392. this.request.onError = dojo.hitch(this, error);
  393. //Increment the start parameter. This is the dojo.data API's
  394. //version of paging.
  395. this.request.start = start;
  396. this._noImages = false;
  397. //Execute the request for data.
  398. this.imageStore.fetch(this.request);
  399. },
  400. _loadImage: function(data, index, callback){
  401. // summary:
  402. // Loads an image.
  403. var store = this.imageStore;
  404. var url = store.getValue(data,this.imageThumbAttr);
  405. var imgContainer = dojo.create("div", {
  406. id: "img_" + this.widgetid + "_" + index
  407. });
  408. var img = dojo.create("img", {}, imgContainer);
  409. img._index = index;
  410. img._data = data;
  411. this._thumbs[index] = imgContainer;
  412. var loadingDiv;
  413. if(this.useLoadNotifier){
  414. loadingDiv = dojo.create("div", {
  415. id: "loadingDiv_" + this.widgetid+"_" + index
  416. }, imgContainer);
  417. //If this widget was previously told that the main image for this
  418. //thumb has been loaded, make the loading indicator transparent.
  419. this._setThumbClass(loadingDiv,
  420. this._loadedImages[index] ? "thumbLoaded":"thumbNotifier");
  421. }
  422. var size = dojo.marginBox(this.thumbsNode);
  423. var defaultSize;
  424. var sizeParam;
  425. if(this.isHorizontal){
  426. defaultSize = this.thumbWidth;
  427. sizeParam = 'w';
  428. } else{
  429. defaultSize = this.thumbHeight;
  430. sizeParam = 'h';
  431. }
  432. size = size[sizeParam];
  433. var sl = this.thumbScroller.scrollLeft, st = this.thumbScroller.scrollTop;
  434. dojo.style(this.thumbsNode, this._sizeProperty, (size + defaultSize + 20) + "px");
  435. //Remember the scroll values, as changing the size can alter them
  436. this.thumbScroller.scrollLeft = sl;
  437. this.thumbScroller.scrollTop = st;
  438. this.thumbsNode.appendChild(imgContainer);
  439. dojo.connect(img, "onload", this, dojo.hitch(this, function(){
  440. if(store != this.imageStore){
  441. // If the store has changed, ignore this load event
  442. return false;
  443. }
  444. this.resize();
  445. // Have to use a timeout here to prevent a call stack that gets
  446. // so deep that IE throws stack overflow errors
  447. setTimeout(callback, 0);
  448. return false;
  449. }));
  450. dojo.connect(img, "onclick", this, function(evt){
  451. dojo.publish(this.getClickTopicName(), [{
  452. index: evt.target._index,
  453. data: evt.target._data,
  454. url: img.getAttribute("src"),
  455. largeUrl: this.imageStore.getValue(data,this.imageLargeAttr),
  456. title: this.imageStore.getValue(data,this.titleAttr),
  457. link: this.imageStore.getValue(data,this.linkAttr)
  458. }]);
  459. return false;
  460. });
  461. dojo.addClass(img, "imageGalleryThumb");
  462. img.setAttribute("src", url);
  463. var title = this.imageStore.getValue(data, this.titleAttr);
  464. if(title){ img.setAttribute("title",title); }
  465. this._updateNavControls();
  466. },
  467. _updateNavControls: function(){
  468. // summary:
  469. // Updates the navigation controls to hide/show them when at
  470. // the first or last images.
  471. var cells = [];
  472. var change = function(node, add){
  473. var fn = add ? "addClass" : "removeClass";
  474. dojo[fn](node,"enabled");
  475. dojo[fn](node,"thumbClickable");
  476. };
  477. var pos = this.isHorizontal ? "scrollLeft" : "scrollTop";
  478. var size = this.isHorizontal ? "offsetWidth" : "offsetHeight";
  479. change(this.navPrev, (this.thumbScroller[pos] > 0));
  480. var last = this._thumbs[this._thumbs.length - 1];
  481. var addClass = (this.thumbScroller[pos] + this._scrollerSize < this.thumbsNode[size]);
  482. change(this.navNext, addClass);
  483. }
  484. });
  485. });