{"version":3,"file":"table.js","sources":["../../../../src/tile-layout/table-utils/table.ts"],"sourcesContent":["import { GridConfig, GridCoords, GridSpan, TableOf } from '../model';\n\nexport const newTable = (rows: number, cols: number, value: T): TableOf =>\n new Array(rows).fill(null).map(_ => new Array(cols).fill(value));\n\nexport const copyTable = (table: TableOf): TableOf =>\n [...table].map(row => [...row]);\n\nexport const tableSize = (table: TableOf) => {\n const rows = table.length;\n const cols = rows ? table[0].length : 0;\n return { rows, cols };\n};\n\nexport const isEmptyRow = (row: Array) => !row.find(cell => !!cell);\n\nexport const trimLocation = (\n table: TableOf,\n { col, row }: GridCoords\n) => {\n const { cols, rows } = tableSize(table);\n return {\n col: col < 0 ? 0 : col >= cols ? cols - 1 : col,\n row: row < 0 ? 0 : row >= rows ? rows - 1 : row,\n };\n};\nexport const pointToLocation = (\n table: TableOf,\n x: number,\n y: number,\n { elementWidth, elementHeight }: GridConfig\n) => {\n const col = Math.floor(x / elementWidth);\n const row = Math.floor(y / elementHeight);\n return trimLocation(table, { col, row });\n};\n\nexport const fitsInTable = (\n coords: GridCoords & GridSpan,\n table: TableOf\n) => {\n const { rows, cols } = tableSize(table);\n for (let row = coords.row; row < coords.row + coords.rowSpan; row++) {\n for (let col = coords.col; col < coords.col + coords.colSpan; col++) {\n if (row > rows || col > cols) return false;\n if (table[row][col]) return false;\n }\n }\n return true;\n};\n\nexport const placeInTable = (\n coords: GridCoords & GridSpan,\n data: T,\n table: TableOf\n): TableOf => {\n const newTable = copyTable(table);\n for (let row = coords.row; row < coords.row + coords.rowSpan; row++) {\n for (let col = coords.col; col < coords.col + coords.colSpan; col++) {\n newTable[row][col] = data;\n }\n }\n return newTable;\n};\n\nexport const findFirstFittingPosition = (\n span: GridSpan,\n table: TableOf\n): GridCoords | undefined => {\n const { rows, cols } = tableSize(table);\n let free = true;\n for (let tableRow = 0; tableRow <= rows - span.rowSpan; tableRow++) {\n for (let tableCol = 0; tableCol <= cols - span.colSpan; tableCol++) {\n //check if all cells are available\n free = true;\n for (let cellRow = 0; cellRow < span.rowSpan; cellRow++) {\n for (let cellCol = 0; cellCol < span.colSpan; cellCol++) {\n free = free && !table[tableRow + cellRow][tableCol + cellCol];\n }\n }\n if (free) {\n return { row: tableRow, col: tableCol };\n }\n }\n }\n};\n"],"names":[],"mappings":";;IAEa,QAAQ,GAAG,UAAI,IAAY,EAAE,IAAY,EAAE,KAAQ;IAC9D,OAAA,IAAI,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,UAAA,CAAC,IAAI,OAAA,IAAI,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAA,CAAC;AAAhE,EAAiE;IAEtD,SAAS,GAAG,UAAI,KAAiB;IAC5C,OAAA,yBAAI,KAAK,GAAE,GAAG,CAAC,UAAA,GAAG,IAAI,gCAAI,GAAG,KAAC,CAAC;AAA/B,EAAgC;IAErB,SAAS,GAAG,UAAI,KAAiB;IAC5C,IAAM,IAAI,GAAG,KAAK,CAAC,MAAM,CAAC;IAC1B,IAAM,IAAI,GAAG,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC;IACxC,OAAO,EAAE,IAAI,MAAA,EAAE,IAAI,MAAA,EAAE,CAAC;AACxB,EAAE;IAEW,UAAU,GAAG,UAAI,GAAa,IAAK,OAAA,CAAC,GAAG,CAAC,IAAI,CAAC,UAAA,IAAI,IAAI,OAAA,CAAC,CAAC,IAAI,GAAA,CAAC,IAAC;IAE7D,YAAY,GAAG,UAC1B,KAAiB,EACjB,EAAwB;QAAtB,GAAG,SAAA,EAAE,GAAG,SAAA;IAEJ,IAAA,KAAiB,SAAS,CAAC,KAAK,CAAC,EAA/B,IAAI,UAAA,EAAE,IAAI,UAAqB,CAAC;IACxC,OAAO;QACL,GAAG,EAAE,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,GAAG,IAAI,IAAI,GAAG,IAAI,GAAG,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