mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-19 13:19:39 +02:00
* chore: add formatting and linting for javascript code relates to #1295 * use spaces instaed * add to recommended extensions * only enforce lint * auto save
This commit is contained in:
parent
fa3b8b078c
commit
4ad28d6eff
30 changed files with 740 additions and 377 deletions
|
@ -1,81 +1,95 @@
|
|||
import { Controller } from "@hotwired/stimulus"
|
||||
import { Controller } from "@hotwired/stimulus";
|
||||
|
||||
// Connects to data-controller="bulk-select"
|
||||
export default class extends Controller {
|
||||
static targets = ["row", "group", "selectionBar", "selectionBarText", "bulkEditDrawerTitle"]
|
||||
static targets = [
|
||||
"row",
|
||||
"group",
|
||||
"selectionBar",
|
||||
"selectionBarText",
|
||||
"bulkEditDrawerTitle",
|
||||
];
|
||||
static values = {
|
||||
resource: String,
|
||||
selectedIds: { type: Array, default: [] }
|
||||
}
|
||||
selectedIds: { type: Array, default: [] },
|
||||
};
|
||||
|
||||
connect() {
|
||||
document.addEventListener("turbo:load", this._updateView)
|
||||
document.addEventListener("turbo:load", this._updateView);
|
||||
|
||||
this._updateView()
|
||||
this._updateView();
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
document.removeEventListener("turbo:load", this._updateView)
|
||||
document.removeEventListener("turbo:load", this._updateView);
|
||||
}
|
||||
|
||||
bulkEditDrawerTitleTargetConnected(element) {
|
||||
element.innerText = `Edit ${this.selectedIdsValue.length} ${this._pluralizedResourceName()}`
|
||||
element.innerText = `Edit ${
|
||||
this.selectedIdsValue.length
|
||||
} ${this._pluralizedResourceName()}`;
|
||||
}
|
||||
|
||||
submitBulkRequest(e) {
|
||||
const form = e.target.closest("form");
|
||||
const scope = e.params.scope
|
||||
this._addHiddenFormInputsForSelectedIds(form, `${scope}[entry_ids][]`, this.selectedIdsValue)
|
||||
form.requestSubmit()
|
||||
const scope = e.params.scope;
|
||||
this._addHiddenFormInputsForSelectedIds(
|
||||
form,
|
||||
`${scope}[entry_ids][]`,
|
||||
this.selectedIdsValue,
|
||||
);
|
||||
form.requestSubmit();
|
||||
}
|
||||
|
||||
togglePageSelection(e) {
|
||||
if (e.target.checked) {
|
||||
this._selectAll()
|
||||
this._selectAll();
|
||||
} else {
|
||||
this.deselectAll()
|
||||
this.deselectAll();
|
||||
}
|
||||
}
|
||||
|
||||
toggleGroupSelection(e) {
|
||||
const group = this.groupTargets.find(group => group.contains(e.target))
|
||||
const group = this.groupTargets.find((group) => group.contains(e.target));
|
||||
|
||||
this._rowsForGroup(group).forEach(row => {
|
||||
this._rowsForGroup(group).forEach((row) => {
|
||||
if (e.target.checked) {
|
||||
this._addToSelection(row.dataset.id)
|
||||
this._addToSelection(row.dataset.id);
|
||||
} else {
|
||||
this._removeFromSelection(row.dataset.id)
|
||||
this._removeFromSelection(row.dataset.id);
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
toggleRowSelection(e) {
|
||||
if (e.target.checked) {
|
||||
this._addToSelection(e.target.dataset.id)
|
||||
this._addToSelection(e.target.dataset.id);
|
||||
} else {
|
||||
this._removeFromSelection(e.target.dataset.id)
|
||||
this._removeFromSelection(e.target.dataset.id);
|
||||
}
|
||||
}
|
||||
|
||||
deselectAll() {
|
||||
this.selectedIdsValue = []
|
||||
this.element.querySelectorAll('input[type="checkbox"]').forEach(el => el.checked = false)
|
||||
this.selectedIdsValue = [];
|
||||
this.element.querySelectorAll('input[type="checkbox"]').forEach((el) => {
|
||||
el.checked = false;
|
||||
});
|
||||
}
|
||||
|
||||
selectedIdsValueChanged() {
|
||||
this._updateView()
|
||||
this._updateView();
|
||||
}
|
||||
|
||||
_addHiddenFormInputsForSelectedIds(form, paramName, transactionIds) {
|
||||
this._resetFormInputs(form, paramName);
|
||||
|
||||
transactionIds.forEach(id => {
|
||||
transactionIds.forEach((id) => {
|
||||
const input = document.createElement("input");
|
||||
input.type = 'hidden'
|
||||
input.name = paramName
|
||||
input.value = id
|
||||
form.appendChild(input)
|
||||
})
|
||||
input.type = "hidden";
|
||||
input.name = paramName;
|
||||
input.value = id;
|
||||
form.appendChild(input);
|
||||
});
|
||||
}
|
||||
|
||||
_resetFormInputs(form, paramName) {
|
||||
|
@ -84,51 +98,58 @@ export default class extends Controller {
|
|||
}
|
||||
|
||||
_rowsForGroup(group) {
|
||||
return this.rowTargets.filter(row => group.contains(row))
|
||||
return this.rowTargets.filter((row) => group.contains(row));
|
||||
}
|
||||
|
||||
_addToSelection(idToAdd) {
|
||||
this.selectedIdsValue = Array.from(
|
||||
new Set([...this.selectedIdsValue, idToAdd])
|
||||
)
|
||||
new Set([...this.selectedIdsValue, idToAdd]),
|
||||
);
|
||||
}
|
||||
|
||||
_removeFromSelection(idToRemove) {
|
||||
this.selectedIdsValue = this.selectedIdsValue.filter(id => id !== idToRemove)
|
||||
this.selectedIdsValue = this.selectedIdsValue.filter(
|
||||
(id) => id !== idToRemove,
|
||||
);
|
||||
}
|
||||
|
||||
_selectAll() {
|
||||
this.selectedIdsValue = this.rowTargets.map(t => t.dataset.id)
|
||||
this.selectedIdsValue = this.rowTargets.map((t) => t.dataset.id);
|
||||
}
|
||||
|
||||
_updateView = () => {
|
||||
this._updateSelectionBar()
|
||||
this._updateGroups()
|
||||
this._updateRows()
|
||||
}
|
||||
this._updateSelectionBar();
|
||||
this._updateGroups();
|
||||
this._updateRows();
|
||||
};
|
||||
|
||||
_updateSelectionBar() {
|
||||
const count = this.selectedIdsValue.length
|
||||
this.selectionBarTextTarget.innerText = `${count} ${this._pluralizedResourceName()} selected`
|
||||
this.selectionBarTarget.hidden = count === 0
|
||||
this.selectionBarTarget.querySelector("input[type='checkbox']").checked = count > 0
|
||||
const count = this.selectedIdsValue.length;
|
||||
this.selectionBarTextTarget.innerText = `${count} ${this._pluralizedResourceName()} selected`;
|
||||
this.selectionBarTarget.hidden = count === 0;
|
||||
this.selectionBarTarget.querySelector("input[type='checkbox']").checked =
|
||||
count > 0;
|
||||
}
|
||||
|
||||
_pluralizedResourceName() {
|
||||
return `${this.resourceValue}${this.selectedIdsValue.length === 1 ? "" : "s"}`
|
||||
return `${this.resourceValue}${
|
||||
this.selectedIdsValue.length === 1 ? "" : "s"
|
||||
}`;
|
||||
}
|
||||
|
||||
_updateGroups() {
|
||||
this.groupTargets.forEach(group => {
|
||||
const rows = this.rowTargets.filter(row => group.contains(row))
|
||||
const groupSelected = rows.length > 0 && rows.every(row => this.selectedIdsValue.includes(row.dataset.id))
|
||||
group.querySelector("input[type='checkbox']").checked = groupSelected
|
||||
})
|
||||
this.groupTargets.forEach((group) => {
|
||||
const rows = this.rowTargets.filter((row) => group.contains(row));
|
||||
const groupSelected =
|
||||
rows.length > 0 &&
|
||||
rows.every((row) => this.selectedIdsValue.includes(row.dataset.id));
|
||||
group.querySelector("input[type='checkbox']").checked = groupSelected;
|
||||
});
|
||||
}
|
||||
|
||||
_updateRows() {
|
||||
this.rowTargets.forEach(row => {
|
||||
row.checked = this.selectedIdsValue.includes(row.dataset.id)
|
||||
})
|
||||
this.rowTargets.forEach((row) => {
|
||||
row.checked = this.selectedIdsValue.includes(row.dataset.id);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue