From db2bcf04ee73f245f154a263172b5ccb72734375 Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Sun, 23 May 2021 02:48:17 +0300 Subject: [PATCH 1/3] Load issue/PR context popup data only when needed --- web_src/js/components/ContextPopup.vue | 109 +++++++++++++++++++++++++ web_src/js/features/contextpopup.js | 77 +++++------------ 2 files changed, 131 insertions(+), 55 deletions(-) create mode 100644 web_src/js/components/ContextPopup.vue diff --git a/web_src/js/components/ContextPopup.vue b/web_src/js/components/ContextPopup.vue new file mode 100644 index 0000000000000..95d1b4e91e86a --- /dev/null +++ b/web_src/js/components/ContextPopup.vue @@ -0,0 +1,109 @@ + + + diff --git a/web_src/js/features/contextpopup.js b/web_src/js/features/contextpopup.js index c16820cf1f740..8583c6253cd9f 100644 --- a/web_src/js/features/contextpopup.js +++ b/web_src/js/features/contextpopup.js @@ -1,7 +1,6 @@ -import {htmlEscape} from 'escape-goat'; -import {svg} from '../svg.js'; +import Vue from 'vue'; -const {AppSubUrl} = window.config; +import ContextPopup from '../components/ContextPopup.vue'; export default function initContextPopups() { const refIssues = $('.ref-issue'); @@ -9,68 +8,36 @@ export default function initContextPopups() { refIssues.each(function () { const [index, _issues, repo, owner] = $(this).attr('href').replace(/[#?].*$/, '').split('/').reverse(); - issuePopup(owner, repo, index, $(this)); - }); -} -function issuePopup(owner, repo, index, $element) { - $.get(`${AppSubUrl}/api/v1/repos/${owner}/${repo}/issues/${index}`, (issue) => { - const createdAt = new Date(issue.created_at).toLocaleDateString(undefined, {year: 'numeric', month: 'short', day: 'numeric'}); + const el = document.createElement('div'); + el.className = 'ui custom popup hidden'; + el.innerHTML = '
'; + this.parentNode.insertBefore(el, this.nextSibling); - let body = issue.body.replace(/\n+/g, ' '); - if (body.length > 85) { - body = `${body.substring(0, 85)}...`; - } + const View = Vue.extend({ + render: (createElement) => createElement(ContextPopup), + }); - 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'; - } - labels += `
${htmlEscape(label.name)}
`; - } - if (labels.length > 0) { - labels = `

${labels}

`; - } + const view = new View(); - let octicon, color; - if (issue.pull_request !== null) { - if (issue.state === 'open') { - color = 'green'; - octicon = 'octicon-git-pull-request'; // Open PR - } else if (issue.pull_request.merged === true) { - color = 'purple'; - octicon = 'octicon-git-merge'; // Merged PR - } else { - color = 'red'; - octicon = 'octicon-git-pull-request'; // Closed PR - } - } else if (issue.state === 'open') { - color = 'green'; - octicon = 'octicon-issue-opened'; // Open Issue - } else { - color = 'red'; - octicon = 'octicon-issue-closed'; // Closed Issue + try { + view.$mount(el.firstChild); + } catch (err) { + console.error(err); + el.textContent = 'ContextPopup failed to load'; } - $element.popup({ + $(this).popup({ variation: 'wide', delay: { show: 250 }, - html: ` -
-

${htmlEscape(issue.repository.full_name)} on ${createdAt}

-

${svg(octicon)} ${htmlEscape(issue.title)} #${index}

-

${htmlEscape(body)}

- ${labels} -
-` + onShow: () => { + view.$emit('load-context-popup', {owner, repo, index}, () => { + $(this).popup('reposition'); + }); + }, + popup: $(el), }); }); } From 2c2ff8daa1569fc4f705b4403e8aaf574f34a187 Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Sun, 23 May 2021 03:26:18 +0300 Subject: [PATCH 2/3] Add SVG icon Vue component --- web_src/js/components/ContextPopup.vue | 19 ++++++++++++------- web_src/js/svg.js | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/web_src/js/components/ContextPopup.vue b/web_src/js/components/ContextPopup.vue index 95d1b4e91e86a..26f656c8245ad 100644 --- a/web_src/js/components/ContextPopup.vue +++ b/web_src/js/components/ContextPopup.vue @@ -3,7 +3,7 @@

{{ issue.repository.full_name }} on {{ createdAt }}

-

{{ issue.title }} #{{ issue.number }}

+

{{ issue.title }} #{{ issue.number }}

{{ body }}