-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflexcol.mosaic.js
101 lines (81 loc) · 3.22 KB
/
flexcol.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
/* @preserve
*
* Flexcol.mosaic v1.0.0
* https://github.com/sunpietro/flexcol-mosaic
*
* Copyright 2015 Piotr Nalepa/sunpietro
* http://blog.piotrnalepa.pl
*
* Released under the MIT license
* https://github.com/sunpietro/flexcol-mosaic/blob/master/LICENSE
*
* Date: 2015-08-06T18:00Z
*/
;(function (window, document) {
'use strict';
var flexcolMosaic = function (customParams) {
var params = {
columns: 3,
containerSelector: '.flexcol-container',
itemSelector: '.flexcol-item'
},
overflowInitValue = 0.5,
overflowIncreaseValue = 0.25,
calculationsTimeout,
setFlexContainerHeight,
makeHeightCalculations,
correctOverflow,
setColumns,
container,
items,
key;
for (key in customParams) {
if (customParams.hasOwnProperty(key)) {
params[key] = customParams[key];
}
}
container = document.querySelector(params.containerSelector);
items = Array.prototype.slice.call(container.querySelectorAll(params.itemSelector));
setFlexContainerHeight = function setFlexContainerHeight () {
window.clearTimeout(calculationsTimeout);
params.columns = parseInt(params.columns, 10);
calculationsTimeout = window.setTimeout(makeHeightCalculations, 100);
};
setColumns = function setColumns (columnsNumber) {
params.columns = columnsNumber;
setFlexContainerHeight();
};
correctOverflow = function correctOverflow (finalHeight, itemMinHeight) {
function checkOverflow (height, multiply) {
if (container.offsetWidth < container.scrollWidth) {
container.style.height = finalHeight + (height * multiply) + 'px';
checkOverflow(height, multiply + overflowIncreaseValue);
}
}
checkOverflow(itemMinHeight, overflowInitValue);
};
makeHeightCalculations = function makeHeightCalculations () {
var itemsHeight = 0,
itemMaxHeight = 0,
itemMinHeight = 0,
finalHeight = 0;
container.style.height = 'auto';
items.forEach(function (item) {
var itemOffset = item.getBoundingClientRect();
itemMaxHeight = itemOffset.height > itemMaxHeight ? itemOffset.height : itemMaxHeight;
itemMinHeight = !itemMinHeight || itemOffset.height < itemMinHeight ? itemOffset.height : itemMinHeight;
itemsHeight += itemOffset.height;
});
finalHeight = (itemsHeight / params.columns);
container.style.height = finalHeight + 'px';
correctOverflow(finalHeight, itemMinHeight);
};
window.addEventListener('load', setFlexContainerHeight);
window.addEventListener('resize', setFlexContainerHeight);
window.addEventListener('orientationchange', setFlexContainerHeight);
return {
setColumns: setColumns
};
};
window.FlexcolMosaic = flexcolMosaic;
})(window, window.document);