UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

614 lines (503 loc) 24.2 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"]; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"]; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2")); var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _react = _interopRequireWildcard(require("react")); var _mobx = require("mobx"); var _mobxReactLite = require("mobx-react-lite"); var _classnames = _interopRequireDefault(require("classnames")); var _omit = _interopRequireDefault(require("lodash/omit")); var _isFunction = _interopRequireDefault(require("lodash/isFunction")); var _dataset = require("choerodon-ui/dataset"); var _UnitConvertor = require("../../../lib/_util/UnitConvertor"); var _measureScrollbar = _interopRequireDefault(require("../../../lib/_util/measureScrollbar")); var _Column = require("./Column"); var _TableContext = _interopRequireDefault(require("./TableContext")); var _utils = require("./utils"); var _enum = require("./enum"); var _TableCellInner = _interopRequireDefault(require("./TableCellInner")); var _treeUtils = require("../_util/treeUtils"); var _TableGroupCellInner = _interopRequireDefault(require("./TableGroupCellInner")); var _TableStore = require("./TableStore"); var _AggregationTree = require("./AggregationTree"); var _AggregationTreeGroups = _interopRequireDefault(require("./AggregationTreeGroups")); function getRowSpan(group, tableStore) { if (tableStore.headerTableGroups.length) { var subGroups = group.subGroups, subHGroups = group.subHGroups; if (subHGroups) { return _dataset.IteratorHelper.iteratorReduce(subHGroups.values(), function (rowSpan, group) { return Math.max(rowSpan, group.records.length); }, 0); } return subGroups.reduce(function (rowSpan, subGroup) { return rowSpan + (subGroup.subGroups.length ? getRowSpan(subGroup, tableStore) : 0) + subGroup.records.length; }, 0); } return group.expandedRecords.length; } function getTdElementByTarget(target) { if (target.tagName.toLowerCase() !== "td") { return getTdElementByTarget(target.parentElement); } return target; } var AUTO_SCROLL_SENSITIVITY = 50; // 控制开始滚动的边距 var AUTO_SCROLL_SPEED = 20; // 滚动速率 var TableCell = function TableCell(props) { var _classNames, _classNames3; var columnGroup = props.columnGroup, record = props.record, isDragging = props.isDragging, provided = props.provided, isDragDisabled = props.isDragDisabled, colSpan = props.colSpan, className = props.className, children = props.children, disabled = props.disabled, groupPath = props.groupPath, rowIndex = props.rowIndex, virtualHeight = props.virtualHeight, intersectionRef = props.intersectionRef, isFixedRowHeight = props.isFixedRowHeight, isRenderCell = props.isRenderCell; var mousePosition = _react["default"].useRef(null); var dragDisabled = (0, _isFunction["default"])(isDragDisabled) ? isDragDisabled(record) : isDragDisabled; var column = columnGroup.column, key = columnGroup.key; var _useContext = (0, _react.useContext)(_TableContext["default"]), tableStore = _useContext.tableStore, prefixCls = _useContext.prefixCls, dataSet = _useContext.dataSet, expandIconAsCell = _useContext.expandIconAsCell, tableAggregation = _useContext.aggregation, rowHeight = _useContext.rowHeight; var clipboard = tableStore.clipboard, startChooseCell = tableStore.startChooseCell, endChooseCell = tableStore.endChooseCell, isFinishChooseCell = tableStore.isFinishChooseCell, currentEditorName = tableStore.currentEditorName, drawCopyBorder = tableStore.drawCopyBorder, dragColumnAlign = tableStore.dragColumnAlign, rowDraggable = tableStore.rowDraggable; var cellPrefix = "".concat(prefixCls, "-cell"); var tableColumnOnCell = tableStore.getConfig('tableColumnOnCell'); var __tableGroup = column.__tableGroup, style = column.style, lock = column.lock, onCell = column.onCell, aggregation = column.aggregation; var _ref = __tableGroup && groupPath && groupPath.find(function (_ref3) { var _ref4 = (0, _slicedToArray2["default"])(_ref3, 1), path = _ref4[0]; return path.name === __tableGroup.name; }) || [undefined, false], _ref2 = (0, _slicedToArray2["default"])(_ref, 2), group = _ref2[0], isLast = _ref2[1]; var _ref5 = group || !groupPath || !groupPath.length ? [undefined, false] : groupPath[groupPath.length - 1], _ref6 = (0, _slicedToArray2["default"])(_ref5, 1), rowGroup = _ref6[0]; (0, _react.useEffect)(function () { return function () { stopAutoScroll(); document.removeEventListener('mouseup', handleDocumentMouseUp); }; }, []); var getInnerNode = (0, _react.useCallback)(function (col, onCellStyle, inAggregation, headerGroup) { return record ? /*#__PURE__*/_react["default"].createElement(_TableCellInner["default"], { column: col, record: record, style: onCellStyle, disabled: disabled, inAggregation: inAggregation, prefixCls: cellPrefix, colSpan: colSpan, headerGroup: headerGroup, rowGroup: rowGroup }, children) : undefined; }, [record, disabled, children, cellPrefix, colSpan, rowGroup]); var isBuiltInColumn = tableStore.isBuiltInColumn(column); var columnOnCell = !isBuiltInColumn && (onCell || tableColumnOnCell); var cellExternalProps = typeof columnOnCell === 'function' && record ? columnOnCell({ dataSet: dataSet, record: record, column: column }) : {}; var handleClickCapture = (0, _react.useCallback)((0, _mobx.action)(function (e) { if (tableStore.currentEditorName && key === _TableStore.DRAG_KEY) { tableStore.blurEditor(); } if (record && !(0, _utils.isDisabledRow)(record) && e.target.dataset.selectionKey !== _TableStore.SELECTION_KEY) { dataSet.current = record; } var onClickCapture = cellExternalProps.onClickCapture; if (typeof onClickCapture === 'function') { onClickCapture(e); } }), [record, dataSet, cellExternalProps]); var handleMouseDown = (0, _react.useCallback)((0, _mobx.action)(function (event) { var target = event.target; if (key === _TableStore.DRAG_KEY || key === _TableStore.ROW_NUMBER_KEY) return; // 往上层一直找到 td 元素 var startTarget = getTdElementByTarget(target); var colIndex = tableStore.columnGroups.leafs.findIndex(function (x) { return x.column.name === key || x.column.key === key; }); if (colIndex < 0) return; if (startTarget) { var initPosition = { rowIndex: rowIndex, colIndex: colIndex, target: startTarget }; if (tableStore.startChooseCell && event.shiftKey) { tableStore.shiftKey = true; tableStore.endChooseCell = initPosition; } else { tableStore.shiftKey = false; tableStore.startChooseCell = initPosition; tableStore.endChooseCell = initPosition; } drawCopyBorder(tableStore.startChooseCell.target, startTarget); tableStore.isFinishChooseCell = false; document.addEventListener('mouseup', handleDocumentMouseUp, { once: true }); } }), [endChooseCell]); var handleDocumentMouseUp = (0, _react.useCallback)((0, _mobx.action)(function () { tableStore.isFinishChooseCell = true; stopAutoScroll(); }), []); var handleMouseOver = (0, _react.useCallback)((0, _mobx.action)(function (event) { if (rowDraggable && !dragColumnAlign || key === _TableStore.DRAG_KEY || key === _TableStore.ROW_NUMBER_KEY) return; if (startChooseCell && !isFinishChooseCell && !currentEditorName) { var colIndex = tableStore.columnGroups.leafs.findIndex(function (x) { return x.column.name === key || x.column.key === key; }); if (colIndex >= 0) { var startTarget = startChooseCell.target; var endTarget = getTdElementByTarget(event.target); tableStore.endChooseCell = { colIndex: colIndex, rowIndex: rowIndex, target: endTarget }; if (endTarget) { drawCopyBorder(startTarget, endTarget); } } autoScroll(event); } }), [startChooseCell, isFinishChooseCell, currentEditorName]); var autoScroll = function autoScroll(event) { // 控制滚动条自动滚动 var node = tableStore.node, rightColumnGroups = tableStore.rightColumnGroups, leftColumnGroups = tableStore.leftColumnGroups, overflowY = tableStore.overflowY, overflowX = tableStore.overflowX, lastScrollLeft = tableStore.lastScrollLeft, lastScrollTop = tableStore.lastScrollTop; var tableContentWrap = node.tableContentWrap, tableBodyWrap = node.tableBodyWrap; var overflowWrapper = tableBodyWrap || tableContentWrap; if (!overflowWrapper) return; var tableRect = overflowWrapper.getBoundingClientRect(); var overflowYWidth = overflowY ? (0, _measureScrollbar["default"])() : 0; var height = tableRect.height, width = tableRect.width, x = tableRect.x, y = tableRect.y; var mouseX = event.clientX - x; var mouseY = event.clientY - y; mousePosition.current = { x: mouseX, y: mouseY }; var maxScrollLeft = overflowWrapper.scrollWidth - overflowWrapper.clientWidth; var maxScrollHeight = overflowWrapper.scrollHeight - overflowWrapper.clientHeight; var hasEnteredVerticalSensitivityArea = mouseY <= AUTO_SCROLL_SENSITIVITY && lastScrollTop !== 0 || lastScrollTop !== maxScrollHeight && mouseY >= height - AUTO_SCROLL_SENSITIVITY - (overflowX ? (0, _measureScrollbar["default"])('horizontal') : 0); var hasEnteredHorizontalSensitivityArea = mouseX >= leftColumnGroups.width - AUTO_SCROLL_SENSITIVITY && mouseX <= AUTO_SCROLL_SENSITIVITY + leftColumnGroups.width || maxScrollLeft !== lastScrollLeft && mouseX >= width - rightColumnGroups.width - AUTO_SCROLL_SENSITIVITY - overflowYWidth && mouseX <= width - rightColumnGroups.width + AUTO_SCROLL_SENSITIVITY - overflowYWidth; var hasEnteredSensitivityArea = hasEnteredVerticalSensitivityArea || hasEnteredHorizontalSensitivityArea; if (hasEnteredSensitivityArea) { // 鼠标在sensitivity区域,开始自动滚动 startAutoScroll(overflowWrapper); } else { // 鼠标离开sensitivity区域,停止自动滚动 stopAutoScroll(); } }; var startAutoScroll = function startAutoScroll(overflowWrapper) { if (tableStore.autoScrollRAF || !mousePosition.current) { return; } var execScroll = (0, _mobx.action)(function () { var rightColumnGroups = tableStore.rightColumnGroups, leftColumnGroups = tableStore.leftColumnGroups, overflowX = tableStore.overflowX, overflowY = tableStore.overflowY, lastScrollLeft = tableStore.lastScrollLeft, lastScrollTop = tableStore.lastScrollTop; var _mousePosition$curren = mousePosition.current, mouseX = _mousePosition$curren.x, mouseY = _mousePosition$curren.y; var _overflowWrapper$getB = overflowWrapper.getBoundingClientRect(), height = _overflowWrapper$getB.height, width = _overflowWrapper$getB.width; var deltaX = 0; var deltaY = 0; var factor = 0; var scrollVerticalWidth = overflowY ? (0, _measureScrollbar["default"])() : 0; var scrollHorizontalWidth = overflowX ? (0, _measureScrollbar["default"])('horizontal') : 0; var maxScrollLeft = overflowWrapper.scrollWidth - overflowWrapper.clientWidth; var maxScrollHeight = overflowWrapper.scrollHeight - overflowWrapper.clientHeight; if (mouseY <= AUTO_SCROLL_SENSITIVITY && overflowY && lastScrollTop !== 0) { // 向上滚动,距离越近,滚动的速度越快 factor = (AUTO_SCROLL_SENSITIVITY - mouseY) / -AUTO_SCROLL_SENSITIVITY; deltaY = AUTO_SCROLL_SPEED; } else if (mouseY >= height - scrollHorizontalWidth - AUTO_SCROLL_SENSITIVITY && overflowY && maxScrollHeight !== lastScrollTop) { // 向下滚动,距离越近,滚动的速度越快 factor = (mouseY - (height - scrollHorizontalWidth - AUTO_SCROLL_SENSITIVITY)) / AUTO_SCROLL_SENSITIVITY; deltaY = AUTO_SCROLL_SPEED; } else if (mouseX - leftColumnGroups.width <= AUTO_SCROLL_SENSITIVITY && overflowX) { // 滚动到最左边,距离越近,滚动的速度越快 factor = (AUTO_SCROLL_SENSITIVITY - mouseX + leftColumnGroups.width) / -AUTO_SCROLL_SENSITIVITY; deltaX = AUTO_SCROLL_SPEED; } else if (mouseX >= width - rightColumnGroups.width - scrollVerticalWidth - AUTO_SCROLL_SENSITIVITY && overflowX && maxScrollLeft !== lastScrollLeft) { // 滚动到最右边,距离越近,滚动的速度越快 factor = (mouseX - (width - rightColumnGroups.width - AUTO_SCROLL_SENSITIVITY - scrollVerticalWidth)) / AUTO_SCROLL_SENSITIVITY; deltaX = AUTO_SCROLL_SPEED; } if (deltaX !== 0 || deltaY !== 0) { overflowWrapper.scrollTo({ left: lastScrollLeft + deltaX * factor, top: lastScrollTop + deltaY * factor }); } tableStore.autoScrollRAF = requestAnimationFrame(execScroll); }); execScroll(); }; var stopAutoScroll = function stopAutoScroll() { if (tableStore.autoScrollRAF) { cancelAnimationFrame(tableStore.autoScrollRAF); (0, _mobx.runInAction)(function () { tableStore.autoScrollRAF = null; }); } }; var handleMouseUp = (0, _react.useCallback)((0, _mobx.action)(function () { tableStore.isFinishChooseCell = true; stopAutoScroll(); }), []); var isChoose = (0, _react.useMemo)(function () { var colIndex = tableStore.columnGroups.leafs.findIndex(function (x) { return x.column.name === key || x.column.key === key; }); if (startChooseCell && startChooseCell.colIndex === colIndex && startChooseCell.rowIndex === rowIndex) { return true; } if (endChooseCell && endChooseCell.colIndex === colIndex && endChooseCell.rowIndex === rowIndex) { return true; } if (endChooseCell && startChooseCell) { var finalStartRowIndex = startChooseCell.rowIndex; var finalStartColumnIndex = startChooseCell.colIndex; var finalEndRowIndex = endChooseCell.rowIndex; var finalEndColumnIndex = endChooseCell.colIndex; if (finalStartRowIndex > finalEndRowIndex) { finalStartRowIndex = endChooseCell.rowIndex; finalEndRowIndex = startChooseCell.rowIndex; } if (finalStartColumnIndex > finalEndColumnIndex) { finalStartColumnIndex = endChooseCell.colIndex; finalEndColumnIndex = startChooseCell.colIndex; } if (colIndex >= finalStartColumnIndex && colIndex <= finalEndColumnIndex && rowIndex >= finalStartRowIndex && rowIndex <= finalEndRowIndex) { return true; } } return false; }, [startChooseCell, endChooseCell]); var isFirstChoosed = (0, _react.useMemo)(function () { var colIndex = tableStore.columnGroups.leafs.findIndex(function (x) { return x.column.name === key || x.column.key === key; }); return startChooseCell && startChooseCell.rowIndex === rowIndex && startChooseCell.colIndex === colIndex; }, [startChooseCell]); var aggregationTreeRenderer = (0, _react.useCallback)(function (_ref7) { var colGroup = _ref7.colGroup, style = _ref7.style; return getInnerNode(colGroup.column, style, true, colGroup.headerGroup); }, [getInnerNode]); var columnLock = (0, _utils.isStickySupport)() && (0, _utils.getColumnLock)(lock); var baseStyle = function () { if (columnLock) { if (columnLock === _enum.ColumnLock.left) { return (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, style), {}, { left: (0, _UnitConvertor.pxToRem)(columnGroup.left, true) }); } if (columnLock === _enum.ColumnLock.right) { return (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, style), {}, { right: (0, _UnitConvertor.pxToRem)(colSpan && colSpan > 1 ? 0 : columnGroup.right, true) }); } } return style; }(); var baseClassName = (0, _classnames["default"])(cellPrefix, (_classNames = {}, (0, _defineProperty2["default"])(_classNames, "".concat(cellPrefix, "-fix-").concat(columnLock), columnLock), (0, _defineProperty2["default"])(_classNames, "".concat(cellPrefix, "-no-transition"), !tableStore.tableColumnResizeTransition), _classNames)); var intersectionProps = {}; if (intersectionRef) { intersectionProps.ref = intersectionRef; if (virtualHeight !== undefined) { intersectionProps.style = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, baseStyle), {}, { height: virtualHeight }); } } if (!record) { return /*#__PURE__*/_react["default"].createElement("td", (0, _extends2["default"])({ className: baseClassName, style: baseStyle, "data-index": key, colSpan: colSpan }, intersectionProps)); } var indexInGroup = group ? group.expandedRecords.indexOf(record) : -1; var groupCell = indexInGroup === 0 || tableStore.virtual && indexInGroup > -1 && tableStore.virtualStartIndex === rowIndex; if (group && !groupCell) { return null; } var getAggregationTreeGroups = function getAggregationTreeGroups($aggregation) { if ($aggregation) { var childrenInAggregation = columnGroup.childrenInAggregation; if (childrenInAggregation) { var groups = childrenInAggregation.columns; return (0, _AggregationTree.groupedAggregationTree)({ rowGroup: __tableGroup ? group : rowGroup, headerGroup: columnGroup.headerGroup, record: record, groups: groups, column: column, renderer: aggregationTreeRenderer }); } } }; var groupRowSpan = groupCell && group ? getRowSpan(group, tableStore) - indexInGroup : undefined; var rowSpan = groupRowSpan === 1 ? undefined : groupRowSpan; var renderInnerNode = function renderInnerNode($aggregation, onCellStyle) { if (expandIconAsCell && children) { return /*#__PURE__*/_react["default"].createElement("span", { className: (0, _classnames["default"])("".concat(cellPrefix, "-inner"), (0, _defineProperty2["default"])({}, "".concat(cellPrefix, "-inner-row-height-fixed"), rowHeight !== 'auto')), style: (0, _objectSpread2["default"])({ textAlign: 'center' }, onCellStyle) }, children); } var aggregationList = getAggregationTreeGroups($aggregation); if (groupCell && group && __tableGroup) { return /*#__PURE__*/_react["default"].createElement(_TableGroupCellInner["default"], { rowSpan: rowSpan, group: group, column: column, isFixedRowHeight: isFixedRowHeight }, aggregationList); } if (aggregationList) { var _column$renderer = column.renderer, renderer = _column$renderer === void 0 ? _Column.defaultAggregationRenderer : _column$renderer; var treeGroups = /*#__PURE__*/_react["default"].createElement(_AggregationTreeGroups["default"], { trees: aggregationList }); return /*#__PURE__*/_react["default"].createElement("span", { className: "".concat(cellPrefix, "-inner") }, renderer({ text: treeGroups, record: record, dataSet: dataSet, aggregation: tableAggregation, headerGroup: columnGroup.headerGroup, rowGroup: rowGroup, aggregationTree: aggregationList })); } return getInnerNode(column, onCellStyle, undefined, columnGroup.headerGroup); }; var scope = groupCell ? 'row' : undefined; var TCell = scope ? 'th' : 'td'; if (!isRenderCell) { var hasEditor = aggregation ? (0, _treeUtils.treeSome)(column.children || [], function (node) { return !!(0, _utils.getEditorByColumnAndRecord)(node, record); }) : !!(0, _utils.getEditorByColumnAndRecord)(column, record); var emptyCellProps = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({ colSpan: colSpan, rowSpan: rowSpan, 'data-index': key }, cellExternalProps), {}, { className: (0, _classnames["default"])(baseClassName, cellExternalProps.className), scope: scope, onClickCapture: handleClickCapture }); if (hasEditor) { emptyCellProps.onFocus = function (e) { tableStore.activeEmptyCell = e.target; }; emptyCellProps.onBlur = function () { delete tableStore.activeEmptyCell; }; emptyCellProps.tabIndex = 0; } return /*#__PURE__*/_react["default"].createElement(TCell, (0, _extends2["default"])({}, emptyCellProps, intersectionProps, { style: (0, _objectSpread2["default"])((0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, baseStyle), cellExternalProps.style), intersectionProps.style) })); } var cellStyle = (0, _objectSpread2["default"])((0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, baseStyle), cellExternalProps.style), provided && { cursor: dragDisabled ? 'not-allowed' : 'move' }); var classString = (0, _classnames["default"])(baseClassName, (_classNames3 = {}, (0, _defineProperty2["default"])(_classNames3, "".concat(cellPrefix, "-aggregation"), aggregation), (0, _defineProperty2["default"])(_classNames3, "".concat(cellPrefix, "-last-group"), groupCell && isLast), (0, _defineProperty2["default"])(_classNames3, "".concat(cellPrefix, "-choosed"), clipboard && clipboard.copy && isChoose), (0, _defineProperty2["default"])(_classNames3, "".concat(cellPrefix, "-first-choosed"), clipboard && clipboard.copy && isFirstChoosed), _classNames3), column.className, className, cellExternalProps.className); var widthDraggingStyle = function widthDraggingStyle() { var draggingStyle = {}; if (isDragging) { var dom = tableStore.node.element.querySelector(".".concat(prefixCls, "-tbody .").concat(prefixCls, "-cell[data-index=\"").concat(key, "\"]")); if (dom) { draggingStyle.width = dom.clientWidth; draggingStyle.whiteSpace = 'nowrap'; } } return draggingStyle; }; // 只有全局属性时候的样式可以继承给下级满足对td的样式能够一致表现 var onCellStyle = !isBuiltInColumn && tableColumnOnCell === columnOnCell && typeof tableColumnOnCell === 'function' ? (0, _omit["default"])(cellExternalProps.style, ['width', 'height']) : undefined; var clipboardCopyEvents = {}; if (clipboard && clipboard.copy) { clipboardCopyEvents = { onMouseDown: handleMouseDown, onMouseOver: handleMouseOver, onMouseUp: handleMouseUp }; } return /*#__PURE__*/_react["default"].createElement(TCell, (0, _extends2["default"])({ colSpan: colSpan, rowSpan: rowSpan }, cellExternalProps, { className: classString, "data-index": key }, provided && provided.dragHandleProps, intersectionProps, { style: (0, _objectSpread2["default"])((0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, (0, _omit["default"])(cellStyle, ['width', 'height'])), widthDraggingStyle()), intersectionProps.style), scope: scope, onClickCapture: handleClickCapture }, clipboardCopyEvents), renderInnerNode(aggregation, onCellStyle)); }; TableCell.displayName = 'TableCell'; var _default = (0, _mobxReactLite.observer)(TableCell); exports["default"] = _default; //# sourceMappingURL=TableCell.js.map