vben
2020-10-30 84c9d78fa7feffe3430b7c85dc97383cd7193ba1
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import type { ColEx } from '../types';
import type { AdvanceState } from '../types/hooks';
import type { ComputedRef, Ref } from 'vue';
import type { FormProps, FormSchema } from '../types/form';
 
import { computed, unref, watch } from 'vue';
import { isBoolean, isFunction, isNumber, isObject } from '/@/utils/is';
 
import { useBreakpoint } from '/@/hooks/event/useBreakpoint';
 
const BASIC_COL_LEN = 24;
 
interface UseAdvancedContext {
  advanceState: AdvanceState;
  emit: EmitType;
  getMergePropsRef: ComputedRef<FormProps>;
  getProps: ComputedRef<FormProps>;
  getSchema: ComputedRef<FormSchema[]>;
  formModel: any;
  defaultValueRef: Ref<any>;
}
 
export default function ({
  advanceState,
  emit,
  getMergePropsRef,
  getProps,
  getSchema,
  formModel,
  defaultValueRef,
}: UseAdvancedContext) {
  const { realWidthRef, screenEnum, screenRef } = useBreakpoint();
  const getEmptySpanRef = computed((): number => {
    if (!advanceState.isAdvanced) {
      return 0;
    }
    const emptySpan = unref(getMergePropsRef).emptySpan || 0;
 
    if (isNumber(emptySpan)) {
      return emptySpan;
    }
    if (isObject(emptySpan)) {
      const { span = 0 } = emptySpan;
      const screen = unref(screenRef) as string;
 
      const screenSpan = (emptySpan as any)[screen.toLowerCase()];
      return screenSpan || span || 0;
    }
    return 0;
  });
 
  const getActionPropsRef = computed(() => {
    const {
      resetButtonOptions,
      submitButtonOptions,
      showActionButtonGroup,
      showResetButton,
      showSubmitButton,
      showAdvancedButton,
      actionColOptions,
    } = unref(getProps);
    return {
      resetButtonOptions,
      submitButtonOptions,
      show: showActionButtonGroup,
      showResetButton,
      showSubmitButton,
      showAdvancedButton,
      actionColOptions,
    };
  });
  watch(
    [() => unref(getSchema), () => advanceState.isAdvanced, () => unref(realWidthRef)],
    () => {
      const { showAdvancedButton } = unref(getProps);
      if (showAdvancedButton) {
        updateAdvanced();
      }
    },
    { immediate: true }
  );
 
  function getAdvanced(itemCol: Partial<ColEx>, itemColSum = 0, isLastAction = false) {
    const width = unref(realWidthRef);
 
    const mdWidth =
      parseInt(itemCol.md as string) ||
      parseInt(itemCol.xs as string) ||
      parseInt(itemCol.sm as string) ||
      (itemCol.span as number) ||
      BASIC_COL_LEN;
    const lgWidth = parseInt(itemCol.lg as string) || mdWidth;
    const xlWidth = parseInt(itemCol.xl as string) || lgWidth;
    const xxlWidth = parseInt(itemCol.xxl as string) || xlWidth;
    if (width <= screenEnum.LG) {
      itemColSum += mdWidth;
    } else if (width < screenEnum.XL) {
      itemColSum += lgWidth;
    } else if (width < screenEnum.XXL) {
      itemColSum += xlWidth;
    } else {
      itemColSum += xxlWidth;
    }
    if (isLastAction) {
      advanceState.hideAdvanceBtn = false;
      if (itemColSum <= BASIC_COL_LEN * 2) {
        // 小于等于2行时,不显示收起展开按钮
        advanceState.hideAdvanceBtn = true;
        advanceState.isAdvanced = true;
      } else if (
        itemColSum > BASIC_COL_LEN * 2 &&
        itemColSum <= BASIC_COL_LEN * (unref(getMergePropsRef).autoAdvancedLine || 3)
      ) {
        advanceState.hideAdvanceBtn = false;
 
        // 大于3行默认收起
      } else if (!advanceState.isLoad) {
        advanceState.isLoad = true;
        advanceState.isAdvanced = !advanceState.isAdvanced;
      }
      return { isAdvanced: advanceState.isAdvanced, itemColSum };
    }
    if (itemColSum > BASIC_COL_LEN) {
      return { isAdvanced: advanceState.isAdvanced, itemColSum };
    } else {
      // 第一行始终显示
      return { isAdvanced: true, itemColSum };
    }
  }
 
  function updateAdvanced() {
    let itemColSum = 0;
    let realItemColSum = 0;
    for (const schema of unref(getSchema)) {
      const { show, colProps } = schema;
      let isShow = true;
 
      if (isBoolean(show)) {
        isShow = show;
      }
 
      if (isFunction(show)) {
        isShow = show({
          schema: schema,
          model: formModel,
          field: schema.field,
          values: {
            ...unref(defaultValueRef),
            ...formModel,
          },
        });
      }
 
      if (isShow && colProps) {
        const { itemColSum: sum, isAdvanced } = getAdvanced(colProps, itemColSum);
 
        itemColSum = sum || 0;
        if (isAdvanced) {
          realItemColSum = itemColSum;
        }
        schema.isAdvanced = isAdvanced;
      }
    }
 
    advanceState.actionSpan = (realItemColSum % BASIC_COL_LEN) + unref(getEmptySpanRef);
 
    getAdvanced(
      unref(getActionPropsRef).actionColOptions || { span: BASIC_COL_LEN },
      itemColSum,
      true
    );
    emit('advanced-change');
  }
 
  function handleToggleAdvanced() {
    advanceState.isAdvanced = !advanceState.isAdvanced;
  }
  return { getActionPropsRef, handleToggleAdvanced };
}