2019-06-07 22:51:01 +08:00
|
|
|
import {slideDown, slideUp} from "../services/animations";
|
2022-11-16 00:04:46 +08:00
|
|
|
import {Component} from "./component";
|
2019-06-07 22:51:01 +08:00
|
|
|
|
2017-12-10 21:46:50 +08:00
|
|
|
/**
|
|
|
|
* Collapsible
|
|
|
|
* Provides some simple logic to allow collapsible sections.
|
|
|
|
*/
|
2022-11-16 00:04:46 +08:00
|
|
|
export class Collapsible extends Component {
|
2017-12-10 21:46:50 +08:00
|
|
|
|
2022-11-16 00:04:46 +08:00
|
|
|
setup() {
|
|
|
|
this.container = this.$el;
|
|
|
|
this.trigger = this.$refs.trigger;
|
|
|
|
this.content = this.$refs.content;
|
2017-12-10 21:46:50 +08:00
|
|
|
|
2022-11-16 00:04:46 +08:00
|
|
|
if (this.trigger) {
|
|
|
|
this.trigger.addEventListener('click', this.toggle.bind(this));
|
|
|
|
this.openIfContainsError();
|
|
|
|
}
|
2017-12-10 21:46:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
open() {
|
2022-11-16 00:04:46 +08:00
|
|
|
this.container.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() {
|
2022-11-16 00:04:46 +08:00
|
|
|
this.container.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() {
|
2022-11-16 00:04:46 +08:00
|
|
|
if (this.container.classList.contains('open')) {
|
2017-12-10 21:46:50 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 00:04:46 +08:00
|
|
|
}
|