Files
symfony-k3s/assets/app.js
T

94 lines
3.4 KiB
JavaScript

import './styles/app.scss';
import './styles/brain-dump.scss';
import htmx from 'htmx.org';
window.htmx = htmx;
document.body.addEventListener('htmx:beforeRequest', function () {
const error = document.getElementById('request-failed');
if (error) error.hidden = true;
});
document.body.addEventListener('htmx:responseError', function () {
const error = document.getElementById('request-failed');
if (error) error.hidden = false;
});
document.body.addEventListener('htmx:sendError', function () {
const error = document.getElementById('request-failed');
if (error) error.hidden = false;
});
function activateComposerTab(tab) {
const composer = tab.closest('.task-composer');
if (!composer) return;
composer.querySelectorAll('[role="tab"]').forEach((candidate) => {
const selected = candidate === tab;
candidate.setAttribute('aria-selected', String(selected));
candidate.tabIndex = selected ? 0 : -1;
const panel = document.getElementById(candidate.getAttribute('aria-controls'));
if (panel) panel.hidden = !selected;
});
}
document.addEventListener('click', (event) => {
const tab = event.target.closest('.task-composer [role="tab"]');
if (tab) activateComposerTab(tab);
const closeButton = event.target.closest('[data-close-brain-dump-dialog]');
if (closeButton) closeButton.closest('dialog')?.close();
});
document.addEventListener('keydown', (event) => {
const tab = event.target.closest?.('.task-composer [role="tab"]');
if (!tab || !['ArrowLeft', 'ArrowRight', 'Home', 'End'].includes(event.key)) return;
const tabs = [...tab.closest('[role="tablist"]').querySelectorAll('[role="tab"]')];
const currentIndex = tabs.indexOf(tab);
const nextIndex = event.key === 'Home' ? 0
: event.key === 'End' ? tabs.length - 1
: (currentIndex + (event.key === 'ArrowRight' ? 1 : -1) + tabs.length) % tabs.length;
event.preventDefault();
tabs[nextIndex].focus();
activateComposerTab(tabs[nextIndex]);
});
document.body.addEventListener('htmx:afterSwap', (event) => {
const dialog = event.detail.target?.querySelector('.brain-dump-proposal-dialog');
if (!dialog) return;
dialog.addEventListener('close', () => dialog.remove(), {once: true});
if (typeof dialog.showModal === 'function' && !dialog.open) dialog.showModal();
});
document.addEventListener('click', (event) => {
const button = event.target.closest('[data-weather-location]');
if (!button) return;
const status = document.querySelector('[data-weather-location-status]');
if (!navigator.geolocation) {
if (status) status.textContent = 'Your browser does not support location services.';
return;
}
navigator.geolocation.getCurrentPosition(async ({coords}) => {
const payload = new URLSearchParams({
_token: button.dataset.locationToken,
latitude: String(coords.latitude),
longitude: String(coords.longitude),
});
const response = await fetch(button.dataset.locationUrl, {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: payload,
});
if (!response.ok) {
if (status) status.textContent = 'Location could not be saved.';
return;
}
window.location.reload();
}, () => {
if (status) status.textContent = 'Location permission was not granted.';
});
});