Files
santeh-ray/static/js/header-category-dropdown.js
Azimkin 29d8302a4f
All checks were successful
Build and Push Docker Image / build (push) Successful in 51s
feat: add favorites feature for users
- Implemented `Favorite` model with unique user-item constraint and timestamped records
- Added `favorites:index` and `favorites:toggle` views with AJAX support for toggling favorites
- Built templates and front-end assets for the favorites page and integration with item grids
- Extended header with favorites link and dynamic counter
- Added tests covering models, views, and templates for the favorites functionality
- Updated admin to enable management of favorites via Django admin interface
2026-05-10 20:06:12 +02:00

57 lines
1.9 KiB
JavaScript

document.addEventListener('DOMContentLoaded', function () {
document.querySelectorAll('[data-category-dropdown]').forEach(function (dropdown) {
const trigger = dropdown.querySelector('[data-category-trigger]');
const label = dropdown.querySelector('[data-selected-label]');
const valueInput = dropdown.querySelector('[data-category-value]');
const menu = dropdown.querySelector('[data-category-menu]');
const options = dropdown.querySelectorAll('[data-category-option]');
if (!trigger || !label || !valueInput || !menu) {
return;
}
const setExpanded = function (expanded) {
trigger.setAttribute('aria-expanded', expanded ? 'true' : 'false');
dropdown.classList.toggle('is-open', expanded);
};
const closeDropdown = function () {
setExpanded(false);
};
const openDropdown = function () {
setExpanded(true);
};
trigger.addEventListener('click', function (event) {
event.preventDefault();
if (dropdown.classList.contains('is-open')) {
closeDropdown();
return;
}
openDropdown();
});
options.forEach(function (option) {
option.addEventListener('click', function () {
label.textContent = option.dataset.label || option.textContent.trim();
valueInput.value = option.dataset.value || '';
closeDropdown();
});
});
document.addEventListener('click', function (event) {
if (!dropdown.contains(event.target)) {
closeDropdown();
}
});
document.addEventListener('keydown', function (event) {
if (event.key === 'Escape') {
closeDropdown();
}
});
});
});