document.addEventListener("DOMContentLoaded", () => {
const yes = document.createElement("button");
const showMessage = () => {
const messageWrapper = document.createElement("div");
const message = document.createElement("p");
message.innerText =
"Чтобы очистить список, нажмите правую кнопку мыши";
messageWrapper.classList.add("messageWrapper");
setTimeout(() => {
document.body.appendChild(messageWrapper);
messageWrapper.appendChild(message);
messageWrapper.classList.add("messageWrapperUnvis");
setTimeout(() => {
messageWrapper.classList.remove("messageWrapperUnvis");
}, 100);
}, 100);
setTimeout(() => {
messageWrapper.classList.add("messageWrapperUnvis");
setTimeout(() => {
messageWrapper.remove();
}, 1000);
}, 2500);
};
const modalWindow = () => {
document.addEventListener("contextmenu", (e) => {
const wrapper = document.createElement("div");
const buttonsWrapper = document.createElement("div");
const question = document.createElement("p");
const no = document.createElement("button");
let currentDiv = null;
e.preventDefault();
if (e.target.matches("#input")) {
wrapper.remove();
return;
}
document.addEventListener("contextmenu", () => {
if (currentDiv) {
currentDiv.remove();
}
});
document.addEventListener("click", (e) => {
if (currentDiv && !currentDiv.contains(e.target)) {
currentDiv.remove();
}
});
question.innerText = "Очистить список?";
yes.innerText = "Да";
no.innerText = "Нет";
wrapper.classList.add("wrapper");
buttonsWrapper.classList.add("buttonsWrapper");
question.classList.add("question");
yes.classList.add("yes");
no.classList.add("no");
yes.addEventListener("mouseenter", () => {
yes.classList.toggle("buttonHover");
});
yes.addEventListener("mouseleave", () => {
yes.classList.remove("buttonHover");
});
no.addEventListener("mouseenter", () => {
no.classList.add("buttonHover");
});
no.addEventListener("mouseleave", () => {
no.classList.remove("buttonHover");
});
currentDiv = wrapper;
wrapper.appendChild(question);
wrapper.appendChild(buttonsWrapper);
buttonsWrapper.appendChild(yes);
buttonsWrapper.appendChild(no);
document.body.appendChild(wrapper);
wrapper.style.left = e.clientX + 7 + "px";
wrapper.style.top = e.clientY + 15 + "px";
yes.addEventListener("click", (e) => {
if (currentDiv) {
currentDiv.remove();
yes.classList.remove("buttonHover");
}
});
no.addEventListener("click", (e) => {
if (currentDiv) {
currentDiv.remove();
no.classList.remove("buttonHover");
}
});
});
};
const addNewItem = () => {
const input = document.querySelector("#input");
const list = document.querySelector("#list");
input.addEventListener("keypress", (e) => {
const nListItem = document.createElement("li");
const remove = document.createElement("span");
yes.addEventListener("click", () => {
nListItem.classList.add("unvisible");
setTimeout(() => {
list.innerHTML = "";
}, 600);
});
if (e.key === "Enter") {
if (input.value !== "") {
nListItem.innerText = input.value;
remove.innerHTML = '<img src="trash1.png" alt="">';
remove.classList.add("unvisible");
nListItem.appendChild(remove);
list.appendChild(nListItem);
input.value = "";
}
}
remove.addEventListener("click", (e) => {
nListItem.classList.add("remove");
e.stopImmediatePropagation();
setTimeout(() => {
nListItem.remove();
}, 1000);
});
nListItem.addEventListener("mouseenter", () => {
remove.classList.add("visible");
remove.classList.remove("unvisible");
});
nListItem.addEventListener("mouseleave", () => {
remove.classList.remove("visible");
remove.classList.add("unvisible");
});
let isClicked = false;
nListItem.addEventListener("click", () => {
if (!isClicked) {
list.insertBefore(nListItem, list.firstChild);
nListItem.classList.add("liChecked");
isClicked = true;
} else {
nListItem.classList.remove("liChecked");
isClicked = false;
}
});
list.addEventListener("click", (e) => {
if (e.target.tagName === "LI") {
setTimeout(() => {
list.appendChild(e.target);
}, 500);
}
});
});
};
const setup = () => {
setTimeout(() => {
showMessage();
}, 1000);
modalWindow();
addNewItem();
};
setup();
});
messageWrapper.classList.add("messageWrapperUnvis");
setTimeout(() => {
messageWrapper.classList.remove("messageWrapperUnvis");
}, 100);