2026-01-15 16:59:56 +01:00
|
|
|
/**
|
|
|
|
|
* Controls Module
|
|
|
|
|
*
|
|
|
|
|
* Handles generic UI controls functionality:
|
|
|
|
|
* - Toggle sliders (Ja/Nej switches)
|
2026-01-16 23:25:05 +01:00
|
|
|
* - Select dropdowns
|
2026-01-15 16:59:56 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Controller for generic UI controls
|
|
|
|
|
*/
|
|
|
|
|
export class ControlsController {
|
|
|
|
|
constructor() {
|
|
|
|
|
this.initToggleSliders();
|
2026-01-16 22:03:22 +01:00
|
|
|
this.initSelectDropdowns();
|
2026-01-15 16:59:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize all toggle sliders on the page
|
|
|
|
|
* Toggle slider: Ja/Nej button switch with data-value attribute
|
|
|
|
|
* Clicking anywhere on the slider toggles the value
|
|
|
|
|
*/
|
|
|
|
|
private initToggleSliders(): void {
|
|
|
|
|
document.querySelectorAll('swp-toggle-slider').forEach(slider => {
|
|
|
|
|
slider.addEventListener('click', () => {
|
|
|
|
|
const el = slider as HTMLElement;
|
|
|
|
|
const newValue = el.dataset.value === 'yes' ? 'no' : 'yes';
|
|
|
|
|
el.dataset.value = newValue;
|
|
|
|
|
|
|
|
|
|
// Dispatch custom event for listeners
|
|
|
|
|
slider.dispatchEvent(new CustomEvent('toggle', {
|
|
|
|
|
bubbles: true,
|
|
|
|
|
detail: { value: newValue }
|
|
|
|
|
}));
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-01-16 22:03:22 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize all select dropdowns on the page
|
|
|
|
|
*/
|
|
|
|
|
private initSelectDropdowns(): void {
|
|
|
|
|
document.querySelectorAll('swp-select').forEach(select => {
|
|
|
|
|
const trigger = select.querySelector('button');
|
2026-01-16 23:25:05 +01:00
|
|
|
const dropdown = select.querySelector('swp-select-dropdown');
|
2026-01-16 22:03:22 +01:00
|
|
|
const options = select.querySelectorAll('swp-select-option');
|
|
|
|
|
|
2026-01-16 23:25:05 +01:00
|
|
|
if (!trigger || !dropdown) return;
|
2026-01-16 22:03:22 +01:00
|
|
|
|
2026-01-16 23:25:05 +01:00
|
|
|
// Toggle dropdown on button click
|
|
|
|
|
trigger.addEventListener('click', (e) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
const isOpen = select.classList.toggle('open');
|
|
|
|
|
trigger.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
|
2026-01-16 22:03:22 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Handle option selection
|
|
|
|
|
options.forEach(option => {
|
|
|
|
|
option.addEventListener('click', () => {
|
|
|
|
|
const value = (option as HTMLElement).dataset.value;
|
|
|
|
|
const label = option.textContent?.trim() || '';
|
|
|
|
|
|
|
|
|
|
// Update selected state
|
|
|
|
|
options.forEach(o => o.classList.remove('selected'));
|
|
|
|
|
option.classList.add('selected');
|
|
|
|
|
|
|
|
|
|
// Update trigger display
|
|
|
|
|
const valueEl = trigger.querySelector('swp-select-value');
|
|
|
|
|
if (valueEl) {
|
|
|
|
|
valueEl.textContent = label;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 23:25:05 +01:00
|
|
|
// Update color dot if present (for color pickers)
|
|
|
|
|
const triggerDot = trigger.querySelector('swp-color-dot');
|
|
|
|
|
if (triggerDot && value) {
|
|
|
|
|
triggerDot.className = `is-${value}`;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 22:03:22 +01:00
|
|
|
// Update data-value on select element
|
|
|
|
|
(select as HTMLElement).dataset.value = value;
|
|
|
|
|
|
2026-01-16 23:25:05 +01:00
|
|
|
// Close dropdown
|
|
|
|
|
select.classList.remove('open');
|
|
|
|
|
trigger.setAttribute('aria-expanded', 'false');
|
2026-01-16 22:03:22 +01:00
|
|
|
|
|
|
|
|
// Dispatch custom event
|
|
|
|
|
select.dispatchEvent(new CustomEvent('change', {
|
|
|
|
|
bubbles: true,
|
|
|
|
|
detail: { value, label }
|
|
|
|
|
}));
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-01-16 23:25:05 +01:00
|
|
|
|
|
|
|
|
// Click outside to close any open dropdown
|
|
|
|
|
document.addEventListener('click', () => {
|
|
|
|
|
this.closeAllSelects();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Keyboard navigation
|
|
|
|
|
document.addEventListener('keydown', (e) => {
|
|
|
|
|
const openSelect = document.querySelector('swp-select.open');
|
|
|
|
|
if (!openSelect) return;
|
|
|
|
|
|
|
|
|
|
// Escape to close
|
|
|
|
|
if (e.key === 'Escape') {
|
|
|
|
|
this.closeAllSelects();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Letter key to jump to option
|
|
|
|
|
if (e.key.length === 1 && /[a-zA-ZæøåÆØÅ]/.test(e.key)) {
|
|
|
|
|
const options = openSelect.querySelectorAll('swp-select-option');
|
|
|
|
|
const letter = e.key.toLowerCase();
|
|
|
|
|
|
|
|
|
|
for (const option of options) {
|
|
|
|
|
const text = option.textContent?.trim().toLowerCase() || '';
|
|
|
|
|
if (text.startsWith(letter)) {
|
|
|
|
|
// Scroll into view and highlight
|
|
|
|
|
option.scrollIntoView({ block: 'nearest' });
|
|
|
|
|
options.forEach(o => o.classList.remove('highlighted'));
|
|
|
|
|
option.classList.add('highlighted');
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Enter to select highlighted option
|
|
|
|
|
if (e.key === 'Enter') {
|
|
|
|
|
const highlighted = openSelect.querySelector('swp-select-option.highlighted') as HTMLElement;
|
|
|
|
|
if (highlighted) {
|
|
|
|
|
highlighted.click();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private closeAllSelects(): void {
|
|
|
|
|
document.querySelectorAll('swp-select.open').forEach(select => {
|
|
|
|
|
select.classList.remove('open');
|
|
|
|
|
select.querySelector('button')?.setAttribute('aria-expanded', 'false');
|
|
|
|
|
});
|
2026-01-16 22:03:22 +01:00
|
|
|
}
|
2026-01-15 16:59:56 +01:00
|
|
|
}
|