rDrama/files/assets/js/emoji_modal/inline_user_modal.js

124 lines
2.8 KiB
JavaScript
Raw Normal View History

2024-03-07 16:24:38 +00:00
let userSearchDictionaryState = "inactive";
let globalUsers = [];
2024-03-07 17:57:46 +00:00
const userMAXXX = 50;
2024-03-07 16:24:38 +00:00
const usersSearchDictionary = {
2024-03-07 16:49:18 +00:00
array: [],
2024-03-07 16:24:38 +00:00
2024-03-07 16:49:18 +00:00
updateTag: function(userName) {
if (userName === undefined)
2024-03-07 16:24:38 +00:00
return;
let low = 0;
2024-03-07 16:49:18 +00:00
let high = this.array.length;
2024-03-07 16:24:38 +00:00
while (low < high) {
let mid = (low + high) >>> 1;
2024-03-07 16:49:18 +00:00
if (this.array[mid] < userName)
2024-03-07 16:24:38 +00:00
low = mid + 1;
else
high = mid;
}
let target = low;
2024-03-07 16:49:18 +00:00
this.array.splice(target, 0, userName);
2024-03-07 16:24:38 +00:00
},
completeSearch: function(query) {
query = query.toLowerCase()
const result = new Set();
2024-03-07 17:57:46 +00:00
for (let i = 0; i < this.array.length; i++) {
if (result.size > userMAXXX) break;
2024-03-07 16:49:18 +00:00
if (this.array[i].toLowerCase().includes(query))
result.add(this.array[i])
2024-03-07 17:57:46 +00:00
}
2024-03-07 16:24:38 +00:00
return result;
}
};
function makeUsersSearchDictionary() {
const headers = new Headers({xhr: "xhr"})
const user_params = document.getElementById('user_params').value
return fetch(`/users.csv${user_params}`, {
headers,
})
.then(res => res.json())
.then(users => {
for (let i = 0; i < users.length; i++)
{
const user = users[i];
2024-03-07 16:49:18 +00:00
usersSearchDictionary.updateTag(user);
2024-03-07 16:38:09 +00:00
globalUsers.push(user);
2024-03-07 16:24:38 +00:00
}
userSearchDictionaryState = "ready";
})
}
function curr_word_is_user()
{
2024-03-07 18:03:15 +00:00
return current_word && current_word.charAt(0) == "@" && current_word.charAt(current_word.length-1) != "@";
2024-03-07 16:24:38 +00:00
}
function openUserSpeedModal()
{
switch (userSearchDictionaryState) {
case "inactive":
userSearchDictionaryState = "loading"
return makeUsersSearchDictionary();
case "loading":
return Promise.reject();
case "ready":
return Promise.resolve();
default:
throw Error("Unknown user engine state");
}
}
function populate_inline_user_modal(results, textbox)
{
selecting = true;
if (!results || results.size === 0)
{
inline_carot_modal.style.display = "none";
2024-03-07 17:57:46 +00:00
return;
2024-03-07 16:24:38 +00:00
}
user_index = 0;
inline_carot_modal.scrollTop = 0;
inline_carot_modal.innerHTML = "";
let i = 0;
2024-03-07 16:38:09 +00:00
for (let name of results)
2024-03-07 16:24:38 +00:00
{
2024-03-07 17:57:46 +00:00
i++;
2024-03-07 16:24:38 +00:00
let user_option = document.createElement("div");
user_option.className = "inline-modal-option user-option " + (i === 1 ? "selected" : "");
user_option.tabIndex = 0;
2024-03-07 16:30:01 +00:00
let user_option_img = document.createElement("img");
user_option_img.className = "pp20";
user_option_img.src = `/@${name}/pic`
2024-03-07 16:24:38 +00:00
let user_option_text = document.createElement("span");
user_option_text.textContent = name;
user_option.addEventListener('click', () => {
2024-03-07 16:24:38 +00:00
close_inline_emoji_modal()
2024-04-20 15:46:38 +00:00
replaceText(textbox, current_word, `:${name}: `)
2024-03-07 16:24:38 +00:00
if (typeof markdown === "function" && textbox.dataset.preview) {
markdown(textbox)
}
});
user_option.appendChild(user_option_img);
user_option.appendChild(user_option_text);
2024-03-07 16:24:38 +00:00
inline_carot_modal.appendChild(user_option);
}
if (i === 0) inline_carot_modal.style.display = "none";
else inline_carot_modal.style.display = "initial";
}