@integec/grid-tools
Version:
Integ Grid Tools
139 lines (107 loc) • 4.14 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getSelectedData = exports.selector = exports.down = exports.up = exports.right = exports.left = exports.isRowSelected = exports.isCellSelected = exports.normalizeSelection = undefined;
var _range = require('ramda/src/range');
var _range2 = _interopRequireDefault(_range);
var _isNil = require('ramda/src/isNil');
var _isNil2 = _interopRequireDefault(_isNil);
var _utils = require('./utils');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var empty = {};
var normalizeSelection = exports.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 };
};
var isCellSelected = exports.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;
};
var isRowSelected = exports.isRowSelected = function isRowSelected(rowIndex, selection) {
var _normalizeSelection2 = normalizeSelection(selection),
y1 = _normalizeSelection2.y1,
y2 = _normalizeSelection2.y2;
return rowIndex <= y2 && rowIndex >= y1;
};
var left = exports.left = function left(selection, expand) {
var x1 = selection.x1,
y1 = selection.y1,
x2 = selection.x2,
y2 = selection.y2;
if (!expand) {
var _x = Math.max(x1 - 1, 0);
return { x1: _x, x2: _x, y1: y1, y2: y1 };
}
var x = Math.max(x2 - 1, 0);
return { x1: x1, x2: x, y1: y1, y2: y2 };
};
var right = exports.right = function right(selection, expand, colCount) {
var x1 = selection.x1,
y1 = selection.y1,
x2 = selection.x2,
y2 = selection.y2;
if (!expand) {
var _x2 = Math.min(x1 + 1, colCount - 1);
return { x1: _x2, x2: _x2, y1: y1, y2: y1 };
}
var x = Math.min(x2 + 1, colCount - 1);
return { x1: x1, x2: x, y1: y1, y2: y2 };
};
var up = exports.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 };
};
var down = exports.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 };
};
var selector = exports.selector = { left: left, right: right, up: up, down: down };
var getSelectedData = exports.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)
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));
});
};
//# sourceMappingURL=selection-util.js.map