|
1 | | -import {htmlEscape} from 'escape-goat'; |
2 | | -import {svg} from '../svg.js'; |
| 1 | +import Vue from 'vue'; |
3 | 2 |
|
4 | | -const {AppSubUrl} = window.config; |
| 3 | +import ContextPopup from '../components/ContextPopup.vue'; |
5 | 4 |
|
6 | 5 | export default function initContextPopups() { |
7 | 6 | const refIssues = $('.ref-issue'); |
8 | 7 | if (!refIssues.length) return; |
9 | 8 |
|
10 | 9 | refIssues.each(function () { |
11 | 10 | const [index, _issues, repo, owner] = $(this).attr('href').replace(/[#?].*$/, '').split('/').reverse(); |
12 | | - issuePopup(owner, repo, index, $(this)); |
13 | | - }); |
14 | | -} |
15 | 11 |
|
16 | | -function issuePopup(owner, repo, index, $element) { |
17 | | - $.get(`${AppSubUrl}/api/v1/repos/${owner}/${repo}/issues/${index}`, (issue) => { |
18 | | - const createdAt = new Date(issue.created_at).toLocaleDateString(undefined, {year: 'numeric', month: 'short', day: 'numeric'}); |
| 12 | + const el = document.createElement('div'); |
| 13 | + el.className = 'ui custom popup hidden'; |
| 14 | + el.innerHTML = '<div></div>'; |
| 15 | + this.parentNode.insertBefore(el, this.nextSibling); |
19 | 16 |
|
20 | | - let body = issue.body.replace(/\n+/g, ' '); |
21 | | - if (body.length > 85) { |
22 | | - body = `${body.substring(0, 85)}...`; |
23 | | - } |
| 17 | + const View = Vue.extend({ |
| 18 | + render: (createElement) => createElement(ContextPopup), |
| 19 | + }); |
24 | 20 |
|
25 | | - let labels = ''; |
26 | | - for (let i = 0; i < issue.labels.length; i++) { |
27 | | - const label = issue.labels[i]; |
28 | | - const red = parseInt(label.color.substring(0, 2), 16); |
29 | | - const green = parseInt(label.color.substring(2, 4), 16); |
30 | | - const blue = parseInt(label.color.substring(4, 6), 16); |
31 | | - let color = '#ffffff'; |
32 | | - if ((red * 0.299 + green * 0.587 + blue * 0.114) > 125) { |
33 | | - color = '#000000'; |
34 | | - } |
35 | | - labels += `<div class="ui label" style="color: ${color}; background-color:#${label.color};">${htmlEscape(label.name)}</div>`; |
36 | | - } |
37 | | - if (labels.length > 0) { |
38 | | - labels = `<p>${labels}</p>`; |
39 | | - } |
| 21 | + const view = new View(); |
40 | 22 |
|
41 | | - let octicon, color; |
42 | | - if (issue.pull_request !== null) { |
43 | | - if (issue.state === 'open') { |
44 | | - color = 'green'; |
45 | | - octicon = 'octicon-git-pull-request'; // Open PR |
46 | | - } else if (issue.pull_request.merged === true) { |
47 | | - color = 'purple'; |
48 | | - octicon = 'octicon-git-merge'; // Merged PR |
49 | | - } else { |
50 | | - color = 'red'; |
51 | | - octicon = 'octicon-git-pull-request'; // Closed PR |
52 | | - } |
53 | | - } else if (issue.state === 'open') { |
54 | | - color = 'green'; |
55 | | - octicon = 'octicon-issue-opened'; // Open Issue |
56 | | - } else { |
57 | | - color = 'red'; |
58 | | - octicon = 'octicon-issue-closed'; // Closed Issue |
| 23 | + try { |
| 24 | + view.$mount(el.firstChild); |
| 25 | + } catch (err) { |
| 26 | + console.error(err); |
| 27 | + el.textContent = 'ContextPopup failed to load'; |
59 | 28 | } |
60 | 29 |
|
61 | | - $element.popup({ |
| 30 | + $(this).popup({ |
62 | 31 | variation: 'wide', |
63 | 32 | delay: { |
64 | 33 | show: 250 |
65 | 34 | }, |
66 | | - html: ` |
67 | | -<div> |
68 | | - <p><small>${htmlEscape(issue.repository.full_name)} on ${createdAt}</small></p> |
69 | | - <p><span class="${color}">${svg(octicon)}</span> <strong>${htmlEscape(issue.title)}</strong> #${index}</p> |
70 | | - <p>${htmlEscape(body)}</p> |
71 | | - ${labels} |
72 | | -</div> |
73 | | -` |
| 35 | + onShow: () => { |
| 36 | + view.$emit('load-context-popup', {owner, repo, index}, () => { |
| 37 | + $(this).popup('reposition'); |
| 38 | + }); |
| 39 | + }, |
| 40 | + popup: $(el), |
74 | 41 | }); |
75 | 42 | }); |
76 | 43 | } |
0 commit comments