-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
114 lines (92 loc) · 2.89 KB
/
index.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
var extend = require('xtend/mutable'),
css = require('mucss/css'),
getMargins = require('mucss/margins');
/**
* Cascade instance constructor
* @param {DOMObject} element - cascade container
* @param {Object} options - initialization options
* @constructor
*/
function Cascade(element, options) {
if (!(this instanceof Cascade)) return new Cascade(element, options);
var defaults = {
childrenSelector: null,
minWidth: 300,
autoResize: true
};
//apply options
extend(this, defaults, options);
//get DOM elements
this.element = element;
if (this.childrenSelector) {
this.children = this.element.querySelectorAll(childrenSelector);
} else {
this.children = this.element.children;
}
//init DOM elements
if (getComputedStyle(this.element).position == 'static') {
css(this.element, 'position', 'relative');
}
//set this instance reflow to its prototype reflow bind to 'this' value
this.reflow = this.reflow.bind(this);
this.reflow();
if (this.autoResize) {
window.addEventListener('resize', this.reflow.bind(this));
}
}
extend(Cascade.prototype, {
/**
* Reposition elements according to they sizes and positions
* Can be called e.g. after adding a new elemenet
* @return {Void}
*/
reflow: function() {
var self = this,
elementWidth = self.element.offsetWidth,
columnsNumber = Math.floor(elementWidth/self.minWidth),
columnWidth = Math.floor(elementWidth / columnsNumber),
//create array with zeros to monitor columns current height
columnsHeights = [];
for (var i=0; i<columnsNumber; i++) {
columnsHeights[i] = 0;
}
Array.prototype.forEach.call(self.children, function(child) {
//get the index of the array with minimum height
var columnIndex = columnsHeights.indexOf(Math.min.apply(Math, columnsHeights)),
margins = getMargins(child),
// horizontal and vertical sums of box model properties for the child
horizontalSpace =
margins.left + margins.right,
verticalSpace =
margins.top + margins.bottom;
css(child, {
position: 'absolute',
'box-sizing': 'border-box',
//width is the column width excluding paddings, margins and borders
width: columnWidth - horizontalSpace,
//if min-width is set on the element it can break cascade items layout
'min-width': 0,
//top is under the bottom element
top: columnsHeights[columnIndex],
//left is width of column and index production
left: columnWidth*columnIndex
});
//add current element's height to the column height
columnsHeights[columnIndex] += child.clientHeight + verticalSpace;
});
//set the highest column height to the container
css(self.element, 'height', Math.max.apply(Math, columnsHeights));
}
});
//jQuery support
if (window.jQuery) {
(function($) {
$.fn.cascade = function(options) {
$(this).each(function() {
new Cascade(this, options);
});
return this;
};
}(window.jQuery));
}
module.exports = Cascade;