tons of new and fixed js logic

This commit is contained in:
mst 2024-11-26 15:06:50 +03:00
parent 29379ca493
commit f416e5cf9e

View file

@ -42,8 +42,10 @@
shareBtns.forEach((shareBtn) => { shareBtns.forEach((shareBtn) => {
shareBtn.addEventListener("click", async () => { shareBtn.addEventListener("click", async () => {
try { try {
if (navigator.canShare(shareData)) {
await navigator.share(shareData); await navigator.share(shareData);
} }
}
catch (err) { catch (err) {
console.error(err); console.error(err);
} }
@ -52,6 +54,30 @@
}; };
</script> </script>
<script> <script>
const questionModal = document.getElementById('question-modal');
questionModal.addEventListener('show.bs.modal', event => {
// Button that triggered the modal
let button = event.relatedTarget;
// Extract info from data-bs-* attributes
let questionId = button.getAttribute('data-q-id');
let questionContent = document.querySelector(`.question-${questionId}`).innerText;
let answerContent = document.getElementById(`a-${questionId}-content`).innerText;
let submitBtn = document.getElementById('q-modal-submit');
// Define the cfg variables
const trimContentAfter = "{{ cfg.trimContentAfter }}";
const instanceFullBaseUrl = "{{ cfg.instance.fullBaseUrl }}";
let questionText = questionContent.length > trimContentAfter ? questionContent.substring(0, trimContentAfter) + '…' : questionContent;
let answerText = answerContent.length > trimContentAfter ? answerContent.substring(0, trimContentAfter) + '…' : answerContent;
let questionUrl = `${instanceFullBaseUrl}/q/${questionId}/`;
let encodedContent = encodeURI(`${questionText} — ${answerText} ${questionUrl}`);
// Set up the shareOnFediverse function
submitBtn.addEventListener('click', function() {
shareOnFediverse(questionId, encodedContent);
});
});
document.addEventListener('DOMContentLoaded', function () { document.addEventListener('DOMContentLoaded', function () {
const collapseElements = document.querySelectorAll('.collapse.question-cw'); const collapseElements = document.querySelectorAll('.collapse.question-cw');
const toggleButtons = document.querySelectorAll('.cw-btn'); const toggleButtons = document.querySelectorAll('.cw-btn');
@ -90,7 +116,7 @@
className: `alert alert-success shadow alert-dismissible`, className: `alert alert-success shadow alert-dismissible`,
close: true close: true
}).showToast(); }).showToast();
} };
function copyFull(text) { function copyFull(text) {
navigator.clipboard.writeText(text); navigator.clipboard.writeText(text);
Toastify({ Toastify({
@ -102,8 +128,9 @@
className: `alert alert-success shadow alert-dismissible`, className: `alert alert-success shadow alert-dismissible`,
close: true close: true
}).showToast(); }).showToast();
} };
{% if not cfg.lockInbox %}
const input = document.getElementById('question'); const input = document.getElementById('question');
const charCount = document.getElementById('charCount'); const charCount = document.getElementById('charCount');
function updateCharCount() { function updateCharCount() {
@ -124,30 +151,20 @@
} }
} }
input.addEventListener('input', updateCharCount); input.addEventListener('input', updateCharCount);
document.getElementById('question-form').reset();
{% endif %}
function shareOnFediverse(questionId, contentToShare) { function shareOnFediverse(questionId, contentToShare) {
const instanceDomain = document.getElementById(`fediInstance-${questionId}`).value.trim(); const instanceDomain = document.getElementById(`fediInstance`).value.trim();
const shareUrl = `https://${instanceDomain}/share?text=${contentToShare}`; const shareUrl = `https://${instanceDomain}/share?text=${contentToShare}`;
window.open(shareUrl, '_blank'); window.open(shareUrl, '_blank');
} }
</script>
<script>
document.getElementById('question-form').reset();
const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]'); const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]');
const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl)); const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl));
document.addEventListener('htmx:beforeRequest', function(event) {
if (event.detail.target.id != "question-count") {
document.getElementById('ask-btn').setAttribute('disabled', true);
}
});
document.addEventListener('htmx:afterRequest', function(event) { document.addEventListener('htmx:afterRequest', function(event) {
if (event.detail.target.id != "question-count") {
document.getElementById('ask-btn').removeAttribute('disabled');
}
const jsonResponse = event.detail.xhr.response; const jsonResponse = event.detail.xhr.response;
if (jsonResponse) { if (jsonResponse) {
const parsed = JSON.parse(jsonResponse); const parsed = JSON.parse(jsonResponse);