2024-07-07 15:32:30 +00:00
|
|
|
import {isElemHidden, onInputDebounce, submitEventSubmitter, toggleElem} from '../utils/dom.ts';
|
|
|
|
import {GET} from '../modules/fetch.ts';
|
2023-05-10 15:50:58 +00:00
|
|
|
|
2023-09-19 00:50:30 +00:00
|
|
|
const {appSubUrl} = window.config;
|
2023-05-10 15:50:58 +00:00
|
|
|
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() {
|
2024-06-10 20:49:33 +00:00
|
|
|
const goto = document.querySelector('#issue-list-quick-goto');
|
2024-03-23 18:18:45 +00:00
|
|
|
if (!goto) return;
|
2023-05-10 15:50:58 +00:00
|
|
|
|
2024-03-23 18:18:45 +00:00
|
|
|
const form = goto.closest('form');
|
|
|
|
const input = form.querySelector('input[name=q]');
|
|
|
|
const repoLink = goto.getAttribute('data-repo-link');
|
2023-05-10 15:50:58 +00:00
|
|
|
|
2024-03-23 18:18:45 +00:00
|
|
|
form.addEventListener('submit', (e) => {
|
2023-05-10 15:50:58 +00:00
|
|
|
// if there is no goto button, or the form is submitted by non-quick-goto elements, submit the form directly
|
2024-03-23 18:18:45 +00:00
|
|
|
let doQuickGoto = !isElemHidden(goto);
|
2024-02-17 20:48:10 +00:00
|
|
|
const submitter = submitEventSubmitter(e);
|
2024-03-23 18:18:45 +00:00
|
|
|
if (submitter !== form && submitter !== input && submitter !== goto) doQuickGoto = false;
|
2023-05-10 15:50:58 +00:00
|
|
|
if (!doQuickGoto) return;
|
|
|
|
|
|
|
|
// if there is a goto button, use its link
|
|
|
|
e.preventDefault();
|
2024-03-23 18:18:45 +00:00
|
|
|
window.location.href = goto.getAttribute('data-issue-goto-link');
|
2023-05-10 15:50:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const onInput = async () => {
|
2024-03-23 18:18:45 +00:00
|
|
|
const searchText = input.value;
|
2023-05-10 15:50:58 +00:00
|
|
|
// try to check whether the parsed goto link is valid
|
|
|
|
let targetUrl = parseIssueListQuickGotoLink(repoLink, searchText);
|
|
|
|
if (targetUrl) {
|
2024-04-30 02:36:32 +00:00
|
|
|
const res = await GET(`${targetUrl}/info`); // backend: GetIssueInfo, it only checks whether the issue exists by status code
|
2023-05-10 15:50:58 +00:00
|
|
|
if (res.status !== 200) targetUrl = '';
|
|
|
|
}
|
|
|
|
// if the input value has changed, then ignore the result
|
2024-03-23 18:18:45 +00:00
|
|
|
if (input.value !== searchText) return;
|
2023-05-10 15:50:58 +00:00
|
|
|
|
2024-03-23 18:18:45 +00:00
|
|
|
toggleElem(goto, Boolean(targetUrl));
|
|
|
|
goto.setAttribute('data-issue-goto-link', targetUrl);
|
2023-05-10 15:50:58 +00:00
|
|
|
};
|
|
|
|
|
2024-03-23 18:18:45 +00:00
|
|
|
input.addEventListener('input', onInputDebounce(onInput));
|
2023-05-10 15:50:58 +00:00
|
|
|
onInput();
|
|
|
|
}
|