@blueprintjs/table
Version:
Scalable interactive table component
207 lines • 9.23 kB
JavaScript
/*
* 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.
*/
import { __assign } from "tslib";
import { RegionCardinality, Regions } from "../../regions";
import { FocusMode, } from "../cellTypes";
import * as Errors from "../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.
*/
export 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);
}
function getFocusModeFromEnabled(enableFocusedCell) {
if (enableFocusedCell === void 0) { enableFocusedCell = false; }
return enableFocusedCell ? 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.
*/
export 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);
}
export function getFocusedCellFromCoordinates(focusedCell) {
return focusedCell != null ? __assign({ type: FocusMode.CELL }, focusedCell) : undefined;
}
/**
* 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`.
*/
export function getFocusedOrLastSelectedIndex(selectedRegions, focusedRegion) {
if (selectedRegions.length === 0) {
return undefined;
}
else if (focusedRegion != null) {
return focusedRegion.focusSelectionIndex;
}
else {
return selectedRegions.length - 1;
}
}
/**
* Returns the proper focused region for the given set of initial conditions.
*/
export function getInitialFocusedRegion(focusMode, focusedRegionFromProps, focusedRegionFromState, selectedRegions) {
return validateFocusedRegion(focusMode, getInitialFocusedCell(focusedRegionFromProps, focusedRegionFromState, selectedRegions));
}
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: FocusMode.CELL };
}
var lastIndex = selectedRegions.length - 1;
// focus the top-left cell of the last selection
return __assign(__assign({}, Regions.getFocusCellCoordinatesFromRegion(selectedRegions[lastIndex])), { focusSelectionIndex: lastIndex, type: FocusMode.CELL });
}
/**
* Returns a focused region that matches the given focus mode if possible. If such a conversion is not possible,
* returns undefined instead.
*/
export function validateFocusedRegion(focusMode, focusedRegion) {
if (focusMode == null) {
return undefined;
}
if (focusedRegion.type === focusMode) {
return focusedRegion;
}
if (focusedRegion.type === FocusMode.CELL && focusMode === FocusMode.ROW) {
return {
focusSelectionIndex: focusedRegion.focusSelectionIndex,
row: focusedRegion.row,
type: focusMode,
};
}
if (focusedRegion.type === FocusMode.ROW && focusMode === FocusMode.CELL) {
return {
col: 0,
focusSelectionIndex: focusedRegion.focusSelectionIndex,
row: focusedRegion.row,
type: focusMode,
};
}
return undefined;
}
/**
* Returns `true` if the focused region is located along the top boundary of the
* provided region, or `false` otherwise.
*/
export function isFocusAtRegionTop(region, focusedRegion) {
return region.rows != null && focusedRegion.row === region.rows[0];
}
/**
* Returns `true` if the focused region is located along the bottom boundary of
* the provided region, or `false` otherwise.
*/
export function isFocusAtRegionBottom(region, focusedRegion) {
return region.rows != null && focusedRegion.row === region.rows[1];
}
/**
* Returns `true` if the focused region is located along the left boundary of the
* provided region, or `false` otherwise.
*/
export function isFocusAtRegionLeft(region, focusedRegion) {
return region.cols != null && getFocusedColumn(focusedRegion) === region.cols[0];
}
/**
* Returns `true` if the focused region is located along the right boundary of the
* provided region, or `false` otherwise.
*/
export function isFocusAtRegionRight(region, focusedRegion) {
return region.cols != null && getFocusedColumn(focusedRegion) === region.cols[1];
}
/**
* Returns the column associated with this region, if there is one.
*/
export function getFocusedColumn(focusedRegion) {
switch (focusedRegion.type) {
case FocusMode.CELL:
return focusedRegion.col;
case FocusMode.ROW:
return undefined;
}
}
export function toFocusedRegion(focusMode, cellCoords, focusSelectionIndex) {
if (focusSelectionIndex === void 0) { focusSelectionIndex = 0; }
switch (focusMode) {
case FocusMode.CELL:
return __assign(__assign({ type: FocusMode.CELL }, cellCoords), { focusSelectionIndex: focusSelectionIndex });
case FocusMode.ROW:
return { focusSelectionIndex: focusSelectionIndex, row: cellCoords.row, type: FocusMode.ROW };
case undefined:
return undefined;
}
}
/**
* 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.
*/
export function expandFocusedRegion(focusedRegion, newRegion) {
switch (Regions.getRegionCardinality(newRegion)) {
case RegionCardinality.FULL_COLUMNS: {
var _a = getExpandedRegionIndices(focusedRegion, newRegion, "col", "cols"), indexStart = _a[0], indexEnd = _a[1];
return Regions.column(indexStart, indexEnd);
}
case RegionCardinality.FULL_ROWS: {
var _b = getExpandedRegionIndices(focusedRegion, newRegion, "row", "rows"), indexStart = _b[0], indexEnd = _b[1];
return Regions.row(indexStart, indexEnd);
}
case 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.cell(rowIndexStart, colIndexStart, rowIndexEnd, colIndexEnd);
default:
// i.e. `case RegionCardinality.FULL_TABLE:`
return Regions.table();
}
}
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];
}
export function areFocusedRegionsEqual(left, right) {
if (left.type === FocusMode.CELL && right.type === FocusMode.CELL) {
return left.row === right.row && left.col === right.col;
}
else if (left.type === FocusMode.ROW && right.type === FocusMode.ROW) {
return left.row === right.row;
}
else {
return false;
}
}
//# sourceMappingURL=focusedCellUtils.js.map