2022-01-29 05:00:11 +08:00
|
|
|
import $ from 'jquery';
|
2021-10-17 01:28:04 +08:00
|
|
|
import {htmlEscape} from 'escape-goat';
|
|
|
|
|
2021-10-21 15:37:43 +08:00
|
|
|
const {appSubUrl} = window.config;
|
2022-10-19 20:40:28 +08:00
|
|
|
const looksLikeEmailAddressCheck = /^\S+@\S+$/;
|
|
|
|
|
2021-11-09 17:27:25 +08:00
|
|
|
export function initCompSearchUserBox() {
|
2021-10-17 01:28:04 +08:00
|
|
|
const $searchUserBox = $('#search-user-box');
|
2022-10-19 20:40:28 +08:00
|
|
|
const allowEmailInput = $searchUserBox.attr('data-allow-email') === 'true';
|
|
|
|
const allowEmailDescription = $searchUserBox.attr('data-allow-email-description');
|
2021-10-17 01:28:04 +08:00
|
|
|
$searchUserBox.search({
|
|
|
|
minCharacters: 2,
|
|
|
|
apiSettings: {
|
2023-04-15 02:48:36 +08:00
|
|
|
url: `${appSubUrl}/user/search?active=1&q={query}`,
|
2021-10-17 01:28:04 +08:00
|
|
|
onResponse(response) {
|
|
|
|
const items = [];
|
2022-10-19 20:40:28 +08:00
|
|
|
const searchQuery = $searchUserBox.find('input').val();
|
|
|
|
const searchQueryUppercase = searchQuery.toUpperCase();
|
2021-10-17 01:28:04 +08:00
|
|
|
$.each(response.data, (_i, item) => {
|
|
|
|
const resultItem = {
|
2024-02-02 01:10:16 +08:00
|
|
|
title: item.login,
|
2021-10-17 01:28:04 +08:00
|
|
|
image: item.avatar_url
|
|
|
|
};
|
2024-02-02 01:10:16 +08:00
|
|
|
if (item.full_name) {
|
|
|
|
resultItem.description = htmlEscape(item.full_name);
|
|
|
|
}
|
2021-10-17 01:28:04 +08:00
|
|
|
if (searchQueryUppercase === item.login.toUpperCase()) {
|
|
|
|
items.unshift(resultItem);
|
|
|
|
} else {
|
|
|
|
items.push(resultItem);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-10-19 20:40:28 +08:00
|
|
|
if (allowEmailInput && items.length === 0 && looksLikeEmailAddressCheck.test(searchQuery)) {
|
|
|
|
const resultItem = {
|
|
|
|
title: searchQuery,
|
|
|
|
description: allowEmailDescription
|
|
|
|
};
|
|
|
|
items.push(resultItem);
|
|
|
|
}
|
|
|
|
|
2021-10-17 01:28:04 +08:00
|
|
|
return {results: items};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
searchFields: ['login', 'full_name'],
|
|
|
|
showNoResults: false
|
|
|
|
});
|
|
|
|
}
|