ImageThumbView.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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.mobile.app.ImageThumbView"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.mobile.app.ImageThumbView"] = true;
  8. dojo.provide("dojox.mobile.app.ImageThumbView");
  9. dojo.experimental("dojox.mobile.app.ImageThumbView");
  10. dojo.require("dijit._WidgetBase");
  11. dojo.require("dojo.string");
  12. dojo.declare("dojox.mobile.app.ImageThumbView", dijit._WidgetBase, {
  13. // summary:
  14. // An image thumbnail gallery
  15. // items: Array
  16. // The data items from which the image urls are retrieved.
  17. // If an item is a string, it is expected to be a URL. Otherwise
  18. // by default it is expected to have a 'url' member. This can
  19. // be configured using the 'urlParam' attribute on this widget.
  20. items: [],
  21. // urlParam: String
  22. // The paramter name used to retrieve an image url from a JSON object
  23. urlParam: "url",
  24. labelParam: null,
  25. itemTemplate: '<div class="mblThumbInner">' +
  26. '<div class="mblThumbOverlay"></div>' +
  27. '<div class="mblThumbMask">' +
  28. '<div class="mblThumbSrc" style="background-image:url(${url})"></div>' +
  29. '</div>' +
  30. '</div>',
  31. minPadding: 4,
  32. maxPerRow: 3,
  33. maxRows: -1,
  34. baseClass: "mblImageThumbView",
  35. thumbSize: "medium",
  36. animationEnabled: true,
  37. selectedIndex: -1,
  38. cache: null,
  39. cacheMustMatch: false,
  40. clickEvent: "onclick",
  41. cacheBust: false,
  42. disableHide: false,
  43. constructor: function(params, node){
  44. },
  45. postCreate: function(){
  46. this.inherited(arguments);
  47. var _this = this;
  48. var hoverCls = "mblThumbHover";
  49. this.addThumb = dojo.hitch(this, this.addThumb);
  50. this.handleImgLoad = dojo.hitch(this, this.handleImgLoad);
  51. this.hideCached = dojo.hitch(this, this.hideCached);
  52. this._onLoadImages = {};
  53. this.cache = [];
  54. this.visibleImages = [];
  55. this._cacheCounter = 0;
  56. this.connect(this.domNode, this.clickEvent, function(event){
  57. var itemNode = _this._getItemNodeFromEvent(event);
  58. if(itemNode && !itemNode._cached){
  59. _this.onSelect(itemNode._item, itemNode._index, _this.items);
  60. dojo.query(".selected", this.domNode).removeClass("selected");
  61. dojo.addClass(itemNode, "selected");
  62. }
  63. });
  64. dojo.addClass(this.domNode, this.thumbSize);
  65. this.resize();
  66. this.render();
  67. },
  68. onSelect: function(item, index, items){
  69. // summary:
  70. // Dummy function that is triggered when an image is selected.
  71. },
  72. _setAnimationEnabledAttr: function(value){
  73. this.animationEnabled = value;
  74. dojo[value ? "addClass" : "removeClass"](this.domNode, "animated");
  75. },
  76. _setItemsAttr: function(items){
  77. this.items = items || [];
  78. var urls = {};
  79. var i;
  80. for(i = 0; i < this.items.length; i++){
  81. urls[this.items[i][this.urlParam]] = 1;
  82. }
  83. var clearedUrls = [];
  84. for(var url in this._onLoadImages){
  85. if(!urls[url] && this._onLoadImages[url]._conn){
  86. dojo.disconnect(this._onLoadImages[url]._conn);
  87. this._onLoadImages[url].src = null;
  88. clearedUrls.push(url);
  89. }
  90. }
  91. for(i = 0; i < clearedUrls.length; i++){
  92. delete this._onLoadImages[url];
  93. }
  94. this.render();
  95. },
  96. _getItemNode: function(node){
  97. while(node && !dojo.hasClass(node, "mblThumb") && node != this.domNode){
  98. node = node.parentNode;
  99. }
  100. return (node == this.domNode) ? null : node;
  101. },
  102. _getItemNodeFromEvent: function(event){
  103. if(event.touches && event.touches.length > 0){
  104. event = event.touches[0];
  105. }
  106. return this._getItemNode(event.target);
  107. },
  108. resize: function(){
  109. this._thumbSize = null;
  110. this._size = dojo.contentBox(this.domNode);
  111. this.disableHide = true;
  112. this.render();
  113. this.disableHide = false;
  114. },
  115. hideCached: function(){
  116. // summary:
  117. // Hides all cached nodes, so that they're no invisible and overlaying
  118. // other screen elements.
  119. for(var i = 0; i < this.cache.length; i++){
  120. if (this.cache[i]) {
  121. dojo.style(this.cache[i], "display", "none");
  122. }
  123. }
  124. },
  125. render: function(){
  126. var i;
  127. var url;
  128. var item;
  129. var thumb;
  130. while(this.visibleImages && this.visibleImages.length > 0){
  131. thumb = this.visibleImages.pop();
  132. this.cache.push(thumb);
  133. if (!this.disableHide) {
  134. dojo.addClass(thumb, "hidden");
  135. }
  136. thumb._cached = true;
  137. }
  138. if(this.cache && this.cache.length > 0){
  139. setTimeout(this.hideCached, 1000);
  140. }
  141. if(!this.items || this.items.length == 0){
  142. return;
  143. }
  144. for(i = 0; i < this.items.length; i++){
  145. item = this.items[i];
  146. url = (dojo.isString(item) ? item : item[this.urlParam]);
  147. this.addThumb(item, url, i);
  148. if(this.maxRows > 0 && (i + 1) / this.maxPerRow >= this.maxRows){
  149. break;
  150. }
  151. }
  152. if(!this._thumbSize){
  153. return;
  154. }
  155. var column = 0;
  156. var row = -1;
  157. var totalThumbWidth = this._thumbSize.w + (this.padding * 2);
  158. var totalThumbHeight = this._thumbSize.h + (this.padding * 2);
  159. var nodes = this.thumbNodes =
  160. dojo.query(".mblThumb", this.domNode);
  161. var pos = 0;
  162. nodes = this.visibleImages;
  163. for(i = 0; i < nodes.length; i++){
  164. if(nodes[i]._cached){
  165. continue;
  166. }
  167. if(pos % this.maxPerRow == 0){
  168. row ++;
  169. }
  170. column = pos % this.maxPerRow;
  171. this.place(
  172. nodes[i],
  173. (column * totalThumbWidth) + this.padding, // x position
  174. (row * totalThumbHeight) + this.padding // y position
  175. );
  176. if(!nodes[i]._loading){
  177. dojo.removeClass(nodes[i], "hidden");
  178. }
  179. if(pos == this.selectedIndex){
  180. dojo[pos == this.selectedIndex ? "addClass" : "removeClass"]
  181. (nodes[i], "selected");
  182. }
  183. pos++;
  184. }
  185. var numRows = Math.ceil(pos / this.maxPerRow);
  186. this._numRows = numRows;
  187. this.setContainerHeight((numRows * (this._thumbSize.h + this.padding * 2)));
  188. },
  189. setContainerHeight: function(amount){
  190. dojo.style(this.domNode, "height", amount + "px");
  191. },
  192. addThumb: function(item, url, index){
  193. var thumbDiv;
  194. var cacheHit = false;
  195. if(this.cache.length > 0){
  196. // Reuse a previously created node if possible
  197. var found = false;
  198. // Search for an image with the same url first
  199. for(var i = 0; i < this.cache.length; i++){
  200. if(this.cache[i]._url == url){
  201. thumbDiv = this.cache.splice(i, 1)[0];
  202. found = true;
  203. break
  204. }
  205. }
  206. // if no image with the same url is found, just take the last one
  207. if(!thumbDiv && !this.cacheMustMatch){
  208. thumbDiv = this.cache.pop();
  209. dojo.removeClass(thumbDiv, "selected");
  210. } else {
  211. cacheHit = true;
  212. }
  213. }
  214. if(!thumbDiv){
  215. // Create a new thumb
  216. thumbDiv = dojo.create("div", {
  217. "class": "mblThumb hidden",
  218. innerHTML: dojo.string.substitute(this.itemTemplate, {
  219. url: url
  220. }, null, this)
  221. }, this.domNode);
  222. }
  223. if(this.labelParam) {
  224. var labelNode = dojo.query(".mblThumbLabel", thumbDiv)[0];
  225. if(!labelNode) {
  226. labelNode = dojo.create("div", {
  227. "class": "mblThumbLabel"
  228. }, thumbDiv);
  229. }
  230. labelNode.innerHTML = item[this.labelParam] || "";
  231. }
  232. dojo.style(thumbDiv, "display", "");
  233. if (!this.disableHide) {
  234. dojo.addClass(thumbDiv, "hidden");
  235. }
  236. if (!cacheHit) {
  237. var loader = dojo.create("img", {});
  238. loader._thumbDiv = thumbDiv;
  239. loader._conn = dojo.connect(loader, "onload", this.handleImgLoad);
  240. loader._url = url;
  241. thumbDiv._loading = true;
  242. this._onLoadImages[url] = loader;
  243. if (loader) {
  244. loader.src = url;
  245. }
  246. }
  247. this.visibleImages.push(thumbDiv);
  248. thumbDiv._index = index;
  249. thumbDiv._item = item;
  250. thumbDiv._url = url;
  251. thumbDiv._cached = false;
  252. if(!this._thumbSize){
  253. this._thumbSize = dojo.marginBox(thumbDiv);
  254. if(this._thumbSize.h == 0){
  255. this._thumbSize.h = 100;
  256. this._thumbSize.w = 100;
  257. }
  258. if(this.labelParam){
  259. this._thumbSize.h += 8;
  260. }
  261. this.calcPadding();
  262. }
  263. },
  264. handleImgLoad: function(event){
  265. var img = event.target;
  266. dojo.disconnect(img._conn);
  267. dojo.removeClass(img._thumbDiv, "hidden");
  268. img._thumbDiv._loading = false;
  269. img._conn = null;
  270. var url = img._url;
  271. if(this.cacheBust){
  272. url += (url.indexOf("?") > -1 ? "&" : "?")
  273. + "cacheBust=" + (new Date()).getTime() + "_" + (this._cacheCounter++);
  274. }
  275. dojo.query(".mblThumbSrc", img._thumbDiv)
  276. .style("backgroundImage", "url(" + url + ")");
  277. delete this._onLoadImages[img._url];
  278. },
  279. calcPadding: function(){
  280. var width = this._size.w;
  281. var thumbWidth = this._thumbSize.w;
  282. var imgBounds = thumbWidth + this.minPadding;
  283. this.maxPerRow = Math.floor(width / imgBounds);
  284. this.padding = Math.floor((width - (thumbWidth * this.maxPerRow)) / (this.maxPerRow * 2));
  285. },
  286. place: function(node, x, y){
  287. dojo.style(node, {
  288. "-webkit-transform" :"translate(" + x + "px," + y + "px)"
  289. });
  290. },
  291. destroy: function(){
  292. // Stop the loading of any more images
  293. var img;
  294. var counter = 0;
  295. for (var url in this._onLoadImages){
  296. img = this._onLoadImages[url];
  297. if (img) {
  298. img.src = null;
  299. counter++;
  300. }
  301. }
  302. this.inherited(arguments);
  303. }
  304. });
  305. }