2024-06-07 12:44:06 -04:00
|
|
|
import {Controller} from "@hotwired/stimulus"
|
|
|
|
|
|
|
|
// Connects to data-controller="bulk-select"
|
|
|
|
export default class extends Controller {
|
2024-06-07 18:59:46 -04:00
|
|
|
static targets = ["row", "group", "selectionBar", "selectionBarText", "bulkEditDrawerTitle"]
|
2024-06-07 12:44:06 -04:00
|
|
|
static values = {
|
|
|
|
resource: String,
|
|
|
|
selectedIds: {type: Array, default: []}
|
|
|
|
}
|
|
|
|
|
|
|
|
connect() {
|
|
|
|
document.addEventListener("turbo:load", this.#updateView)
|
|
|
|
|
|
|
|
this.#updateView()
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnect() {
|
|
|
|
document.removeEventListener("turbo:load", this.#updateView)
|
|
|
|
}
|
|
|
|
|
2024-06-07 18:59:46 -04:00
|
|
|
bulkEditDrawerTitleTargetConnected(element) {
|
|
|
|
element.innerText = `Edit ${this.selectedIdsValue.length} ${this.#pluralizedResourceName()}`
|
|
|
|
}
|
|
|
|
|
|
|
|
submitBulkRequest(e) {
|
2024-06-07 16:56:30 -04:00
|
|
|
const form = e.target.closest("form");
|
2024-06-07 18:59:46 -04:00
|
|
|
const scope = e.params.scope
|
2024-07-01 10:49:43 -04:00
|
|
|
this.#addHiddenFormInputsForSelectedIds(form, `${scope}[entry_ids][]`, this.selectedIdsValue)
|
2024-06-07 16:56:30 -04:00
|
|
|
form.requestSubmit()
|
|
|
|
}
|
|
|
|
|
2024-06-07 12:44:06 -04:00
|
|
|
togglePageSelection(e) {
|
|
|
|
if (e.target.checked) {
|
|
|
|
this.#selectAll()
|
|
|
|
} else {
|
|
|
|
this.deselectAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleGroupSelection(e) {
|
|
|
|
const group = this.groupTargets.find(group => group.contains(e.target))
|
|
|
|
|
|
|
|
this.#rowsForGroup(group).forEach(row => {
|
|
|
|
if (e.target.checked) {
|
|
|
|
this.#addToSelection(row.dataset.id)
|
|
|
|
} else {
|
|
|
|
this.#removeFromSelection(row.dataset.id)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleRowSelection(e) {
|
|
|
|
if (e.target.checked) {
|
|
|
|
this.#addToSelection(e.target.dataset.id)
|
|
|
|
} else {
|
|
|
|
this.#removeFromSelection(e.target.dataset.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deselectAll() {
|
|
|
|
this.selectedIdsValue = []
|
|
|
|
}
|
|
|
|
|
|
|
|
selectedIdsValueChanged() {
|
|
|
|
this.#updateView()
|
|
|
|
}
|
|
|
|
|
2024-06-07 16:56:30 -04:00
|
|
|
#addHiddenFormInputsForSelectedIds(form, paramName, transactionIds) {
|
2024-06-19 23:50:32 +03:00
|
|
|
this.#resetFormInputs(form, paramName);
|
|
|
|
|
2024-06-07 16:56:30 -04:00
|
|
|
transactionIds.forEach(id => {
|
|
|
|
const input = document.createElement("input");
|
|
|
|
input.type = 'hidden'
|
|
|
|
input.name = paramName
|
|
|
|
input.value = id
|
|
|
|
form.appendChild(input)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-06-19 23:50:32 +03:00
|
|
|
#resetFormInputs(form, paramName) {
|
|
|
|
const existingInputs = form.querySelectorAll(`input[name='${paramName}']`);
|
|
|
|
existingInputs.forEach((input) => input.remove());
|
|
|
|
}
|
|
|
|
|
2024-06-07 12:44:06 -04:00
|
|
|
#rowsForGroup(group) {
|
|
|
|
return this.rowTargets.filter(row => group.contains(row))
|
|
|
|
}
|
|
|
|
|
|
|
|
#addToSelection(idToAdd) {
|
|
|
|
this.selectedIdsValue = Array.from(
|
|
|
|
new Set([...this.selectedIdsValue, idToAdd])
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#removeFromSelection(idToRemove) {
|
|
|
|
this.selectedIdsValue = this.selectedIdsValue.filter(id => id !== idToRemove)
|
|
|
|
}
|
|
|
|
|
|
|
|
#selectAll() {
|
|
|
|
this.selectedIdsValue = this.rowTargets.map(t => t.dataset.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
#updateView = () => {
|
|
|
|
this.#updateSelectionBar()
|
|
|
|
this.#updateGroups()
|
|
|
|
this.#updateRows()
|
|
|
|
}
|
|
|
|
|
|
|
|
#updateSelectionBar() {
|
|
|
|
const count = this.selectedIdsValue.length
|
2024-06-07 18:59:46 -04:00
|
|
|
this.selectionBarTextTarget.innerText = `${count} ${this.#pluralizedResourceName()} selected`
|
2024-06-07 12:44:06 -04:00
|
|
|
this.selectionBarTarget.hidden = count === 0
|
|
|
|
this.selectionBarTarget.querySelector("input[type='checkbox']").checked = count > 0
|
|
|
|
}
|
|
|
|
|
2024-06-07 18:59:46 -04:00
|
|
|
#pluralizedResourceName() {
|
|
|
|
return `${this.resourceValue}${this.selectedIdsValue.length === 1 ? "" : "s"}`
|
|
|
|
}
|
|
|
|
|
2024-06-07 12:44:06 -04:00
|
|
|
#updateGroups() {
|
|
|
|
this.groupTargets.forEach(group => {
|
|
|
|
const rows = this.rowTargets.filter(row => group.contains(row))
|
2024-06-19 06:52:08 -04:00
|
|
|
const groupSelected = rows.length > 0 && rows.every(row => this.selectedIdsValue.includes(row.dataset.id))
|
2024-06-07 12:44:06 -04:00
|
|
|
group.querySelector("input[type='checkbox']").checked = groupSelected
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#updateRows() {
|
|
|
|
this.rowTargets.forEach(row => {
|
|
|
|
row.checked = this.selectedIdsValue.includes(row.dataset.id)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|