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