UNPKG

flex-area-grid

Version:
132 lines 5.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.hasArea = exports.generateMatrix = void 0; var lodash_es_1 = require("lodash-es"); /** * Generates a grid matrix. * @param {number} initialColumns - The number of grid columns. * @param {FunctionComponentElement<TFlexGridItem>[]} children - A grid items. * @return {TMatrix[][]} - Returns a grid matrix using for draw an a grid. */ function generateMatrix(initialColumns, children) { var _a = getArrayDimensional(children, initialColumns), rows = _a[0], columns = _a[1]; return fillMatrix(children, rows, columns); } exports.generateMatrix = generateMatrix; /** * Fill a grid matrix with grid items. * @param {FunctionComponentElement<TFlexGridItem>[]} children - A grid items. * @param {number} rows - The number of grid rows. * @param {number} columns - The number of grid columns. * @return {TMatrix[][]} - Returns a grid matrix using for draw an a grid. */ function fillMatrix(children, rows, columns) { var matrix = createTwoDimensionalArray(rows, columns); children.forEach(function (_a, childIndex) { var _b = _a.props, startRow = _b.startRow, startColumn = _b.startColumn, endRow = _b.endRow, endColumn = _b.endColumn; var useArea = hasArea(startRow, startColumn, endRow, endColumn); if (useArea) { var sR = startRow; var eR = endRow; var sC = startColumn; var eC = endColumn; var firstRowIndex = sR - 1; var firstColumnIndex = sC - 1; if (matrix[firstRowIndex][firstColumnIndex] !== undefined) { return; } for (var i = firstRowIndex; i < eR; i++) { for (var j = firstColumnIndex; j < eC; j++) { if (i === firstRowIndex && j === firstColumnIndex) { matrix[i][j] = { rows: eR - sR + 1, columns: eC - sC + 1, itemIndex: childIndex, }; continue; } matrix[i][j] = null; } } return; } var _c = getEmptyCell(matrix), rowIndex = _c[0], columnIndex = _c[1]; matrix[rowIndex][columnIndex] = { rows: 1, columns: 1, itemIndex: childIndex, }; }); return matrix; } /** * Calculate the array dimensional. * @param array - A grid items. * @param initialColumns - An initial grid columns * @return {[number, number]} - Returns a grid row and column numbers. */ function getArrayDimensional(array, initialColumns) { var _a = array.reduce(function (prev, _a) { var _b = _a.props, startRow = _b.startRow, startColumn = _b.startColumn, endRow = _b.endRow, endColumn = _b.endColumn; var areaVolume = hasArea(startRow, startColumn, endRow, endColumn) ? (endRow - startRow + 1) * (endColumn - startColumn + 1) : 1; var currentMatrixVolume = areaVolume + prev.volume; return { volume: currentMatrixVolume, maxColumn: Math.max(prev.maxColumn, endColumn || 0), maxRow: Math.max(prev.maxRow, endRow || 0), }; }, { volume: 0, maxColumn: initialColumns, maxRow: 1, }), volume = _a.volume, maxColumn = _a.maxColumn, maxRow = _a.maxRow; var rows = Math.max(Math.ceil(volume / maxColumn), maxRow); return [rows, maxColumn]; } /** * Check is grid item has an area properties. * @param startRow - A start row position. * @param startColumn - A start column position. * @param endRow - An end row position. * @param endColumn - An end column position. * @return {boolean} - Returns boolean flag. */ function hasArea(startRow, startColumn, endRow, endColumn) { return !(lodash_es_1.isNil(startRow) || lodash_es_1.isNil(startColumn) || lodash_es_1.isNil(endRow) || lodash_es_1.isNil(endColumn)); } exports.hasArea = hasArea; /** * Get empty cell indexes from a grid matrix. * @param matrix - A grid matrix. * @return {[number, number]} - Returns empty cell indexes. */ function getEmptyCell(matrix) { for (var i = 0; i < matrix.length; i++) { for (var j = 0; j < matrix[0].length; j++) { if (matrix[i][j] === undefined) { return [i, j]; } } } return [0, 0]; } /** * Create matrix suing row and column numbers. * @param rows - The number of grid rows. * @param columns - The number of grid columns. * @return {[number, number]} - Returns empty two dimensional array. */ function createTwoDimensionalArray(rows, columns) { var arr = new Array(rows); for (var i = 0; i < rows; i++) { arr[i] = new Array(columns).fill(undefined); } return arr; } //# sourceMappingURL=utils.js.map