You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 lines
15 KiB

{"ast":null,"code":"import { getCurrentInstance, shallowRef, ref, computed, unref } from 'vue';\nimport { debounce } from 'lodash-unified';\nimport { FixedDir } from '../constants.mjs';\nimport { isNumber } from '../../../../utils/types.mjs';\nconst useRow = (props, {\n mainTableRef,\n leftTableRef,\n rightTableRef,\n tableInstance,\n ns,\n isScrolling\n}) => {\n const vm = getCurrentInstance();\n const {\n emit\n } = vm;\n const isResetting = shallowRef(false);\n const expandedRowKeys = ref(props.defaultExpandedRowKeys || []);\n const lastRenderedRowIndex = ref(-1);\n const resetIndex = shallowRef(null);\n const rowHeights = ref({});\n const pendingRowHeights = ref({});\n const leftTableHeights = shallowRef({});\n const mainTableHeights = shallowRef({});\n const rightTableHeights = shallowRef({});\n const isDynamic = computed(() => isNumber(props.estimatedRowHeight));\n function onRowsRendered(params) {\n var _a;\n (_a = props.onRowsRendered) == null ? void 0 : _a.call(props, params);\n if (params.rowCacheEnd > unref(lastRenderedRowIndex)) {\n lastRenderedRowIndex.value = params.rowCacheEnd;\n }\n }\n function onRowHovered({\n hovered,\n rowKey\n }) {\n if (isScrolling.value) {\n return;\n }\n const tableRoot = tableInstance.vnode.el;\n const rows = tableRoot.querySelectorAll(`[rowkey=\"${String(rowKey)}\"]`);\n rows.forEach(row => {\n if (hovered) {\n row.classList.add(ns.is(\"hovered\"));\n } else {\n row.classList.remove(ns.is(\"hovered\"));\n }\n });\n }\n function onRowExpanded({\n expanded,\n rowData,\n rowIndex,\n rowKey\n }) {\n var _a, _b;\n const _expandedRowKeys = [...unref(expandedRowKeys)];\n const currentKeyIndex = _expandedRowKeys.indexOf(rowKey);\n if (expanded) {\n if (currentKeyIndex === -1) _expandedRowKeys.push(rowKey);\n } else {\n if (currentKeyIndex > -1) _expandedRowKeys.splice(currentKeyIndex, 1);\n }\n expandedRowKeys.value = _expandedRowKeys;\n emit(\"update:expandedRowKeys\", _expandedRowKeys);\n (_a = props.onRowExpand) == null ? void 0 : _a.call(props, {\n expanded,\n rowData,\n rowIndex,\n rowKey\n });\n (_b = props.onExpandedRowsChange) == null ? void 0 : _b.call(props, _expandedRowKeys);\n }\n const flushingRowHeights = debounce(() => {\n var _a, _b, _c, _d;\n isResetting.value = true;\n rowHeights.value = {\n ...unref(rowHeights),\n ...unref(pendingRowHeights)\n };\n resetAfterIndex(unref(resetIndex), false);\n pendingRowHeights.value = {};\n resetIndex.value = null;\n (_a = mainTableRef.value) == null ? void 0 : _a.forceUpdate();\n (_b = leftTableRef.value) == null ? void 0 : _b.forceUpdate();\n (_c = rightTableRef.value) == null ? void 0 : _c.forceUpdate();\n (_d = vm.proxy) == null ? void 0 : _d.$forceUpdate();\n isResetting.value = false;\n }, 0);\n function resetAfterIndex(index, forceUpdate = false) {\n if (!unref(isDynamic)) return;\n [mainTableRef, leftTableRef, rightTableRef].forEach(tableRef => {\n const table = unref(tableRef);\n if (table) table.resetAfterRowIndex(index, forceUpdate);\n });\n }\n function resetHeights(rowKey, height, rowIdx) {\n const resetIdx = unref(resetIndex);\n if (resetIdx === null) {\n resetIndex.value = rowIdx;\n } else {\n if (resetIdx > rowIdx) {\n resetIndex.value = rowIdx;\n }\n }\n pendingRowHeights.value[rowKey] = height;\n }\n function onRowHeightChange({\n rowKey,\n height,\n rowIndex\n }, fixedDir) {\n if (!fixedDir) {\n mainTableHeights.value[rowKey] = height;\n } else {\n if (fixedDir === FixedDir.RIGHT) {\n rightTableHeights.value[rowKey] = height;\n } else {\n leftTableHeights.value[rowKey] = height;\n }\n }\n const maximumHeight = Math.max(...[leftTableHeights, rightTableHeights, mainTableHeights].map(records => records.value[rowKey] || 0));\n if (unref(rowHeights)[rowKey] !== maximumHeight) {\n resetHeights(rowKey, maximumHeight, rowIndex);\n flushingRowHeights();\n }\n }\n return {\n expandedRowKeys,\n lastRenderedRowIndex,\n isDynamic,\n isResetting,\n rowHeights,\n resetAfterIndex,\n onRowExpanded,\n onRowHovered,\n onRowsRendered,\n onRowHeightChange\n };\n};\nexport { useRow };","map":{"version":3,"names":["useRow","props","mainTableRef","leftTableRef","rightTableRef","tableInstance","ns","isScrolling","vm","getCurrentInstance","emit","isResetting","shallowRef","expandedRowKeys","ref","defaultExpandedRowKeys","lastRenderedRowIndex","resetIndex","rowHeights","pendingRowHeights","leftTableHeights","mainTableHeights","rightTableHeights","isDynamic","computed","isNumber","estimatedRowHeight","onRowsRendered","params","_a","call","rowCacheEnd","unref","value","onRowHovered","hovered","rowKey","tableRoot","vnode","el","rows","querySelectorAll","String","forEach","row","classList","add","is","remove","onRowExpanded","expanded","rowData","rowIndex","_b","_expandedRowKeys","currentKeyIndex","indexOf","push","splice","onRowExpand","onExpandedRowsChange","flushingRowHeights","debounce","_c","_d","resetAfterIndex","forceUpdate","proxy","$forceUpdate","index","tableRef","table","resetAfterRowIndex","resetHeights","height","rowIdx","resetIdx","onRowHeightChange","fixedDir","FixedDir","RIGHT","maximumHeight","Math","max","map","records"],"sources":["../../../../../../../packages/components/table-v2/src/composables/use-row.ts"],"sourcesContent":["import { computed, getCurrentInstance, ref, shallowRef, unref } from 'vue'\nimport { debounce } from 'lodash-unified'\nimport { isNumber } from '@element-plus/utils'\nimport { FixedDir } from '../constants'\n\nimport type { ComponentInternalInstance, Ref, ShallowRef } from 'vue'\nimport type { TableV2Props } from '../table'\nimport type {\n RowExpandParams,\n RowHeightChangedParams,\n RowHoverParams,\n} from '../row'\nimport type { FixedDirection, KeyType } from '../types'\nimport type { onRowRenderedParams } from '../grid'\nimport type { TableGridInstance } from '../table-grid'\nimport type { UseNamespaceReturn } from '@element-plus/hooks'\n\ntype Heights = Record<KeyType, number>\ntype GridInstanceRef = Ref<TableGridInstance | undefined>\n\ntype UseRowProps = {\n mainTableRef: GridInstanceRef\n leftTableRef: GridInstanceRef\n rightTableRef: GridInstanceRef\n tableInstance: ComponentInternalInstance\n ns: UseNamespaceReturn\n isScrolling: ShallowRef<boolean>\n}\n\nexport const useRow = (\n props: TableV2Props,\n {\n mainTableRef,\n leftTableRef,\n rightTableRef,\n tableInstance,\n ns,\n isScrolling,\n }: UseRowProps\n) => {\n const vm = getCurrentInstance()!\n const { emit } = vm\n const isResetting = shallowRef(false)\n const expandedRowKeys = ref<KeyType[]>(props.defaultExpandedRowKeys || [])\n const lastRenderedRowIndex = ref(-1)\n const resetIndex = shallowRef<number | null>(null)\n const rowHeights = ref<Heights>({})\n const pendingRowHeights = ref<Heights>({})\n const leftTableHeights = shallowRef<Heights>({})\n const mainTableHeights = shallowRef<Heights>({})\n const rightTableHeights = shallowRef<Heights>({})\n const isDynamic = computed(() => isNumber(props.estimatedRowHeight))\n\n function onRowsRendered(params: onRowRenderedParams) {\n props.onRowsRendered?.(params)\n\n if (params.rowCacheEnd > unref(lastRenderedRowIndex)) {\n lastRenderedRowIndex.value = params.rowCacheEnd\n }\n }\n\n function onRowHovered({ hovered, rowKey }: RowHoverParams) {\n if (isScrolling.value) {\n return\n }\n const tableRoot = tableInstance!.vnode.el as HTMLElement\n const rows = tableRoot.querySelectorAll(`[rowkey=\"${String(rowKey)}\"]`)\n rows.forEach((row) => {\n if (hovered) {\n row.classList.add(ns.is('hovered'))\n } else {\n row.classList.remove(ns.is('hovered'))\n }\n })\n }\n\n function onRowExpanded({\n expanded,\n rowData,\n rowIndex,\n rowKey,\n }: RowExpandParams) {\n const _expandedRowKeys = [...unref(expandedRowKeys)]\n const currentKeyIndex = _expandedRowKeys.indexOf(rowKey)\n if (expanded) {\n if (currentKeyIndex === -1) _expandedRowKeys.push(rowKey)\n } else {\n if (currentKeyIndex > -1) _expandedRowKeys.splice(currentKeyIndex, 1)\n }\n expandedRowKeys.value = _expandedRowKeys\n\n emit('update:expandedRowKeys', _expandedRowKeys)\n props.onRowExpand?.({\n expanded,\n rowData,\n rowIndex,\n rowKey,\n })\n // If this is not controlled, then use this to notify changes\n props.onExpandedRowsChange?.(_expandedRowKeys)\n }\n\n const flushingRowHeights = debounce(() => {\n isResetting.value = true\n rowHeights.value = { ...unref(rowHeights), ...unref(pendingRowHeights) }\n resetAfterIndex(unref(resetIndex)!, false)\n pendingRowHeights.value = {}\n // force update\n resetIndex.value = null\n mainTableRef.value?.forceUpdate()\n leftTableRef.value?.forceUpdate()\n rightTableRef.value?.forceUpdate()\n vm.proxy?.$forceUpdate()\n isResetting.value = false\n }, 0)\n\n function resetAfterIndex(index: number, forceUpdate = false) {\n if (!unref(isDynamic)) return\n ;[mainTableRef, leftTableRef, rightTableRef].forEach((tableRef) => {\n const table = unref(tableRef)\n if (table) table.resetAfterRowIndex(index, forceUpdate)\n })\n }\n\n function resetHeights(rowKey: KeyType, height: number, rowIdx: number) {\n const resetIdx = unref(resetIndex)\n if (resetIdx === null) {\n resetIndex.value = rowIdx\n } else {\n if (resetIdx > rowIdx) {\n resetIndex.value = rowIdx\n }\n }\n\n pendingRowHeights.value[rowKey] = height\n }\n\n function onRowHeightChange(\n { rowKey, height, rowIndex }: RowHeightChangedParams,\n fixedDir: FixedDirection\n ) {\n if (!fixedDir) {\n mainTableHeights.value[rowKey] = height\n } else {\n if (fixedDir === FixedDir.RIGHT) {\n rightTableHeights.value[rowKey] = height\n } else {\n leftTableHeights.value[rowKey] = height\n }\n }\n\n const maximumHeight = Math.max(\n ...[leftTableHeights, rightTableHeights, mainTableHeights].map(\n (records) => records.value[rowKey] || 0\n )\n )\n\n if (unref(rowHeights)[rowKey] !== maximumHeight) {\n resetHeights(rowKey, maximumHeight, rowIndex)\n flushingRowHeights()\n }\n }\n\n return {\n expandedRowKeys,\n lastRenderedRowIndex,\n isDynamic,\n isResetting,\n rowHeights,\n\n resetAfterIndex,\n onRowExpanded,\n onRowHovered,\n onRowsRendered,\n onRowHeightChange,\n }\n}\n\nexport type UseRowReturn = ReturnType<typeof useRow>\n"],"mappings":";;;;AAIY,MAACA,MAAM,GAAGA,CAACC,KAAK,EAAE;EAC5BC,YAAY;EACZC,YAAY;EACZC,aAAa;EACbC,aAAa;EACbC,EAAE;EACFC;AACF,CAA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