-
Notifications
You must be signed in to change notification settings - Fork 97
/
jquery.justified.js
284 lines (262 loc) · 11.2 KB
/
jquery.justified.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*jshint white:false*/
/* global jQuery: true*/
// the semi-colon before function invocation is a safety net against concatenated
// scripts and/or other plugins which may not be closed properly.
;
(function($, window, document, undefined) {
'use strict';
// undefined is used here as the undefined global variable in ECMAScript 3 is
// mutable (ie. it can be changed by someone else). undefined isn't really being
// passed in so we can ensure the value of it is truly undefined. In ES5, undefined
// can no longer be modified.
// window and document are passed through as local variable rather than global
// as this (slightly) quickens the resolution process and can be more efficiently
// minified (especially when both are regularly referenced in your plugin).
// Create the defaults once
var pluginName = 'justifiedImages';
// The actual plugin constructor
function Plugin(element, options) {
this.element = element;
this.$el = $(element);
this._name = pluginName;
this.init(options);
}
Plugin.prototype = {
defaults: {
template: function(data) {
return '<div class="photo-container" style="height:' + data.displayHeight + 'px;margin-right:' + data.marginRight + 'px;">' +
'<img class="image-thumb" src="' + data.src + '" style="width:' + data.displayWidth + 'px;height:' + data.displayHeight + 'px;" >' +
'</div>';
},
appendBlocks : function(){ return []; },
rowHeight: 150,
maxRowHeight: 350,
handleResize: false,
margin: 1,
imageSelector: 'image-thumb',
imageContainer: 'photo-container'
},
init: function(options) {
this.options = $.extend({}, this.defaults, options);
this.displayImages();
if (this.options.handleResize) {
this.handleResize();
}
},
getBlockInRow: function(rowNum){
var appendBlocks = this.options.appendBlocks();
for (var i = 0; i < appendBlocks.length; i++) {
var block = appendBlocks[i];
if(block.rowNum === rowNum){
return block;
}
}
},
displayImages: function() {
var self = this,
ws = [],
rowNum = 0,
baseLine = 0,
limit = this.options.images.length,
photos = this.options.images,
rows = [],
totalWidth = 0,
appendBlocks = this.options.appendBlocks();
var w = this.$el.width();
var border = parseInt(this.options.margin, 10);
var d = this.$el,
h = parseInt(this.options.rowHeight, 10);
$.each(this.options.images, function(index, image) {
var size = self.options.getSize(image);
var wt = parseInt(size.width, 10);
var ht = parseInt(size.height, 10);
if (ht !== h) {
wt = Math.floor(wt * (h / ht));
}
totalWidth += wt;
ws.push(wt);
});
$.each(appendBlocks, function(index, block){
totalWidth += block.width;
});
var perRowWidth = totalWidth / Math.ceil(totalWidth / w);
console.log('rows', Math.ceil(totalWidth / w));
var tw = 0;
while (baseLine < limit) {
var row = {
width: 0,
photos: []
},
c = 0,
block = this.getBlockInRow(rows.length + 1);
if(block){
row.width += block.width;
tw += block.width;
}
while ((tw + ws[baseLine + c] / 2 <= perRowWidth * (rows.length + 1)) && (baseLine + c < limit)) {
tw += ws[baseLine + c];
row.width += ws[baseLine + c];
row.photos.push({
width: ws[baseLine + c],
photo: photos[baseLine + c]
});
c++;
}
baseLine += c;
rows.push(row);
}
console.log(rows.length, rows);
/*for (var i = 1; i < rows.length; i++) {
var row = rows[i];
for (var j = 0; j < row.photos.length; j++) {
var photo = row.photos[j].photo;
};
}*/
for (var i = 0; i < rows.length; i++) {
var row = rows[i],
lastRow = false;
rowNum = i + 1;
if (this.options.maxRows && rowNum > this.options.maxRows) {
break;
}
if (i === rows.length - 1) {
lastRow = true;
}
tw = -1 * border;
var newBlock = this.getBlockInRow(lastRow ? -1 : rowNum), availableRowWidth = w;
if(newBlock){
availableRowWidth -= newBlock.width;
tw = 0;
}
// Ratio of actual width of row to total width of images to be used.
var r = availableRowWidth / row.width, //Math.min(w / row.width, this.options.maxScale),
c = row.photos.length;
// new height is not original height * ratio
var ht = Math.min(Math.floor(h * r), parseInt(this.options.maxRowHeight,10));
r = ht / this.options.rowHeight;
var domRow = $('<div/>', {
'class': 'picrow'
});
domRow.height(ht + border);
d.append(domRow);
var imagesHtml = '';
for (var j = 0; j < row.photos.length; j++) {
var photo = row.photos[j].photo;
// Calculate new width based on ratio
var wt = Math.floor(row.photos[j].width * r);
tw += wt + border;
imagesHtml += this.renderPhoto(photo, {
src: this.options.thumbnailPath(photo, wt, ht),
width: wt,
height: ht
}, newBlock ? false : j === row.photos.length - 1);
}
if(imagesHtml === ''){
domRow.remove();
continue;
}
domRow.html(imagesHtml);
if ((Math.abs(tw - availableRowWidth) < 0.05 * availableRowWidth)) {
// if total width is slightly smaller than
// actual div width then add 1 to each
// photo width till they match
var k = 0;
while (tw < availableRowWidth) {
var div1 = domRow.find('.' + this.options.imageContainer + ':nth-child(' + (k + 1) + ')'),
img1 = div1.find('.' + this.options.imageSelector);
img1.width(img1.width() + 1);
k = (k + 1) % c;
tw++;
}
// if total width is slightly bigger than
// actual div width then subtract 1 from each
// photo width till they match
k = 0;
while (tw > availableRowWidth) {
var div2 = domRow.find('.' + this.options.imageContainer + ':nth-child(' + (k + 1) + ')'),
img2 = div2.find('.' + this.options.imageSelector);
img2.width(img2.width() - 1);
k = (k + 1) % c;
tw--;
}
} else{
if( availableRowWidth - tw > 0.05 * availableRowWidth ){
var diff = availableRowWidth-tw,
adjustedDiff = 0,
images = domRow.find('.' + this.options.imageContainer),
marginTop = 0;
for(var l = 0 ; l < images.length ; l++ ){
var currentDiff = diff / (images.length),
imgDiv = images.eq(l),
img = imgDiv.find('.' + this.options.imageSelector),
imageWidth = img.width(),
imageHeight = img.height();
if( i === images.length - 1 ){
currentDiff = diff - adjustedDiff;
}
img.width( imageWidth + currentDiff );
img.height( ( imageHeight / imageWidth ) * (imageWidth + currentDiff) );
marginTop = (imageHeight - img.height()) / 2;
img.css('margin-top', marginTop);
adjustedDiff += currentDiff;
}
}
}
if(newBlock){
$('<div />', {
class : this.options.imageContainer + ' added-block',
css : {
width : newBlock.width,
height: ht
},
html : newBlock.html
}).appendTo(domRow);
}
}
},
renderPhoto: function(image, obj, isLast) {
var data = {},
d;
d = $.extend({}, image, {
src: obj.src,
displayWidth: obj.width,
displayHeight: obj.height,
marginRight: isLast ? 0 : this.options.margin
});
if (this.options.dataObject) {
data[this.options.dataObject] = d;
} else {
data = d;
}
return this.options.template(data);
},
handleResize: function() {},
refresh: function(options) {
this.options = $.extend({}, this.defaults, options);
this.$el.empty();
this.displayImages();
}
};
// A really lightweight plugin wrapper around the constructor,
// preventing against multiple instantiations
$.fn[pluginName] = function(option) {
var args = arguments,
result;
this.each(function() {
var $this = $(this),
data = $.data(this, 'plugin_' + pluginName),
options = typeof option === 'object' && option;
if (!data) {
$this.data('plugin_' + pluginName, (data = new Plugin(this, options)));
}else{
if (typeof option === 'string') {
result = data[option].apply(data, Array.prototype.slice.call(args, 1));
} else {
data.refresh.call(data, options);
}
}
});
// To enable plugin returns values
return result || this;
};
})(jQuery, window, document);