gitea源码

heatmap.ts 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import {createApp} from 'vue';
  2. import ActivityHeatmap from '../components/ActivityHeatmap.vue';
  3. import {translateMonth, translateDay} from '../utils.ts';
  4. export function initHeatmap() {
  5. const el = document.querySelector('#user-heatmap');
  6. if (!el) return;
  7. try {
  8. const heatmap: Record<string, number> = {};
  9. for (const {contributions, timestamp} of JSON.parse(el.getAttribute('data-heatmap-data'))) {
  10. // Convert to user timezone and sum contributions by date
  11. const dateStr = new Date(timestamp * 1000).toDateString();
  12. heatmap[dateStr] = (heatmap[dateStr] || 0) + contributions;
  13. }
  14. const values = Object.keys(heatmap).map((v) => {
  15. return {date: new Date(v), count: heatmap[v]};
  16. });
  17. // last heatmap tooltip localization attempt https://github.com/go-gitea/gitea/pull/24131/commits/a83761cbbae3c2e3b4bced71e680f44432073ac8
  18. const locale = {
  19. heatMapLocale: {
  20. months: new Array(12).fill(undefined).map((_, idx) => translateMonth(idx)),
  21. days: new Array(7).fill(undefined).map((_, idx) => translateDay(idx)),
  22. on: ' - ', // no correct locale support for it, because in many languages the sentence is not "something on someday"
  23. more: el.getAttribute('data-locale-more'),
  24. less: el.getAttribute('data-locale-less'),
  25. },
  26. tooltipUnit: 'contributions',
  27. textTotalContributions: el.getAttribute('data-locale-total-contributions'),
  28. noDataText: el.getAttribute('data-locale-no-contributions'),
  29. };
  30. const View = createApp(ActivityHeatmap, {values, locale});
  31. View.mount(el);
  32. el.classList.remove('is-loading');
  33. } catch (err) {
  34. console.error('Heatmap failed to load', err);
  35. el.textContent = 'Heatmap failed to load';
  36. }
  37. }