2022-01-28 22:00:11 +01:00
|
|
|
import $ from 'jquery';
|
2021-10-16 19:28:04 +02:00
|
|
|
import {htmlEscape} from 'escape-goat';
|
|
|
|
|
2021-10-21 09:37:43 +02:00
|
|
|
const {appSubUrl} = window.config;
|
2021-10-16 19:28:04 +02:00
|
|
|
|
2021-11-09 10:27:25 +01:00
|
|
|
export function initCompSearchUserBox() {
|
2021-10-16 19:28:04 +02:00
|
|
|
const $searchUserBox = $('#search-user-box');
|
|
|
|
$searchUserBox.search({
|
|
|
|
minCharacters: 2,
|
|
|
|
apiSettings: {
|
2021-10-21 09:37:43 +02:00
|
|
|
url: `${appSubUrl}/api/v1/users/search?q={query}`,
|
2021-10-16 19:28:04 +02:00
|
|
|
onResponse(response) {
|
|
|
|
const items = [];
|
|
|
|
const searchQueryUppercase = $searchUserBox.find('input').val().toUpperCase();
|
|
|
|
$.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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return {results: items};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
searchFields: ['login', 'full_name'],
|
|
|
|
showNoResults: false
|
|
|
|
});
|
|
|
|
}
|