feedless/app/public/js/index.js

123 lines
3.8 KiB
JavaScript
Raw Normal View History

2020-04-08 20:04:11 +02:00
let escCallback = () => {};
document.onkeydown = (e) => {
const isEsc = e.key === "Escape" || e.key === "Esc";
if (isEsc) escCallback();
};
2020-04-19 12:50:16 +02:00
const openModalFor = (elem, onConfirm, afterClose = null) => {
const overlay = elem.parentElement.querySelector(".overlay");
const modal = elem.parentElement.querySelector(".modal");
const confirmButton = elem.parentElement.querySelector(".modal-confirm");
overlay.style.display = "block";
modal.style.display = "block";
const onClose = () => {
overlay.style.display = "none";
modal.style.display = "none";
if (afterClose) afterClose();
};
overlay.addEventListener("click", onClose);
confirmButton.addEventListener("click", onConfirm);
escCallback = onClose;
return { close: onClose };
};
2020-04-19 12:52:10 +02:00
const composeButton = document.querySelector(".js-compose-secret-message");
2020-04-19 12:50:16 +02:00
const publishButton = document.querySelector(".js-secret-publish");
const sendingMessage = document.querySelector(".js-sending-message");
const messageInput = document.querySelector(".js-secret-message-input");
if (composeButton) {
composeButton.addEventListener("click", () => {
const onPublish = () => {
if (messageInput.value.length == 0) return;
publishButton.style.display = "none";
sendingMessage.innerHTML = "Loading...";
sendingMessage.style.display = "block";
fetch(composeButton.dataset.url, {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: "message=" + encodeURIComponent(messageInput.value),
})
.then(() => {
sendingMessage.innerHTML = "✅ Sent";
setTimeout(() => {
modal.close();
messageInput.value = "";
publishButton.style.display = "block";
sendingMessage.style.display = "none";
}, 1000);
})
.catch(() => {
sendingMessage.innerHTML = "Error";
setTimeout(() => {
publishButton.style.display = "block";
sendingMessage.style.display = "none";
}, 1000);
});
};
const modal = openModalFor(composeButton, onPublish);
});
}
2020-04-19 12:52:10 +02:00
const messages = document.querySelectorAll(".js-secret-message");
2020-04-08 08:44:23 +02:00
messages.forEach((message) => {
message.addEventListener("click", () => {
2020-04-19 12:50:16 +02:00
const afterClose = () => {
const parent = message.parentElement;
2020-04-08 08:44:23 +02:00
parent.parentElement.removeChild(parent);
2020-04-19 12:52:10 +02:00
if (document.querySelectorAll(".js-secret-message").length == 0) {
document.querySelector(".js-secret-messages").style.display = "none";
2020-04-08 08:44:23 +02:00
}
};
2020-04-19 12:50:16 +02:00
const modal = openModalFor(message, () => modal.close(), afterClose);
fetch("/vanish", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: "key=" + encodeURIComponent(message.dataset.key),
});
2020-04-08 08:44:23 +02:00
});
});
2020-04-10 08:53:30 +02:00
const profilePicUpload = document.querySelector(".js-profile-pic-upload");
if (profilePicUpload) {
const placeholder = document.querySelector(".js-profile-pic-placeholder");
const previewImage = () => {
if (profilePicUpload.files && profilePicUpload.files[0]) {
const reader = new FileReader();
reader.onload = (e) => {
placeholder.src = e.target.result;
placeholder.parentElement.style.height = "auto";
};
reader.readAsDataURL(profilePicUpload.files[0]);
}
};
profilePicUpload.addEventListener("change", previewImage);
previewImage();
}
2020-04-13 10:50:30 +02:00
const jsSyncing = document.querySelector(".js-syncing");
if (jsSyncing) {
let checkSyncInterval;
const checkSync = () => {
fetch("/syncing")
.then((result) => result.json())
.then((result) => {
if (!result.syncing) {
clearInterval(checkSyncInterval);
jsSyncing.parentElement.removeChild(jsSyncing);
}
});
};
checkSyncInterval = setInterval(checkSync, 5000);
}