vben
2020-11-27 81baf1d5c4606aab83c0e65397ce4b090c2e4e08
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import type { BasicTableProps } from '../types/table';
import { computed, Ref, onMounted, unref, ref, nextTick, ComputedRef, watch } from 'vue';
 
import { getViewportOffset } from '/@/utils/domUtils';
import { isBoolean } from '/@/utils/is';
 
import { useWindowSizeFn } from '/@/hooks/event/useWindowSizeFn';
import { useProps } from './useProps';
import { useModalContext } from '/@/components/Modal';
 
export function useTableScroll(refProps: ComputedRef<BasicTableProps>, tableElRef: Ref<any>) {
  const { propsRef } = useProps(refProps);
 
  const tableHeightRef: Ref<number | null> = ref(null);
 
  const modalFn = useModalContext();
 
  watch(
    () => unref(propsRef).canResize,
    () => {
      redoHeight();
    }
  );
 
  function redoHeight() {
    const { canResize } = unref(propsRef);
 
    if (!canResize) return;
    calcTableHeight();
  }
 
  let paginationEl: HTMLElement | null;
  let footerEl: HTMLElement | null;
  async function calcTableHeight() {
    const { canResize, resizeHeightOffset, pagination, maxHeight } = unref(propsRef);
    if (!canResize) return;
 
    await nextTick();
    const table = unref(tableElRef) as any;
    if (!table) return;
 
    const tableEl: Element = table.$el;
    if (!tableEl) return;
    const headEl = tableEl.querySelector('.ant-table-thead ');
    if (!headEl) return;
 
    // 表格距离底部高度
    const { bottomIncludeBody } = getViewportOffset(headEl);
    // 表格高度+距离底部高度-自定义偏移量
 
    const paddingHeight = 32;
    const borderHeight = 2 * 2;
    // 分页器高度
 
    let paginationHeight = 2;
    if (!isBoolean(pagination)) {
      if (!paginationEl) {
        paginationEl = tableEl.querySelector('.ant-pagination') as HTMLElement;
      }
      if (paginationEl) {
        const offsetHeight = paginationEl.offsetHeight;
        paginationHeight += offsetHeight || 0;
      } else {
        // TODO 先固定24
        paginationHeight += 24;
      }
    }
 
    let footerHeight = 0;
    if (!isBoolean(pagination)) {
      if (!footerEl) {
        footerEl = tableEl.querySelector('.ant-table-footer') as HTMLElement;
      } else {
        const offsetHeight = footerEl.offsetHeight;
        footerHeight += offsetHeight || 0;
      }
    }
    let headerHeight = 0;
    if (headEl) {
      headerHeight = (headEl as HTMLElement).offsetHeight;
    }
    tableHeightRef.value =
      bottomIncludeBody -
      (resizeHeightOffset || 0) -
      paddingHeight -
      borderHeight -
      paginationHeight -
      footerHeight -
      headerHeight;
 
    setTimeout(() => {
      tableHeightRef.value =
        tableHeightRef.value! > maxHeight! ? (maxHeight as number) : tableHeightRef.value;
      //  解决表格放modal内的时候,modal自适应高度计算问题
      modalFn?.redoModalHeight?.();
    }, 16);
  }
 
  const getCanResize = computed(() => {
    const { canResize, scroll } = unref(propsRef);
    return canResize && !(scroll || {}).y;
  });
 
  useWindowSizeFn(calcTableHeight, 100);
 
  onMounted(() => {
    if (unref(getCanResize)) {
      nextTick(() => {
        calcTableHeight();
      });
    }
  });
  const getScrollRef = computed(() => {
    const tableHeight = unref(tableHeightRef);
    const { canResize, scroll } = unref(propsRef);
 
    return {
      x: '100%',
      y: canResize ? tableHeight : null,
      scrollToFirstRowOnChange: false,
      ...scroll,
    };
  });
  return { getScrollRef, redoHeight };
}