vben
2020-12-01 962f90de445d7935ad76ea7b74a98f12ce9a7498
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
126
127
128
129
130
import { BasicColumn, BasicTableProps } from '../types/table';
import { PaginationProps } from '../types/pagination';
import { unref, ComputedRef, Ref, computed, watchEffect, ref, toRaw } from 'vue';
import { isBoolean, isArray, isObject } from '/@/utils/is';
import { PAGE_SIZE } from '../const';
import { useProps } from './useProps';
import { useI18n } from '/@/hooks/web/useI18n';
 
const { t } = useI18n();
export function useColumns(
  refProps: ComputedRef<BasicTableProps>,
  getPaginationRef: ComputedRef<false | PaginationProps>
) {
  const { propsRef } = useProps(refProps);
  const columnsRef = (ref(unref(propsRef).columns) as unknown) as Ref<BasicColumn[]>;
  const cacheColumnsRef = (ref(unref(propsRef).columns) as unknown) as Ref<BasicColumn[]>;
 
  const getColumnsRef = computed(() => {
    const props = unref(propsRef);
    const { showIndexColumn, indexColumnProps, ellipsis, actionColumn, isTreeTable } = props;
 
    const columns = unref(columnsRef);
    if (!columns) {
      return [];
    }
    let pushIndexColumns = false;
    columns.forEach((item) => {
      const { children } = item;
      handleItem(item, !!ellipsis);
 
      handleChildren(children, !!ellipsis);
 
      const indIndex = columns.findIndex((column) => column.flag === 'INDEX');
      if (showIndexColumn && !isTreeTable) {
        pushIndexColumns = indIndex === -1;
      } else if (!showIndexColumn && !isTreeTable && indIndex !== -1) {
        columns.splice(indIndex, 1);
      }
    });
 
    if (pushIndexColumns) {
      const isFixedLeft = columns.some((item) => item.fixed === 'left');
 
      columns.unshift({
        flag: 'INDEX',
        width: 50,
        title: t('component.table.index'),
        align: 'center',
        customRender: ({ index }) => {
          const getPagination = unref(getPaginationRef);
          if (isBoolean(getPagination)) {
            return `${index + 1}`;
          }
          const { current = 1, pageSize = PAGE_SIZE } = getPagination;
          const currentIndex = (current - 1) * pageSize + index + 1;
          return currentIndex;
        },
        ...(isFixedLeft
          ? {
              fixed: 'left',
            }
          : {}),
        ...indexColumnProps,
      });
    }
    if (actionColumn) {
      const hasIndex = columns.findIndex((column) => column.flag === 'ACTION');
      if (hasIndex === -1) {
        columns.push({
          ...columns[hasIndex],
          fixed: 'right',
          ...actionColumn,
          flag: 'ACTION',
        });
      }
    }
    return columns;
  });
 
  watchEffect(() => {
    const columns = toRaw(unref(propsRef).columns);
    columnsRef.value = columns;
    cacheColumnsRef.value = columns;
  });
 
  function handleItem(item: BasicColumn, ellipsis: boolean) {
    const { key, dataIndex } = item;
    item.align = item.align || 'center';
    if (ellipsis) {
      if (!key) {
        item.key = dataIndex;
      }
      if (!isBoolean(item.ellipsis)) {
        Object.assign(item, {
          ellipsis,
        });
      }
    }
  }
 
  function handleChildren(children: BasicColumn[] | undefined, ellipsis: boolean) {
    if (!children) return;
    children.forEach((item) => {
      const { children } = item;
      handleItem(item, ellipsis);
      handleChildren(children, ellipsis);
    });
  }
 
  function setColumns(columns: BasicColumn[] | string[]) {
    if (!isArray(columns)) return;
 
    if (columns.length <= 0) {
      columnsRef.value = [];
      return;
    }
 
    const firstColumn = columns[0];
    if (isObject(firstColumn)) {
      columnsRef.value = columns as any;
    } else {
      const newColumns = unref(cacheColumnsRef).filter((item) =>
        (columns as string[]).includes(`${item.key}`! || item.dataIndex!)
      );
      columnsRef.value = newColumns;
    }
  }
 
  return { getColumnsRef, setColumns };
}