2020-02-11 18:53:18 -07:00
|
|
|
import { svg } from '../utils.js';
|
|
|
|
|
|
|
|
const { AppSubUrl } = window.config;
|
|
|
|
|
|
|
|
export default function initContextPopups() {
|
2020-01-19 21:39:21 -07: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-11 18:53:18 -07:00
|
|
|
issuePopup(owner, repo, index, $(this));
|
2020-01-19 21:39:21 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-11 18:53:18 -07:00
|
|
|
function issuePopup(owner, repo, index, $element) {
|
|
|
|
$.get(`${AppSubUrl}/api/v1/repos/${owner}/${repo}/issues/${index}`, (issue) => {
|
2020-01-19 21:39:21 -07:00
|
|
|
const createdAt = new Date(issue.created_at).toLocaleDateString(undefined, { year: 'numeric', month: 'short', day: 'numeric' });
|
|
|
|
|
|
|
|
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];
|
2020-02-08 04:48:17 -07:00
|
|
|
const labelName = emojify.replace(label.name);
|
2020-01-19 21:39:21 -07:00
|
|
|
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';
|
|
|
|
}
|
2020-02-08 04:48:17 -07:00
|
|
|
labels += `<div class="ui label" style="color: ${color}; background-color:#${label.color};">${labelName}</div>`;
|
2020-01-19 21:39:21 -07:00
|
|
|
}
|
|
|
|
if (labels.length > 0) {
|
|
|
|
labels = `<p>${labels}</p>`;
|
|
|
|
}
|
|
|
|
|
2020-02-11 18:53:18 -07:00
|
|
|
let octicon, color;
|
2020-01-19 21:39:21 -07:00
|
|
|
if (issue.pull_request !== null) {
|
|
|
|
if (issue.state === 'open') {
|
2020-02-11 18:53:18 -07:00
|
|
|
color = 'green';
|
|
|
|
octicon = 'octicon-git-pull-request'; // Open PR
|
2020-01-19 21:39:21 -07:00
|
|
|
} else if (issue.pull_request.merged === true) {
|
2020-02-11 18:53:18 -07:00
|
|
|
color = 'purple';
|
|
|
|
octicon = 'octicon-git-merge'; // Merged PR
|
2020-01-19 21:39:21 -07:00
|
|
|
} else {
|
2020-02-11 18:53:18 -07:00
|
|
|
color = 'red';
|
|
|
|
octicon = 'octicon-git-pull-request'; // Closed PR
|
2020-01-19 21:39:21 -07:00
|
|
|
}
|
|
|
|
} else if (issue.state === 'open') {
|
2020-02-11 18:53:18 -07:00
|
|
|
color = 'green';
|
|
|
|
octicon = 'octicon-issue-opened'; // Open Issue
|
2020-01-19 21:39:21 -07:00
|
|
|
} else {
|
2020-02-11 18:53:18 -07:00
|
|
|
color = 'red';
|
|
|
|
octicon = 'octicon-issue-closed'; // Closed Issue
|
2020-01-19 21:39:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
$element.popup({
|
|
|
|
variation: 'wide',
|
|
|
|
delay: {
|
|
|
|
show: 250
|
|
|
|
},
|
|
|
|
html: `
|
|
|
|
<div>
|
|
|
|
<p><small>${issue.repository.full_name} on ${createdAt}</small></p>
|
2020-02-11 18:53:18 -07:00
|
|
|
<p><span class="${color}">${svg(octicon, 16)}</span> <strong>${issue.title}</strong> #${index}</p>
|
2020-01-19 21:39:21 -07:00
|
|
|
<p>${body}</p>
|
|
|
|
${labels}
|
|
|
|
</div>
|
|
|
|
`
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|