vben
2020-10-18 7101587b9676c91e9079044a096df08848f1f602
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
import { TabItem, tabStore } from '/@/store/modules/tab';
import type { PropType } from 'vue';
import { getScaleAction, TabContentProps } from './tab.data';
 
import { defineComponent, unref, computed } from 'vue';
import { Dropdown } from '/@/components/Dropdown/index';
import Icon from '/@/components/Icon/index';
import { DoubleRightOutlined } from '@ant-design/icons-vue';
import { appStore } from '/@/store/modules/app';
 
import { TabContentEnum } from './tab.data';
import { useTabDropdown } from './useTabDropdown';
 
export default defineComponent({
  name: 'TabContent',
  props: {
    tabItem: {
      type: Object as PropType<TabItem>,
      default: null,
    },
    type: {
      type: Number as PropType<number>,
      default: TabContentEnum.TAB_TYPE,
    },
    trigger: {
      type: Array as PropType<string[]>,
      default: () => {
        return ['contextmenu'];
      },
    },
  },
  setup(props) {
    const getProjectConfigRef = computed(() => {
      return appStore.getProjectConfig;
    });
 
    const getIsScaleRef = computed(() => {
      const {
        menuSetting: { show: showMenu },
        headerSetting: { show: showHeader },
      } = unref(getProjectConfigRef);
      return !showMenu && !showHeader;
    });
 
    function handleContextMenu(e: Event) {
      if (!props.tabItem) return;
      const tableItem = props.tabItem;
      e.preventDefault();
      const index = unref(tabStore.getTabsState).findIndex((tab) => tab.path === tableItem.path);
 
      tabStore.commitCurrentContextMenuIndexState(index);
      tabStore.commitCurrentContextMenuState(props.tabItem);
    }
 
    /**
     * @description: 渲染图标
     */
    function renderIcon() {
      const { tabItem } = props;
      if (!tabItem) return;
      const icon = tabItem.meta && tabItem.meta.icon;
      if (!icon || !unref(getProjectConfigRef).multiTabsSetting.showIcon) return null;
      return <Icon icon={icon} class="align-middle mb-1" />;
    }
    function renderTabContent() {
      const { tabItem: { meta } = {} } = props;
      return (
        <div class={`multiple-tabs-content__content `} onContextmenu={handleContextMenu}>
          {renderIcon()}
          <span class="ml-1">{meta && meta.title}</span>
        </div>
      );
    }
    function renderExtraContent() {
      return (
        <span class={`multiple-tabs-content__extra `}>
          <DoubleRightOutlined />
        </span>
      );
    }
 
    const { getDropMenuList, handleMenuEvent } = useTabDropdown(props as TabContentProps);
 
    return () => {
      const { trigger, type } = props;
      const {
        multiTabsSetting: { showQuick },
      } = unref(getProjectConfigRef);
 
      const isTab = !showQuick ? true : type === TabContentEnum.TAB_TYPE;
      const scaleAction = getScaleAction(
        unref(getIsScaleRef) ? '缩小' : '放大',
        unref(getIsScaleRef)
      );
      const dropMenuList = unref(getDropMenuList) || [];
 
      return (
        <Dropdown
          dropMenuList={!isTab ? [scaleAction, ...dropMenuList] : dropMenuList}
          trigger={isTab ? trigger : ['hover']}
          onMenuEvent={handleMenuEvent}
        >
          {() => (isTab ? renderTabContent() : renderExtraContent())}
        </Dropdown>
      );
    };
  },
});