45 lines
1.5 KiB
HTML
45 lines
1.5 KiB
HTML
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
|
|
<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
|
|
<script>
|
|
function initFlatpickr(context=document) {
|
|
try {
|
|
context.querySelectorAll('.datepicker').forEach(function(el) {
|
|
// prevent double-init
|
|
if (el._flatpickr) return;
|
|
flatpickr(el, {dateFormat: 'Y-m-d'});
|
|
});
|
|
} catch (e) {
|
|
console.error('flatpickr init error', e);
|
|
}
|
|
}
|
|
|
|
document.addEventListener('DOMContentLoaded', function () { initFlatpickr(); });
|
|
|
|
// HTMX: re-run init after swaps so dynamically loaded forms get datepickers
|
|
// Use `document` so this script can be included before `<body>` exists.
|
|
document.addEventListener('htmx:afterSwap', function(evt) {
|
|
// evt.detail ? htmx fires on the document; evt.target is the swapped element
|
|
initFlatpickr(evt.target || document);
|
|
});
|
|
|
|
// Ensure HTMX includes Django CSRF token on non-GET requests by reading cookie
|
|
// This helps POSTs from modal forms succeed under Django's CSRF protection.
|
|
function getCookie(name) {
|
|
const value = `; ${document.cookie}`;
|
|
const parts = value.split(`; ${name}=`);
|
|
if (parts.length === 2) return parts.pop().split(';').shift();
|
|
return null;
|
|
}
|
|
|
|
document.addEventListener('htmx:configRequest', function(evt){
|
|
try {
|
|
const csrftoken = getCookie('csrftoken');
|
|
if(csrftoken){
|
|
evt.detail.headers['X-CSRFToken'] = csrftoken;
|
|
}
|
|
} catch(e) {
|
|
console.error('htmx csrf attach error', e);
|
|
}
|
|
});
|
|
</script>
|