forked from tin-cat/jquery-mosaic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.mosaic.js
executable file
·328 lines (266 loc) · 11.5 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
jQuery Mosaic v0.15.3
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() {
// Priority of parameters : JS options > HTML data options > DEFAULT options
base.options = o = $.extend({}, $.Mosaic.defaults, base.$el.data(), options);
$(base.el).addClass("jQueryMosaic");
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).width($(item).attr('width'));
if ($(item).attr('height'))
$(item).height($(item).attr('height'));
});
base.fit();
if (o.refitOnResize)
$(window).on('resize', null, null, function() {
if ($(base.el).is(':hidden'))
return;
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]), > video: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 (!numberOf)
numberOf = items.length - start;
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, isDoNotSetHeight) {
var width = Math.floor(base.getItemWidthForGivenHeight(item, height));
$(item).width(width + 'px');
if (isDoNotSetHeight)
$(item).height('auto');
else
$(item).height(Math.floor(height));
base.handleHighResForItem(item);
return width;
}
base.handleHighResForItem = function(item) {
if (!o.highResImagesWidthThreshold)
return;
var width = $(item).width();
if (width > o.highResImagesWidthThreshold) {
// Set high res version images where needed
// Get all elements inside this item having a high-res-background-image-url data, including the item itself, if it has it also
var itemsWithHighResData = $('[data-high-res-background-image-url]', item);
if ($(item).data('high-res-background-image-url'))
itemsWithHighResData = $(itemsWithHighResData).add(item);
$(itemsWithHighResData).each(function() {
$(this).css('background-image', 'url("' + $(this).data('high-res-background-image-url') + '"');
$(this).addClass('highRes');
});
// Get all images inside this item having a high-res-image-url data, including the item itself, if it has it also
var itemsWithHighResData = $('[data-high-res-image-src]', item);
if ($(item).data('high-res-image-src'))
itemsWithHighResData = $(itemsWithHighResData).add(item);
$(itemsWithHighResData).each(function() {
$(this).attr('src', $(this).data('high-res-image-src'));
$(this).addClass('highRes');
});
}
}
base.calculateHeightToFit = function(items) {
var sumAspectRatios = 0;
items.each(function() {
sumAspectRatios += parseFloat(base.getItemAspectRatio(this));
});
return (baseWidth - (o.innerGap * (items.length - 1))) / sumAspectRatios;
}
// Based on the method by glen.codes to get subpixel attributes of elements: https://glen.codes/getting-the-subpixel-width-of-an-element/, to try and solve the bug in jQuery versions < 3: https://github.com/jquery/jquery/issues/1724
base.retrieveBaseWidth = function() {
var value = parseFloat($(base.el).width());
baseWidth = $.isNumeric(value) ? value : 0;
}
base.isBelowResponsiveWidthThreshold = function() {
return o.responsiveWidthThreshold && baseWidth < o.responsiveWidthThreshold;
}
base.fit = function() {
base.retrieveBaseWidth();
if (base.isBelowResponsiveWidthThreshold()) {
var items = base.getItems();
if (o.maxItemsToShowWhenResponsiveThresholdSurpassed) {
$(items).slice(o.maxItemsToShowWhenResponsiveThresholdSurpassed).remove();
items = $(items).slice(0, o.maxItemsToShowWhenResponsiveThresholdSurpassed);
}
items.each(function() {
var height = base.getItemHeightForGivenWidth(this, baseWidth);
$(this).width(baseWidth)
if ($(this).data('only-force-height-when-necessary'))
$(this).height('auto');
else
$(this).height(height);
if (o.innerGap)
$(this).css('margin-bottom', o.innerGap);
});
return;
}
var items, height;
var itemsToUse = 1;
var startIndex = 0;
var isAnyFitted = false;
var row = 1;
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 (!base.isBelowResponsiveWidthThreshold() && o.maxRows && row == o.maxRows) {
base.getItemsSubset(startIndex).each(function() { $(this).hide(); });
return;
}
row ++;
}
// If maxRowHeight has not been met at any point (might happen when specifying short maxRowHeights)
if (!isAnyFitted) {
if (o.showTailWhenNotEnoughItemsForEvenOneRow)
o.maxRowHeightPolicy = 'tail';
base.fitItems(base.getItems());
}
}
base.fitItems = function(items) {
var isDoNothing = false;
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
var isDoNothing = true;
break;
case 'tail':
height = o.maxRowHeight;
var isTail = true;
break;
}
}
items.each(function() { $(this).show(); });
var accumulatedWidth = 0;
items.each(function(idx) {
accumulatedWidth += base.setItemSizeByGivenHeight(this, height, isDoNothing && $(this).data('only-force-height-when-necessary'));
if (o.innerGap) {
$(this).css('margin-right', idx < items.length - 1 ? o.innerGap : 0);
$(this).css('margin-bottom', o.innerGap);
}
});
// Compensate the last element for accumulated floored decimal widths leaving a gap at the end
if (!isTail && 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
maxRows: false, // In some scenarios you might need fine control about the maximum number of rows of the mosaic. If specified, the mosaic won't have more than this number of rows. If responsiveWidthThreshold is specified, maxRows are not considered when the threshold has been reached.
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
responsiveWidthThreshold: false, // The minimum width for which to keep building the mosaic. If specified, when the width is less than this, the mosaic building logic is not applied, and one item per row is always shown. This might help you avoid resulting item sizes that are too small and might break complex html/css inside them, specially when aiming for great responsive mosaics.
maxItemsToShowWhenResponsiveThresholdSurpassed: false, // If set (and also responsiveWidthThreshold is set), only this amount of items will be shown when the responsiveWidthThreshold is met
showTailWhenNotEnoughItemsForEvenOneRow: false, // If this is set to true, when there are not enough items to fill even a single row, they will be shown anyway even if they do not complete the row horizontally. If left to false, no mosaic will be shown in such occasions.
};
$.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);