1 | import { __assign } from 'tslib';
|
---|
2 | import { isEmptyRow, tableSize, newTable, findFirstFittingPosition, placeInTable } from './table.js';
|
---|
3 |
|
---|
4 | var interceptTiles = function (table, startRow, startCol, rowSpan, colSpan) {
|
---|
5 | var interceptTiles = [];
|
---|
6 | var _a = tableSize(table), rows = _a.rows, cols = _a.cols;
|
---|
7 | for (var row = startRow; row < startRow + rowSpan; row++) {
|
---|
8 | if (row >= rows)
|
---|
9 | continue;
|
---|
10 | var _loop_1 = function (col) {
|
---|
11 | if (col >= cols)
|
---|
12 | return "continue";
|
---|
13 | var cell = table[row][col];
|
---|
14 | if (cell && !interceptTiles.find(function (t) { return t.data === cell.data; })) {
|
---|
15 | interceptTiles.push(cell);
|
---|
16 | }
|
---|
17 | };
|
---|
18 | for (var col = startCol; col < startCol + colSpan; col++) {
|
---|
19 | _loop_1(col);
|
---|
20 | }
|
---|
21 | }
|
---|
22 | return interceptTiles.sort(function (a, b) { return a.col + a.row * cols - (b.col + b.row * cols); });
|
---|
23 | };
|
---|
24 | var tableToTilesList = function (table) {
|
---|
25 | var _a = tableSize(table), rows = _a.rows, cols = _a.cols;
|
---|
26 | return interceptTiles(table, 0, 0, rows, cols);
|
---|
27 | };
|
---|
28 | var tilesListToTable = function (tiles, columns) {
|
---|
29 | var maxRows = tiles.reduce(function (memo, tile) { return memo + tile.rowSpan; }, 0);
|
---|
30 | var table = newTable(maxRows, columns, undefined);
|
---|
31 | tiles.forEach(function (tile) {
|
---|
32 | var position = findFirstFittingPosition(tile, table);
|
---|
33 | if (position) {
|
---|
34 | var fittedTile = __assign(__assign({}, tile), position);
|
---|
35 | table = placeInTable(fittedTile, fittedTile, table);
|
---|
36 | }
|
---|
37 | });
|
---|
38 | //trim the table
|
---|
39 | return table.filter(function (row) { return !isEmptyRow(row); });
|
---|
40 | };
|
---|
41 |
|
---|
42 | export { interceptTiles, tableToTilesList, tilesListToTable };
|
---|
43 | //# sourceMappingURL=tiles-table.js.map
|
---|