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