Skip to content

Commit 561e7a9

Browse files
gary-kimzeripath
andauthored
Migrate ActivityHeatmap to Vue SFC (#10953)
* Migrate ActivityHeatmap to Vue SFC Signed-off-by: Gary Kim <[email protected]> * Readd vue compiler alias Signed-off-by: Gary Kim <[email protected]> * Remove unneeded use of v-html Signed-off-by: Gary Kim <[email protected]> Co-authored-by: zeripath <[email protected]>
1 parent ca33a95 commit 561e7a9

File tree

2 files changed

+83
-92
lines changed

2 files changed

+83
-92
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<template>
2+
<div>
3+
<div v-show="isLoading">
4+
<slot name="loading"></slot>
5+
</div>
6+
<h4 class="total-contributions" v-if="!isLoading">
7+
{{ totalContributions }} total contributions in the last 12 months
8+
</h4>
9+
<calendar-heatmap v-show="!isLoading" :locale="locale" :no-data-text="locale.no_contributions" :tooltip-unit="locale.contributions" :end-date="endDate" :values="values" :range-color="colorRange"/>
10+
</div>
11+
</template>
12+
13+
<script>
14+
import {CalendarHeatmap} from 'vue-calendar-heatmap';
15+
const {AppSubUrl, heatmapUser} = window.config;
16+
17+
export default {
18+
name: "ActivityHeatmap",
19+
components: {
20+
CalendarHeatmap
21+
},
22+
data() {
23+
return {
24+
isLoading: true,
25+
colorRange: [],
26+
endDate: null,
27+
values: [],
28+
totalContributions: 0,
29+
suburl: AppSubUrl,
30+
user: heatmapUser,
31+
locale: {
32+
contributions: 'contributions',
33+
no_contributions: 'No contributions',
34+
},
35+
};
36+
},
37+
mounted() {
38+
this.colorRange = [
39+
this.getColor(0),
40+
this.getColor(1),
41+
this.getColor(2),
42+
this.getColor(3),
43+
this.getColor(4),
44+
this.getColor(5)
45+
];
46+
this.endDate = new Date();
47+
this.loadHeatmap(this.user);
48+
},
49+
methods: {
50+
loadHeatmap(userName) {
51+
const self = this;
52+
$.get(`${this.suburl}/api/v1/users/${userName}/heatmap`, (chartRawData) => {
53+
const chartData = [];
54+
for (let i = 0; i < chartRawData.length; i++) {
55+
self.totalContributions += chartRawData[i].contributions;
56+
chartData[i] = {date: new Date(chartRawData[i].timestamp * 1000), count: chartRawData[i].contributions};
57+
}
58+
self.values = chartData;
59+
self.isLoading = false;
60+
});
61+
},
62+
getColor(idx) {
63+
const el = document.createElement('div');
64+
el.className = `heatmap-color-${idx}`;
65+
document.body.appendChild(el);
66+
67+
const color = getComputedStyle(el).backgroundColor;
68+
69+
document.body.removeChild(el);
70+
71+
return color;
72+
}
73+
},
74+
}
75+
</script>
76+
77+
<style scoped>
78+
79+
</style>

web_src/js/features/userheatmap.js

Lines changed: 4 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,10 @@
11
import Vue from 'vue';
22

3-
const {AppSubUrl, heatmapUser} = window.config;
3+
import ActivityHeatmap from '../components/ActivityHeatmap.vue';
44

5-
export default async function initHeatmap() {
5+
export default async function initUserHeatmap() {
66
const el = document.getElementById('user-heatmap');
77
if (!el) return;
8-
9-
const {CalendarHeatmap} = await import(/* webpackChunkName: "userheatmap" */'vue-calendar-heatmap');
10-
Vue.component('calendarHeatmap', CalendarHeatmap);
11-
12-
const vueDelimeters = ['${', '}'];
13-
14-
Vue.component('activity-heatmap', {
15-
delimiters: vueDelimeters,
16-
17-
props: {
18-
user: {
19-
type: String,
20-
required: true
21-
},
22-
suburl: {
23-
type: String,
24-
required: true
25-
},
26-
locale: {
27-
type: Object,
28-
required: true
29-
}
30-
},
31-
32-
data() {
33-
return {
34-
isLoading: true,
35-
colorRange: [],
36-
endDate: null,
37-
values: [],
38-
totalContributions: 0,
39-
};
40-
},
41-
42-
mounted() {
43-
this.colorRange = [
44-
this.getColor(0),
45-
this.getColor(1),
46-
this.getColor(2),
47-
this.getColor(3),
48-
this.getColor(4),
49-
this.getColor(5)
50-
];
51-
this.endDate = new Date();
52-
this.loadHeatmap(this.user);
53-
},
54-
55-
methods: {
56-
loadHeatmap(userName) {
57-
const self = this;
58-
$.get(`${this.suburl}/api/v1/users/${userName}/heatmap`, (chartRawData) => {
59-
const chartData = [];
60-
for (let i = 0; i < chartRawData.length; i++) {
61-
self.totalContributions += chartRawData[i].contributions;
62-
chartData[i] = {date: new Date(chartRawData[i].timestamp * 1000), count: chartRawData[i].contributions};
63-
}
64-
self.values = chartData;
65-
self.isLoading = false;
66-
});
67-
},
68-
69-
getColor(idx) {
70-
const el = document.createElement('div');
71-
el.className = `heatmap-color-${idx}`;
72-
document.body.appendChild(el);
73-
74-
const color = getComputedStyle(el).backgroundColor;
75-
76-
document.body.removeChild(el);
77-
78-
return color;
79-
}
80-
},
81-
82-
template: '<div><div v-show="isLoading"><slot name="loading"></slot></div><h4 class="total-contributions" v-if="!isLoading"><span v-html="totalContributions"></span> total contributions in the last 12 months</h4><calendar-heatmap v-show="!isLoading" :locale="locale" :no-data-text="locale.no_contributions" :tooltip-unit="locale.contributions" :end-date="endDate" :values="values" :range-color="colorRange"/></div>'
83-
});
84-
85-
new Vue({
86-
delimiters: vueDelimeters,
87-
el,
88-
89-
data: {
90-
suburl: AppSubUrl,
91-
heatmapUser,
92-
locale: {
93-
contributions: 'contributions',
94-
no_contributions: 'No contributions',
95-
},
96-
},
97-
});
8+
const View = Vue.extend(ActivityHeatmap);
9+
new View().$mount(el);
9810
}

0 commit comments

Comments
 (0)