demarches-normaliennes/app/javascript/new_design/support.js

111 lines
2.6 KiB
JavaScript
Raw Normal View History

2020-09-17 09:06:57 +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
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() {
this.radioInput.checked = true;
2020-09-22 12:14:02 +02:00
if (this.controlledNode) {
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() {
this.radioInput.checked = false;
2020-09-22 12:14:02 +02:00
if (this.controlledNode) {
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() {
if (
this.controlledNode &&
this.controlledNode.getAttribute('aria-hidden') === 'true'
) {
2020-09-22 12:14:02 +02:00
this.showContent();
} else {
this.hideContent();
2020-09-22 12:14:02 +02:00
}
}
2020-09-22 12:14:02 +02:00
setAllButtons(buttons) {
this.allButtons = buttons;
}
handleKeydown(event) {
2020-09-22 12:14:02 +02:00
switch (event.keyCode) {
case this.keyCode.RETURN:
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() {
// 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
this.showContent();
2020-09-22 12:14:02 +02:00
}
}
2020-09-16 21:10:23 +02:00
/* Initialize Hide/Show Buttons */
if (document.querySelector('#contact-form')) {
window.addEventListener(
'DOMContentLoaded',
function () {
var buttons = document.querySelectorAll('fieldset[name=type] label');
var expandButtons = [];
buttons.forEach((button) => {
var be = new ButtonExpand(button);
expandButtons.push(be);
});
expandButtons.forEach((button) => button.setAllButtons(expandButtons));
},
false
);
}