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.

contextpopup.js 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import {svg} from '../utils.js';
  2. const {AppSubUrl} = window.config;
  3. export default function initContextPopups() {
  4. const refIssues = $('.ref-issue');
  5. if (!refIssues.length) return;
  6. refIssues.each(function () {
  7. const [index, _issues, repo, owner] = $(this).attr('href').replace(/[#?].*$/, '').split('/').reverse();
  8. issuePopup(owner, repo, index, $(this));
  9. });
  10. }
  11. function issuePopup(owner, repo, index, $element) {
  12. $.get(`${AppSubUrl}/api/v1/repos/${owner}/${repo}/issues/${index}`, (issue) => {
  13. const createdAt = new Date(issue.created_at).toLocaleDateString(undefined, {year: 'numeric', month: 'short', day: 'numeric'});
  14. let body = issue.body.replace(/\n+/g, ' ');
  15. if (body.length > 85) {
  16. body = `${body.substring(0, 85)}...`;
  17. }
  18. let labels = '';
  19. for (let i = 0; i < issue.labels.length; i++) {
  20. const label = issue.labels[i];
  21. const labelName = emojify.replace(label.name);
  22. const red = parseInt(label.color.substring(0, 2), 16);
  23. const green = parseInt(label.color.substring(2, 4), 16);
  24. const blue = parseInt(label.color.substring(4, 6), 16);
  25. let color = '#ffffff';
  26. if ((red * 0.299 + green * 0.587 + blue * 0.114) > 125) {
  27. color = '#000000';
  28. }
  29. labels += `<div class="ui label" style="color: ${color}; background-color:#${label.color};">${labelName}</div>`;
  30. }
  31. if (labels.length > 0) {
  32. labels = `<p>${labels}</p>`;
  33. }
  34. let octicon, color;
  35. if (issue.pull_request !== null) {
  36. if (issue.state === 'open') {
  37. color = 'green';
  38. octicon = 'octicon-git-pull-request'; // Open PR
  39. } else if (issue.pull_request.merged === true) {
  40. color = 'purple';
  41. octicon = 'octicon-git-merge'; // Merged PR
  42. } else {
  43. color = 'red';
  44. octicon = 'octicon-git-pull-request'; // Closed PR
  45. }
  46. } else if (issue.state === 'open') {
  47. color = 'green';
  48. octicon = 'octicon-issue-opened'; // Open Issue
  49. } else {
  50. color = 'red';
  51. octicon = 'octicon-issue-closed'; // Closed Issue
  52. }
  53. $element.popup({
  54. variation: 'wide',
  55. delay: {
  56. show: 250
  57. },
  58. html: `
  59. <div>
  60. <p><small>${issue.repository.full_name} on ${createdAt}</small></p>
  61. <p><span class="${color}">${svg(octicon, 16)}</span> <strong>${issue.title}</strong> #${index}</p>
  62. <p>${body}</p>
  63. ${labels}
  64. </div>
  65. `
  66. });
  67. });
  68. }