UNPKG

@integec/grid-tools

Version:
240 lines (194 loc) 5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSelectedData = exports.selector = exports.selectAll = exports.down = exports.up = exports.right = exports.left = exports.isRowSelected = exports.isCellSelected = exports.normalizeSelection = void 0; var _range2 = _interopRequireDefault(require("ramda/src/range")); var _isNil2 = _interopRequireDefault(require("ramda/src/isNil")); var _utils = require("./utils"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } var empty = {}; var normalizeSelection = function normalizeSelection(selection) { var x1 = selection.x1, y1 = selection.y1, x2 = selection.x2, y2 = selection.y2; if ((0, _isNil2["default"])(x1) && (0, _isNil2["default"])(x2) && (0, _isNil2["default"])(y1) && (0, _isNil2["default"])(y2)) return empty; var xMin = !(0, _isNil2["default"])(x2) ? Math.min(x1, x2) : x1; var xMax = !(0, _isNil2["default"])(x2) ? Math.max(x1, x2) : x1; var yMin = !(0, _isNil2["default"])(y2) ? Math.min(y1, y2) : y1; var yMax = !(0, _isNil2["default"])(y2) ? Math.max(y1, y2) : y1; return { x1: xMin, x2: xMax, y1: yMin, y2: yMax }; }; exports.normalizeSelection = normalizeSelection; var isCellSelected = function isCellSelected(rowIndex, columnIndex, selection) { var _normalizeSelection = normalizeSelection(selection), x1 = _normalizeSelection.x1, x2 = _normalizeSelection.x2, y1 = _normalizeSelection.y1, y2 = _normalizeSelection.y2; return rowIndex <= y2 && rowIndex >= y1 && columnIndex <= x2 && columnIndex >= x1; }; exports.isCellSelected = isCellSelected; var isRowSelected = function isRowSelected(rowIndex, selection) { var _normalizeSelection2 = normalizeSelection(selection), y1 = _normalizeSelection2.y1, y2 = _normalizeSelection2.y2; return rowIndex <= y2 && rowIndex >= y1; }; exports.isRowSelected = isRowSelected; var left = function left(selection, expand, colCount, rowCount) { var x1 = selection.x1, y1 = selection.y1, x2 = selection.x2, y2 = selection.y2; if (!expand) { var _x = x1 - 1; var y = y1; if (_x < 0) { _x = colCount - 1; if (y > 0) { y--; } else { y = rowCount - 1; } } return { x1: _x, x2: _x, y1: y, y2: y }; } var x = Math.max(x2 - 1, 0); return { x1: x1, x2: x, y1: y1, y2: y2 }; }; exports.left = left; var right = function right(selection, expand, colCount, rowCount) { var x1 = selection.x1, y1 = selection.y1, x2 = selection.x2, y2 = selection.y2; if (!expand) { var _x2 = x1 + 1; var y = y1; if (_x2 > colCount - 1) { _x2 = 0; if (y < rowCount - 1) { y++; } else { y = 0; } } return { x1: _x2, x2: _x2, y1: y, y2: y }; } var x = Math.min(x2 + 1, colCount - 1); return { x1: x1, x2: x, y1: y1, y2: y2 }; }; exports.right = right; var up = function up(selection, expand) { var x1 = selection.x1, y1 = selection.y1, x2 = selection.x2, y2 = selection.y2; if (!expand) { var _y = Math.max(y1 - 1, 0); return { x1: x1, y1: _y, x2: x1, y2: _y }; } var y = Math.max(y2 - 1, 0); return { y1: y1, y2: y, x1: x1, x2: x2 }; }; exports.up = up; var down = function down(selection, expand, rowCount) { var x1 = selection.x1, y1 = selection.y1, x2 = selection.x2, y2 = selection.y2; if (!expand) { var _y2 = Math.min(y1 + 1, rowCount - 1); return { x1: x1, y1: _y2, x2: x1, y2: _y2 }; } var y = Math.min(y2 + 1, rowCount - 1); return { x1: x1, x2: x2, y1: y1, y2: y }; }; exports.down = down; var selectAll = function selectAll(colCount, rowCount) { return { x1: 0, x2: colCount - 1, y1: 0, y2: rowCount - 1 }; }; exports.selectAll = selectAll; var selector = { left: left, right: right, up: up, down: down, selectAll: selectAll }; exports.selector = selector; var getSelectedData = function getSelectedData(_ref, _ref2) { var data = _ref.data, headers = _ref.headers; var x1 = _ref2.x1, y1 = _ref2.y1, x2 = _ref2.x2, y2 = _ref2.y2; var rows = (0, _range2["default"])(y1, y2 + 1); var cols = (0, _range2["default"])(x1, x2 + 1); // console.log('rows', rows, 'cols', cols, 'data ', data, ' headers ', headers, { x1, y1, x2, y2 }) var getData = function getData(rowIdx) { return function (colIndex) { return (0, _utils.extractData)({ rowData: data[rowIdx], header: headers[colIndex] }); }; }; return rows.map(function (rowIdx) { return cols.map(getData(rowIdx)); }); }; exports.getSelectedData = getSelectedData; //# sourceMappingURL=selection-util.js.map