2022-12-19 15:11:27 +01:00
|
|
|
import { ApplicationController } from './application_controller';
|
|
|
|
import { hide, show } from '@utils';
|
|
|
|
|
|
|
|
export class SupportController extends ApplicationController {
|
|
|
|
static targets = ['inputRadio', 'content'];
|
|
|
|
|
|
|
|
declare readonly inputRadioTargets: HTMLInputElement[];
|
|
|
|
declare readonly contentTargets: HTMLElement[];
|
|
|
|
|
|
|
|
connect() {
|
|
|
|
this.inputRadioTargets.forEach((inputRadio) => {
|
2023-01-12 18:51:32 +01:00
|
|
|
this.on(inputRadio, 'change', this.onChange.bind(this));
|
|
|
|
this.on(inputRadio, 'keydown', this.onChange.bind(this));
|
2022-12-19 15:11:27 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-12 18:51:32 +01:00
|
|
|
private onChange(event: Event) {
|
2022-12-19 15:11:27 +01:00
|
|
|
const target = event.target as HTMLInputElement;
|
|
|
|
const content = this.getContentForTarget(target);
|
|
|
|
|
|
|
|
this.contentTargets.forEach((content) => {
|
|
|
|
hide(content);
|
|
|
|
content.setAttribute('aria-hidden', 'true');
|
|
|
|
});
|
|
|
|
|
|
|
|
if (target.checked && content) {
|
|
|
|
show(content);
|
|
|
|
content.setAttribute('aria-hidden', 'false');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 18:51:32 +01:00
|
|
|
private getLabelForTarget(target: HTMLInputElement) {
|
2022-12-19 15:11:27 +01:00
|
|
|
const labelSelector = `label[for="${target.id}"]`;
|
|
|
|
return document.querySelector(labelSelector);
|
|
|
|
}
|
|
|
|
|
2023-01-12 18:51:32 +01:00
|
|
|
private getContentForTarget(target: HTMLInputElement) {
|
2022-12-19 15:11:27 +01:00
|
|
|
const label = this.getLabelForTarget(target);
|
|
|
|
if (!label) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const contentSelector = label.getAttribute('aria-controls');
|
|
|
|
|
|
|
|
if (contentSelector) {
|
|
|
|
return document.getElementById(contentSelector);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|