gitea源码

repo-home.ts 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import {stripTags} from '../utils.ts';
  2. import {hideElem, queryElemChildren, showElem, type DOMEvent} from '../utils/dom.ts';
  3. import {POST} from '../modules/fetch.ts';
  4. import {showErrorToast, type Toast} from '../modules/toast.ts';
  5. import {fomanticQuery} from '../modules/fomantic/base.ts';
  6. const {appSubUrl} = window.config;
  7. export function initRepoTopicBar() {
  8. const mgrBtn = document.querySelector<HTMLButtonElement>('#manage_topic');
  9. if (!mgrBtn) return;
  10. const editDiv = document.querySelector('#topic_edit');
  11. const viewDiv = document.querySelector('#repo-topics');
  12. const topicDropdown = editDiv.querySelector('.ui.dropdown');
  13. let lastErrorToast: Toast;
  14. mgrBtn.addEventListener('click', () => {
  15. hideElem([viewDiv, mgrBtn]);
  16. showElem(editDiv);
  17. topicDropdown.querySelector<HTMLInputElement>('input.search').focus();
  18. });
  19. document.querySelector('#cancel_topic_edit').addEventListener('click', () => {
  20. lastErrorToast?.hideToast();
  21. hideElem(editDiv);
  22. showElem([viewDiv, mgrBtn]);
  23. mgrBtn.focus();
  24. });
  25. document.querySelector<HTMLButtonElement>('#save_topic').addEventListener('click', async (e: DOMEvent<MouseEvent, HTMLButtonElement>) => {
  26. lastErrorToast?.hideToast();
  27. const topics = editDiv.querySelector<HTMLInputElement>('input[name=topics]').value;
  28. const data = new FormData();
  29. data.append('topics', topics);
  30. const response = await POST(e.target.getAttribute('data-link'), {data});
  31. if (response.ok) {
  32. const responseData = await response.json();
  33. if (responseData.status === 'ok') {
  34. queryElemChildren(viewDiv, '.repo-topic', (el) => el.remove());
  35. if (topics.length) {
  36. const topicArray = topics.split(',');
  37. topicArray.sort();
  38. for (const topic of topicArray) {
  39. // TODO: sort items in topicDropdown, or items in edit div will have different order to the items in view div
  40. // !!!! it SHOULD and MUST match the code in "home_sidebar_top.tmpl" !!!!
  41. const link = document.createElement('a');
  42. link.classList.add('repo-topic', 'ui', 'large', 'label', 'gt-ellipsis');
  43. link.href = `${appSubUrl}/explore/repos?q=${encodeURIComponent(topic)}&topic=1`;
  44. link.textContent = topic;
  45. viewDiv.append(link);
  46. }
  47. }
  48. hideElem(editDiv);
  49. showElem([viewDiv, mgrBtn]);
  50. }
  51. } else if (response.status === 422) {
  52. // how to test: input topic like " invalid topic " (with spaces), and select it from the list, then "Save"
  53. const responseData = await response.json();
  54. lastErrorToast = showErrorToast(responseData.message, {duration: 5000});
  55. if (responseData.invalidTopics && responseData.invalidTopics.length > 0) {
  56. const {invalidTopics} = responseData;
  57. const topicLabels = queryElemChildren(topicDropdown, 'a.ui.label');
  58. for (const [index, value] of topics.split(',').entries()) {
  59. if (invalidTopics.includes(value)) {
  60. topicLabels[index].classList.remove('green');
  61. topicLabels[index].classList.add('red');
  62. }
  63. }
  64. }
  65. }
  66. });
  67. fomanticQuery(topicDropdown).dropdown({
  68. allowAdditions: true,
  69. forceSelection: false,
  70. fullTextSearch: 'exact',
  71. fields: {name: 'description', value: 'data-value'},
  72. saveRemoteData: false,
  73. label: {
  74. transition: 'horizontal flip',
  75. duration: 200,
  76. variation: false,
  77. },
  78. apiSettings: {
  79. url: `${appSubUrl}/explore/topics/search?q={query}`,
  80. throttle: 500,
  81. cache: false,
  82. onResponse(this: any, res: any) {
  83. const formattedResponse = {
  84. success: false,
  85. results: [] as Array<Record<string, any>>,
  86. };
  87. const query = stripTags(this.urlData.query.trim());
  88. let found_query = false;
  89. const current_topics = [];
  90. for (const el of queryElemChildren(topicDropdown, 'a.ui.label.visible')) {
  91. current_topics.push(el.getAttribute('data-value'));
  92. }
  93. if (res.topics) {
  94. let found = false;
  95. for (const {topic_name} of res.topics) {
  96. // skip currently added tags
  97. if (current_topics.includes(topic_name)) {
  98. continue;
  99. }
  100. if (topic_name.toLowerCase() === query.toLowerCase()) {
  101. found_query = true;
  102. }
  103. formattedResponse.results.push({description: topic_name, 'data-value': topic_name});
  104. found = true;
  105. }
  106. formattedResponse.success = found;
  107. }
  108. if (query.length > 0 && !found_query) {
  109. formattedResponse.success = true;
  110. formattedResponse.results.unshift({description: query, 'data-value': query});
  111. } else if (query.length > 0 && found_query) {
  112. formattedResponse.results.sort((a, b) => {
  113. if (a.description.toLowerCase() === query.toLowerCase()) return -1;
  114. if (b.description.toLowerCase() === query.toLowerCase()) return 1;
  115. if (a.description > b.description) return -1;
  116. if (a.description < b.description) return 1;
  117. return 0;
  118. });
  119. }
  120. return formattedResponse;
  121. },
  122. },
  123. onLabelCreate(value: string) {
  124. value = value.toLowerCase().trim();
  125. this.attr('data-value', value).contents().first().replaceWith(value);
  126. return fomanticQuery(this);
  127. },
  128. onAdd(addedValue: string, _addedText: any, $addedChoice: any) {
  129. addedValue = addedValue.toLowerCase().trim();
  130. $addedChoice[0].setAttribute('data-value', addedValue);
  131. $addedChoice[0].setAttribute('data-text', addedValue);
  132. },
  133. });
  134. }