ImageThumbView.js 8.6 KB

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