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.

common-issue-list.js 2.6KB

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