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