source: extensions/GDThumb/js/gdthumb.js @ 31060

Last change on this file since 31060 was 31060, checked in by SergeD, 9 years ago

version 1.0.15 - refer to changelog for more details

File size: 11.1 KB
Line 
1var GDThumb = {
2
3  max_height: 200,
4  margin: 10,
5  max_first_thumb_width: 0.7,
6  big_thumb: null,
7  big_thumb_block: false,
8  check_pv: false,
9  small_thumb: null,
10  method: 'crop',
11  t: new Array,
12  do_merge: false,
13
14  // Initialize plugin logic, perform necessary steps
15  setup: function(method, max_height, margin, do_merge, big_thumb, check_pv) {
16
17    jQuery('ul#thumbnails').addClass("thumbnails");
18
19    GDThumb.max_height = max_height;
20    GDThumb.margin     = margin;
21    GDThumb.method     = method;
22    GDThumb.check_pv   = check_pv;
23    GDThumb.do_merge   = do_merge;
24    GDThumb.big_thumb  = big_thumb;
25
26    $(window).bind("RVTS_loaded", function() { GDThumb.init(); });
27    GDThumb.init();
28  },
29
30  init: function() {
31    if (GDThumb.do_merge) { GDThumb.merge(); }
32
33    GDThumb.build();
34    jQuery(window).bind('RVTS_loaded', GDThumb.build);
35    jQuery('ul.thumbnails').resize(GDThumb.process);
36    jQuery("ul.thumbnails .thumbLegend.overlay").click( function() { window.location.href = $(this).parent().find('a').attr('href'); });
37    jQuery("ul.thumbnails .thumbLegend.overlay-ex").click( function() { window.location.href = $(this).parent().find('a').attr('href'); });
38  },
39
40  // Merge categories and picture lists
41  merge: function() {
42
43    var mainlists = $('.content ul.thumbnails');
44    if (mainlists.length < 2) {
45      // there is only one list of elements
46    } else {
47      $(".thumbnailCategories li").addClass("album");
48      $(".thumbnailCategories").append($(".content ul#thumbnails").html());     
49      $("ul#thumbnails").remove(); 
50      $("div.loader:eq(1)").remove();
51    }
52  },
53
54  // Build thumb metadata
55  build: function () {
56
57    if ((GDThumb.method == 'square') && (GDThumb.big_thumb != null) && ((GDThumb.big_thumb.height != GDThumb.big_thumb.width) || (GDThumb.big_thumb.height < GDThumb.max_height))){
58      var main_width = jQuery('ul.thumbnails').width();
59      var max_col_count = Math.floor(main_width / GDThumb.max_height);
60      var thumb_width   = Math.floor(main_width / max_col_count) - GDThumb.margin;
61
62      GDThumb.big_thumb.height = (thumb_width * 2) + GDThumb.margin;
63      GDThumb.big_thumb.width  = GDThumb.big_thumb.height;
64      GDThumb.big_thumb.crop   = GDThumb.big_thumb.height;
65      GDThumb.max_height = thumb_width;
66    } 
67
68    GDThumb.t = new Array;
69    $('ul.thumbnails img.thumbnail').each(function(index) {
70      width = parseInt(jQuery(this).attr('width'));
71      height = parseInt(jQuery(this).attr('height'));
72      th = {index: index, width: width, height: height, real_width: width, real_height: height};
73      if (GDThumb.check_pv) {
74        var ratio = th.width / th.height;
75        GDThumb.big_thumb_block = (ratio > 2.2) || (ratio < 0.455);
76      }
77      if ((GDThumb.method == 'square') && (th.height != th.width)) {
78        th.height = GDThumb.max_height;
79        th.width  = GDThumb.max_height;
80        th.crop   = GDThumb.max_height;
81      } else if (height < GDThumb.max_height) {
82        th.width = Math.round(GDThumb.max_height * width / height);
83        th.height = GDThumb.max_height;
84      }
85
86      GDThumb.t.push(th);
87    });
88
89    if (GDThumb.big_thumb_block) {
90      GDThumb.big_thumb = null;
91    }
92
93    first = GDThumb.t[0];
94    if (first) {
95      GDThumb.small_thumb = {index: first.index, width: first.real_width, height: first.real_height, src: jQuery('ul.thumbnails img.thumbnail:first').attr('src')}
96    }
97    jQuery.resize.throttleWindow = false;
98    jQuery.resize.delay = 50;
99    GDThumb.process();
100  },
101
102  // Adjust thumb attributes to match plugin settings
103  process: function() {
104
105    var width_count = GDThumb.margin;
106    var line = 1;
107    var round_rest = 0;
108    var main_width = jQuery('ul.thumbnails').width();
109    var first_thumb = jQuery('ul.thumbnails img.thumbnail:first');
110    var best_size = {width: 1, height: 1};
111
112    if (GDThumb.method == 'square') {
113      if (GDThumb.big_thumb != null) {
114        best_size.width = GDThumb.big_thumb.width;
115        best_size.height = GDThumb.big_thumb.height;
116
117        if (GDThumb.big_thumb.src != first_thumb.attr('src')) {
118          first_thumb.attr('src', GDThumb.big_thumb.src).attr({width: GDThumb.big_thumb.width, height: GDThumb.big_thumb.height});
119          GDThumb.t[0].width = GDThumb.big_thumb.width;
120          GDThumb.t[0].height = GDThumb.big_thumb.height;
121        }
122        GDThumb.t[0].crop = best_size.width;
123        GDThumb.resize(first_thumb, GDThumb.t[0].real_width, GDThumb.t[0].real_height, GDThumb.big_thumb.width, GDThumb.big_thumb.height, true);
124      }else{
125        best_size.width  = GDThumb.max_height;
126        best_size.height = GDThumb.max_height;
127        GDThumb.resize(first_thumb, GDThumb.t[0].real_width, GDThumb.t[0].real_height, GDThumb.t[0].width, GDThumb.t[0].height, true);
128      }
129    } else {
130      if (GDThumb.big_thumb != null && GDThumb.big_thumb.height < main_width * GDThumb.max_first_thumb_width) {
131
132        // Compute best size for landscape picture (we choose bigger height)
133        min_ratio = Math.min(1.05, GDThumb.big_thumb.width/GDThumb.big_thumb.height);
134
135        for(width = GDThumb.big_thumb.width; width/best_size.height>=min_ratio; width--) {
136          width_count = GDThumb.margin;
137          height = GDThumb.margin;
138          max_height = 0;
139          available_width = main_width - (width + GDThumb.margin);
140          line = 1;
141          for (i=1;i<GDThumb.t.length;i++) {
142 
143            width_count += GDThumb.t[i].width + GDThumb.margin;
144            max_height = Math.max(GDThumb.t[i].height, max_height);
145 
146            if (width_count > available_width) {
147              ratio = width_count / available_width;
148              height += Math.round(max_height / ratio);
149              line++;
150              max_height = 0;
151              width_count = GDThumb.margin;
152              if (line > 2) {
153                if (height >= best_size.height && width/height >= min_ratio && height<=GDThumb.big_thumb.height) {
154                  best_size = {width:width,height:height};
155                }
156                break;
157              }
158            }
159          }
160          if (line <= 2) {
161            if (max_height == 0 || line == 1) {
162              height = GDThumb.big_thumb.height;
163            } else {
164              height += max_height;
165            }
166            if (height >= best_size.height && width/height >= min_ratio && height<=GDThumb.big_thumb.height) {
167              best_size = {width:width,height:height}
168            }
169          }
170        }
171        if (GDThumb.big_thumb.src != first_thumb.attr('src')) {
172          first_thumb.attr('src', GDThumb.big_thumb.src).attr({width: GDThumb.big_thumb.width, height: GDThumb.big_thumb.height});
173          GDThumb.t[0].width = GDThumb.big_thumb.width;
174          GDThumb.t[0].height = GDThumb.big_thumb.height;
175        }
176        GDThumb.t[0].crop = best_size.width;
177        GDThumb.resize(first_thumb, GDThumb.big_thumb.width, GDThumb.big_thumb.height, best_size.width, best_size.height, true);
178      }
179    }
180
181    if (best_size.width == 1) {
182      if (GDThumb.small_thumb != null && GDThumb.small_thumb.src != first_thumb.attr('src')) { 
183        first_thumb.prop('src', GDThumb.small_thumb.src).attr({width: GDThumb.small_thumb.width, height: GDThumb.small_thumb.height});
184        GDThumb.t[0].width = GDThumb.small_thumb.width;
185        GDThumb.t[0].height = GDThumb.small_thumb.height;
186      }
187      GDThumb.t[0].crop = false;
188    }
189
190    width_count = GDThumb.margin;
191    max_height = 0;
192    last_height = GDThumb.max_height;
193    line = 1;
194    thumb_process = new Array;
195
196    for (i=GDThumb.t[0].crop!=false?1:0;i<GDThumb.t.length;i++) {
197
198      width_count += GDThumb.t[i].width + GDThumb.margin;
199      max_height = Math.max(GDThumb.t[i].height, max_height);
200      thumb_process.push(GDThumb.t[i]);
201
202      available_width = main_width;
203      if (line <= 2 && GDThumb.t[0].crop !== false) {
204        available_width -= (GDThumb.t[0].crop + GDThumb.margin);
205      }
206
207      if (width_count > available_width) {
208
209        last_thumb = GDThumb.t[i].index;
210        ratio = width_count / available_width;
211        new_height = Math.round(max_height / ratio);
212        round_rest = 0;
213        width_count = GDThumb.margin;
214
215        for (j=0;j<thumb_process.length;j++) {
216
217          if (GDThumb.method == 'square') {
218            new_width  = GDThumb.max_height;
219            new_height = GDThumb.max_height;
220          } else {
221            if (thumb_process[j].index == last_thumb) {
222              new_width = available_width - width_count - GDThumb.margin;
223            } else {
224              new_width = (thumb_process[j].width + round_rest) / ratio;
225              round_rest = new_width - Math.round(new_width);
226              new_width = Math.round(new_width);
227            }
228          }
229          GDThumb.resize(jQuery('ul.thumbnails img.thumbnail').eq(thumb_process[j].index), thumb_process[j].real_width, thumb_process[j].real_height, new_width, new_height, false);
230          last_height = Math.min(last_height, new_height);
231
232          width_count += new_width + GDThumb.margin;
233        }
234        thumb_process = new Array;
235        width_count = GDThumb.margin;
236        max_height = 0;
237        line++;
238      }
239    }
240
241    if (last_height == 0) {
242      last_height = GDThumb.max_height;
243    }
244
245    // Last line does not need to be cropped
246    for (j=0;j<thumb_process.length;j++) {
247      GDThumb.resize(jQuery('ul.thumbnails img.thumbnail').eq(thumb_process[j].index), thumb_process[j].real_width, thumb_process[j].real_height, thumb_process[j].width, last_height, false);
248    }
249
250    if (main_width != jQuery('ul.thumbnails').width()) {
251      GDThumb.process();
252    }
253  },
254
255  resize: function(thumb, width, height, new_width, new_height, is_big) {
256    if ((!is_big) && (GDThumb.method == 'square')) {
257      thumb.css({height: '', width: ''});
258      new_width = new_height;
259
260      if (width < height) {
261        real_height = Math.round(height * new_width / width);
262        real_width  = new_width;
263      } else {
264        real_height = new_width;
265        real_width  = Math.round(width * new_height / height);
266      }
267
268      height_crop = Math.round((real_height - new_height) / 2);
269      width_crop = Math.round((real_width - new_height) / 2);
270      thumb.css({
271        height: real_height+'px',
272        width: real_width+'px'
273      });
274    }else if (GDThumb.method == 'resize' || height < new_height || width < new_width) {
275      real_width = new_width;
276      real_height = new_height;
277      width_crop = 0;
278      height_crop = 0;
279
280      if (is_big) {
281        if (width - new_width > height - new_height) {
282          real_width = Math.round(new_height * width / height);
283          width_crop = Math.round((real_width - new_width)/2);
284        } else {
285          real_height = Math.round(new_width * height / width);
286          height_crop = Math.round((real_height - new_height)/2);
287        }
288      }
289      thumb.css({
290        height: real_height+'px',
291        width: real_width+'px'
292      });
293    } else {
294      thumb.css({height: '', width: ''});
295      height_crop = Math.round((height - new_height)/2);
296      width_crop = Math.round((width - new_width)/2);
297    }
298
299    thumb.parents('li').css({ height: new_height+'px', width: new_width+'px' });
300    thumb.parent('a').css({ clip: 'rect('+height_crop+'px, '+(new_width+width_crop)+'px, '+(new_height+height_crop)+'px, '+width_crop+'px)', top: -height_crop+'px', left: -width_crop+'px' });
301  }
302}
Note: See TracBrowser for help on using the repository browser.