source: extensions/GThumb/js/gthumb.js @ 12710

Last change on this file since 12710 was 12710, checked in by patdenice, 12 years ago

Clean code

File size: 7.1 KB
Line 
1var GThumb = {
2
3  max_height: 200,
4  margin: 10,
5  max_first_thumb_width: 0.7,
6  big_thumb: null,
7  small_thumb: null,
8  method: 'crop',
9  t: new Array,
10
11  queue: jQuery.manageAjax.create('queued', {
12    queue: true, 
13    cacheResponse: false,
14    maxRequests: 3,
15    preventDoubleRequests: false
16  }),
17
18  build: function () {
19
20    GThumb.t = new Array;
21    jQuery('#thumbnails img.thumbnail').each(function() {
22      id = parseInt(this.id.substring(2));
23      width = parseInt(jQuery(this).attr('width'));
24      height = parseInt(jQuery(this).attr('height'));
25      th = {id:id,width:width,height:height,real_width:width,real_height:height};
26      if (height < GThumb.max_height) {
27        th.width = Math.round(GThumb.max_height * width / height);
28        th.height = GThumb.max_height;
29      }
30      GThumb.t.push(th);
31
32      if (jQuery(this).attr('src') == '') {
33        GThumb.addToQueue(id, 1);
34      }
35    });
36
37    jQuery.resize.throttleWindow = false;
38    jQuery.resize.delay = 50;
39    GThumb.process();
40  },
41
42  addToQueue: function (id, loop) {
43
44    GThumb.queue.add({
45      type: 'GET', 
46      url: 'ws.php', 
47      data: {
48        method: 'pwg.images.getGThumbPlusThumbnail',
49        image_id: id,
50        format: 'json'
51      },
52      dataType: 'json',
53      success: function(data) {
54        if (data.stat == 'ok') {
55          jQuery('#gt'+data.result.id).prop('src', data.result.src).show();
56        } else if (loop < 4) {
57          GThumb.addToQueue(id, ++loop);
58        }
59      },
60      error: function() {
61        if (loop < 4) GThumb.addToQueue(id, ++loop);
62      }
63    });
64  },
65
66  process: function() {
67
68    var width_count = GThumb.margin;
69    var line = 1;
70    var round_rest = 0;
71    var main_width = jQuery('#thumbnails').width();
72    var first_thumb = jQuery('#thumbnails img:first');
73    var best_size = {width:1,height:1};
74
75    if (GThumb.big_thumb != null && GThumb.big_thumb.height < main_width * GThumb.max_first_thumb_width) {
76
77      // Compute best size for landscape picture (we choose bigger height)
78      min_ratio = Math.min(1.05, GThumb.big_thumb.width/GThumb.big_thumb.height);
79
80      for(width = GThumb.big_thumb.width; width/best_size.height>=min_ratio; width--) {
81        width_count = GThumb.margin;
82        height = GThumb.margin;
83        max_height = 0;
84        available_width = main_width - (width + GThumb.margin);
85        line = 1;
86        for (i=1;i<GThumb.t.length;i++) {
87
88          width_count += GThumb.t[i].width + GThumb.margin;
89          max_height = Math.max(GThumb.t[i].height, max_height);
90
91          if (width_count > available_width) {
92            ratio = width_count / available_width;
93            height += Math.round(max_height / ratio);
94            line++;
95            max_height = 0;
96            width_count = GThumb.margin;
97            if (line > 2) {
98              if (height >= best_size.height && width/height >= min_ratio && height<=GThumb.big_thumb.height) {
99                best_size = {width:width,height:height}
100              }
101              break;
102            }
103          }
104        }
105        if (line <= 2) {
106          if (max_height == 0 || line == 1) {
107            height = GThumb.big_thumb.height;
108          } else {
109            height += max_height;
110          }
111          if (height >= best_size.height && width/height >= min_ratio && height<=GThumb.big_thumb.height) {
112            best_size = {width:width,height:height}
113          }
114        }
115      }
116
117      if (GThumb.big_thumb.src != first_thumb.attr('src')) {
118        first_thumb.attr('src', GThumb.big_thumb.src).attr({width:GThumb.big_thumb.width,height:GThumb.big_thumb.height});
119        GThumb.t[0].width = GThumb.big_thumb.width;
120        GThumb.t[0].height = GThumb.big_thumb.height;
121      }
122      GThumb.t[0].crop = best_size.width;
123      GThumb.resize(first_thumb, GThumb.big_thumb.width, GThumb.big_thumb.height, best_size.width, best_size.height, true);
124
125    }
126
127    if (best_size.width == 1) {
128      if (GThumb.small_thumb != null && GThumb.small_thumb.src != first_thumb.attr('src')) { 
129        first_thumb.prop('src', GThumb.small_thumb.src).attr({width:GThumb.small_thumb.width,height:GThumb.small_thumb.height});
130        GThumb.t[0].width = GThumb.small_thumb.width;
131        GThumb.t[0].height = GThumb.small_thumb.height;
132      }
133      GThumb.t[0].crop = false;
134    }
135
136    width_count = GThumb.margin;
137    max_height = 0;
138    line = 1;
139    thumb_process = new Array;
140
141    for (i=GThumb.t[0].crop!=false?1:0;i<GThumb.t.length;i++) {
142
143      width_count += GThumb.t[i].width + GThumb.margin;
144      max_height = Math.max(GThumb.t[i].height, max_height);
145      thumb_process.push(GThumb.t[i]);
146
147      available_width = main_width;
148      if (line <= 2 && GThumb.t[0].crop !== false) {
149        available_width -= (GThumb.t[0].crop + GThumb.margin);
150      }
151
152      if (width_count > available_width) {
153
154        last_thumb = GThumb.t[i].id;
155        ratio = width_count / available_width;
156        new_height = Math.round(max_height / ratio);
157        round_rest = 0;
158        width_count = GThumb.margin;
159
160        for (j=0;j<thumb_process.length;j++) {
161
162          if (thumb_process[j].id == last_thumb) {
163            new_width = available_width - width_count - GThumb.margin;
164          } else {
165            new_width = (thumb_process[j].width + round_rest) / ratio;
166            round_rest = new_width - Math.round(new_width);
167            new_width = Math.round(new_width);
168          }
169          GThumb.resize(jQuery('#gt'+thumb_process[j].id), thumb_process[j].real_width, thumb_process[j].real_height, new_width, new_height, false);
170
171          width_count += new_width + GThumb.margin;
172        }
173        thumb_process = new Array;
174        width_count = GThumb.margin;
175        max_height = 0;
176        line++;
177      }
178    }
179
180    // Last line does not need to be cropped
181    for (j=0;j<thumb_process.length;j++) {
182      GThumb.resize(jQuery('#gt'+thumb_process[j].id), thumb_process[j].real_width, thumb_process[j].real_height, thumb_process[j].width, max_height, false);
183    }
184
185    if (main_width != jQuery('#thumbnails').width()) {
186      GThumb.process();
187    }
188  },
189
190  resize: function(thumb, width, height, new_width, new_height, is_big) {
191
192    if (GThumb.method == 'resize' || height < new_height || width < new_width) {
193      real_width = new_width;
194      real_height = new_height;
195      width_crop = 0;
196      height_crop = 0;
197
198      if (is_big) {
199        if (width - new_width > height - new_height) {
200          real_width = Math.round(new_height * width / height);
201          width_crop = Math.round((real_width - new_width)/2);
202        } else {
203          real_height = Math.round(new_width * height / width);
204          height_crop = Math.round((real_height - new_height)/2);
205        }
206      }
207      thumb.css({
208        height: real_height+'px',
209        width: real_width+'px'
210      });
211    } else {
212      thumb.css({height: '', width: ''});
213      height_crop = Math.round((height - new_height)/2);
214      width_crop = Math.round((width - new_width)/2);
215    }
216
217    thumb.parents('li').css({
218      height: new_height+'px',
219      width: new_width+'px'
220    });
221    thumb.parent('a').css({
222      clip: 'rect('+height_crop+'px, '+(new_width+width_crop)+'px, '+(new_height+height_crop)+'px, '+width_crop+'px)',
223      top: -height_crop+'px',
224      left: -width_crop+'px'
225    });
226  }
227}
Note: See TracBrowser for help on using the repository browser.