forked from tin-cat/jquery-mosaic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.mosaic.js
executable file
·275 lines (230 loc) · 11 KB
/
jquery.mosaic.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
/*
jQuery Mosaic v0.131
https://github.com/tin-cat/jquery-mosaic
A jquery plugin by Tin.cat to build beautifully arranged and responsive mosaics of html elements maintaining their original aspect ratio. Works wonderfully with images by creating a visually ordered and pleasant mosaic (much like mosaics on Flickr, 500px and Google+) without gaps between elements, but at the same time respecting aspect ratios. Reacts to window resizes and adapts responsively to any screen size. See it working on https://skinography.net
*/
(function($){
$.Mosaic = function(el, options) {
var base = this, o;
base.el = el;
base.$el = $(el);
base.$el.data('Mosaic', base);
var baseWidth;
var refitTimeout = false;
base.init = function() {
base.options = o = $.extend({}, $.Mosaic.defaults, options);
$(base.el).addClass("mosaic");
if (o.outerMargin)
$(base.el).css('padding', o.outerMargin);
if (o.innerGap)
$(base.el).css('margin-bottom', o.innerGap * -1);
// If width and height are specified via attribute, set width and height as css properties to solve weird IE problem.
base.getItems().each(function(idx, item) {
if ($(item).attr('width'))
$(item).css('width', $(item).attr('width'));
if ($(item).attr('height'))
$(item).css('height', $(item).attr('height'));
});
base.fit();
if (o.refitOnResize)
$(window).on('resize', null, null, function() {
if (o.refitOnResizeDelay) {
if (refitTimeout)
clearTimeout(refitTimeout);
refitTimeout = setTimeout(function() {
base.fit()
}, o.refitOnResizeDelay);
}
else
base.fit()
});
}
base.getItems = function() {
return $('> div:not([data-no-mosaic=true]), > a:not([data-no-mosaic=true]), > img:not([data-no-mosaic=true])', base.el);
}
base.getItemAtIndex = function(index) {
if (!base.getItems()[index])
return false;
return $(base.getItems()[index]);
}
base.getItemsSubset = function(start, numberOf) {
var items = base.getItems();
if (start > items.length)
return false;
if (start + numberOf > items.length)
numberOf = items.length - start;
return items.slice(start, start + numberOf);
}
base.isLastItemsSubset = function(start, numberOf) {
var items = base.getItems();
if (start > items.length)
return true;
if (start + numberOf > items.length)
return true;
return false;
}
base.getItemWidth = function(item) {
if ($(item).outerWidth())
return $(item).outerWidth();
if ($(item).attr('width'))
return $(item).attr('width');
}
base.getItemHeight = function(item) {
if ($(item).outerHeight())
return $(item).outerHeight();
if ($(item).attr('height'))
return $(item).attr('height');
}
base.getItemAspectRatio = function(item) {
if ($(item).data('aspect-ratio'))
return $(item).data('aspect-ratio');
if (base.getItemWidth(item) && base.getItemHeight(item)) {
var aspectRatio = base.getItemWidth(item) / base.getItemHeight(item);
$(item).data('aspect-ratio', aspectRatio);
return aspectRatio;
}
return o.defaultAspectRatio;
}
base.getItemWidthForGivenHeight = function(item, height) {
return height * base.getItemAspectRatio(item);
}
base.getItemHeightForGivenWidth = function(item, width) {
return width / base.getItemAspectRatio(item);
}
base.setItemSizeByGivenHeight = function(item, height) {
var width = Math.floor(base.getItemWidthForGivenHeight(item, height));
$(item)
.css('height', Math.floor(height) + 'px')
.css('width', width + 'px');
if (o.highResImagesWidthThreshold) {
if (width > o.highResImagesWidthThreshold) {
var highResBackgroundImage = $(item).data('high-res-background-image-url');
if (
highResBackgroundImage
&&
!$(item).data('low-res-background-image-url')
) {
$(item).data('low-res-background-image-url', $(item).css('background-image'));
$(item).css('background-image', 'url("' + highResBackgroundImage + '")');
$(item).addClass('highRes');
}
var highResImage = $(item).data('high-res-image-src');
if (
highResImage
&&
!$(item).data('low-res-image-src')
) {
$(item).data('low-res-image-src', $(item).attr('src'));
$(item).attr('src', highResImage);
$(item).addClass('highRes');
}
}
else {
var lowResBackgroundImage = $(item).data('low-res-background-image-url');
if (lowResBackgroundImage) {
$(item).css('background-image', lowResBackgroundImage);
$(item).data('low-res-background-image-url', false);
$(item).removeClass('highRes');
}
var lowResImage = $(item).data('low-res-image-src');
if (lowResImage) {
$(item).attr('src', lowResImage);
$(item).data('low-res-image-src', false);
$(item).removeClass('highRes');
}
}
}
return width;
}
base.calculateHeightToFit = function(items) {
var sumAspectRatios = 0;
items.each(function() {
sumAspectRatios += parseFloat(base.getItemAspectRatio(this));
});
return (baseWidth - (o.innerGap * (items.length - 1))) / sumAspectRatios;
}
base.retrieveBaseWidth = function() {
baseWidth = Math.floor($(base.el).width());
}
base.fit = function() {
base.retrieveBaseWidth();
var items, height;
var itemsToUse = 1;
var startIndex = 0;
var isAnyFitted = false;
while (true) {
items = base.getItemsSubset(startIndex, itemsToUse);
if (base.isLastItemsSubset(startIndex, itemsToUse)) {
if (items.length) {
base.fitItems(items);
}
break;
}
height = base.calculateHeightToFit(items);
if (height > o.maxRowHeight) {
itemsToUse ++;
continue;
}
base.fitItems(items);
startIndex += itemsToUse;
itemsToUse = 1;
isAnyFitted = true;
}
// If maxRowHeight has not been met at any point (might happen when specifying short maxRowHeights)
if (!isAnyFitted)
base.fitItems(base.getItems());
}
base.fitItems = function(items) {
var height = base.calculateHeightToFit(items);
if (height > o.maxRowHeight) {
switch (o.maxRowHeightPolicy) {
case 'skip':
items.each(function() { $(this).hide(); });
return;
break;
case 'crop':
height = o.maxRowHeight;
break;
case 'oversize':
// Do nothing
break;
}
}
items.each(function() { $(this).show(); });
var accumulatedWidth = 0;
items.each(function(idx) {
accumulatedWidth += base.setItemSizeByGivenHeight(this, height);
if (o.innerGap) {
$(this).css('margin-right', idx < items.length - 1 ? o.innerGap : 0);
$(this).css('margin-bottom', o.innerGap);
}
});
// Compensate the last element to compensate for accumulated floored decimal widths leaving a gap at the end
if (accumulatedWidth != (baseWidth - ((items.length - 1) * o.innerGap))) {
difference = (baseWidth - ((items.length - 1) * o.innerGap)) - accumulatedWidth;
var width = items.last().width();
items.last().width(width + difference);
}
}
base.init();
}
$.Mosaic.defaults = {
maxRowHeight: 400, // The maximum desired height of rows
refitOnResize: true, // Whether to rebuild the mosaic when the window is resized or not
refitOnResizeDelay: false, // Milliseconds to wait after a resize event to refit the mosaic. Useful when creating huge mosaics that can take some CPU time on the user's browser. Leave it to false to refit the mosaic in realtime.
defaultAspectRatio: 1, // The aspect ratio to use when none has been specified, or can't be calculated
maxRowHeightPolicy: 'skip', // Sometimes some of the remaining items cannot be fitted on a row without surpassing the maxRowHeight. For those cases, choose one of the available settings for maxRowHeightPolicy: "skip": Does not renders the unfitting items. "crop": caps the desired height to the specified maxRowHeight, resulting in those items not keeping their aspect ratios. "oversize": Renders the items respecting their aspect ratio but surpassing the specified maxRowHeight
highResImagesWidthThreshold: 350, // The item width on which to start using the the provided high resolution image instead of the normal one. High resolution images are specified via the "data-high-res-image-src" or "data-high-res-background-image-url" html element properties of each item.
outerMargin: 0, // A margin size in pixels for the outher edge of the whole mosaic
innerGap: 0 // A gap size in pixels to leave a space between elements
};
$.fn.Mosaic = function(options, params) {
return this.each(function(){
var me = $(this).data('Mosaic');
if ((typeof(options)).match('object|undefined'))
new $.Mosaic(this, options);
else
eval('me.'+options)(params);
});
}
})(jQuery);