Theme Inspinia
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jquery.justifiedgallery.js 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. Justified Gallery
  3. Version: 2.1
  4. Author: Miro Mannino
  5. Author URI: http://miromannino.it
  6. Copyright 2012 Miro Mannino (miro.mannino@gmail.com)
  7. This file is part of Justified Gallery.
  8. This work is licensed under the Creative Commons Attribution 3.0 Unported License.
  9. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/
  10. or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
  11. */
  12. (function($){
  13. $.fn.justifiedGallery = function(options){
  14. //TODO fare impostazione 'rel' che sostituisce tutti i link con il rel specificato
  15. var settings = $.extend( {
  16. 'sizeRangeSuffixes' : {'lt100':'_t', 'lt240':'_m', 'lt320':'_n', 'lt500':'', 'lt640':'_z', 'lt1024':'_b'},
  17. 'rowHeight' : 120,
  18. 'margins' : 1,
  19. 'justifyLastRow' : true,
  20. 'fixedHeight' : false,
  21. 'captions' : true,
  22. 'rel' : null, //rewrite the rel of each analyzed links
  23. 'target' : null, //rewrite the target of all links
  24. 'extension' : /\.[^.]+$/,
  25. 'refreshTime' : 500,
  26. 'onComplete' : null
  27. }, options);
  28. function getErrorHtml(message, classOfError){
  29. return "<div class=\"jg-error " + classOfError + "\"style=\"\">" + message + "</div>";
  30. }
  31. return this.each(function(index, cont){
  32. $(cont).addClass("justifiedGallery");
  33. var loaded = 0;
  34. var images = new Array($(cont).find("img").length);
  35. if(images.length == 0) return;
  36. $(cont).append("<div class=\"jg-loading\"><div class=\"jg-loading-img\"></div></div>");
  37. $(cont).find("a").each(function(index, entry){
  38. var imgEntry = $(entry).find("img");
  39. images[index] = new Array(5);
  40. images[index]["src"] = (typeof $(imgEntry).data("safe-src") != 'undefined') ? $(imgEntry).data("safe-src") : $(imgEntry).attr("src");
  41. images[index]["alt"] = $(imgEntry).attr("alt");
  42. images[index]["href"] = $(entry).attr("href");
  43. images[index]["title"] = $(entry).attr("title");
  44. images[index]["rel"] = (settings.rel != null) ? settings.rel : $(entry).attr("rel");
  45. images[index]["target"] = (settings.target != null) ? settings.target : $(entry).attr("target");
  46. images[index]["extension"] = images[index]["src"].match(settings.extension)[0];
  47. $(entry).remove(); //remove the image, we have its data
  48. var img = new Image();
  49. $(img).load(function() {
  50. if(images[index]["height"] != settings.rowHeight)
  51. images[index]["width"] = Math.ceil(this.width / (this.height / settings.rowHeight));
  52. else
  53. images[index]["width"] = this.width;
  54. images[index]["height"] = settings.rowHeight;
  55. var usedSizeRangeRegExp = new RegExp("(" + settings.sizeRangeSuffixes.lt100 + "|"
  56. + settings.sizeRangeSuffixes.lt240 + "|"
  57. + settings.sizeRangeSuffixes.lt320 + "|"
  58. + settings.sizeRangeSuffixes.lt500 + "|"
  59. + settings.sizeRangeSuffixes.lt640 + "|"
  60. + settings.sizeRangeSuffixes.lt1024 + ")$");
  61. images[index]["src"] = images[index]["src"].replace(settings.extension, "").replace(usedSizeRangeRegExp, "");
  62. if(++loaded == images.length) startProcess(cont, images, settings);
  63. });
  64. $(img).error(function() {
  65. $(cont).prepend(getErrorHtml("The image can't be loaded: \"" + images[index]["src"] +"\"", "jg-usedPrefixImageNotFound"));
  66. images[index] = null;
  67. if(++loaded == images.length) startProcess(cont, images, settings);
  68. });
  69. $(img).attr('src', images[index]["src"]);
  70. });
  71. });
  72. function startProcess(cont, images, settings){
  73. //FadeOut the loading image and FadeIn the images after their loading
  74. $(cont).find(".jg-loading").fadeOut(500, function(){
  75. $(this).remove(); //remove the loading image
  76. processesImages($, cont, images, 0, settings);
  77. if($.isFunction(settings.onComplete)) settings.onComplete.call(this, cont);
  78. });
  79. }
  80. function buildImage(image, suffix, nw, nh, l, minRowHeight, settings){
  81. var ris;
  82. ris = "<div class=\"jg-image\" style=\"left:" + l + "px\">";
  83. ris += " <a href=\"" + image["href"] + "\" ";
  84. if (typeof image["rel"] != 'undefined') ris += "rel=\"" + image["rel"] + "\"";
  85. if (typeof image["target"] != 'undefined') ris += "target=\"" + image["target"] + "\"";
  86. ris += "title=\"" + image["title"] + "\">";
  87. ris += " <img alt=\"" + image["alt"] + "\" src=\"" + image["src"] + suffix + image.extension + "\"";
  88. ris += "style=\"width: " + nw + "px; height: " + nh + "px;\">";
  89. if(settings.captions)
  90. ris += " <div style=\"bottom:" + (nh - minRowHeight) + "px;\" class=\"jg-image-label\">" + image["alt"] + "</div>";
  91. ris += " </a></div>";
  92. return ris;
  93. }
  94. function buildContRow(row, images, extraW, settings){
  95. var j, l = 0;
  96. var minRowHeight;
  97. for(var j = 0; j < row.length; j++){
  98. row[j]["nh"] = Math.ceil(images[row[j]["indx"]]["height"] *
  99. ((images[row[j]["indx"]]["width"] + extraW) /
  100. images[row[j]["indx"]]["width"]));
  101. row[j]["nw"] = images[row[j]["indx"]]["width"] + extraW;
  102. row[j]["suffix"] = getSuffix(row[j]["nw"], row[j]["nh"], settings);
  103. row[j]["l"] = l;
  104. if(!settings.fixedHeight){
  105. if(j == 0)
  106. minRowHeight = row[j]["nh"];
  107. else
  108. if(minRowHeight > row[j]["nh"]) minRowHeight = row[j]["nh"];
  109. }
  110. l += row[j]["nw"] + settings.margins;
  111. }
  112. if(settings.fixedHeight) minRowHeight = settings.rowHeight;
  113. var rowCont = "";
  114. for(var j = 0; j < row.length; j++){
  115. rowCont += buildImage(images[row[j]["indx"]], row[j]["suffix"],
  116. row[j]["nw"], row[j]["nh"], row[j]["l"], minRowHeight, settings);
  117. }
  118. return "<div class=\"jg-row\" style=\"height: " + minRowHeight + "px; margin-bottom:" + settings.margins + "px;\">" + rowCont + "</div>";
  119. }
  120. function getSuffix(nw, nh, settings){
  121. var n;
  122. if(nw > nh) n = nw; else n = nh;
  123. if(n <= 100){
  124. return settings.sizeRangeSuffixes.lt100; //thumbnail (longest side:100)
  125. }else if(n <= 240){
  126. return settings.sizeRangeSuffixes.lt240; //small (longest side:240)
  127. }else if(n <= 320){
  128. return settings.sizeRangeSuffixes.lt320; //small (longest side:320)
  129. }else if(n <= 500){
  130. return settings.sizeRangeSuffixes.lt500; //small (longest side:320)
  131. }else if(n <= 640){
  132. return settings.sizeRangeSuffixes.lt640; //medium (longest side:640)
  133. }else{
  134. return settings.sizeRangeSuffixes.lt1024; //large (longest side:1024)
  135. }
  136. }
  137. function processesImages($, cont, images, lastRowWidth, settings){
  138. var row = new Array();
  139. var row_i, i;
  140. var partialRowWidth = 0;
  141. var extraW;
  142. var rowWidth = $(cont).width();
  143. for(i = 0, row_i = 0; i < images.length; i++){
  144. if(images[i] == null) continue;
  145. if(partialRowWidth + images[i]["width"] + settings.margins <= rowWidth){
  146. //we can add the image
  147. partialRowWidth += images[i]["width"] + settings.margins;
  148. row[row_i] = new Array(5);
  149. row[row_i]["indx"] = i;
  150. row_i++;
  151. }else{
  152. //the row is full
  153. extraW = Math.ceil((rowWidth - partialRowWidth + 1) / row.length);
  154. $(cont).append(buildContRow(row, images, extraW, settings));
  155. row = new Array();
  156. row[0] = new Array(5);
  157. row[0]["indx"] = i;
  158. row_i = 1;
  159. partialRowWidth = images[i]["width"] + settings.margins;
  160. }
  161. }
  162. //last row----------------------
  163. //now we have all the images index loaded in the row arra
  164. if(settings.justifyLastRow){
  165. extraW = Math.ceil((rowWidth - partialRowWidth + 1) / row.length);
  166. }else{
  167. extraW = 0;
  168. }
  169. $(cont).append(buildContRow(row, images, extraW, settings));
  170. //---------------------------
  171. //Captions---------------------
  172. if(settings.captions){
  173. $(cont).find(".jg-image").mouseenter(function(sender){
  174. $(sender.currentTarget).find(".jg-image-label").stop();
  175. $(sender.currentTarget).find(".jg-image-label").fadeTo(500, 0.7);
  176. });
  177. $(cont).find(".jg-image").mouseleave(function(sender){
  178. $(sender.currentTarget).find(".jg-image-label").stop();
  179. $(sender.currentTarget).find(".jg-image-label").fadeTo(500, 0);
  180. });
  181. }
  182. $(cont).find(".jg-resizedImageNotFound").remove();
  183. //fade in the images that we have changed and need to be reloaded
  184. $(cont).find(".jg-image img").load(function(){
  185. $(this).fadeTo(500, 1);
  186. }).error(function(){
  187. $(cont).prepend(getErrorHtml("The image can't be loaded: \"" + $(this).attr("src") +"\"", "jg-resizedImageNotFound"));
  188. }).each(function(){
  189. if(this.complete) $(this).load();
  190. });
  191. checkWidth($, cont, images, rowWidth, settings);
  192. }
  193. function checkWidth($, cont, images, lastRowWidth, settings){
  194. var id = setInterval(function(){
  195. if(lastRowWidth != $(cont).width()){
  196. $(cont).find(".jg-row").remove();
  197. clearInterval(id);
  198. processesImages($, cont, images, lastRowWidth, settings);
  199. return;
  200. }
  201. }, settings.refreshTime);
  202. }
  203. }
  204. })(jQuery);