2020-10-20 10:31:47 +08:00
|
|
|
import { nextTick, ref, isRef, Ref } from 'vue'
|
2021-05-09 10:46:11 +08:00
|
|
|
import { hasOwn } from '@vue/shared'
|
2020-10-20 10:31:47 +08:00
|
|
|
import scrollbarWidth from '@element-plus/utils/scrollbar-width'
|
|
|
|
import isServer from '@element-plus/utils/isServer'
|
|
|
|
import { parseHeight } from './util'
|
2021-05-13 17:55:04 +08:00
|
|
|
import { TableColumnCtx } from './table-column/defaults'
|
|
|
|
import { TableHeader } from './table-header/index'
|
|
|
|
import { Table } from './table/defaults'
|
|
|
|
import { Store } from './store/index'
|
|
|
|
class TableLayout<T> {
|
2020-10-20 10:31:47 +08:00
|
|
|
observers: TableHeader[]
|
2021-05-13 17:55:04 +08:00
|
|
|
table: Table<T>
|
|
|
|
store: Store<T>
|
|
|
|
columns: TableColumnCtx<T>[]
|
2020-10-20 10:31:47 +08:00
|
|
|
fit: boolean
|
|
|
|
showHeader: boolean
|
|
|
|
|
|
|
|
height: Ref<null | number>
|
|
|
|
scrollX: Ref<boolean>
|
|
|
|
scrollY: Ref<boolean>
|
|
|
|
bodyWidth: Ref<null | number>
|
|
|
|
fixedWidth: Ref<null | number>
|
|
|
|
rightFixedWidth: Ref<null | number>
|
|
|
|
tableHeight: Ref<null | number>
|
|
|
|
headerHeight: Ref<null | number> // Table Header Height
|
|
|
|
appendHeight: Ref<null | number> // Append Slot Height
|
|
|
|
footerHeight: Ref<null | number> // Table Footer Height
|
|
|
|
viewportHeight: Ref<null | number> // Table Height - Scroll Bar Height
|
|
|
|
bodyHeight: Ref<null | number> // Table Height - Table Header Height
|
|
|
|
fixedBodyHeight: Ref<null | number> // Table Height - Table Header Height - Scroll Bar Height
|
|
|
|
gutterWidth: number
|
2021-05-13 17:55:04 +08:00
|
|
|
constructor(options: Record<string, any>) {
|
2020-10-20 10:31:47 +08:00
|
|
|
this.observers = []
|
|
|
|
this.table = null
|
|
|
|
this.store = null
|
|
|
|
this.columns = []
|
|
|
|
this.fit = true
|
|
|
|
this.showHeader = true
|
|
|
|
this.height = ref(null)
|
|
|
|
this.scrollX = ref(false)
|
|
|
|
this.scrollY = ref(false)
|
|
|
|
this.bodyWidth = ref(null)
|
|
|
|
this.fixedWidth = ref(null)
|
|
|
|
this.rightFixedWidth = ref(null)
|
|
|
|
this.tableHeight = ref(null)
|
|
|
|
this.headerHeight = ref(44)
|
|
|
|
this.appendHeight = ref(0)
|
|
|
|
this.footerHeight = ref(44)
|
|
|
|
this.viewportHeight = ref(null)
|
|
|
|
this.bodyHeight = ref(null)
|
|
|
|
this.fixedBodyHeight = ref(null)
|
|
|
|
this.gutterWidth = scrollbarWidth()
|
|
|
|
for (const name in options) {
|
2021-05-09 10:46:11 +08:00
|
|
|
if (hasOwn(options, name)) {
|
2020-10-20 10:31:47 +08:00
|
|
|
if (isRef(this[name])) {
|
2021-05-09 10:46:11 +08:00
|
|
|
this[name as string].value = options[name]
|
2020-10-20 10:31:47 +08:00
|
|
|
} else {
|
2021-05-09 10:46:11 +08:00
|
|
|
this[name as string] = options[name]
|
2020-10-20 10:31:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!this.table) {
|
|
|
|
throw new Error('table is required for Table Layout')
|
|
|
|
}
|
|
|
|
if (!this.store) {
|
|
|
|
throw new Error('store is required for Table Layout')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 17:55:04 +08:00
|
|
|
updateScrollY() {
|
2020-10-20 10:31:47 +08:00
|
|
|
const height = this.height.value
|
2021-03-26 15:13:02 +08:00
|
|
|
/**
|
|
|
|
* When the height is not initialized, it is null.
|
|
|
|
* After the table is initialized, when the height is not configured, the height is 0.
|
|
|
|
*/
|
2020-10-20 10:31:47 +08:00
|
|
|
if (height === null) return false
|
|
|
|
const bodyWrapper = this.table.refs.bodyWrapper as HTMLElement
|
|
|
|
if (this.table.vnode.el && bodyWrapper) {
|
2021-03-26 15:13:02 +08:00
|
|
|
let scrollY = true
|
2020-10-20 10:31:47 +08:00
|
|
|
const prevScrollY = this.scrollY.value
|
2021-03-26 15:13:02 +08:00
|
|
|
/**
|
|
|
|
* When bodyHeight has no value,
|
|
|
|
* it means that the table height is not set,
|
|
|
|
* and the scroll bar will never appear
|
|
|
|
*/
|
|
|
|
if (this.bodyHeight.value === null) {
|
|
|
|
scrollY = false
|
|
|
|
} else {
|
|
|
|
const body = bodyWrapper.querySelector('.el-table__body') as HTMLElement
|
|
|
|
scrollY = body.offsetHeight > this.bodyHeight.value
|
|
|
|
}
|
2020-10-20 10:31:47 +08:00
|
|
|
this.scrollY.value = scrollY
|
|
|
|
return prevScrollY !== scrollY
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-05-13 17:55:04 +08:00
|
|
|
setHeight(value: string | number, prop = 'height') {
|
2020-10-20 10:31:47 +08:00
|
|
|
if (isServer) return
|
|
|
|
const el = this.table.vnode.el
|
|
|
|
value = parseHeight(value)
|
|
|
|
this.height.value = Number(value)
|
|
|
|
|
|
|
|
if (!el && (value || value === 0))
|
|
|
|
return nextTick(() => this.setHeight(value, prop))
|
|
|
|
|
|
|
|
if (typeof value === 'number') {
|
|
|
|
el.style[prop] = value + 'px'
|
|
|
|
this.updateElsHeight()
|
|
|
|
} else if (typeof value === 'string') {
|
|
|
|
el.style[prop] = value
|
|
|
|
this.updateElsHeight()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 17:55:04 +08:00
|
|
|
setMaxHeight(value: string | number) {
|
2020-10-20 10:31:47 +08:00
|
|
|
this.setHeight(value, 'max-height')
|
|
|
|
}
|
|
|
|
|
2021-05-13 17:55:04 +08:00
|
|
|
getFlattenColumns(): TableColumnCtx<T>[] {
|
2020-10-20 10:31:47 +08:00
|
|
|
const flattenColumns = []
|
|
|
|
const columns = this.table.store.states.columns.value
|
|
|
|
columns.forEach(column => {
|
|
|
|
if (column.isColumnGroup) {
|
|
|
|
// eslint-disable-next-line prefer-spread
|
|
|
|
flattenColumns.push.apply(flattenColumns, column.columns)
|
|
|
|
} else {
|
|
|
|
flattenColumns.push(column)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return flattenColumns
|
|
|
|
}
|
|
|
|
|
2021-05-13 17:55:04 +08:00
|
|
|
updateElsHeight() {
|
2020-10-20 10:31:47 +08:00
|
|
|
if (!this.table.$ready) return nextTick(() => this.updateElsHeight())
|
2021-05-13 17:55:04 +08:00
|
|
|
const { headerWrapper, appendWrapper, footerWrapper } = this.table.refs
|
2020-10-20 10:31:47 +08:00
|
|
|
this.appendHeight.value = appendWrapper ? appendWrapper.offsetHeight : 0
|
|
|
|
if (this.showHeader && !headerWrapper) return
|
|
|
|
|
2021-05-13 17:55:04 +08:00
|
|
|
const headerTrElm: HTMLElement = headerWrapper
|
2020-10-20 10:31:47 +08:00
|
|
|
? headerWrapper.querySelector('.el-table__header tr')
|
|
|
|
: null
|
2021-05-13 17:55:04 +08:00
|
|
|
const noneHeader = this.headerDisplayNone(headerTrElm)
|
2020-10-20 10:31:47 +08:00
|
|
|
|
|
|
|
const headerHeight = (this.headerHeight.value = !this.showHeader
|
|
|
|
? 0
|
|
|
|
: headerWrapper.offsetHeight)
|
|
|
|
if (
|
|
|
|
this.showHeader &&
|
|
|
|
!noneHeader &&
|
|
|
|
headerWrapper.offsetWidth > 0 &&
|
|
|
|
(this.table.store.states.columns.value || []).length > 0 &&
|
|
|
|
headerHeight < 2
|
|
|
|
) {
|
|
|
|
return nextTick(() => this.updateElsHeight())
|
|
|
|
}
|
|
|
|
const tableHeight = (this.tableHeight.value = this.table.vnode.el.clientHeight)
|
|
|
|
const footerHeight = (this.footerHeight.value = footerWrapper
|
|
|
|
? footerWrapper.offsetHeight
|
|
|
|
: 0)
|
|
|
|
if (this.height.value !== null) {
|
|
|
|
this.bodyHeight.value =
|
|
|
|
tableHeight - headerHeight - footerHeight + (footerWrapper ? 1 : 0)
|
|
|
|
}
|
|
|
|
this.fixedBodyHeight.value = this.scrollX.value
|
|
|
|
? this.bodyHeight.value - this.gutterWidth
|
|
|
|
: this.bodyHeight.value
|
|
|
|
|
|
|
|
this.viewportHeight.value = this.scrollX.value
|
2021-01-24 16:44:26 +08:00
|
|
|
? tableHeight - this.gutterWidth
|
2020-10-20 10:31:47 +08:00
|
|
|
: tableHeight
|
|
|
|
|
|
|
|
this.updateScrollY()
|
|
|
|
this.notifyObservers('scrollable')
|
|
|
|
}
|
|
|
|
|
2021-05-13 17:55:04 +08:00
|
|
|
headerDisplayNone(elm: HTMLElement) {
|
2020-10-20 10:31:47 +08:00
|
|
|
if (!elm) return true
|
|
|
|
let headerChild = elm
|
|
|
|
while (headerChild.tagName !== 'DIV') {
|
|
|
|
if (getComputedStyle(headerChild).display === 'none') {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
headerChild = headerChild.parentElement
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-05-13 17:55:04 +08:00
|
|
|
updateColumnsWidth() {
|
2020-10-20 10:31:47 +08:00
|
|
|
if (isServer) return
|
|
|
|
const fit = this.fit
|
|
|
|
const bodyWidth = this.table.vnode.el.clientWidth
|
|
|
|
let bodyMinWidth = 0
|
|
|
|
|
|
|
|
const flattenColumns = this.getFlattenColumns()
|
|
|
|
const flexColumns = flattenColumns.filter(
|
|
|
|
column => typeof column.width !== 'number',
|
|
|
|
)
|
|
|
|
flattenColumns.forEach(column => {
|
|
|
|
// Clean those columns whose width changed from flex to unflex
|
|
|
|
if (typeof column.width === 'number' && column.realWidth)
|
|
|
|
column.realWidth = null
|
|
|
|
})
|
|
|
|
if (flexColumns.length > 0 && fit) {
|
|
|
|
flattenColumns.forEach(column => {
|
2021-05-13 17:55:04 +08:00
|
|
|
bodyMinWidth += Number(column.width || column.minWidth || 80)
|
2020-10-20 10:31:47 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
const scrollYWidth = this.scrollY.value ? this.gutterWidth : 0
|
|
|
|
|
|
|
|
if (bodyMinWidth <= bodyWidth - scrollYWidth) {
|
|
|
|
// DON'T HAVE SCROLL BAR
|
|
|
|
this.scrollX.value = false
|
|
|
|
|
|
|
|
const totalFlexWidth = bodyWidth - scrollYWidth - bodyMinWidth
|
|
|
|
|
|
|
|
if (flexColumns.length === 1) {
|
|
|
|
flexColumns[0].realWidth =
|
2021-05-13 17:55:04 +08:00
|
|
|
Number(flexColumns[0].minWidth || 80) + totalFlexWidth
|
2020-10-20 10:31:47 +08:00
|
|
|
} else {
|
|
|
|
const allColumnsWidth = flexColumns.reduce(
|
2021-05-13 17:55:04 +08:00
|
|
|
(prev, column) => prev + Number(column.minWidth || 80),
|
2020-10-20 10:31:47 +08:00
|
|
|
0,
|
|
|
|
)
|
|
|
|
const flexWidthPerPixel = totalFlexWidth / allColumnsWidth
|
|
|
|
let noneFirstWidth = 0
|
|
|
|
|
|
|
|
flexColumns.forEach((column, index) => {
|
|
|
|
if (index === 0) return
|
|
|
|
const flexWidth = Math.floor(
|
2021-05-13 17:55:04 +08:00
|
|
|
Number(column.minWidth || 80) * flexWidthPerPixel,
|
2020-10-20 10:31:47 +08:00
|
|
|
)
|
|
|
|
noneFirstWidth += flexWidth
|
2021-05-13 17:55:04 +08:00
|
|
|
column.realWidth = Number(column.minWidth || 80) + flexWidth
|
2020-10-20 10:31:47 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
flexColumns[0].realWidth =
|
2021-05-13 17:55:04 +08:00
|
|
|
Number(flexColumns[0].minWidth || 80) +
|
|
|
|
totalFlexWidth -
|
|
|
|
noneFirstWidth
|
2020-10-20 10:31:47 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// HAVE HORIZONTAL SCROLL BAR
|
|
|
|
this.scrollX.value = true
|
2021-05-13 17:55:04 +08:00
|
|
|
flexColumns.forEach(function(column) {
|
|
|
|
column.realWidth = Number(column.minWidth)
|
2020-10-20 10:31:47 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
this.bodyWidth.value = Math.max(bodyMinWidth, bodyWidth)
|
2020-10-30 11:27:56 +08:00
|
|
|
this.table.state.resizeState.value.width = this.bodyWidth.value
|
2020-10-20 10:31:47 +08:00
|
|
|
} else {
|
|
|
|
flattenColumns.forEach(column => {
|
|
|
|
if (!column.width && !column.minWidth) {
|
|
|
|
column.realWidth = 80
|
|
|
|
} else {
|
2021-05-13 17:55:04 +08:00
|
|
|
column.realWidth = Number(column.width || column.minWidth)
|
2020-10-20 10:31:47 +08:00
|
|
|
}
|
|
|
|
bodyMinWidth += column.realWidth
|
|
|
|
})
|
|
|
|
this.scrollX.value = bodyMinWidth > bodyWidth
|
|
|
|
|
|
|
|
this.bodyWidth.value = bodyMinWidth
|
|
|
|
}
|
|
|
|
|
|
|
|
const fixedColumns = this.store.states.fixedColumns.value
|
|
|
|
|
|
|
|
if (fixedColumns.length > 0) {
|
|
|
|
let fixedWidth = 0
|
2021-05-13 17:55:04 +08:00
|
|
|
fixedColumns.forEach(function(column) {
|
|
|
|
fixedWidth += Number(column.realWidth || column.width)
|
2020-10-20 10:31:47 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
this.fixedWidth.value = fixedWidth
|
|
|
|
}
|
|
|
|
|
|
|
|
const rightFixedColumns = this.store.states.rightFixedColumns.value
|
|
|
|
if (rightFixedColumns.length > 0) {
|
|
|
|
let rightFixedWidth = 0
|
2021-05-13 17:55:04 +08:00
|
|
|
rightFixedColumns.forEach(function(column) {
|
|
|
|
rightFixedWidth += Number(column.realWidth || column.width)
|
2020-10-20 10:31:47 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
this.rightFixedWidth.value = rightFixedWidth
|
|
|
|
}
|
|
|
|
this.notifyObservers('columns')
|
|
|
|
}
|
|
|
|
|
2021-05-13 17:55:04 +08:00
|
|
|
addObserver(observer: TableHeader) {
|
2020-10-20 10:31:47 +08:00
|
|
|
this.observers.push(observer)
|
|
|
|
}
|
|
|
|
|
2021-05-13 17:55:04 +08:00
|
|
|
removeObserver(observer: TableHeader) {
|
2020-10-20 10:31:47 +08:00
|
|
|
const index = this.observers.indexOf(observer)
|
|
|
|
if (index !== -1) {
|
|
|
|
this.observers.splice(index, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 17:55:04 +08:00
|
|
|
notifyObservers(event: string) {
|
2020-10-20 10:31:47 +08:00
|
|
|
const observers = this.observers
|
|
|
|
observers.forEach(observer => {
|
|
|
|
switch (event) {
|
|
|
|
case 'columns':
|
2020-10-30 11:27:56 +08:00
|
|
|
observer.state?.onColumnsChange(this)
|
2020-10-20 10:31:47 +08:00
|
|
|
break
|
|
|
|
case 'scrollable':
|
2020-10-30 11:27:56 +08:00
|
|
|
observer.state?.onScrollableChange(this)
|
2020-10-20 10:31:47 +08:00
|
|
|
break
|
|
|
|
default:
|
|
|
|
throw new Error(`Table Layout don't have event ${event}.`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default TableLayout
|