diff --git a/ts_web/elements/dees-dashboardgrid.ts b/ts_web/elements/dees-dashboardgrid.ts index a4d2896..d833a04 100644 --- a/ts_web/elements/dees-dashboardgrid.ts +++ b/ts_web/elements/dees-dashboardgrid.ts @@ -552,50 +552,31 @@ export class DeesDashboardgrid extends DeesElement { const margins = this.getMargins(); const cellHeightValue = this.getCellHeight(); - // Convert margin to percentage to match renderWidget calculations - const marginHorizontalPercent = (margins.horizontal / containerRect.width) * 100; - const cellWidthPercent = (100 - marginHorizontalPercent * (this.columns + 1)) / this.columns; - const cellWidthPixels = containerRect.width * cellWidthPercent / 100; - - // Get mouse position relative to grid container + // Get widget position relative to grid container const mouseX = e.clientX - containerRect.left - this.dragOffsetX; const mouseY = e.clientY - containerRect.top - this.dragOffsetY; - // Calculate which cell the mouse is over by finding the closest cell center - let gridX = 0; - let minDistance = Infinity; + // Use pixel calculations for accuracy + const totalWidth = containerRect.width; + const totalMarginWidth = margins.horizontal * (this.columns + 1); + const availableWidth = totalWidth - totalMarginWidth; + const cellWidthPx = availableWidth / this.columns; - // Check distance to center of each possible column position - for (let i = 0; i < this.columns; i++) { - // Calculate position in pixels (matching renderWidget percentage formula) - const leftPercent = i * cellWidthPercent + (i + 1) * marginHorizontalPercent; - const cellLeftPixels = containerRect.width * leftPercent / 100; - const cellCenterPixels = cellLeftPixels + cellWidthPixels / 2; - const distance = Math.abs(mouseX - cellCenterPixels); - - if (distance < minDistance) { - minDistance = distance; - gridX = i; - } + // Calculate grid X position + // Account for the initial margin and then repeating pattern of cell+margin + let gridX = 0; + if (mouseX > margins.horizontal) { + const adjustedX = mouseX - margins.horizontal; + const cellPlusMargin = cellWidthPx + margins.horizontal; + gridX = Math.floor(adjustedX / cellPlusMargin + 0.5); // +0.5 for rounding to nearest } - // For Y: find closest row center + // Calculate grid Y position let gridY = 0; - minDistance = Infinity; - - // Check reasonable number of rows - for (let i = 0; i < 100; i++) { - const cellTop = i * cellHeightValue + (i + 1) * margins.vertical; - const cellCenter = cellTop + cellHeightValue / 2; - const distance = Math.abs(mouseY - cellCenter); - - if (distance < minDistance) { - minDistance = distance; - gridY = i; - } - - // Stop checking if we're too far away - if (cellTop > mouseY + cellHeightValue) break; + if (mouseY > margins.vertical) { + const adjustedY = mouseY - margins.vertical; + const cellPlusMargin = cellHeightValue + margins.vertical; + gridY = Math.floor(adjustedY / cellPlusMargin + 0.5); // +0.5 for rounding to nearest } const clampedX = Math.max(0, Math.min(gridX, this.columns - this.draggedWidget.w));