const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
const now = new Date();
let contributions;
(() => {
const dom = document.querySelector('#contributions');
if (!dom) {
return;
}
contributions = JSON.parse(dom.getAttribute('data'));
let year = 0;
for (const item of contributions) {
item.publishDate = decodeURI(item.publishDate).replace(' ','T');
item.date = new Date(item.publishDate);
if (item.date.getFullYear() > year) {
year = item.date.getFullYear();
}
item.title = decodeURI(item.title);
}
yearList();
yearly(year.toString());
})();
function yearly(year) {
let startDate;
let endDate;
if (year != now.getFullYear().toString()) {
const date = new Date(year);
startDate = new Date(date.getTime() - date.getDay() * 24 * 60 * 60 * 1000);
endDate = new Date(date.getFullYear(), 11, 31);
} else {
endDate = now;
startDate = new Date(endDate.getTime() - 364 * 24 * 60 * 60 * 1000 - endDate.getDay() * 24 * 60 * 60 * 1000);
}
startDate.setHours(0, 0, 0, 0);
endDate.setHours(23, 59, 59, 999);
const posts = [];
const ms = [];
for (const item of contributions) {
if (item.date >= startDate && item.date <= endDate) {
posts.push(item);
if (!ms.includes(item.date.getMonth())) {
ms.push(item.date.getMonth());
}
}
}
posts.sort((a, b) => { return b - a });
document.querySelector('#posts-activity').innerHTML = '';
for (const month of ms) {
const node = document.createElement('div');
node.innerHTML = monthly(year, month, posts);
document.querySelector('#posts-activity').appendChild(node);
}
graph(year, posts, startDate, endDate);
const yearList = document.querySelectorAll('.js-year-link');
for (const elem of yearList) {
if (elem.innerText == year) {
elem.classList.add('selected');
} else {
elem.classList.remove('selected');
}
}
}
function monthly(year, month, posts) {
const monthPosts = posts.filter(post => post.date.getMonth() === month);
let liHtml = '';
for (const post of monthPosts) {
liHtml += `
${post.title}
`;
}
let html = `
${months[month]} ${monthPosts.length > 0 ? monthPosts[0].date.getFullYear() : year}
`;
return html;
}
function yearList() {
const years = [];
for (const item of contributions) {
const year = item.date.getFullYear();
if (!years.includes(year)) {
years.push(year);
}
}
years.sort((a, b) => { return b - a });
for (let i = 0; i < years.length; i++) {
const year = years[i];
const node = document.createElement('li');
node.innerHTML = `${year}`;
document.querySelector('#year-list').appendChild(node);
}
}
function graph(year, posts, startDate, endDate) {
const postsStr = posts.length === 1 ? "post" : "posts";
if (year == now.getFullYear().toString()) {
document.querySelector('#posts-count').innerText = `${posts.length} ${postsStr} in the last year`;
} else {
document.querySelector('#posts-count').innerText = `${posts.length} ${postsStr} in ${year}`;
}
let html = ``;
const count = {};
for (const post of posts) {
const date = `${post.date.getFullYear()}-${(post.date.getMonth() + 1).toString().padStart(2, '0')}-${post.date.getDate().toString().padStart(2, '0')}`;
if (count[date] == undefined) {
count[date] = 1;
} else {
count[date]++;
}
}
const monthPos = [];
let startMonth = -1;
for (let i = 0; i < 53; i++) {
html += ``;
for (let j = 0; j < 7; j++) {
const date = new Date(startDate.getTime() + (i * 7 + j) * 24 * 60 * 60 * 1000);
const dataDate = `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`;
if (date > endDate) {
continue;
}
if (j == 0) {
if (i <= 51) {
if (startMonth != date.getMonth()) {
monthPos.push(i);
startMonth = date.getMonth();
}
}
}
let c;
if (count[dataDate] == undefined) {
c = 0;
} else {
c = count[dataDate];
}
let color;
switch (c) {
case 0:
color = "#ebedf0";
break;
case 1:
color = "#c6e48b";
break;
case 2:
color = "#7bc96f";
break;
case 3:
color = "#239a3b";
break;
default:
color = "#196127";
}
html += ``;
}
html += '';
}
if (monthPos[1] - monthPos[0] < 2) {
monthPos[0] = -1;
}
for (let i = 0; i < monthPos.length; i++) {
const month = monthPos[i];
if (month == -1) {
continue;
}
html += `${months[(i + startDate.getMonth()) % 12]}`;
}
html += `
Sun
Mon
Tue
Wed
Thu
Fri
Sat
`;
document.querySelector('#graph-svg').innerHTML = html;
stopClickEvent('.day');
}