2021-03-05 21:54:01 +00:00
|
|
|
import {htmlEscape} from 'escape-goat';
|
2020-07-12 09:10:56 +00:00
|
|
|
import {svg} from '../svg.js';
|
2020-02-12 01:53:18 +00:00
|
|
|
|
2020-03-11 19:34:54 +00:00
|
|
|
const {AppSubUrl} = window.config;
|
2020-02-12 01:53:18 +00:00
|
|
|
|
|
|
|
export default function initContextPopups() {
|
2020-01-20 04:39:21 +00:00
|
|
|
const refIssues = $('.ref-issue');
|
|
|
|
if (!refIssues.length) return;
|
|
|
|
|
|
|
|
refIssues.each(function () {
|
|
|
|
const [index, _issues, repo, owner] = $(this).attr('href').replace(/[#?].*$/, '').split('/').reverse();
|
2020-02-12 01:53:18 +00:00
|
|
|
issuePopup(owner, repo, index, $(this));
|
2020-01-20 04:39:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:53:18 +00:00
|
|
|
function issuePopup(owner, repo, index, $element) {
|
|
|
|
$.get(`${AppSubUrl}/api/v1/repos/${owner}/${repo}/issues/${index}`, (issue) => {
|
2020-03-11 19:34:54 +00:00
|
|
|
const createdAt = new Date(issue.created_at).toLocaleDateString(undefined, {year: 'numeric', month: 'short', day: 'numeric'});
|
2020-01-20 04:39:21 +00:00
|
|
|
|
|
|
|
let body = issue.body.replace(/\n+/g, ' ');
|
|
|
|
if (body.length > 85) {
|
|
|
|
body = `${body.substring(0, 85)}...`;
|
|
|
|
}
|
|
|
|
|
|
|
|
let labels = '';
|
|
|
|
for (let i = 0; i < issue.labels.length; i++) {
|
|
|
|
const label = issue.labels[i];
|
|
|
|
const red = parseInt(label.color.substring(0, 2), 16);
|
|
|
|
const green = parseInt(label.color.substring(2, 4), 16);
|
|
|
|
const blue = parseInt(label.color.substring(4, 6), 16);
|
|
|
|
let color = '#ffffff';
|
|
|
|
if ((red * 0.299 + green * 0.587 + blue * 0.114) > 125) {
|
|
|
|
color = '#000000';
|
|
|
|
}
|
2021-03-05 21:54:01 +00:00
|
|
|
labels += `<div class="ui label" style="color: ${color}; background-color:#${label.color};">${htmlEscape(label.name)}</div>`;
|
2020-01-20 04:39:21 +00:00
|
|
|
}
|
|
|
|
if (labels.length > 0) {
|
|
|
|
labels = `<p>${labels}</p>`;
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:53:18 +00:00
|
|
|
let octicon, color;
|
2020-01-20 04:39:21 +00:00
|
|
|
if (issue.pull_request !== null) {
|
|
|
|
if (issue.state === 'open') {
|
2020-02-12 01:53:18 +00:00
|
|
|
color = 'green';
|
|
|
|
octicon = 'octicon-git-pull-request'; // Open PR
|
2020-01-20 04:39:21 +00:00
|
|
|
} else if (issue.pull_request.merged === true) {
|
2020-02-12 01:53:18 +00:00
|
|
|
color = 'purple';
|
|
|
|
octicon = 'octicon-git-merge'; // Merged PR
|
2020-01-20 04:39:21 +00:00
|
|
|
} else {
|
2020-02-12 01:53:18 +00:00
|
|
|
color = 'red';
|
|
|
|
octicon = 'octicon-git-pull-request'; // Closed PR
|
2020-01-20 04:39:21 +00:00
|
|
|
}
|
|
|
|
} else if (issue.state === 'open') {
|
2020-02-12 01:53:18 +00:00
|
|
|
color = 'green';
|
|
|
|
octicon = 'octicon-issue-opened'; // Open Issue
|
2020-01-20 04:39:21 +00:00
|
|
|
} else {
|
2020-02-12 01:53:18 +00:00
|
|
|
color = 'red';
|
|
|
|
octicon = 'octicon-issue-closed'; // Closed Issue
|
2020-01-20 04:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$element.popup({
|
|
|
|
variation: 'wide',
|
|
|
|
delay: {
|
|
|
|
show: 250
|
|
|
|
},
|
|
|
|
html: `
|
|
|
|
<div>
|
2021-03-05 21:54:01 +00:00
|
|
|
<p><small>${htmlEscape(issue.repository.full_name)} on ${createdAt}</small></p>
|
|
|
|
<p><span class="${color}">${svg(octicon)}</span> <strong>${htmlEscape(issue.title)}</strong> #${index}</p>
|
|
|
|
<p>${htmlEscape(body)}</p>
|
2020-01-20 04:39:21 +00:00
|
|
|
${labels}
|
|
|
|
</div>
|
|
|
|
`
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|