forked from ryo-ma/github-profile-trophy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard.ts
88 lines (80 loc) · 2.28 KB
/
card.ts
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
import { UserInfo } from "./user_info.ts";
import { TrophyList } from "./trophy_list.ts";
import { Trophy } from "./trophy.ts";
import { Theme } from "./theme.ts";
export class Card {
private width = 0;
private height = 0;
constructor(
private titles: Array<string>,
private ranks: Array<string>,
private maxColumn: number,
private maxRow: number,
private panelSize: number,
private marginWidth: number,
private marginHeight: number,
private noBackground: boolean,
private noFrame: boolean,
) {
this.width = panelSize * this.maxColumn +
this.marginWidth * (this.maxColumn - 1);
}
render(
userInfo: UserInfo,
theme: Theme,
): string {
const trophyList = new TrophyList(userInfo);
trophyList.filterByHideen();
if (this.titles.length != 0) {
trophyList.filterByTitles(this.titles);
}
if (this.ranks.length != 0) {
trophyList.filterByRanks(this.ranks);
}
trophyList.sortByRank();
const row = this.getRow(trophyList);
this.height = this.getHeight(row);
return `
<svg
width="${this.width}"
height="${this.height}"
viewBox="0 0 ${this.width} ${this.height}"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
${this.renderTrophy(trophyList, theme)}
</svg>`;
}
private getRow(trophyList: TrophyList) {
let row = Math.floor((trophyList.length - 1) / this.maxColumn) + 1;
if (row > this.maxRow) {
row = this.maxRow;
}
return row;
}
private getHeight(row: number) {
// Calculate the height of card from turns
return this.panelSize * row + this.marginHeight * (row - 1);
}
private renderTrophy(trophyList: TrophyList, theme: Theme) {
return trophyList.getArray.reduce(
(sum: string, trophy: Trophy, i: number) => {
const currentColumn = i % this.maxColumn;
const currentRow = Math.floor(i / this.maxColumn);
const x = this.panelSize * currentColumn +
this.marginWidth * currentColumn;
const y = this.panelSize * currentRow + this.marginHeight * currentRow;
return sum +
trophy.render(
theme,
x,
y,
this.panelSize,
this.noBackground,
this.noFrame,
);
},
"",
);
}
}