ft_transcendence/frontend/static/js/views/Search.js

228 lines
5.8 KiB
JavaScript
Raw Permalink Normal View History

import AbstractView from "./abstracts/AbstractView.js";
import {client} from "../index.js";
2023-12-15 14:32:43 -05:00
import {Message} from "../api/chat/message.js"
2023-11-30 10:36:21 -05:00
2023-12-11 08:54:39 -05:00
export default class extends AbstractView {
2023-11-30 10:36:21 -05:00
constructor(params) {
super(params, "Search");
}
async postInit() {
2023-12-15 14:32:43 -05:00
let search = document.getElementById("input_user");
search.oninput = this.users;
2023-12-11 08:54:39 -05:00
2023-12-15 14:32:43 -05:00
let chat_input = document.getElementById("input_chat");
//chat_input.addEventListener("keydown", this.chat_manager)
this.last_add_chat = undefined;
2023-12-11 08:54:39 -05:00
this.users();
2023-12-15 14:32:43 -05:00
this.chat();
2023-12-11 08:54:39 -05:00
}
async users() {
2023-12-15 14:32:43 -05:00
let search = document.getElementById("input_user").value.toLowerCase();
2023-12-11 08:54:39 -05:00
2023-12-12 04:04:46 -05:00
let logged = await client.isAuthentificate();
2023-12-11 08:54:39 -05:00
let users = await client.profiles.all();
2023-11-30 10:36:21 -05:00
let list_users = document.getElementById('list_users');
2023-12-11 08:54:39 -05:00
list_users.innerHTML = "";
2023-12-15 14:32:43 -05:00
users.filter(user => user.username.toLowerCase().startsWith(search) == true).forEach((user) => {
if (user.user_id == null) {
console.log("list User one with id null;");
return;
}
2023-11-30 10:36:21 -05:00
var new_user = document.createElement("li");
2023-12-11 08:54:39 -05:00
// username
let username = document.createElement("a");
username.href = `/profiles/${user.user_id}`;
username.appendChild(document.createTextNode(user.username));
new_user.appendChild(username);
2023-12-11 10:14:27 -05:00
// space
new_user.appendChild(document.createTextNode(" "));
// chat
2023-12-15 14:32:43 -05:00
if (logged && client.me.user_id != user.user_id) {
let add_chat = document.createElement("a");
2023-12-16 10:14:46 -05:00
add_chat.id = "add_chat_off";
add_chat.onclick = async () => {
2023-12-15 14:32:43 -05:00
if (client.channel != undefined) {
client.channel.members_id.forEach((member_id) => {
if (member_id == user.user_id)
client.channel = undefined;
});
if (client.channel == undefined) {
add_chat.id = "add_chat_off";
this.last_add_chat = undefined;
2023-12-15 14:32:43 -05:00
return this.hideChat();
}
2023-12-15 14:32:43 -05:00
client.channel.disconnect();
}
client.channel = await client.channels.createChannel([client.me.user_id , user.user_id], this.chat);
this.chat();
if (this.last_add_chat != undefined)
this.last_add_chat.id = "add_chat_off";
this.last_add_chat = add_chat;
2023-12-16 10:14:46 -05:00
add_chat.id = "add_chat_on";
};
2023-12-15 14:32:43 -05:00
add_chat.appendChild(document.createTextNode("Chat"));
new_user.appendChild(add_chat);
2023-12-16 10:14:46 -05:00
new_user.appendChild(document.createTextNode(" "));
2023-12-15 14:32:43 -05:00
2023-12-12 04:04:46 -05:00
}
2023-12-11 08:54:39 -05:00
// break line
new_user.appendChild(document.createElement("br"));
// avatar
var img = document.createElement("img");
img.src = user.avatar_url;
new_user.appendChild(img);
2023-12-11 10:14:27 -05:00
2023-11-30 10:36:21 -05:00
list_users.appendChild(new_user);
2023-12-11 08:54:39 -05:00
});
2023-12-12 04:04:46 -05:00
//console.log(list_users);
2023-11-30 10:36:21 -05:00
}
2023-12-15 14:32:43 -05:00
async chat() {
2023-12-20 17:48:52 -05:00
let users = await client.profiles.all();
2023-12-15 14:32:43 -05:00
let logged = await client.isAuthentificate();
/*let reload = document.getElementById("messages");
2023-12-15 14:32:43 -05:00
if (reload != null)
reload.remove();*/
2023-12-15 14:32:43 -05:00
let reload = document.getElementById("members");
2023-12-15 14:32:43 -05:00
if (reload != null)
reload.remove();
if (client.channel == undefined || !logged)
return ;
let chats = document.getElementById("chats");
if (document.getElementById("chat") == null) {
let chat = document.createElement("div");
chat.id = "chat";
chats.appendChild(chat);
}
2023-12-15 14:32:43 -05:00
// div des messages
let messages = document.getElementById("messages");
if (messages == null) {
messages = document.createElement("div");
messages.id = "messages";
if (document.getElementById("input_chat") == null)
chat.appendChild(messages);
else
document.getElementById("input_chat").before(messages);
}
// les messages, réecriture seulement du dernier
let i = 0;
client.channel.messages.forEach((message) => {
2023-12-19 06:42:46 -05:00
if (messages.children[i] == null || message.content != messages.children[i].innerText) {
let text = document.createElement("p");
text.appendChild(document.createTextNode(message.content));
if (message.author_id == client.me.user_id)
text.id = "you";
else
text.id = "other";
messages.appendChild(text);
}
i++;
});
2023-12-15 14:32:43 -05:00
// Input pour rentrer un message
if (document.getElementById("input_chat") == null) {
let chat_input = document.createElement("input");
chat_input.id="input_chat";
chat_input.type="text";
chat_input.name="message";
chat_input.placeholder="message bozo";
chat_input.maxLength=255;
chat.appendChild(chat_input);
2023-12-20 17:48:52 -05:00
chat_input.onkeydown = async () => {
2023-12-15 14:32:43 -05:00
if (event.keyCode == 13 && client.channel != undefined) {
//let chat_input = document.getElementById("input_chat");
let chat_text = chat_input.value;
2023-12-20 17:48:52 -05:00
let receivers_id = [];
client.channel.members_id.forEach((member_id) => {
if (member_id != client.me.user_id)
receivers_id.push(users.filter(user => user.user_id == member_id)[0].user_id);
});
await client.channel.sendMessageChannel(chat_text, receivers_id)
2023-12-15 14:32:43 -05:00
// Reset
chat_input.value = "";
}
2023-12-20 17:48:52 -05:00
};
2023-12-15 14:32:43 -05:00
}
// nom des membres du chat
let members = document.createElement("h2");
members.id = "members";
let usernames = "";
client.channel.members_id.forEach((member_id) => {
if (member_id != client.me.user_id) {
if (usernames.length > 0)
usernames += ", ";
usernames += (users.filter(user => user.user_id == member_id)[0].username);
}
});
members.appendChild(document.createTextNode(usernames));
messages.before(members);
2023-12-18 15:26:18 -05:00
// Scroll to the bottom of messages
messages.scrollTop = messages.scrollHeight;
2023-12-15 14:32:43 -05:00
}
async hideChat() {
let close = document.getElementById("chat");
if (close != null)
close.remove();
}
async leavePage() {
if (client.channel != undefined)
client.channel.disconnect();
client.channel = undefined
}
2023-11-30 10:36:21 -05:00
async getHtml() {
return `
2023-12-11 08:54:39 -05:00
<link rel="stylesheet" href="/static/css/search.css">
2023-11-30 10:36:21 -05:00
2023-12-15 14:32:43 -05:00
<div id="chats">
<div id="users">
<input id="input_user" type="text" name="message" placeholder="userbozo"/>
<ul id="list_users">
</ul>
</div>
2023-11-30 10:36:21 -05:00
</div>
`;
}
}