2023-04-18 17:20:02 -04:00
|
|
|
import {slideDown, slideUp} from '../services/animations';
|
|
|
|
import {Component} from './component';
|
2019-06-07 10:51:01 -04:00
|
|
|
|
2017-12-10 08:46:50 -05:00
|
|
|
/**
|
|
|
|
* Collapsible
|
|
|
|
* Provides some simple logic to allow collapsible sections.
|
|
|
|
*/
|
2022-11-15 11:04:46 -05:00
|
|
|
export class Collapsible extends Component {
|
2017-12-10 08:46:50 -05:00
|
|
|
|
2022-11-15 11:04:46 -05:00
|
|
|
setup() {
|
|
|
|
this.container = this.$el;
|
|
|
|
this.trigger = this.$refs.trigger;
|
|
|
|
this.content = this.$refs.content;
|
2017-12-10 08:46:50 -05:00
|
|
|
|
2022-11-15 11:04:46 -05:00
|
|
|
if (this.trigger) {
|
|
|
|
this.trigger.addEventListener('click', this.toggle.bind(this));
|
|
|
|
this.openIfContainsError();
|
|
|
|
}
|
2017-12-10 08:46:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
open() {
|
2022-11-15 11:04:46 -05:00
|
|
|
this.container.classList.add('open');
|
2019-08-25 10:44:51 -04:00
|
|
|
this.trigger.setAttribute('aria-expanded', 'true');
|
2019-06-07 10:51:01 -04:00
|
|
|
slideDown(this.content, 300);
|
2017-12-10 08:46:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
2022-11-15 11:04:46 -05:00
|
|
|
this.container.classList.remove('open');
|
2019-08-25 10:44:51 -04:00
|
|
|
this.trigger.setAttribute('aria-expanded', 'false');
|
2019-06-07 10:51:01 -04:00
|
|
|
slideUp(this.content, 300);
|
2017-12-10 08:46:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
toggle() {
|
2022-11-15 11:04:46 -05:00
|
|
|
if (this.container.classList.contains('open')) {
|
2017-12-10 08:46:50 -05:00
|
|
|
this.close();
|
|
|
|
} else {
|
|
|
|
this.open();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-16 08:27:17 -05:00
|
|
|
openIfContainsError() {
|
2020-06-29 17:11:03 -04:00
|
|
|
const error = this.content.querySelector('.text-neg.text-small');
|
2019-12-16 08:27:17 -05:00
|
|
|
if (error) {
|
|
|
|
this.open();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-18 17:20:02 -04:00
|
|
|
}
|