116 lines
3.3 KiB
JavaScript
116 lines
3.3 KiB
JavaScript
let rooms = {};
|
||
let roomToDelete = null;
|
||
|
||
function openRoom(currentRoom) {
|
||
alert('Вы вошли в комнату: ' + currentRoom);
|
||
}
|
||
|
||
function closeAdd() {
|
||
document.getElementById('add_members').style.display = 'none';
|
||
}
|
||
|
||
function openAdd() {
|
||
document.getElementById('add_members').style.display = 'flex';
|
||
}
|
||
|
||
function openConfirm(roomName) {
|
||
roomToDelete = roomName;
|
||
document.getElementById("delete-chat").style.display = "flex";
|
||
}
|
||
|
||
function closeConfirm() {
|
||
roomToDelete = null;
|
||
document.getElementById("delete-chat").style.display = "none";
|
||
}
|
||
|
||
function deleteChat() {
|
||
if (roomToDelete && rooms[roomToDelete]) {
|
||
delete rooms[roomToDelete];
|
||
removeRoomFromList(roomToDelete);
|
||
closeConfirm();
|
||
} else {
|
||
alert("Не удалось найти выбранную комнату.");
|
||
}
|
||
}
|
||
|
||
function addMember() {
|
||
const login = document.getElementById('newMemberLogin').value;
|
||
if (login) {
|
||
alert(`Участник с никнеймом '${login}' добавлен`);
|
||
closeAdd();
|
||
} else {
|
||
alert('Пожалуйста, введите логин участника');
|
||
}
|
||
}
|
||
|
||
function openCreateRoomModal() {
|
||
document.getElementById('createRoomModal').style.display = 'block';
|
||
}
|
||
|
||
function closeCreateRoomModal() {
|
||
document.getElementById('createRoomModal').style.display = 'none';
|
||
}
|
||
|
||
function createRoom() {
|
||
const roomName = document.getElementById('newRoomName').value.trim();
|
||
if (roomName === '') {
|
||
alert('Пожалуйста, заполните все поля.');
|
||
return;
|
||
}
|
||
if (rooms[roomName]) {
|
||
alert('Комната с таким названием уже существует.');
|
||
return;
|
||
}
|
||
rooms[roomName] = true;
|
||
addRoomToList(roomName);
|
||
closeCreateRoomModal();
|
||
}
|
||
|
||
function addRoomToList(roomName) {
|
||
const roomList = document.querySelector('.room-list');
|
||
const existingRoomItem = Array.from(roomList.children).find(item => item.querySelector('.room-name').textContent === roomName);
|
||
if (existingRoomItem) {
|
||
existingRoomItem.remove();
|
||
}
|
||
|
||
const roomItem = document.createElement('li');
|
||
roomItem.classList.add('room-item');
|
||
|
||
roomItem.innerHTML = `
|
||
<span class="room-name">${roomName}</span>
|
||
<button class="delete-chat-button" onclick="openConfirm('${roomName}')">Удалить чат</button>
|
||
<button class="add-members-button" onclick="openAdd()">Добавить участников</button>
|
||
<button class="join-button" onclick="openRoom('${roomName}')">Войти</button>
|
||
`;
|
||
|
||
roomList.appendChild(roomItem);
|
||
}
|
||
|
||
function removeRoomFromList(roomName) {
|
||
const roomList = document.querySelector('.room-list');
|
||
const roomItem = Array.from(roomList.children).find(item => item.querySelector('.room-name').textContent === roomName);
|
||
if (roomItem) {
|
||
roomList.removeChild(roomItem);
|
||
}
|
||
}
|
||
|
||
function initializeRoomList() {
|
||
Object.keys(rooms).forEach(roomName => {
|
||
addRoomToList(roomName);
|
||
});
|
||
}
|
||
|
||
initializeRoomList();
|
||
|
||
window.onclick = function(event) {
|
||
if (event.target === document.getElementById('createRoomModal')) {
|
||
closeCreateRoomModal();
|
||
}
|
||
}
|
||
|
||
document.getElementById('newRoomName').addEventListener('keydown', function(event) {
|
||
if (event.key === 'Enter') {
|
||
createRoom();
|
||
}
|
||
});
|