2017-08-06 22:08:03 +02:00
|
|
|
|
|
|
|
class ChapterToggle {
|
|
|
|
|
|
|
|
constructor(elem) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.isOpen = elem.classList.contains('open');
|
|
|
|
elem.addEventListener('click', this.click.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
open() {
|
2018-09-22 17:36:35 +02:00
|
|
|
const list = this.elem.parentNode.querySelector('.inset-list');
|
2017-08-06 22:08:03 +02:00
|
|
|
|
|
|
|
this.elem.classList.add('open');
|
|
|
|
list.style.display = 'block';
|
2018-09-22 17:36:35 +02:00
|
|
|
list.style.maxHeight = '';
|
|
|
|
const maxHeight = list.getBoundingClientRect().height + 10;
|
|
|
|
list.style.maxHeight = '0px';
|
2017-08-06 22:08:03 +02:00
|
|
|
list.style.overflow = 'hidden';
|
2018-09-22 17:36:35 +02:00
|
|
|
list.style.transition = 'max-height ease-in-out 240ms';
|
2017-08-06 22:08:03 +02:00
|
|
|
|
|
|
|
let transitionEndBound = onTransitionEnd.bind(this);
|
|
|
|
function onTransitionEnd() {
|
|
|
|
list.style.overflow = '';
|
2018-09-22 17:36:35 +02:00
|
|
|
list.style.maxHeight = '';
|
2017-08-06 22:08:03 +02:00
|
|
|
list.style.transition = '';
|
2018-09-16 09:39:21 +02:00
|
|
|
list.style.display = `block`;
|
2017-08-06 22:08:03 +02:00
|
|
|
list.removeEventListener('transitionend', transitionEndBound);
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(() => {
|
2018-09-22 17:36:35 +02:00
|
|
|
requestAnimationFrame(() => {
|
|
|
|
list.style.maxHeight = `${maxHeight}px`;
|
|
|
|
list.addEventListener('transitionend', transitionEndBound)
|
|
|
|
});
|
2017-08-06 22:08:03 +02:00
|
|
|
}, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
2018-09-22 17:36:35 +02:00
|
|
|
const list = this.elem.parentNode.querySelector('.inset-list');
|
2017-08-06 22:08:03 +02:00
|
|
|
|
|
|
|
list.style.display = 'block';
|
2018-09-22 17:36:35 +02:00
|
|
|
this.elem.classList.remove('open');
|
|
|
|
list.style.maxHeight = list.getBoundingClientRect().height + 'px';
|
2017-08-06 22:08:03 +02:00
|
|
|
list.style.overflow = 'hidden';
|
2018-09-22 17:36:35 +02:00
|
|
|
list.style.transition = 'max-height ease-in-out 240ms';
|
2017-08-06 22:08:03 +02:00
|
|
|
|
2018-09-22 17:36:35 +02:00
|
|
|
const transitionEndBound = onTransitionEnd.bind(this);
|
2017-08-06 22:08:03 +02:00
|
|
|
function onTransitionEnd() {
|
|
|
|
list.style.overflow = '';
|
2018-09-22 17:36:35 +02:00
|
|
|
list.style.maxHeight = '';
|
2017-08-06 22:08:03 +02:00
|
|
|
list.style.transition = '';
|
|
|
|
list.style.display = 'none';
|
|
|
|
list.removeEventListener('transitionend', transitionEndBound);
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(() => {
|
2018-09-22 17:36:35 +02:00
|
|
|
requestAnimationFrame(() => {
|
|
|
|
list.style.maxHeight = `0px`;
|
|
|
|
list.addEventListener('transitionend', transitionEndBound)
|
|
|
|
});
|
2017-08-06 22:08:03 +02:00
|
|
|
}, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
click(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
this.isOpen ? this.close() : this.open();
|
|
|
|
this.isOpen = !this.isOpen;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-16 09:39:21 +02:00
|
|
|
module.exports = ChapterToggle;
|