code.gitea.io/gitea@v1.21.7/web_src/js/features/common-issue-list.js (about) 1 import $ from 'jquery'; 2 import {isElemHidden, onInputDebounce, submitEventSubmitter, toggleElem} from '../utils/dom.js'; 3 import {GET} from '../modules/fetch.js'; 4 5 const {appSubUrl} = window.config; 6 const reIssueIndex = /^(\d+)$/; // eg: "123" 7 const reIssueSharpIndex = /^#(\d+)$/; // eg: "#123" 8 const reIssueOwnerRepoIndex = /^([-.\w]+)\/([-.\w]+)#(\d+)$/; // eg: "{owner}/{repo}#{index}" 9 10 // if the searchText can be parsed to an "issue goto link", return the link, otherwise return empty string 11 export function parseIssueListQuickGotoLink(repoLink, searchText) { 12 searchText = searchText.trim(); 13 let targetUrl = ''; 14 if (repoLink) { 15 // try to parse it in current repo 16 if (reIssueIndex.test(searchText)) { 17 targetUrl = `${repoLink}/issues/${searchText}`; 18 } else if (reIssueSharpIndex.test(searchText)) { 19 targetUrl = `${repoLink}/issues/${searchText.substr(1)}`; 20 } 21 } else { 22 // try to parse it for a global search (eg: "owner/repo#123") 23 const matchIssueOwnerRepoIndex = searchText.match(reIssueOwnerRepoIndex); 24 if (matchIssueOwnerRepoIndex) { 25 const [_, owner, repo, index] = matchIssueOwnerRepoIndex; 26 targetUrl = `${appSubUrl}/${owner}/${repo}/issues/${index}`; 27 } 28 } 29 return targetUrl; 30 } 31 32 export function initCommonIssueListQuickGoto() { 33 const $goto = $('#issue-list-quick-goto'); 34 if (!$goto.length) return; 35 36 const $form = $goto.closest('form'); 37 const $input = $form.find('input[name=q]'); 38 const repoLink = $goto.attr('data-repo-link'); 39 40 $form.on('submit', (e) => { 41 // if there is no goto button, or the form is submitted by non-quick-goto elements, submit the form directly 42 let doQuickGoto = !isElemHidden($goto); 43 const submitter = submitEventSubmitter(e); 44 if (submitter !== $form[0] && submitter !== $input[0] && submitter !== $goto[0]) doQuickGoto = false; 45 if (!doQuickGoto) return; 46 47 // if there is a goto button, use its link 48 e.preventDefault(); 49 window.location.href = $goto.attr('data-issue-goto-link'); 50 }); 51 52 const onInput = async () => { 53 const searchText = $input.val(); 54 55 // try to check whether the parsed goto link is valid 56 let targetUrl = parseIssueListQuickGotoLink(repoLink, searchText); 57 if (targetUrl) { 58 const res = await GET(`${targetUrl}/info`); 59 if (res.status !== 200) targetUrl = ''; 60 } 61 62 // if the input value has changed, then ignore the result 63 if ($input.val() !== searchText) return; 64 65 toggleElem($goto, Boolean(targetUrl)); 66 $goto.attr('data-issue-goto-link', targetUrl); 67 }; 68 69 $input.on('input', onInputDebounce(onInput)); 70 onInput(); 71 }