2020-09-17 09:06:57 +02:00
|
|
|
//
|
2022-08-30 12:37:08 +02:00
|
|
|
// This content is inspired by w3c aria example, rewritten for better RGAA compatibility.
|
2020-09-17 09:06:57 +02:00
|
|
|
// https://www.w3.org/TR/wai-aria-practices-1.1/examples/disclosure/disclosure-faq.html
|
|
|
|
//
|
2019-02-21 17:50:18 +01:00
|
|
|
|
2020-09-22 12:14:02 +02:00
|
|
|
class ButtonExpand {
|
|
|
|
constructor(domNode) {
|
|
|
|
this.domNode = domNode;
|
2020-09-16 21:10:23 +02:00
|
|
|
|
2020-09-22 12:14:02 +02:00
|
|
|
this.keyCode = Object.freeze({
|
|
|
|
RETURN: 13
|
|
|
|
});
|
2020-09-16 21:10:23 +02:00
|
|
|
|
2020-09-22 12:14:02 +02:00
|
|
|
this.allButtons = [];
|
|
|
|
this.controlledNode = false;
|
2020-09-16 21:10:23 +02:00
|
|
|
|
2020-09-22 12:14:02 +02:00
|
|
|
var id = this.domNode.getAttribute('aria-controls');
|
2020-09-16 21:10:23 +02:00
|
|
|
|
2020-09-22 12:14:02 +02:00
|
|
|
if (id) {
|
|
|
|
this.controlledNode = document.getElementById(id);
|
|
|
|
}
|
2020-09-16 21:10:23 +02:00
|
|
|
|
2022-08-30 12:37:08 +02:00
|
|
|
this.radioInput = this.domNode.querySelector('input[type="radio"]');
|
|
|
|
|
2020-09-22 12:14:02 +02:00
|
|
|
this.hideContent();
|
2020-09-16 21:10:23 +02:00
|
|
|
|
2020-09-22 12:14:02 +02:00
|
|
|
this.domNode.addEventListener('keydown', this.handleKeydown.bind(this));
|
|
|
|
this.domNode.addEventListener('click', this.handleClick.bind(this));
|
2020-09-16 21:10:23 +02:00
|
|
|
}
|
|
|
|
|
2020-09-22 12:14:02 +02:00
|
|
|
showContent() {
|
2022-08-30 12:37:08 +02:00
|
|
|
this.radioInput.checked = true;
|
|
|
|
|
2020-09-22 12:14:02 +02:00
|
|
|
if (this.controlledNode) {
|
2022-08-30 12:37:08 +02:00
|
|
|
this.controlledNode.setAttribute('aria-hidden', 'false');
|
2020-09-22 12:14:02 +02:00
|
|
|
this.controlledNode.classList.remove('hidden');
|
2020-09-16 21:10:23 +02:00
|
|
|
}
|
2020-09-22 12:14:02 +02:00
|
|
|
|
|
|
|
this.allButtons.forEach((b) => {
|
|
|
|
if (b != this) {
|
|
|
|
b.hideContent();
|
|
|
|
}
|
|
|
|
});
|
2020-09-16 21:10:23 +02:00
|
|
|
}
|
|
|
|
|
2020-09-22 12:14:02 +02:00
|
|
|
hideContent() {
|
2022-08-30 12:37:08 +02:00
|
|
|
this.radioInput.checked = false;
|
|
|
|
|
2020-09-22 12:14:02 +02:00
|
|
|
if (this.controlledNode) {
|
2022-08-30 12:37:08 +02:00
|
|
|
this.controlledNode.setAttribute('aria-hidden', 'true');
|
2020-09-22 12:14:02 +02:00
|
|
|
this.controlledNode.classList.add('hidden');
|
|
|
|
}
|
2020-09-16 21:10:23 +02:00
|
|
|
}
|
|
|
|
|
2020-09-22 12:14:02 +02:00
|
|
|
toggleExpand() {
|
2022-08-30 12:37:08 +02:00
|
|
|
if (
|
|
|
|
this.controlledNode &&
|
|
|
|
this.controlledNode.getAttribute('aria-hidden') === 'true'
|
|
|
|
) {
|
2020-09-22 12:14:02 +02:00
|
|
|
this.showContent();
|
2022-08-30 12:37:08 +02:00
|
|
|
} else {
|
|
|
|
this.hideContent();
|
2020-09-22 12:14:02 +02:00
|
|
|
}
|
|
|
|
}
|
2019-03-14 11:41:54 +01:00
|
|
|
|
2020-09-22 12:14:02 +02:00
|
|
|
setAllButtons(buttons) {
|
|
|
|
this.allButtons = buttons;
|
|
|
|
}
|
2020-09-16 21:41:02 +02:00
|
|
|
|
2022-08-30 12:37:08 +02:00
|
|
|
handleKeydown(event) {
|
2020-09-22 12:14:02 +02:00
|
|
|
switch (event.keyCode) {
|
|
|
|
case this.keyCode.RETURN:
|
2022-08-30 12:37:08 +02:00
|
|
|
this.showContent();
|
2020-09-16 21:10:23 +02:00
|
|
|
|
2020-09-22 12:14:02 +02:00
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
break;
|
2020-09-16 21:10:23 +02:00
|
|
|
|
2020-09-22 12:14:02 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-09-16 21:10:23 +02:00
|
|
|
}
|
|
|
|
|
2020-09-22 12:14:02 +02:00
|
|
|
handleClick() {
|
2022-08-30 12:37:08 +02:00
|
|
|
// NOTE: click event is also fired on input and label activations
|
|
|
|
// ie., not necessarily by a mouse click but any user inputs, like keyboard navigation with arrows keys.
|
|
|
|
// Cf https://www.w3.org/TR/2012/WD-html5-20121025/content-models.html#interactive-content
|
2020-09-16 21:10:23 +02:00
|
|
|
|
2022-08-30 12:37:08 +02:00
|
|
|
this.showContent();
|
2020-09-22 12:14:02 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-16 21:10:23 +02:00
|
|
|
|
|
|
|
/* Initialize Hide/Show Buttons */
|
|
|
|
|
2020-09-17 13:23:34 +02:00
|
|
|
if (document.querySelector('#contact-form')) {
|
|
|
|
window.addEventListener(
|
2022-04-21 18:48:34 +02:00
|
|
|
'DOMContentLoaded',
|
2020-09-17 13:23:34 +02:00
|
|
|
function () {
|
2022-08-30 12:37:08 +02:00
|
|
|
var buttons = document.querySelectorAll('fieldset[name=type] label');
|
2020-09-17 13:23:34 +02:00
|
|
|
var expandButtons = [];
|
|
|
|
|
|
|
|
buttons.forEach((button) => {
|
|
|
|
var be = new ButtonExpand(button);
|
|
|
|
expandButtons.push(be);
|
|
|
|
});
|
|
|
|
expandButtons.forEach((button) => button.setAllButtons(expandButtons));
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|