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.

SearchUserBox.js 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import $ from 'jquery';
  2. import {htmlEscape} from 'escape-goat';
  3. const {appSubUrl} = window.config;
  4. const looksLikeEmailAddressCheck = /^\S+@\S+$/;
  5. export function initCompSearchUserBox() {
  6. const searchUserBox = document.getElementById('search-user-box');
  7. if (!searchUserBox) return;
  8. const $searchUserBox = $(searchUserBox);
  9. const allowEmailInput = searchUserBox.getAttribute('data-allow-email') === 'true';
  10. const allowEmailDescription = searchUserBox.getAttribute('data-allow-email-description') ?? undefined;
  11. $searchUserBox.search({
  12. minCharacters: 2,
  13. apiSettings: {
  14. url: `${appSubUrl}/user/search?active=1&q={query}`,
  15. onResponse(response) {
  16. const items = [];
  17. const searchQuery = $searchUserBox.find('input').val();
  18. const searchQueryUppercase = searchQuery.toUpperCase();
  19. $.each(response.data, (_i, item) => {
  20. const resultItem = {
  21. title: item.login,
  22. image: item.avatar_url,
  23. };
  24. if (item.full_name) {
  25. resultItem.description = htmlEscape(item.full_name);
  26. }
  27. if (searchQueryUppercase === item.login.toUpperCase()) {
  28. items.unshift(resultItem);
  29. } else {
  30. items.push(resultItem);
  31. }
  32. });
  33. if (allowEmailInput && items.length === 0 && looksLikeEmailAddressCheck.test(searchQuery)) {
  34. const resultItem = {
  35. title: searchQuery,
  36. description: allowEmailDescription,
  37. };
  38. items.push(resultItem);
  39. }
  40. return {results: items};
  41. },
  42. },
  43. searchFields: ['login', 'full_name'],
  44. showNoResults: false,
  45. });
  46. }