2023-04-18 23:20:02 +02:00
|
|
|
import {slideUp, slideDown} from '../services/animations';
|
|
|
|
import {Component} from './component';
|
2017-08-06 22:08:03 +02:00
|
|
|
|
2022-11-15 17:04:46 +01:00
|
|
|
export class ExpandToggle extends Component {
|
2017-08-06 22:08:03 +02:00
|
|
|
|
2023-04-19 16:20:04 +02:00
|
|
|
setup() {
|
2022-11-15 17:04:46 +01:00
|
|
|
this.targetSelector = this.$opts.targetSelector;
|
|
|
|
this.isOpen = this.$opts.isOpen === 'true';
|
|
|
|
this.updateEndpoint = this.$opts.updateEndpoint;
|
2019-04-14 14:01:51 +02:00
|
|
|
|
|
|
|
// Listener setup
|
2022-11-15 17:04:46 +01:00
|
|
|
this.$el.addEventListener('click', this.click.bind(this));
|
2017-08-06 22:08:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
open(elemToToggle) {
|
2019-06-07 17:00:34 +02:00
|
|
|
slideDown(elemToToggle, 200);
|
2017-08-06 22:08:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
close(elemToToggle) {
|
2019-06-07 17:00:34 +02:00
|
|
|
slideUp(elemToToggle, 200);
|
2017-08-06 22:08:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
click(event) {
|
|
|
|
event.preventDefault();
|
2019-04-14 14:01:51 +02:00
|
|
|
|
2022-11-15 17:04:46 +01:00
|
|
|
const matchingElems = document.querySelectorAll(this.targetSelector);
|
2023-04-18 23:20:02 +02:00
|
|
|
for (const match of matchingElems) {
|
2023-04-19 16:20:04 +02:00
|
|
|
const action = this.isOpen ? this.close : this.open;
|
|
|
|
action(match);
|
2017-08-06 22:08:03 +02:00
|
|
|
}
|
2019-04-14 14:01:51 +02:00
|
|
|
|
2017-08-06 22:08:03 +02:00
|
|
|
this.isOpen = !this.isOpen;
|
2019-04-14 14:01:51 +02:00
|
|
|
this.updateSystemAjax(this.isOpen);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateSystemAjax(isOpen) {
|
|
|
|
window.$http.patch(this.updateEndpoint, {
|
2023-04-18 23:20:02 +02:00
|
|
|
expand: isOpen ? 'true' : 'false',
|
2019-04-14 14:01:51 +02:00
|
|
|
});
|
2017-08-06 22:08:03 +02:00
|
|
|
}
|
|
|
|
|
2023-04-18 23:20:02 +02:00
|
|
|
}
|