2022-01-28 14:00:11 -07:00
|
|
|
import $ from 'jquery';
|
2021-10-16 11:28:04 -06:00
|
|
|
import {htmlEscape} from 'escape-goat';
|
|
|
|
|
2021-10-21 01:37:43 -06:00
|
|
|
const {appSubUrl} = window.config;
|
2022-10-19 06:40:28 -06:00
|
|
|
const looksLikeEmailAddressCheck = /^\S+@\S+$/;
|
|
|
|
|
2021-11-09 02:27:25 -07:00
|
|
|
export function initCompSearchUserBox() {
|
2024-06-10 14:49:33 -06:00
|
|
|
const searchUserBox = document.querySelector('#search-user-box');
|
2024-03-19 18:09:52 -06:00
|
|
|
if (!searchUserBox) return;
|
|
|
|
|
|
|
|
const $searchUserBox = $(searchUserBox);
|
|
|
|
const allowEmailInput = searchUserBox.getAttribute('data-allow-email') === 'true';
|
|
|
|
const allowEmailDescription = searchUserBox.getAttribute('data-allow-email-description') ?? undefined;
|
2021-10-16 11:28:04 -06:00
|
|
|
$searchUserBox.search({
|
|
|
|
minCharacters: 2,
|
|
|
|
apiSettings: {
|
2023-04-14 12:48:36 -06:00
|
|
|
url: `${appSubUrl}/user/search?active=1&q={query}`,
|
2021-10-16 11:28:04 -06:00
|
|
|
onResponse(response) {
|
|
|
|
const items = [];
|
2022-10-19 06:40:28 -06:00
|
|
|
const searchQuery = $searchUserBox.find('input').val();
|
|
|
|
const searchQueryUppercase = searchQuery.toUpperCase();
|
2021-10-16 11:28:04 -06:00
|
|
|
$.each(response.data, (_i, item) => {
|
|
|
|
const resultItem = {
|
2024-02-01 10:10:16 -07:00
|
|
|
title: item.login,
|
2024-03-22 08:06:53 -06:00
|
|
|
image: item.avatar_url,
|
2021-10-16 11:28:04 -06:00
|
|
|
};
|
2024-02-01 10:10:16 -07:00
|
|
|
if (item.full_name) {
|
|
|
|
resultItem.description = htmlEscape(item.full_name);
|
|
|
|
}
|
2021-10-16 11:28:04 -06:00
|
|
|
if (searchQueryUppercase === item.login.toUpperCase()) {
|
|
|
|
items.unshift(resultItem);
|
|
|
|
} else {
|
|
|
|
items.push(resultItem);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-03-25 12:37:55 -06:00
|
|
|
if (allowEmailInput && !items.length && looksLikeEmailAddressCheck.test(searchQuery)) {
|
2022-10-19 06:40:28 -06:00
|
|
|
const resultItem = {
|
|
|
|
title: searchQuery,
|
2024-03-22 08:06:53 -06:00
|
|
|
description: allowEmailDescription,
|
2022-10-19 06:40:28 -06:00
|
|
|
};
|
|
|
|
items.push(resultItem);
|
|
|
|
}
|
|
|
|
|
2021-10-16 11:28:04 -06:00
|
|
|
return {results: items};
|
2024-03-22 08:06:53 -06:00
|
|
|
},
|
2021-10-16 11:28:04 -06:00
|
|
|
},
|
|
|
|
searchFields: ['login', 'full_name'],
|
2024-03-22 08:06:53 -06:00
|
|
|
showNoResults: false,
|
2021-10-16 11:28:04 -06:00
|
|
|
});
|
|
|
|
}
|