import { isDef } from '../../utils' ; import { TableColumnProps , TableColumnWidthType , TableExpandOptionsType } from './types' ; const defaultMinWidth = 0 ; export function transformWidth ( theWidth ) { return typeof theWidth === 'number' ? theWidth : parseFloat ( theWidth ! ) ; } export function computeColumnWidth ( mergedColumns , totalWidth ) { const TableContainerWidth = totalWidth ; const TableColumnsTotalWidth = mergedColumns . reduce ( ( acc , column ) => acc + ( isDef ( column . width ) || isDef ( column . minWidth ) ? Math . max ( isDef ( column . width ) ? transformWidth ( column . width ! ) : 0 , isDef ( column . minWidth ) ? transformWidth ( column . minWidth ! ) : 0 ) : defaultMinWidth ) , 0 ) ; const needResize = TableContainerWidth - TableColumnsTotalWidth > 0 ; if ( ! needResize ) { return mergedColumns . reduce ( ( pre , column ) => { return Object . assign ( { } , pre , { [ column . dataProp ] : isDef ( column . width ) || isDef ( column . minWidth ) ? Math . max ( isDef ( column . width ) ? transformWidth ( column . width ! ) : 0 , isDef ( column . minWidth ) ? transformWidth ( column . minWidth ! ) : 0 ) : defaultMinWidth , } ) ; } , { } ) ; } const columnWidthDic = { } ; let columnTotalFixedWidth = 0 ; mergedColumns . forEach ( column => { if ( isDef ( column . width ) ) { columnWidthDic [ column . dataProp ] = transformWidth ( column . width ! ) ; columnTotalFixedWidth += columnWidthDic [ column . dataProp ] ; } } ) ; const unFixedColumns = mergedColumns . filter ( column => ! isDef ( column . width ) ) ; let remainWidth = TableContainerWidth - columnTotalFixedWidth ; let satifyAllCondition = false ; while ( unFixedColumns . length && ! satifyAllCondition ) { const columnAverageWidth = remainWidth / unFixedColumns . length ; let longerColumn ; let longerColumnWidth = 0 ; unFixedColumns . forEach ( column => { if ( isDef ( column . minWidth ) ) { const value = transformWidth ( column . minWidth ! ) ; if ( value > columnAverageWidth && value >= longerColumnWidth ) { longerColumnWidth = value ; longerColumn = column ; } } } ) ; if ( longerColumn ) { remainWidth -= longerColumnWidth ; unFixedColumns . splice ( unFixedColumns . indexOf ( longerColumn ) , 1 ) ; continue ; } let shorterColumn ; let shorterColumnWidth = 0 ; unFixedColumns . forEach ( column => { if ( isDef ( column . maxWidth ) ) { const value = transformWidth ( column . maxWidth ! ) ; if ( value < columnAverageWidth && value <= shorterColumnWidth ) { shorterColumnWidth = value ; shorterColumn = column ; } } } ) ; if ( shorterColumn ) { remainWidth -= shorterColumnWidth ; unFixedColumns . splice ( unFixedColumns . indexOf ( shorterColumn ) , 1 ) ; continue ; } satifyAllCondition = true ; } const columnAverageWidth = Math . floor ( remainWidth / unFixedColumns . length ) ; unFixedColumns . forEach ( ( column , columnIndex ) => { if ( columnIndex === unFixedColumns . length - 1 ) { columnWidthDic [ column . dataProp ] = Math . floor ( remainWidth ) ; return ; } columnWidthDic [ column . dataProp ] = columnAverageWidth ; remainWidth -= columnAverageWidth ; } ) ; return columnWidthDic ; }