Fix broken column after cloning product

This commit is contained in:
kernal053
2024-08-23 15:44:42 +05:30
parent a50be52cde
commit e35a5179bb

View File

@@ -6,7 +6,7 @@ export default class ColumnPreferencesController extends Controller {
connect() {
this.table = document.querySelector('table[data-column-preferences-target="table"]');
this.cols = Array.from(this.table.querySelectorAll('col'));
this.colSpanCells = this.table.querySelectorAll('th[colspan],td[colspan]');
this.colSpanCells = Array.from(this.table.querySelectorAll('th[colspan],td[colspan]'));
// Initialise data-default-col-span
this.colSpanCells.forEach((cell)=> {
cell.dataset.defaultColSpan ||= cell.colSpan;
@@ -19,6 +19,8 @@ export default class ColumnPreferencesController extends Controller {
// On checkbox changed
element.addEventListener("change", this.#showHideColumn.bind(this));
}
this.#observeProductsTableRows();
}
// private
@@ -30,14 +32,39 @@ export default class ColumnPreferencesController extends Controller {
this.table.classList.toggle(`hide-${name}`, !element.checked);
// Reset cell colspans
const hiddenColCount = this.checkboxes.filter((checkbox)=> !checkbox.checked).length;
for(const cell of this.colSpanCells) {
const span = parseInt(cell.dataset.defaultColSpan, 10) - hiddenColCount;
cell.colSpan = span;
this.#updateColSpanCell(cell);
};
}
#showHideElement(element, show) {
element.style.display = show ? "" : "none";
}
#observeProductsTableRows(){
this.productsTableObserver = new MutationObserver((mutations, _observer) => {
const mutationRecord = mutations[0];
if(mutationRecord){
const productRowElement = mutationRecord.addedNodes[0];
if(productRowElement){
const newColSpanCell = productRowElement.querySelector('td[colspan]');
newColSpanCell.dataset.defaultColSpan ||= newColSpanCell.colSpan;
this.#updateColSpanCell(newColSpanCell);
this.colSpanCells.push(newColSpanCell);
}
}
});
this.productsTableObserver.observe(this.table, { childList: true });
}
#hiddenColCount(){
return this.checkboxes.filter((checkbox)=> !checkbox.checked).length;
}
#updateColSpanCell(cell){
cell.colSpan = parseInt(cell.dataset.defaultColSpan, 10) - this.#hiddenColCount();
}
}