You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

userheatmap.js 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import Vue from 'vue';
  2. const {AppSubUrl, heatmapUser} = window.config;
  3. export default async function initHeatmap() {
  4. const el = document.getElementById('user-heatmap');
  5. if (!el) return;
  6. const {CalendarHeatmap} = await import(/* webpackChunkName: "userheatmap" */'vue-calendar-heatmap');
  7. Vue.component('calendarHeatmap', CalendarHeatmap);
  8. const vueDelimeters = ['${', '}'];
  9. Vue.component('activity-heatmap', {
  10. delimiters: vueDelimeters,
  11. props: {
  12. user: {
  13. type: String,
  14. required: true
  15. },
  16. suburl: {
  17. type: String,
  18. required: true
  19. },
  20. locale: {
  21. type: Object,
  22. required: true
  23. }
  24. },
  25. data() {
  26. return {
  27. isLoading: true,
  28. colorRange: [],
  29. endDate: null,
  30. values: [],
  31. totalContributions: 0,
  32. };
  33. },
  34. mounted() {
  35. this.colorRange = [
  36. this.getColor(0),
  37. this.getColor(1),
  38. this.getColor(2),
  39. this.getColor(3),
  40. this.getColor(4),
  41. this.getColor(5)
  42. ];
  43. this.endDate = new Date();
  44. this.loadHeatmap(this.user);
  45. },
  46. methods: {
  47. loadHeatmap(userName) {
  48. const self = this;
  49. $.get(`${this.suburl}/api/v1/users/${userName}/heatmap`, (chartRawData) => {
  50. const chartData = [];
  51. for (let i = 0; i < chartRawData.length; i++) {
  52. self.totalContributions += chartRawData[i].contributions;
  53. chartData[i] = {date: new Date(chartRawData[i].timestamp * 1000), count: chartRawData[i].contributions};
  54. }
  55. self.values = chartData;
  56. self.isLoading = false;
  57. });
  58. },
  59. getColor(idx) {
  60. const el = document.createElement('div');
  61. el.className = `heatmap-color-${idx}`;
  62. document.body.appendChild(el);
  63. const color = getComputedStyle(el).backgroundColor;
  64. document.body.removeChild(el);
  65. return color;
  66. }
  67. },
  68. 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>'
  69. });
  70. new Vue({
  71. delimiters: vueDelimeters,
  72. el,
  73. data: {
  74. suburl: AppSubUrl,
  75. heatmapUser,
  76. locale: {
  77. contributions: 'contributions',
  78. no_contributions: 'No contributions',
  79. },
  80. },
  81. });
  82. }