-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathgitStyleTrackGraphRender.ts
More file actions
165 lines (148 loc) · 4.24 KB
/
gitStyleTrackGraphRender.ts
File metadata and controls
165 lines (148 loc) · 4.24 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { ContributionCellData, ContributionGraphConfig } from "src/types";
import { mapBy } from "src/util/utils";
import { BaseGraphRender } from "./graphRender";
import { distanceBeforeTheStartOfWeek } from "src/util/dateUtils";
import {
localizedMonthMapping,
localizedWeekDayMapping,
} from "src/i18/messages";
export class GitStyleTrackGraphRender extends BaseGraphRender {
constructor() {
super();
}
graphType(): string {
return "default";
}
render(root: HTMLElement, graphConfig: ContributionGraphConfig): void {
const graphEl = this.createGraphEl(root);
// main
const main = this.createMainEl(graphEl, graphConfig);
// title
if (graphConfig.title && graphConfig.title.trim() != "") {
this.renderTitle(graphConfig, main);
}
// main -> charts
const chartsEl = createDiv({
cls: ["charts", "default"],
parent: main,
});
this.renderCellRuleIndicator(graphConfig, main);
const activityContainer = this.renderActivityContainer(graphConfig, main);
// main -> week day indicator(text cell)
const weekTextColumns = createDiv({
cls: "column",
parent: chartsEl,
});
this.renderWeekIndicator(weekTextColumns, graphConfig);
const contributionData: ContributionCellData[] =
this.generateContributionData(graphConfig);
// fill HOLE cell at the left most column if start date is not ${startOfWeek}
if (contributionData.length > 0) {
const from = contributionData[0];
const weekDayOfFromDate = from.weekDay;
const firstHoleCount = distanceBeforeTheStartOfWeek(
graphConfig.startOfWeek || 0,
weekDayOfFromDate,
);
for (let i = 0; i < firstHoleCount; i++) {
contributionData.unshift({
date: "$HOLE$",
weekDay: -1,
month: -1,
monthDate: -1,
year: -1,
value: 0,
});
}
}
const contributionMapByYearMonth = mapBy(
contributionData,
(item) => `${item.year}-${item.month + 1}`,
(item) => item.value,
(a, b) => a + b,
);
// main -> charts contributionData
const cellRules = this.getCellRules(graphConfig);
let columnEl;
for (let i = 0; i < contributionData.length; i++) {
// i % 7 == 0 means new column
if (i % 7 == 0) {
columnEl = document.createElement("div");
columnEl.className = "column";
chartsEl.appendChild(columnEl);
}
const contributionItem = contributionData[i];
// main -> charts -> column -> month indicator
if (contributionItem.monthDate == 1) {
const monthCell = createDiv({
cls: "month-indicator",
parent: columnEl,
text: "",
});
monthCell.innerText = localizedMonthMapping(contributionItem.month);
this.bindMonthTips(
monthCell,
contributionItem,
contributionMapByYearMonth,
);
}
// main -> charts -> column -> cell
const cellEl = document.createElement("div");
columnEl?.appendChild(cellEl);
if (contributionItem.value == 0) {
if (contributionItem.date != "$HOLE$") {
cellEl.className = "cell empty";
this.applyCellGlobalStyle(cellEl, graphConfig);
this.applyCellStyleRule(cellEl, contributionItem, cellRules);
this.bindCellAttribute(cellEl, contributionItem);
} else {
cellEl.className = "cell";
this.applyCellGlobalStylePartial(cellEl, graphConfig, [
"minWidth",
"minHeight",
]);
}
} else {
cellEl.className = "cell";
this.applyCellGlobalStyle(cellEl, graphConfig);
this.applyCellStyleRule(
cellEl,
contributionItem,
cellRules,
() => cellRules[0],
);
this.bindCellAttribute(cellEl, contributionItem);
this.bindCellClickEvent(
cellEl,
contributionItem,
graphConfig,
activityContainer,
);
this.bindCellTips(cellEl, contributionItem);
}
}
}
renderWeekIndicator(
weekdayContainer: HTMLDivElement,
graphConfig: ContributionGraphConfig,
) {
const startOfWeek = graphConfig.startOfWeek || 0;
for (let i = 0; i < 7; i++) {
const weekdayCell = document.createElement("div");
weekdayCell.className = "cell week-indicator";
this.applyCellGlobalStyle(weekdayCell, graphConfig);
switch (i) {
case 1:
case 3:
case 5:
weekdayCell.innerText = localizedWeekDayMapping(
(i + startOfWeek || 0) % 7,
);
break;
default:
break;
}
weekdayContainer.appendChild(weekdayCell);
}
}
}