dawarich/app/javascript/controllers/checkbox_select_all_controller.js

39 lines
1 KiB
JavaScript
Raw Normal View History

import BaseController from "./base_controller"
2024-05-23 14:12:23 -04:00
// Connects to data-controller="checkbox-select-all"
export default class extends BaseController {
static targets = ["parent", "child", "deleteButton"]
2024-05-23 14:12:23 -04:00
connect() {
this.parentTarget.checked = false
this.childTargets.map(x => x.checked = false)
this.updateDeleteButtonVisibility()
2024-05-23 14:12:23 -04:00
}
toggleChildren() {
if (this.parentTarget.checked) {
this.childTargets.map(x => x.checked = true)
} else {
this.childTargets.map(x => x.checked = false)
}
this.updateDeleteButtonVisibility()
2024-05-23 14:12:23 -04:00
}
toggleParent() {
if (this.childTargets.map(x => x.checked).includes(false)) {
this.parentTarget.checked = false
} else {
this.parentTarget.checked = true
}
this.updateDeleteButtonVisibility()
}
updateDeleteButtonVisibility() {
const hasCheckedItems = this.childTargets.some(target => target.checked)
if (this.hasDeleteButtonTarget) {
this.deleteButtonTarget.style.display = hasCheckedItems ? 'inline-block' : 'none'
}
2024-05-23 14:12:23 -04:00
}
}