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-03-20 00:09:52 +00:00
|
|
|
const searchUserBox = document.getElementById('search-user-box');
|
|
|
|
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 17:28:04 +00:00
|
|
|
$searchUserBox.search({
|
|
|
|
minCharacters: 2,
|
|
|
|
apiSettings: {
|
2023-04-14 18:48:36 +00:00
|
|
|
url: `${appSubUrl}/user/search?active=1&q={query}`,
|
2021-10-16 17:28:04 +00:00
|
|
|
onResponse(response) {
|
|
|
|
const items = [];
|
2022-10-19 12:40:28 +00:00
|
|
|
const searchQuery = $searchUserBox.find('input').val();
|
|
|
|
const searchQueryUppercase = searchQuery.toUpperCase();
|
2021-10-16 17:28:04 +00:00
|
|
|
$.each(response.data, (_i, item) => {
|
|
|
|
const resultItem = {
|
2024-02-01 17:10:16 +00:00
|
|
|
title: item.login,
|
2021-10-16 17:28:04 +00:00
|
|
|
image: item.avatar_url
|
|
|
|
};
|
2024-02-01 17:10:16 +00:00
|
|
|
if (item.full_name) {
|
|
|
|
resultItem.description = htmlEscape(item.full_name);
|
|
|
|
}
|
2021-10-16 17:28:04 +00:00
|
|
|
if (searchQueryUppercase === item.login.toUpperCase()) {
|
|
|
|
items.unshift(resultItem);
|
|
|
|
} else {
|
|
|
|
items.push(resultItem);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-10-19 12:40:28 +00:00
|
|
|
if (allowEmailInput && items.length === 0 && looksLikeEmailAddressCheck.test(searchQuery)) {
|
|
|
|
const resultItem = {
|
|
|
|
title: searchQuery,
|
|
|
|
description: allowEmailDescription
|
|
|
|
};
|
|
|
|
items.push(resultItem);
|
|
|
|
}
|
|
|
|
|
2021-10-16 17:28:04 +00:00
|
|
|
return {results: items};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
searchFields: ['login', 'full_name'],
|
|
|
|
showNoResults: false
|
|
|
|
});
|
|
|
|
}
|