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

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