Files
vxe-table/packages/tools/resize.ts
2021-04-10 23:37:45 +08:00

86 lines
2.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import XEUtils from 'xe-utils'
import GlobalConfig from '../v-x-e-table/src/conf'
/**
* 监听 resize 事件
* 如果项目中已使用了 resize-observer-polyfill那么只需要将方法定义全局该组件就会自动使用
*/
let resizeTimeout: any
/* eslint-disable no-use-before-define */
const eventStore: XEResizeObserver[] = []
const defaultInterval = 500
function eventHandle () {
if (eventStore.length) {
eventStore.forEach((item) => {
item.tarList.forEach((observer) => {
const { target, width, heighe } = observer
const clientWidth = target.clientWidth
const clientHeight = target.clientHeight
const rWidth = clientWidth && width !== clientWidth
const rHeight = clientHeight && heighe !== clientHeight
if (rWidth || rHeight) {
observer.width = clientWidth
observer.heighe = clientHeight
setTimeout(item.callback)
}
})
})
/* eslint-disable @typescript-eslint/no-use-before-define */
eventListener()
}
}
function eventListener () {
clearTimeout(resizeTimeout)
resizeTimeout = setTimeout(eventHandle, GlobalConfig.resizeInterval || defaultInterval)
}
export class XEResizeObserver {
tarList: {
target: Element;
width: number;
heighe: number;
}[] = []
callback: (...args: any[]) => void
constructor (callback: (...args: any[]) => void) {
this.callback = callback
}
observe (target: Element): void {
if (target) {
const { tarList } = this
if (!tarList.some(observer => observer.target === target)) {
tarList.push({
target,
width: target.clientWidth,
heighe: target.clientHeight
})
}
if (!eventStore.length) {
eventListener()
}
if (!eventStore.some((item) => item === this)) {
eventStore.push(this)
}
}
}
unobserve (target: Element): void {
XEUtils.remove(eventStore, item => item.tarList.some(observer => observer.target === target))
}
disconnect (): void {
XEUtils.remove(eventStore, item => item === this)
}
}
export function createResizeEvent (callback: (...args: any[]) => void): any {
if (window.ResizeObserver) {
return new window.ResizeObserver(callback)
}
return new XEResizeObserver(callback)
}