UNPKG

@blueprintjs/table

Version:

Scalable interactive table component

224 lines 10.8 kB
"use strict"; /* * Copyright 2017 Palantir Technologies, Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.areFocusedRegionsEqual = exports.expandFocusedRegion = exports.toFocusedRegion = exports.getFocusedColumn = exports.isFocusAtRegionRight = exports.isFocusAtRegionLeft = exports.isFocusAtRegionBottom = exports.isFocusAtRegionTop = exports.validateFocusedRegion = exports.getInitialFocusedRegion = exports.getFocusedOrLastSelectedIndex = exports.getFocusedCellFromCoordinates = exports.getFocusedRegionFromProps = exports.getFocusModeFromProps = void 0; var tslib_1 = require("tslib"); var regions_1 = require("../../regions"); var cellTypes_1 = require("../cellTypes"); var Errors = tslib_1.__importStar(require("../errors")); /** * Returns the inferred focus mode from the table props. This prefers the new focus mode API, falling back to the * deprecated enableFocusedCell API if that is not provided. */ function getFocusModeFromProps(props) { // eslint-disable-next-line @typescript-eslint/no-deprecated var enableFocusedCell = props.enableFocusedCell, focusMode = props.focusMode; return focusMode !== null && focusMode !== void 0 ? focusMode : getFocusModeFromEnabled(enableFocusedCell); } exports.getFocusModeFromProps = getFocusModeFromProps; function getFocusModeFromEnabled(enableFocusedCell) { if (enableFocusedCell === void 0) { enableFocusedCell = false; } return enableFocusedCell ? cellTypes_1.FocusMode.CELL : undefined; } /** * Returns the inferred focused region from the table props. This prefers the new focus mode API, falling back to the * deprecated API if a focused region is not provided. */ function getFocusedRegionFromProps(props) { // eslint-disable-next-line @typescript-eslint/no-deprecated var focusedRegion = props.focusedRegion, focusedCell = props.focusedCell; return focusedRegion !== null && focusedRegion !== void 0 ? focusedRegion : getFocusedCellFromCoordinates(focusedCell); } exports.getFocusedRegionFromProps = getFocusedRegionFromProps; function getFocusedCellFromCoordinates(focusedCell) { return focusedCell != null ? tslib_1.__assign({ type: cellTypes_1.FocusMode.CELL }, focusedCell) : undefined; } exports.getFocusedCellFromCoordinates = getFocusedCellFromCoordinates; /** * Returns the `focusedSelectionIndex` if both the focused region and that * property are defined, or the last index of `selectedRegions` otherwise. If * `selectedRegions` is empty, the function always returns `undefined`. */ function getFocusedOrLastSelectedIndex(selectedRegions, focusedRegion) { if (selectedRegions.length === 0) { return undefined; } else if (focusedRegion != null) { return focusedRegion.focusSelectionIndex; } else { return selectedRegions.length - 1; } } exports.getFocusedOrLastSelectedIndex = getFocusedOrLastSelectedIndex; /** * Returns the proper focused region for the given set of initial conditions. */ function getInitialFocusedRegion(focusMode, focusedRegionFromProps, focusedRegionFromState, selectedRegions) { return validateFocusedRegion(focusMode, getInitialFocusedCell(focusedRegionFromProps, focusedRegionFromState, selectedRegions)); } exports.getInitialFocusedRegion = getInitialFocusedRegion; function getInitialFocusedCell(focusedRegionFromProps, focusedRegionFromState, selectedRegions) { var _a; return (_a = focusedRegionFromProps !== null && focusedRegionFromProps !== void 0 ? focusedRegionFromProps : focusedRegionFromState) !== null && _a !== void 0 ? _a : getInitialFocusedCellFromSelection(selectedRegions); } function getInitialFocusedCellFromSelection(selectedRegions) { if (selectedRegions.length === 0) { return { col: 0, focusSelectionIndex: 0, row: 0, type: cellTypes_1.FocusMode.CELL }; } var lastIndex = selectedRegions.length - 1; // focus the top-left cell of the last selection return tslib_1.__assign(tslib_1.__assign({}, regions_1.Regions.getFocusCellCoordinatesFromRegion(selectedRegions[lastIndex])), { focusSelectionIndex: lastIndex, type: cellTypes_1.FocusMode.CELL }); } /** * Returns a focused region that matches the given focus mode if possible. If such a conversion is not possible, * returns undefined instead. */ function validateFocusedRegion(focusMode, focusedRegion) { if (focusMode == null) { return undefined; } if (focusedRegion.type === focusMode) { return focusedRegion; } if (focusedRegion.type === cellTypes_1.FocusMode.CELL && focusMode === cellTypes_1.FocusMode.ROW) { return { focusSelectionIndex: focusedRegion.focusSelectionIndex, row: focusedRegion.row, type: focusMode, }; } if (focusedRegion.type === cellTypes_1.FocusMode.ROW && focusMode === cellTypes_1.FocusMode.CELL) { return { col: 0, focusSelectionIndex: focusedRegion.focusSelectionIndex, row: focusedRegion.row, type: focusMode, }; } return undefined; } exports.validateFocusedRegion = validateFocusedRegion; /** * Returns `true` if the focused region is located along the top boundary of the * provided region, or `false` otherwise. */ function isFocusAtRegionTop(region, focusedRegion) { return region.rows != null && focusedRegion.row === region.rows[0]; } exports.isFocusAtRegionTop = isFocusAtRegionTop; /** * Returns `true` if the focused region is located along the bottom boundary of * the provided region, or `false` otherwise. */ function isFocusAtRegionBottom(region, focusedRegion) { return region.rows != null && focusedRegion.row === region.rows[1]; } exports.isFocusAtRegionBottom = isFocusAtRegionBottom; /** * Returns `true` if the focused region is located along the left boundary of the * provided region, or `false` otherwise. */ function isFocusAtRegionLeft(region, focusedRegion) { return region.cols != null && getFocusedColumn(focusedRegion) === region.cols[0]; } exports.isFocusAtRegionLeft = isFocusAtRegionLeft; /** * Returns `true` if the focused region is located along the right boundary of the * provided region, or `false` otherwise. */ function isFocusAtRegionRight(region, focusedRegion) { return region.cols != null && getFocusedColumn(focusedRegion) === region.cols[1]; } exports.isFocusAtRegionRight = isFocusAtRegionRight; /** * Returns the column associated with this region, if there is one. */ function getFocusedColumn(focusedRegion) { switch (focusedRegion.type) { case cellTypes_1.FocusMode.CELL: return focusedRegion.col; case cellTypes_1.FocusMode.ROW: return undefined; } } exports.getFocusedColumn = getFocusedColumn; function toFocusedRegion(focusMode, cellCoords, focusSelectionIndex) { if (focusSelectionIndex === void 0) { focusSelectionIndex = 0; } switch (focusMode) { case cellTypes_1.FocusMode.CELL: return tslib_1.__assign(tslib_1.__assign({ type: cellTypes_1.FocusMode.CELL }, cellCoords), { focusSelectionIndex: focusSelectionIndex }); case cellTypes_1.FocusMode.ROW: return { focusSelectionIndex: focusSelectionIndex, row: cellCoords.row, type: cellTypes_1.FocusMode.ROW }; case undefined: return undefined; } } exports.toFocusedRegion = toFocusedRegion; /** * Expands an existing region to new region based on the current focused region. * The focused region is an invariant and should not move as a result of this * operation. This function is used, for instance, to expand a selected region * on shift+click. */ function expandFocusedRegion(focusedRegion, newRegion) { switch (regions_1.Regions.getRegionCardinality(newRegion)) { case regions_1.RegionCardinality.FULL_COLUMNS: { var _a = getExpandedRegionIndices(focusedRegion, newRegion, "col", "cols"), indexStart = _a[0], indexEnd = _a[1]; return regions_1.Regions.column(indexStart, indexEnd); } case regions_1.RegionCardinality.FULL_ROWS: { var _b = getExpandedRegionIndices(focusedRegion, newRegion, "row", "rows"), indexStart = _b[0], indexEnd = _b[1]; return regions_1.Regions.row(indexStart, indexEnd); } case regions_1.RegionCardinality.CELLS: var _c = getExpandedRegionIndices(focusedRegion, newRegion, "row", "rows"), rowIndexStart = _c[0], rowIndexEnd = _c[1]; var _d = getExpandedRegionIndices(focusedRegion, newRegion, "col", "cols"), colIndexStart = _d[0], colIndexEnd = _d[1]; return regions_1.Regions.cell(rowIndexStart, colIndexStart, rowIndexEnd, colIndexEnd); default: // i.e. `case RegionCardinality.FULL_TABLE:` return regions_1.Regions.table(); } } exports.expandFocusedRegion = expandFocusedRegion; function getExpandedRegionIndices(focusedRegion, newRegion, focusedCellDimension, regionDimension) { var _a; var sourceIndex = focusedCellDimension === "row" ? focusedRegion.row : (_a = getFocusedColumn(focusedRegion)) !== null && _a !== void 0 ? _a : 0; // THIS IS QUESTIONABLE AT BEST var _b = newRegion[regionDimension], destinationIndex = _b[0], destinationIndexEnd = _b[1]; if (destinationIndex !== destinationIndexEnd) { if (regionDimension === "rows") { throw new Error(Errors.TABLE_EXPAND_FOCUSED_REGION_MULTI_ROW_REGION); } else if (regionDimension === "cols") { throw new Error(Errors.TABLE_EXPAND_FOCUSED_REGION_MULTI_COLUMN_REGION); } } return sourceIndex <= destinationIndex ? [sourceIndex, destinationIndex] : [destinationIndex, sourceIndex]; } function areFocusedRegionsEqual(left, right) { if (left.type === cellTypes_1.FocusMode.CELL && right.type === cellTypes_1.FocusMode.CELL) { return left.row === right.row && left.col === right.col; } else if (left.type === cellTypes_1.FocusMode.ROW && right.type === cellTypes_1.FocusMode.ROW) { return left.row === right.row; } else { return false; } } exports.areFocusedRegionsEqual = areFocusedRegionsEqual; //# sourceMappingURL=focusedCellUtils.js.map