BookStack/resources/js/components/expand-toggle.js
Dan Brown f41c02cbd7
Some checks are pending
lint-js / build (push) Waiting to run
test-js / build (push) Waiting to run
TS: Converted app file and animations service
Extracted functions out of app file during changes to clean up.
Altered animation function to use normal css prop names instead of JS
CSS prop names.
2024-10-11 15:19:19 +01:00

43 lines
1.1 KiB
JavaScript

import {slideUp, slideDown} from '../services/animations.ts';
import {Component} from './component';
export class ExpandToggle extends Component {
setup() {
this.targetSelector = this.$opts.targetSelector;
this.isOpen = this.$opts.isOpen === 'true';
this.updateEndpoint = this.$opts.updateEndpoint;
// Listener setup
this.$el.addEventListener('click', this.click.bind(this));
}
open(elemToToggle) {
slideDown(elemToToggle, 200);
}
close(elemToToggle) {
slideUp(elemToToggle, 200);
}
click(event) {
event.preventDefault();
const matchingElems = document.querySelectorAll(this.targetSelector);
for (const match of matchingElems) {
const action = this.isOpen ? this.close : this.open;
action(match);
}
this.isOpen = !this.isOpen;
this.updateSystemAjax(this.isOpen);
}
updateSystemAjax(isOpen) {
window.$http.patch(this.updateEndpoint, {
expand: isOpen ? 'true' : 'false',
});
}
}