[d565449] | 1 | import { __spreadArray, __read } from 'tslib';
|
---|
| 2 |
|
---|
| 3 | var newTable = function (rows, cols, value) {
|
---|
| 4 | return new Array(rows).fill(null).map(function (_) { return new Array(cols).fill(value); });
|
---|
| 5 | };
|
---|
| 6 | var copyTable = function (table) {
|
---|
| 7 | return __spreadArray([], __read(table)).map(function (row) { return __spreadArray([], __read(row)); });
|
---|
| 8 | };
|
---|
| 9 | var tableSize = function (table) {
|
---|
| 10 | var rows = table.length;
|
---|
| 11 | var cols = rows ? table[0].length : 0;
|
---|
| 12 | return { rows: rows, cols: cols };
|
---|
| 13 | };
|
---|
| 14 | var isEmptyRow = function (row) { return !row.find(function (cell) { return !!cell; }); };
|
---|
| 15 | var trimLocation = function (table, _a) {
|
---|
| 16 | var col = _a.col, row = _a.row;
|
---|
| 17 | var _b = tableSize(table), cols = _b.cols, rows = _b.rows;
|
---|
| 18 | return {
|
---|
| 19 | col: col < 0 ? 0 : col >= cols ? cols - 1 : col,
|
---|
| 20 | row: row < 0 ? 0 : row >= rows ? rows - 1 : row,
|
---|
| 21 | };
|
---|
| 22 | };
|
---|
| 23 | var pointToLocation = function (table, x, y, _a) {
|
---|
| 24 | var elementWidth = _a.elementWidth, elementHeight = _a.elementHeight;
|
---|
| 25 | var col = Math.floor(x / elementWidth);
|
---|
| 26 | var row = Math.floor(y / elementHeight);
|
---|
| 27 | return trimLocation(table, { col: col, row: row });
|
---|
| 28 | };
|
---|
| 29 | var fitsInTable = function (coords, table) {
|
---|
| 30 | var _a = tableSize(table), rows = _a.rows, cols = _a.cols;
|
---|
| 31 | for (var row = coords.row; row < coords.row + coords.rowSpan; row++) {
|
---|
| 32 | for (var col = coords.col; col < coords.col + coords.colSpan; col++) {
|
---|
| 33 | if (row > rows || col > cols)
|
---|
| 34 | return false;
|
---|
| 35 | if (table[row][col])
|
---|
| 36 | return false;
|
---|
| 37 | }
|
---|
| 38 | }
|
---|
| 39 | return true;
|
---|
| 40 | };
|
---|
| 41 | var placeInTable = function (coords, data, table) {
|
---|
| 42 | var newTable = copyTable(table);
|
---|
| 43 | for (var row = coords.row; row < coords.row + coords.rowSpan; row++) {
|
---|
| 44 | for (var col = coords.col; col < coords.col + coords.colSpan; col++) {
|
---|
| 45 | newTable[row][col] = data;
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 | return newTable;
|
---|
| 49 | };
|
---|
| 50 | var findFirstFittingPosition = function (span, table) {
|
---|
| 51 | var _a = tableSize(table), rows = _a.rows, cols = _a.cols;
|
---|
| 52 | var free = true;
|
---|
| 53 | for (var tableRow = 0; tableRow <= rows - span.rowSpan; tableRow++) {
|
---|
| 54 | for (var tableCol = 0; tableCol <= cols - span.colSpan; tableCol++) {
|
---|
| 55 | //check if all cells are available
|
---|
| 56 | free = true;
|
---|
| 57 | for (var cellRow = 0; cellRow < span.rowSpan; cellRow++) {
|
---|
| 58 | for (var cellCol = 0; cellCol < span.colSpan; cellCol++) {
|
---|
| 59 | free = free && !table[tableRow + cellRow][tableCol + cellCol];
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 | if (free) {
|
---|
| 63 | return { row: tableRow, col: tableCol };
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | };
|
---|
| 68 |
|
---|
| 69 | export { copyTable, findFirstFittingPosition, fitsInTable, isEmptyRow, newTable, placeInTable, pointToLocation, tableSize, trimLocation };
|
---|
| 70 | //# sourceMappingURL=table.js.map
|
---|