2022-01-29 02:30:11 +05:30
|
|
|
import $ from 'jquery';
|
2021-10-16 22:58:04 +05:30
|
|
|
import {htmlEscape} from 'escape-goat';
|
|
|
|
|
2021-10-21 13:07:43 +05:30
|
|
|
const {appSubUrl} = window.config;
|
2022-10-19 18:10:28 +05:30
|
|
|
const looksLikeEmailAddressCheck = /^\S+@\S+$/;
|
|
|
|
|
2021-11-09 14:57:25 +05:30
|
|
|
export function initCompSearchUserBox() {
|
2024-03-20 05:39:52 +05:30
|
|
|
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 22:58:04 +05:30
|
|
|
$searchUserBox.search({
|
|
|
|
minCharacters: 2,
|
|
|
|
apiSettings: {
|
2023-04-15 00:18:36 +05:30
|
|
|
url: `${appSubUrl}/user/search?active=1&q={query}`,
|
2021-10-16 22:58:04 +05:30
|
|
|
onResponse(response) {
|
|
|
|
const items = [];
|
2022-10-19 18:10:28 +05:30
|
|
|
const searchQuery = $searchUserBox.find('input').val();
|
|
|
|
const searchQueryUppercase = searchQuery.toUpperCase();
|
2021-10-16 22:58:04 +05:30
|
|
|
$.each(response.data, (_i, item) => {
|
|
|
|
const resultItem = {
|
2024-02-01 22:40:16 +05:30
|
|
|
title: item.login,
|
2024-03-22 19:36:53 +05:30
|
|
|
image: item.avatar_url,
|
2021-10-16 22:58:04 +05:30
|
|
|
};
|
2024-02-01 22:40:16 +05:30
|
|
|
if (item.full_name) {
|
|
|
|
resultItem.description = htmlEscape(item.full_name);
|
|
|
|
}
|
2021-10-16 22:58:04 +05:30
|
|
|
if (searchQueryUppercase === item.login.toUpperCase()) {
|
|
|
|
items.unshift(resultItem);
|
|
|
|
} else {
|
|
|
|
items.push(resultItem);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-03-26 00:07:55 +05:30
|
|
|
if (allowEmailInput && !items.length && looksLikeEmailAddressCheck.test(searchQuery)) {
|
2022-10-19 18:10:28 +05:30
|
|
|
const resultItem = {
|
|
|
|
title: searchQuery,
|
2024-03-22 19:36:53 +05:30
|
|
|
description: allowEmailDescription,
|
2022-10-19 18:10:28 +05:30
|
|
|
};
|
|
|
|
items.push(resultItem);
|
|
|
|
}
|
|
|
|
|
2021-10-16 22:58:04 +05:30
|
|
|
return {results: items};
|
2024-03-22 19:36:53 +05:30
|
|
|
},
|
2021-10-16 22:58:04 +05:30
|
|
|
},
|
|
|
|
searchFields: ['login', 'full_name'],
|
2024-03-22 19:36:53 +05:30
|
|
|
showNoResults: false,
|
2021-10-16 22:58:04 +05:30
|
|
|
});
|
|
|
|
}
|