diff options
author | silverwind <me@silverwind.io> | 2020-03-11 20:34:54 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-11 21:34:54 +0200 |
commit | e03d6277698f4e1e03d9336ba017bae130d4353c (patch) | |
tree | 418c84dd378bc86093f9837de80f92c13fe87f68 /web_src/js/features/contextpopup.js | |
parent | 984b85c1a72f9f1561447a8492bc3d33a87e3641 (diff) | |
download | gitea-e03d6277698f4e1e03d9336ba017bae130d4353c.tar.gz gitea-e03d6277698f4e1e03d9336ba017bae130d4353c.zip |
Misc JS linting and naming tweaks (#10652)
- lowercase all js filenames except Vue components
- enable new lint rules, mostly focused on shorter code
- autofix new lint violations
- apply misc transformations indexOf -> includes and onevent-> addEventListener
Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com>
Diffstat (limited to 'web_src/js/features/contextpopup.js')
-rw-r--r-- | web_src/js/features/contextpopup.js | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/web_src/js/features/contextpopup.js b/web_src/js/features/contextpopup.js new file mode 100644 index 0000000000..6feaa768c0 --- /dev/null +++ b/web_src/js/features/contextpopup.js @@ -0,0 +1,76 @@ +import {svg} from '../utils.js'; + +const {AppSubUrl} = window.config; + +export default function initContextPopups() { + const refIssues = $('.ref-issue'); + if (!refIssues.length) return; + + 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'}); + + 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 labelName = emojify.replace(label.name); + 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};">${labelName}</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({ + variation: 'wide', + delay: { + show: 250 + }, + html: ` +<div> + <p><small>${issue.repository.full_name} on ${createdAt}</small></p> + <p><span class="${color}">${svg(octicon, 16)}</span> <strong>${issue.title}</strong> #${index}</p> + <p>${body}</p> + ${labels} +</div> +` + }); + }); +} |