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

Last change on this file since 30225 was 30225, checked in by SergeD, 10 years ago

GDThumb 1.0.12

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