2017-08-06 16:08:03 -04:00
|
|
|
|
|
|
|
class ExpandToggle {
|
|
|
|
|
|
|
|
constructor(elem) {
|
|
|
|
this.elem = elem;
|
2019-04-14 08:01:51 -04:00
|
|
|
|
|
|
|
// Component state
|
|
|
|
this.isOpen = elem.getAttribute('expand-toggle-is-open') === 'yes';
|
|
|
|
this.updateEndpoint = elem.getAttribute('expand-toggle-update-endpoint');
|
2017-08-06 16:08:03 -04:00
|
|
|
this.selector = elem.getAttribute('expand-toggle');
|
2019-04-14 08:01:51 -04:00
|
|
|
|
|
|
|
// Listener setup
|
2017-08-06 16:08:03 -04:00
|
|
|
elem.addEventListener('click', this.click.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
open(elemToToggle) {
|
|
|
|
elemToToggle.style.display = 'block';
|
|
|
|
elemToToggle.style.height = '';
|
|
|
|
let height = elemToToggle.getBoundingClientRect().height;
|
|
|
|
elemToToggle.style.height = '0px';
|
|
|
|
elemToToggle.style.overflow = 'hidden';
|
|
|
|
elemToToggle.style.transition = 'height ease-in-out 240ms';
|
|
|
|
|
|
|
|
let transitionEndBound = onTransitionEnd.bind(this);
|
|
|
|
function onTransitionEnd() {
|
|
|
|
elemToToggle.style.overflow = '';
|
|
|
|
elemToToggle.style.height = '';
|
|
|
|
elemToToggle.style.transition = '';
|
|
|
|
elemToToggle.removeEventListener('transitionend', transitionEndBound);
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
elemToToggle.style.height = `${height}px`;
|
|
|
|
elemToToggle.addEventListener('transitionend', transitionEndBound)
|
|
|
|
}, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(elemToToggle) {
|
|
|
|
elemToToggle.style.display = 'block';
|
|
|
|
elemToToggle.style.height = elemToToggle.getBoundingClientRect().height + 'px';
|
|
|
|
elemToToggle.style.overflow = 'hidden';
|
|
|
|
elemToToggle.style.transition = 'all ease-in-out 240ms';
|
|
|
|
|
|
|
|
let transitionEndBound = onTransitionEnd.bind(this);
|
|
|
|
function onTransitionEnd() {
|
|
|
|
elemToToggle.style.overflow = '';
|
|
|
|
elemToToggle.style.height = '';
|
|
|
|
elemToToggle.style.transition = '';
|
|
|
|
elemToToggle.style.display = 'none';
|
|
|
|
elemToToggle.removeEventListener('transitionend', transitionEndBound);
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
elemToToggle.style.height = `0px`;
|
|
|
|
elemToToggle.addEventListener('transitionend', transitionEndBound)
|
|
|
|
}, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
click(event) {
|
|
|
|
event.preventDefault();
|
2019-04-14 08:01:51 -04:00
|
|
|
|
|
|
|
const matchingElems = document.querySelectorAll(this.selector);
|
|
|
|
for (let match of matchingElems) {
|
|
|
|
this.isOpen ? this.close(match) : this.open(match);
|
2017-08-06 16:08:03 -04:00
|
|
|
}
|
2019-04-14 08:01:51 -04:00
|
|
|
|
2017-08-06 16:08:03 -04:00
|
|
|
this.isOpen = !this.isOpen;
|
2019-04-14 08:01:51 -04:00
|
|
|
this.updateSystemAjax(this.isOpen);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateSystemAjax(isOpen) {
|
|
|
|
window.$http.patch(this.updateEndpoint, {
|
|
|
|
expand: isOpen ? 'true' : 'false'
|
|
|
|
});
|
2017-08-06 16:08:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-09 16:17:35 -05:00
|
|
|
export default ExpandToggle;
|