-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathutils.js
More file actions
87 lines (77 loc) · 2.41 KB
/
utils.js
File metadata and controls
87 lines (77 loc) · 2.41 KB
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
import { card } from "../../../constants/card.config";
const loop = (states, pos, cb) => states[pos].map(cb);
const getDistribution = (cardsLength) => {
const states = [
[0, 0, 0],
[0, 1, 0],
[1, 1, 0],
[1, 1, 1],
];
const jumpRelation = [1, 2, 1];
const statePosition = cardsLength % states.length;
const stateMultiplier = Math.trunc(cardsLength / states.length);
return {
inLength: () =>
loop(states, statePosition, (column, pos) => {
return column + jumpRelation[pos] * stateMultiplier;
}),
inRange: () => {
let endRange = 0;
let maxColumnSize = 0;
return loop(states, statePosition, (column, pos) => {
maxColumnSize = column + jumpRelation[pos] * stateMultiplier;
endRange += maxColumnSize;
return [endRange - maxColumnSize, endRange];
});
},
};
};
class BlogCardStyle {
constructor({
gridRow,
widthImg,
gridColumn,
flexDirection,
paddingLeftContent,
paddingTopContent,
paddingBottomTitle,
paddingBlockFooter,
}) {
this.gridRow = gridRow;
this.widthImg = widthImg;
this.gridColumn = gridColumn;
this.flexDirection = flexDirection;
this.paddingLeftContent = paddingLeftContent;
this.paddingTopContent = paddingTopContent;
this.paddingBottomTitle = paddingBottomTitle;
this.paddingBlockFooter = paddingBlockFooter;
}
}
const getStyles = (dataLength) => {
let styles = [];
const distribution = getDistribution(dataLength).inLength();
distribution.forEach((maxColumnSize, pos) => {
const columnStart = pos + 1;
const columnEnd = pos + 2;
const isMiddle = pos !== 0 && pos !== distribution.length - 1;
for (let index = 0; index < maxColumnSize; index++) {
const rowStart = isMiddle ? index + 1 : 2 * index + 1;
const rowEnd = isMiddle ? index + 2 : rowStart + 2;
styles = [
...styles,
new BlogCardStyle({
gridRow: `${rowStart} / ${rowEnd}`,
widthImg: isMiddle ? `${card.blog.viewport_img_height}em` : "initial",
gridColumn: `${columnStart} / ${columnEnd}`,
flexDirection: isMiddle ? "row" : "column",
paddingLeftContent: Number(isMiddle),
paddingTopContent: isMiddle ? "initial" : 1,
paddingBottomTitle: isMiddle ? 0.5 : 1,
paddingBlockFooter: isMiddle ? 0.5 : 1,
}),
];
}
});
return styles;
};
export { getStyles };