mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.7 KiB
69 lines
2.7 KiB
5 months ago
|
import {isElemHidden, onInputDebounce, submitEventSubmitter, toggleElem} from '../utils/dom.ts';
|
||
|
import {GET} from '../modules/fetch.ts';
|
||
2 years ago
|
|
||
1 year ago
|
const {appSubUrl} = window.config;
|
||
2 years ago
|
const reIssueIndex = /^(\d+)$/; // eg: "123"
|
||
|
const reIssueSharpIndex = /^#(\d+)$/; // eg: "#123"
|
||
|
const reIssueOwnerRepoIndex = /^([-.\w]+)\/([-.\w]+)#(\d+)$/; // eg: "{owner}/{repo}#{index}"
|
||
|
|
||
|
// if the searchText can be parsed to an "issue goto link", return the link, otherwise return empty string
|
||
|
export function parseIssueListQuickGotoLink(repoLink, searchText) {
|
||
|
searchText = searchText.trim();
|
||
|
let targetUrl = '';
|
||
|
if (repoLink) {
|
||
|
// try to parse it in current repo
|
||
|
if (reIssueIndex.test(searchText)) {
|
||
|
targetUrl = `${repoLink}/issues/${searchText}`;
|
||
|
} else if (reIssueSharpIndex.test(searchText)) {
|
||
|
targetUrl = `${repoLink}/issues/${searchText.substr(1)}`;
|
||
|
}
|
||
|
} else {
|
||
|
// try to parse it for a global search (eg: "owner/repo#123")
|
||
|
const matchIssueOwnerRepoIndex = searchText.match(reIssueOwnerRepoIndex);
|
||
|
if (matchIssueOwnerRepoIndex) {
|
||
|
const [_, owner, repo, index] = matchIssueOwnerRepoIndex;
|
||
|
targetUrl = `${appSubUrl}/${owner}/${repo}/issues/${index}`;
|
||
|
}
|
||
|
}
|
||
|
return targetUrl;
|
||
|
}
|
||
|
|
||
|
export function initCommonIssueListQuickGoto() {
|
||
6 months ago
|
const goto = document.querySelector('#issue-list-quick-goto');
|
||
8 months ago
|
if (!goto) return;
|
||
2 years ago
|
|
||
8 months ago
|
const form = goto.closest('form');
|
||
|
const input = form.querySelector('input[name=q]');
|
||
|
const repoLink = goto.getAttribute('data-repo-link');
|
||
2 years ago
|
|
||
8 months ago
|
form.addEventListener('submit', (e) => {
|
||
2 years ago
|
// if there is no goto button, or the form is submitted by non-quick-goto elements, submit the form directly
|
||
8 months ago
|
let doQuickGoto = !isElemHidden(goto);
|
||
9 months ago
|
const submitter = submitEventSubmitter(e);
|
||
8 months ago
|
if (submitter !== form && submitter !== input && submitter !== goto) doQuickGoto = false;
|
||
2 years ago
|
if (!doQuickGoto) return;
|
||
|
|
||
|
// if there is a goto button, use its link
|
||
|
e.preventDefault();
|
||
8 months ago
|
window.location.href = goto.getAttribute('data-issue-goto-link');
|
||
2 years ago
|
});
|
||
|
|
||
|
const onInput = async () => {
|
||
8 months ago
|
const searchText = input.value;
|
||
2 years ago
|
// try to check whether the parsed goto link is valid
|
||
|
let targetUrl = parseIssueListQuickGotoLink(repoLink, searchText);
|
||
|
if (targetUrl) {
|
||
7 months ago
|
const res = await GET(`${targetUrl}/info`); // backend: GetIssueInfo, it only checks whether the issue exists by status code
|
||
2 years ago
|
if (res.status !== 200) targetUrl = '';
|
||
|
}
|
||
|
// if the input value has changed, then ignore the result
|
||
8 months ago
|
if (input.value !== searchText) return;
|
||
2 years ago
|
|
||
8 months ago
|
toggleElem(goto, Boolean(targetUrl));
|
||
|
goto.setAttribute('data-issue-goto-link', targetUrl);
|
||
2 years ago
|
};
|
||
|
|
||
8 months ago
|
input.addEventListener('input', onInputDebounce(onInput));
|
||
2 years ago
|
onInput();
|
||
|
}
|