-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayout.js
100 lines (81 loc) · 2.01 KB
/
layout.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
module.exports = function (width, maxHeight, margin, ratios) {
var availWidth; // avalable width for elements.
var height = margin;
var elements = [];
var length = ratios.length;
var _offset = 0; // current row index offset.
var _weight = 0; // combined weight of ratios of row elements.
var _height; // height of the current row.
var _count; // number of elements in current row.
var index = 0;
// calculate rows and heights
while (index < length) {
_weight += ratios[index++];
_count = index - _offset;
availWidth = width - ((_count + 1) * margin);
// calculate the current height for the row with rowWeight.
_height = Math.round(availWidth / _weight);
// if row height low enough calculate layout
if (_height <= maxHeight || index === length) {
var partial = (_height > maxHeight);
if (partial) {
_height = maxHeight;
}
var w, rounded;
var decimals = [];
var i = _offset;
while (i < index) {
w = ratios[i] * _height;
rounded = Math.round(w);
decimals.push({
decimals: w - rounded,
index: i
});
availWidth -= rounded;
elements.push({
x: 0,
y: height,
width: rounded,
height: _height
});
i++;
}
// distribute available width.
if (!partial && availWidth !== 0) {
var inc = 1;
if (availWidth < 0) {
inc = -inc;
availWidth = -availWidth;
}
decimals.sort(function (a, b) {
return a.decimals - b.decimals;
});
if (inc > 0) {
decimals.reverse();
}
i = 0;
while (availWidth) {
elements[decimals[i].index].width += inc;
availWidth--;
i = (i < decimals.length) ? i + 1 : 0;
}
}
// add x positions.
i = _offset;
var x = margin;
while (i < index) {
elements[i].x = x;
x += elements[i].width + margin;
i++;
}
height += _height + margin;
// ready row variables for next row.
_offset = index;
_weight = 0;
}
}
return {
height: height,
elements: elements
};
};