function init(){const gridContainers=document.querySelectorAll(".vr--grid-pos-container");if(!gridContainers.length)return;for(const gridContainer of gridContainers){gridContainer.gridRows=parseInt(gridContainer.dataset.rows)||1;gridContainer.gridCols=parseInt(gridContainer.dataset.cols)||6;gridContainer.dataField=document.getElementById(gridContainer.dataset.fieldId);gridContainer.selectedIndices=gridContainer.selectedIndices||[];const fragment=document.createDocumentFragment();for(let i=0;itoggleCell(gridContainer,i));fragment.appendChild(gridItem)}gridContainer.appendChild(fragment);loadSelection(gridContainer)}function toggleCell(gridContainer,index){if(gridContainer.selectedIndices.includes(index)){removeRowOrColumn(gridContainer,index)}else{gridContainer.selectedIndices.push(index);fillSelection(gridContainer)}updateGrid(gridContainer);saveSelection(gridContainer)}function removeRowOrColumn(gridContainer,index){const row=Math.floor(index/gridContainer.gridCols);const col=index%gridContainer.gridCols;if(col===0){gridContainer.selectedIndices=gridContainer.selectedIndices.filter(cell=>{const cellRow=Math.floor(cell/gridContainer.gridCols);return cellRow{const cellCol=cell%gridContainer.gridCols;return cellCol!==col});adjustRectangleAfterColumnRemoval(gridContainer)}}function adjustRectangleAfterColumnRemoval(gridContainer){if(gridContainer.selectedIndices.length===0)return;const firstCol=Math.min(...gridContainer.selectedIndices.map(cell=>cell%gridContainer.gridCols));const lastCol=Math.max(...gridContainer.selectedIndices.map(cell=>cell%gridContainer.gridCols));for(let col=firstCol;col<=lastCol;col++){const isColumnEmpty=!gridContainer.selectedIndices.some(cell=>cell%gridContainer.gridCols===col);if(isColumnEmpty){gridContainer.selectedIndices=gridContainer.selectedIndices.filter(cell=>cell%gridContainer.gridColsMath.floor(cell/gridContainer.gridCols)));const lastRow=Math.max(...gridContainer.selectedIndices.map(cell=>Math.floor(cell/gridContainer.gridCols)));const firstCol=Math.min(...gridContainer.selectedIndices.map(cell=>cell%gridContainer.gridCols));const lastCol=Math.max(...gridContainer.selectedIndices.map(cell=>cell%gridContainer.gridCols));const newSelection=[];for(let row=firstRow;row<=lastRow;row++){for(let col=firstCol;col<=lastCol;col++){const cellIndex=row*gridContainer.gridCols+col;newSelection.push(cellIndex)}}gridContainer.selectedIndices=newSelection}function updateGrid(gridContainer){gridContainer.querySelectorAll(".grid-cell").forEach(cell=>{cell.classList.remove("selected")});gridContainer.selectedIndices.forEach(index=>{gridContainer.querySelector(`.grid-cell[data-index="${index}"]`)?.classList.add("selected")})}function saveSelection(gridContainer){const gridDataInput=gridContainer.dataField;if(gridDataInput){gridDataInput.value=JSON.stringify(selectedIndices)}}function loadSelection(gridContainer){const gridDataInput=gridContainer.dataField;if(gridDataInput){selectedIndices=JSON.parse(gridDataInput.value);updateGrid(gridContainer)}}}document.addEventListener("DOMContentLoaded",()=>{init()});document.addEventListener("turbo:render",()=>{init()});