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