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

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

Add GThumb+ plugin

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