|
1 |
| -const horizontalGapPercentage = 0.4; |
| 1 | +const minHorizontalGapPercentage = 0.3; |
2 | 2 | const verticalGapPercentage = 0.3;
|
3 | 3 |
|
| 4 | +const bestHorizontalFit = (windowWidth, objectWidth) => { |
| 5 | + let columns = 1; |
| 6 | + let percentage = |
| 7 | + (windowWidth - columns * objectWidth) / (objectWidth * (1 + columns)); |
| 8 | + let prevPercentage = percentage; |
| 9 | + |
| 10 | + while (percentage >= minHorizontalGapPercentage) { |
| 11 | + prevPercentage = percentage; |
| 12 | + columns += 1; |
| 13 | + percentage = |
| 14 | + (windowWidth - columns * objectWidth) / (objectWidth * (1 + columns)); |
| 15 | + } |
| 16 | + |
| 17 | + return [columns - 1, prevPercentage]; |
| 18 | +}; |
| 19 | + |
4 | 20 | export function listView(collection) {
|
5 | 21 | const width = window.innerWidth;
|
6 | 22 | const length = collection.length;
|
7 | 23 |
|
8 |
| - const objWidth = collection[0].width; |
9 |
| - const objHorizontalGap = parseInt(horizontalGapPercentage * objWidth); |
10 | 24 | const objHeight = collection[0].height;
|
11 | 25 | const objVerticalGap = parseInt(verticalGapPercentage * objHeight);
|
12 |
| - |
13 |
| - let cols = (width - objHorizontalGap) / (objWidth + objHorizontalGap); |
14 |
| - const decimal = cols % 1; |
15 |
| - |
16 |
| - const minDecimal = |
17 |
| - (objWidth + 0.5 * objHorizontalGap) / (objWidth + objHorizontalGap); |
18 |
| - |
19 |
| - if (decimal >= minDecimal) { |
20 |
| - cols = cols + 1; |
21 |
| - } |
22 |
| - cols = Math.floor(cols); |
| 26 | + const objWidth = collection[0].width; |
| 27 | + const [cols, horizontalGapPercentage] = bestHorizontalFit(width, objWidth); |
| 28 | + const objHorizontalGap = parseInt(horizontalGapPercentage * objWidth); |
23 | 29 |
|
24 | 30 | const rows = Math.ceil(length / cols);
|
25 | 31 |
|
|
0 commit comments