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() {
|
2021-10-16 11:28:04 -06:00
|
|
|
const $searchUserBox = $('#search-user-box');
|
2022-10-19 06:40:28 -06:00
|
|
|
const allowEmailInput = $searchUserBox.attr('data-allow-email') === 'true';
|
|
|
|
const allowEmailDescription = $searchUserBox.attr('data-allow-email-description');
|
2021-10-16 11:28:04 -06:00
|
|
|
$searchUserBox.search({
|
|
|
|
minCharacters: 2,
|
|
|
|
apiSettings: {
|
2022-04-07 12:59:56 -06:00
|
|
|
url: `${appSubUrl}/user/search?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) => {
|
|
|
|
let title = item.login;
|
|
|
|
if (item.full_name && item.full_name.length > 0) {
|
|
|
|
title += ` (${htmlEscape(item.full_name)})`;
|
|
|
|
}
|
|
|
|
const resultItem = {
|
|
|
|
title,
|
|
|
|
image: item.avatar_url
|
|
|
|
};
|
|
|
|
if (searchQueryUppercase === item.login.toUpperCase()) {
|
|
|
|
items.unshift(resultItem);
|
|
|
|
} else {
|
|
|
|
items.push(resultItem);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-10-19 06:40:28 -06:00
|
|
|
if (allowEmailInput && items.length === 0 && looksLikeEmailAddressCheck.test(searchQuery)) {
|
|
|
|
const resultItem = {
|
|
|
|
title: searchQuery,
|
|
|
|
description: allowEmailDescription
|
|
|
|
};
|
|
|
|
items.push(resultItem);
|
|
|
|
}
|
|
|
|
|
2021-10-16 11:28:04 -06:00
|
|
|
return {results: items};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
searchFields: ['login', 'full_name'],
|
|
|
|
showNoResults: false
|
|
|
|
});
|
|
|
|
}
|