mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 02:46:04 +01:00 
			
		
		
		
	Load issue/PR context popup data only when needed (#15955)
* Load issue/PR context popup data only when needed * Add SVG icon Vue component * Remove unneeded check
This commit is contained in:
		| @@ -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 = '<div></div>'; | ||||
|     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), | ||||
|     }); | ||||
|  | ||||
|     const view = new View(); | ||||
|  | ||||
|     try { | ||||
|       view.$mount(el.firstChild); | ||||
|     } catch (err) { | ||||
|       console.error(err); | ||||
|       el.textContent = 'ContextPopup failed to load'; | ||||
|     } | ||||
|  | ||||
|     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 += `<div class="ui label" style="color: ${color}; background-color:#${label.color};">${htmlEscape(label.name)}</div>`; | ||||
|     } | ||||
|     if (labels.length > 0) { | ||||
|       labels = `<p>${labels}</p>`; | ||||
|     } | ||||
|  | ||||
|     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 | ||||
|     } | ||||
|  | ||||
|     $element.popup({ | ||||
|     $(this).popup({ | ||||
|       variation: 'wide', | ||||
|       delay: { | ||||
|         show: 250 | ||||
|       }, | ||||
|       html: ` | ||||
| <div> | ||||
|   <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> | ||||
|   ${labels} | ||||
| </div> | ||||
| ` | ||||
|       onShow: () => { | ||||
|         view.$emit('load-context-popup', {owner, repo, index}, () => { | ||||
|           $(this).popup('reposition'); | ||||
|         }); | ||||
|       }, | ||||
|       popup: $(el), | ||||
|     }); | ||||
|   }); | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user