2019-06-07 22:51:01 +08:00
|
|
|
import {slideDown, slideUp} from "../services/animations";
|
|
|
|
|
2017-12-10 21:46:50 +08:00
|
|
|
/**
|
|
|
|
* Collapsible
|
|
|
|
* Provides some simple logic to allow collapsible sections.
|
|
|
|
*/
|
|
|
|
class Collapsible {
|
|
|
|
|
|
|
|
constructor(elem) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.trigger = elem.querySelector('[collapsible-trigger]');
|
|
|
|
this.content = elem.querySelector('[collapsible-content]');
|
|
|
|
|
|
|
|
if (!this.trigger) return;
|
|
|
|
this.trigger.addEventListener('click', this.toggle.bind(this));
|
2019-12-16 21:27:17 +08:00
|
|
|
this.openIfContainsError();
|
2017-12-10 21:46:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
open() {
|
|
|
|
this.elem.classList.add('open');
|
2019-08-25 22:44:51 +08:00
|
|
|
this.trigger.setAttribute('aria-expanded', 'true');
|
2019-06-07 22:51:01 +08:00
|
|
|
slideDown(this.content, 300);
|
2017-12-10 21:46:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
|
|
|
this.elem.classList.remove('open');
|
2019-08-25 22:44:51 +08:00
|
|
|
this.trigger.setAttribute('aria-expanded', 'false');
|
2019-06-07 22:51:01 +08:00
|
|
|
slideUp(this.content, 300);
|
2017-12-10 21:46:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
toggle() {
|
|
|
|
if (this.elem.classList.contains('open')) {
|
|
|
|
this.close();
|
|
|
|
} else {
|
|
|
|
this.open();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-16 21:27:17 +08:00
|
|
|
openIfContainsError() {
|
2020-06-30 05:11:03 +08:00
|
|
|
const error = this.content.querySelector('.text-neg.text-small');
|
2019-12-16 21:27:17 +08:00
|
|
|
if (error) {
|
|
|
|
this.open();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-10 21:46:50 +08:00
|
|
|
}
|
|
|
|
|
2018-11-10 05:17:35 +08:00
|
|
|
export default Collapsible;
|