debian-mirror-gitlab/app/assets/javascripts/pages/users/activity_calendar.js

328 lines
8.7 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2020-04-08 14:13:33 +05:30
import { last } from 'lodash';
2018-03-17 18:26:18 +05:30
import { scaleLinear, scaleThreshold } from 'd3-scale';
import { select } from 'd3-selection';
2018-11-08 19:23:39 +05:30
import dateFormat from 'dateformat';
2018-03-17 18:26:18 +05:30
import { getDayName, getDayDifference } from '~/lib/utils/datetime_utility';
import axios from '~/lib/utils/axios_utils';
import flash from '~/flash';
2019-07-31 22:56:46 +05:30
import { n__, s__, __ } from '~/locale';
2018-03-17 18:26:18 +05:30
const d3 = { select, scaleLinear, scaleThreshold };
2017-09-10 17:25:29 +05:30
2019-07-07 11:18:12 +05:30
const firstDayOfWeekChoices = Object.freeze({
sunday: 0,
monday: 1,
saturday: 6,
});
2017-09-10 17:25:29 +05:30
const LOADING_HTML = `
<div class="text-center">
2020-03-13 15:44:24 +05:30
<div class="spinner spinner-md"></div>
2017-09-10 17:25:29 +05:30
</div>
`;
function getSystemDate(systemUtcOffsetSeconds) {
const date = new Date();
const localUtcOffsetMinutes = 0 - date.getTimezoneOffset();
const systemUtcOffsetMinutes = systemUtcOffsetSeconds / 60;
2018-10-15 14:42:47 +05:30
date.setMinutes(date.getMinutes() - localUtcOffsetMinutes + systemUtcOffsetMinutes);
2017-09-10 17:25:29 +05:30
return date;
}
function formatTooltipText({ date, count }) {
const dateObject = new Date(date);
2018-03-17 18:26:18 +05:30
const dateDayName = getDayName(dateObject);
2018-11-08 19:23:39 +05:30
const dateText = dateFormat(dateObject, 'mmm d, yyyy');
2017-09-10 17:25:29 +05:30
2019-07-31 22:56:46 +05:30
let contribText = __('No contributions');
2017-09-10 17:25:29 +05:30
if (count > 0) {
2019-07-31 22:56:46 +05:30
contribText = n__('%d contribution', '%d contributions', count);
2017-09-10 17:25:29 +05:30
}
return `${contribText}<br />${dateDayName} ${dateText}`;
}
2018-10-15 14:42:47 +05:30
const initColorKey = () =>
d3
.scaleLinear()
.range(['#acd5f2', '#254e77'])
.domain([0, 3]);
2017-09-10 17:25:29 +05:30
export default class ActivityCalendar {
2018-12-05 23:21:45 +05:30
constructor(
container,
activitiesContainer,
timestamps,
calendarActivitiesPath,
utcOffset = 0,
2019-07-07 11:18:12 +05:30
firstDayOfWeek = firstDayOfWeekChoices.sunday,
2018-12-05 23:21:45 +05:30
monthsAgo = 12,
) {
2017-09-10 17:25:29 +05:30
this.calendarActivitiesPath = calendarActivitiesPath;
this.clickDay = this.clickDay.bind(this);
this.currentSelectedDate = '';
this.daySpace = 1;
this.daySize = 15;
2018-10-15 14:42:47 +05:30
this.daySizeWithSpace = this.daySize + this.daySpace * 2;
this.monthNames = [
2019-07-31 22:56:46 +05:30
__('Jan'),
__('Feb'),
__('Mar'),
__('Apr'),
__('May'),
__('Jun'),
__('Jul'),
__('Aug'),
__('Sep'),
__('Oct'),
__('Nov'),
__('Dec'),
2018-10-15 14:42:47 +05:30
];
2017-09-10 17:25:29 +05:30
this.months = [];
2018-10-15 14:42:47 +05:30
this.firstDayOfWeek = firstDayOfWeek;
2018-12-05 23:21:45 +05:30
this.activitiesContainer = activitiesContainer;
this.container = container;
2017-09-10 17:25:29 +05:30
// Loop through the timestamps to create a group of objects
// The group of objects will be grouped based on the day of the week they are
this.timestampsTmp = [];
let group = 0;
const today = getSystemDate(utcOffset);
today.setHours(0, 0, 0, 0, 0);
2018-12-05 23:21:45 +05:30
const timeAgo = new Date(today);
timeAgo.setMonth(today.getMonth() - monthsAgo);
2017-09-10 17:25:29 +05:30
2018-12-05 23:21:45 +05:30
const days = getDayDifference(timeAgo, today);
2017-09-10 17:25:29 +05:30
for (let i = 0; i <= days; i += 1) {
2018-12-05 23:21:45 +05:30
const date = new Date(timeAgo);
2017-09-10 17:25:29 +05:30
date.setDate(date.getDate() + i);
const day = date.getDay();
2018-11-08 19:23:39 +05:30
const count = timestamps[dateFormat(date, 'yyyy-mm-dd')] || 0;
2017-09-10 17:25:29 +05:30
// Create a new group array if this is the first day of the week
// or if is first object
2018-10-15 14:42:47 +05:30
if ((day === this.firstDayOfWeek && i !== 0) || i === 0) {
2017-09-10 17:25:29 +05:30
this.timestampsTmp.push([]);
group += 1;
}
// Push to the inner array the values that will be used to render map
const innerArray = this.timestampsTmp[group - 1];
innerArray.push({ count, date, day });
}
// Init color functions
this.colorKey = initColorKey();
this.color = this.initColor();
// Init the svg element
this.svg = this.renderSvg(container, group);
this.renderDays();
this.renderMonths();
this.renderDayTitles();
this.renderKey();
// Init tooltips
$(`${container} .js-tooltip`).tooltip({ html: true });
}
// Add extra padding for the last month label if it is also the last column
getExtraWidthPadding(group) {
let extraWidthPadding = 0;
const lastColMonth = this.timestampsTmp[group - 1][0].date.getMonth();
const secondLastColMonth = this.timestampsTmp[group - 2][0].date.getMonth();
if (lastColMonth !== secondLastColMonth) {
2018-03-17 18:26:18 +05:30
extraWidthPadding = 6;
2017-09-10 17:25:29 +05:30
}
return extraWidthPadding;
}
renderSvg(container, group) {
2018-10-15 14:42:47 +05:30
const width = (group + 1) * this.daySizeWithSpace + this.getExtraWidthPadding(group);
return d3
.select(container)
2017-09-10 17:25:29 +05:30
.append('svg')
2018-10-15 14:42:47 +05:30
.attr('width', width)
.attr('height', 167)
.attr('class', 'contrib-calendar');
}
dayYPos(day) {
return this.daySizeWithSpace * ((day + 7 - this.firstDayOfWeek) % 7);
2017-09-10 17:25:29 +05:30
}
renderDays() {
2018-10-15 14:42:47 +05:30
this.svg
.selectAll('g')
.data(this.timestampsTmp)
.enter()
.append('g')
2017-09-10 17:25:29 +05:30
.attr('transform', (group, i) => {
2020-04-08 14:13:33 +05:30
group.forEach((stamp, a) => {
2019-03-02 22:35:43 +05:30
if (a === 0 && stamp.day === this.firstDayOfWeek) {
2017-09-10 17:25:29 +05:30
const month = stamp.date.getMonth();
2018-10-15 14:42:47 +05:30
const x = this.daySizeWithSpace * i + 1 + this.daySizeWithSpace;
2020-04-08 14:13:33 +05:30
const lastMonth = last(this.months);
2017-09-10 17:25:29 +05:30
if (
lastMonth == null ||
(month !== lastMonth.month && x - this.daySizeWithSpace !== lastMonth.x)
) {
this.months.push({ month, x });
}
}
});
2018-10-15 14:42:47 +05:30
return `translate(${this.daySizeWithSpace * i + 1 + this.daySizeWithSpace}, 18)`;
2017-09-10 17:25:29 +05:30
})
.selectAll('rect')
2018-10-15 14:42:47 +05:30
.data(stamp => stamp)
.enter()
.append('rect')
.attr('x', '0')
.attr('y', stamp => this.dayYPos(stamp.day))
.attr('width', this.daySize)
.attr('height', this.daySize)
2019-02-15 15:39:39 +05:30
.attr('fill', stamp =>
stamp.count !== 0 ? this.color(Math.min(stamp.count, 40)) : '#ededed',
2018-10-15 14:42:47 +05:30
)
.attr('title', stamp => formatTooltipText(stamp))
.attr('class', 'user-contrib-cell js-tooltip')
.attr('data-container', 'body')
.on('click', this.clickDay);
2017-09-10 17:25:29 +05:30
}
renderDayTitles() {
const days = [
{
2019-07-31 22:56:46 +05:30
text: s__('DayTitle|M'),
2018-10-15 14:42:47 +05:30
y: 29 + this.dayYPos(1),
},
{
2019-07-31 22:56:46 +05:30
text: s__('DayTitle|W'),
2018-10-15 14:42:47 +05:30
y: 29 + this.dayYPos(3),
},
{
2019-07-31 22:56:46 +05:30
text: s__('DayTitle|F'),
2018-10-15 14:42:47 +05:30
y: 29 + this.dayYPos(5),
2017-09-10 17:25:29 +05:30
},
];
2019-03-02 22:35:43 +05:30
2019-07-07 11:18:12 +05:30
if (this.firstDayOfWeek === firstDayOfWeekChoices.monday) {
2019-03-02 22:35:43 +05:30
days.push({
2019-07-31 22:56:46 +05:30
text: s__('DayTitle|S'),
2019-03-02 22:35:43 +05:30
y: 29 + this.dayYPos(7),
});
2019-07-07 11:18:12 +05:30
} else if (this.firstDayOfWeek === firstDayOfWeekChoices.saturday) {
days.push({
2019-07-31 22:56:46 +05:30
text: s__('DayTitle|S'),
2019-07-07 11:18:12 +05:30
y: 29 + this.dayYPos(6),
});
2019-03-02 22:35:43 +05:30
}
2018-10-15 14:42:47 +05:30
this.svg
.append('g')
2017-09-10 17:25:29 +05:30
.selectAll('text')
2018-10-15 14:42:47 +05:30
.data(days)
.enter()
.append('text')
.attr('text-anchor', 'middle')
.attr('x', 8)
.attr('y', day => day.y)
.text(day => day.text)
.attr('class', 'user-contrib-text');
2017-09-10 17:25:29 +05:30
}
renderMonths() {
2018-10-15 14:42:47 +05:30
this.svg
.append('g')
2017-09-10 17:25:29 +05:30
.attr('direction', 'ltr')
.selectAll('text')
2018-10-15 14:42:47 +05:30
.data(this.months)
.enter()
.append('text')
.attr('x', date => date.x)
.attr('y', 10)
.attr('class', 'user-contrib-text')
.text(date => this.monthNames[date.month]);
2017-09-10 17:25:29 +05:30
}
renderKey() {
2018-10-15 14:42:47 +05:30
const keyValues = [
2019-07-31 22:56:46 +05:30
__('no contributions'),
__('1-9 contributions'),
__('10-19 contributions'),
__('20-29 contributions'),
__('30+ contributions'),
2018-10-15 14:42:47 +05:30
];
const keyColors = [
'#ededed',
this.colorKey(0),
this.colorKey(1),
this.colorKey(2),
this.colorKey(3),
];
2017-09-10 17:25:29 +05:30
2018-10-15 14:42:47 +05:30
this.svg
.append('g')
.attr('transform', `translate(18, ${this.daySizeWithSpace * 8 + 16})`)
2017-09-10 17:25:29 +05:30
.selectAll('rect')
2018-10-15 14:42:47 +05:30
.data(keyColors)
.enter()
.append('rect')
.attr('width', this.daySize)
.attr('height', this.daySize)
.attr('x', (color, i) => this.daySizeWithSpace * i)
.attr('y', 0)
.attr('fill', color => color)
.attr('class', 'js-tooltip')
.attr('title', (color, i) => keyValues[i])
.attr('data-container', 'body');
2017-09-10 17:25:29 +05:30
}
initColor() {
2018-10-15 14:42:47 +05:30
const colorRange = [
'#ededed',
this.colorKey(0),
this.colorKey(1),
this.colorKey(2),
this.colorKey(3),
];
return d3
.scaleThreshold()
.domain([0, 10, 20, 30])
.range(colorRange);
2017-09-10 17:25:29 +05:30
}
clickDay(stamp) {
if (this.currentSelectedDate !== stamp.date) {
this.currentSelectedDate = stamp.date;
const date = [
this.currentSelectedDate.getFullYear(),
this.currentSelectedDate.getMonth() + 1,
this.currentSelectedDate.getDate(),
].join('-');
2018-12-05 23:21:45 +05:30
$(this.activitiesContainer).html(LOADING_HTML);
2018-03-17 18:26:18 +05:30
2018-10-15 14:42:47 +05:30
axios
.get(this.calendarActivitiesPath, {
params: {
date,
},
responseType: 'text',
})
2018-12-05 23:21:45 +05:30
.then(({ data }) => $(this.activitiesContainer).html(data))
2018-10-15 14:42:47 +05:30
.catch(() => flash(__('An error occurred while retrieving calendar activity')));
2017-09-10 17:25:29 +05:30
} else {
this.currentSelectedDate = '';
2018-12-05 23:21:45 +05:30
$(this.activitiesContainer).html('');
2017-09-10 17:25:29 +05:30
}
}
}