Skip to content

Instantly share code, notes, and snippets.

@neverything
Created June 3, 2024 11:48
Show Gist options
  • Save neverything/435657f3ba1320d36c81bfc1efe29c92 to your computer and use it in GitHub Desktop.
Save neverything/435657f3ba1320d36c81bfc1efe29c92 to your computer and use it in GitHub Desktop.
function c() {
return {
collapsedGroups: [],
isLoading: !1,
selectedRecords: [],
shouldCheckUniqueSelection: !0,
lastChecked: null, // Add a variable to keep track of the last checked checkbox
init: function () {
this.$wire.$on("deselectAllTableRecords", () => this.deselectAllRecords()),
this.$watch("selectedRecords", () => {
if (!this.shouldCheckUniqueSelection) {
this.shouldCheckUniqueSelection = !0;
return;
}
(this.selectedRecords = [...new Set(this.selectedRecords)]), (this.shouldCheckUniqueSelection = !1);
});
// Initialize checkboxes with event listener
this.initializeCheckboxes();
},
mountAction: function (e, s = null) {
this.$wire.set("selectedTableRecords", this.selectedRecords, !1), this.$wire.mountTableAction(e, s);
},
mountBulkAction: function (e) {
this.$wire.set("selectedTableRecords", this.selectedRecords, !1), this.$wire.mountTableBulkAction(e);
},
toggleSelectRecordsOnPage: function () {
let e = this.getRecordsOnPage();
if (this.areRecordsSelected(e)) {
this.deselectRecords(e);
return;
}
this.selectRecords(e);
},
toggleSelectRecordsInGroup: async function (e) {
if (((this.isLoading = !0), this.areRecordsSelected(this.getRecordsInGroupOnPage(e)))) {
this.deselectRecords(await this.$wire.getGroupedSelectableTableRecordKeys(e));
return;
}
this.selectRecords(await this.$wire.getGroupedSelectableTableRecordKeys(e)), (this.isLoading = !1);
},
getRecordsInGroupOnPage: function (e) {
let s = [];
for (let t of this.$root?.getElementsByClassName("fi-ta-record-checkbox") ?? []) t.dataset.group === e && s.push(t.value);
return s;
},
getRecordsOnPage: function () {
let e = [];
for (let s of this.$root?.getElementsByClassName("fi-ta-record-checkbox") ?? []) e.push(s.value);
return e;
},
selectRecords: function (e) {
for (let s of e) this.isRecordSelected(s) || this.selectedRecords.push(s);
},
deselectRecords: function (e) {
for (let s of e) {
let t = this.selectedRecords.indexOf(s);
t !== -1 && this.selectedRecords.splice(t, 1);
}
},
selectAllRecords: async function () {
(this.isLoading = !0), (this.selectedRecords = await this.$wire.getAllSelectableTableRecordKeys()), (this.isLoading = !1);
},
deselectAllRecords: function () {
this.selectedRecords = [];
},
isRecordSelected: function (e) {
return this.selectedRecords.includes(e);
},
areRecordsSelected: function (e) {
return e.every((s) => this.isRecordSelected(s));
},
toggleCollapseGroup: function (e) {
if (this.isGroupCollapsed(e)) {
this.collapsedGroups.splice(this.collapsedGroups.indexOf(e), 1);
return;
}
this.collapsedGroups.push(e);
},
isGroupCollapsed: function (e) {
return this.collapsedGroups.includes(e);
},
resetCollapsedGroups: function () {
this.collapsedGroups = [];
},
initializeCheckboxes: function () {
let checkboxes = this.$root?.getElementsByClassName("fi-ta-record-checkbox") ?? [];
for (let checkbox of checkboxes) {
checkbox.addEventListener('click', (event) => this.handleCheckboxClick(event, checkbox));
}
},
handleCheckboxClick: function (event, checkbox) {
if (!this.lastChecked) {
this.lastChecked = checkbox;
return;
}
if (event.shiftKey) {
console.log("Shift key pressed");
let checkboxes = Array.from(this.$root?.getElementsByClassName("fi-ta-record-checkbox") ?? []);
let start = checkboxes.indexOf(this.lastChecked);
let end = checkboxes.indexOf(checkbox);
let range = [start, end].sort((a, b) => a - b);
for (let i = range[0]; i <= range[1]; i++) {
checkboxes[i].checked = checkbox.checked;
let value = checkboxes[i].value;
if (checkbox.checked) {
this.selectRecords([value]);
} else {
this.deselectRecords([value]);
}
}
}
this.lastChecked = checkbox;
}
};
}
export { c as default };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment