LayoutHeader.tsx 7.6 KB
Newer Older
V
vben 已提交
1
import { defineComponent, unref, computed, ref } from 'vue';
V
vben 已提交
2
import { Layout, Tooltip, Badge } from 'ant-design-vue';
陈文彬 已提交
3 4 5 6 7 8 9 10 11 12 13 14
import Logo from '/@/layouts/Logo.vue';
import UserDropdown from './UserDropdown';
import LayoutMenu from './LayoutMenu';
import { appStore } from '/@/store/modules/app';
import { MenuModeEnum, MenuSplitTyeEnum, MenuTypeEnum } from '/@/enums/menuEnum';
import LayoutBreadcrumb from './LayoutBreadcrumb';
import {
  RedoOutlined,
  FullscreenExitOutlined,
  FullscreenOutlined,
  GithubFilled,
  LockOutlined,
V
vben 已提交
15
  BugOutlined,
陈文彬 已提交
16 17 18 19 20 21
} from '@ant-design/icons-vue';
import { useFullscreen } from '/@/hooks/web/useFullScreen';
import { useTabs } from '/@/hooks/web/useTabs';
import { GITHUB_URL } from '/@/settings/siteSetting';
import LockAction from './actions/LockActionItem';
import { useModal } from '/@/components/Modal/index';
V
vben 已提交
22
import { errorStore } from '/@/store/modules/error';
V
vben 已提交
23
import { useWindowSizeFn } from '/@/hooks/event/useWindowSize';
C
chen-xt 已提交
24
import NoticeAction from './actions/notice/NoticeActionItem.vue';
陈文彬 已提交
25 26 27 28

export default defineComponent({
  name: 'DefaultLayoutHeader',
  setup() {
V
vben 已提交
29
    const widthRef = ref(200);
V
vben 已提交
30
    const { refreshPage, addTab } = useTabs();
陈文彬 已提交
31 32
    const [register, { openModal }] = useModal();
    const { toggleFullscreen, isFullscreenRef } = useFullscreen();
V
vben 已提交
33

陈文彬 已提交
34 35 36
    const getProjectConfigRef = computed(() => {
      return appStore.getProjectConfig;
    });
V
vben 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    const showTopMenu = computed(() => {
      const getProjectConfig = unref(getProjectConfigRef);
      const {
        menuSetting: { mode, split: splitMenu },
      } = getProjectConfig;
      return mode === MenuModeEnum.HORIZONTAL || splitMenu;
    });

    let logoEl: Element | null;
    useWindowSizeFn(
      () => {
        if (!unref(showTopMenu)) return;
        let width = 0;
        if (!logoEl) {
          logoEl = document.querySelector('.layout-header__logo');
        }
        if (logoEl) {
          width += logoEl.clientWidth;
        }
        widthRef.value = width + 60;
      },
      200,
      { immediate: true }
    );
陈文彬 已提交
61 62 63 64 65 66 67 68 69

    function goToGithub() {
      window.open(GITHUB_URL, '__blank');
    }

    const headerClass = computed(() => {
      const theme = unref(getProjectConfigRef).headerSetting.theme;
      return theme ? `layout-header__header--${theme}` : '';
    });
V
vben 已提交
70 71 72

    function handleToErrorList() {
      errorStore.commitErrorListCountState(0);
V
vben 已提交
73
      addTab('/exception/error-log', true);
V
vben 已提交
74 75
    }

陈文彬 已提交
76 77 78 79 80 81 82 83 84
    /**
     * @description: 锁定屏幕
     */
    function handleLockPage() {
      openModal(true);
    }
    return () => {
      const getProjectConfig = unref(getProjectConfigRef);
      const {
V
vben 已提交
85
        useErrorHandle,
陈文彬 已提交
86
        showLogo,
C
chen-xt 已提交
87 88 89 90 91 92 93 94
        headerSetting: {
          theme: headerTheme,
          useLockPage,
          showRedo,
          showGithub,
          showFullScreen,
          showNotice,
        },
陈文彬 已提交
95 96
        menuSetting: { mode, type: menuType, split: splitMenu, topMenuAlign },
        showBreadCrumb,
Z
ZhaoBin 已提交
97
        showBreadCrumbIcon,
陈文彬 已提交
98 99 100
      } = getProjectConfig;

      const isSidebarType = menuType === MenuTypeEnum.SIDEBAR;
V
vben 已提交
101
      const width = unref(widthRef);
陈文彬 已提交
102
      return (
N
nebv 已提交
103
        <Layout.Header class={['layout-header', 'flex p-0 px-4 ', unref(headerClass)]}>
陈文彬 已提交
104 105
          {() => (
            <>
N
nebv 已提交
106
              <div class="layout-header__content ">
陈文彬 已提交
107 108 109
                {showLogo && !isSidebarType && <Logo class={`layout-header__logo`} />}

                {mode !== MenuModeEnum.HORIZONTAL && showBreadCrumb && !splitMenu && (
Z
ZhaoBin 已提交
110
                  <LayoutBreadcrumb showIcon={showBreadCrumbIcon} />
陈文彬 已提交
111
                )}
V
vben 已提交
112 113 114 115 116
                {unref(showTopMenu) && (
                  <div
                    class={[`layout-header__menu `, `justify-${topMenuAlign}`]}
                    style={{ width: `calc(100% - ${unref(width)}px)` }}
                  >
陈文彬 已提交
117 118 119 120 121 122 123 124 125 126 127
                    <LayoutMenu
                      theme={headerTheme}
                      splitType={splitMenu ? MenuSplitTyeEnum.TOP : MenuSplitTyeEnum.NONE}
                      menuMode={splitMenu ? MenuModeEnum.HORIZONTAL : null}
                      showSearch={false}
                    />
                  </div>
                )}
              </div>

              <div class={`layout-header__action`}>
V
vben 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
                {useErrorHandle && (
                  <Tooltip>
                    {{
                      title: () => '错误日志',
                      default: () => (
                        <Badge
                          count={errorStore.getErrorListCountState}
                          offset={[0, 10]}
                          overflowCount={99}
                        >
                          {() => (
                            <div class={`layout-header__action-item`} onClick={handleToErrorList}>
                              <BugOutlined class={`layout-header__action-icon`} />
                            </div>
                          )}
                        </Badge>
                      ),
                    }}
                  </Tooltip>
                )}

陈文彬 已提交
149 150 151 152 153 154 155 156 157 158 159 160
                {showGithub && (
                  <Tooltip>
                    {{
                      title: () => 'github',
                      default: () => (
                        <div class={`layout-header__action-item`} onClick={goToGithub}>
                          <GithubFilled class={`layout-header__action-icon`} />
                        </div>
                      ),
                    }}
                  </Tooltip>
                )}
V
vben 已提交
161
                {useLockPage && (
陈文彬 已提交
162 163 164 165 166 167 168 169 170 171 172
                  <Tooltip>
                    {{
                      title: () => '锁定屏幕',
                      default: () => (
                        <div class={`layout-header__action-item`} onClick={handleLockPage}>
                          <LockOutlined class={`layout-header__action-icon`} />
                        </div>
                      ),
                    }}
                  </Tooltip>
                )}
C
chen-xt 已提交
173 174 175 176
                {showNotice && (
                  <div>
                    <Tooltip>
                      {{
V
vben 已提交
177 178
                        title: () => '消息通知',
                        default: () => <NoticeAction />,
C
chen-xt 已提交
179 180 181 182
                      }}
                    </Tooltip>
                  </div>
                )}
陈文彬 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
                {showRedo && (
                  <Tooltip>
                    {{
                      title: () => '刷新',
                      default: () => (
                        <div class={`layout-header__action-item`} onClick={refreshPage}>
                          <RedoOutlined class={`layout-header__action-icon`} />
                        </div>
                      ),
                    }}
                  </Tooltip>
                )}
                {showFullScreen && (
                  <Tooltip>
                    {{
                      title: () => (unref(isFullscreenRef) ? '退出全屏' : '全屏'),
                      default: () => {
                        const Icon: any = !unref(isFullscreenRef) ? (
                          <FullscreenOutlined />
                        ) : (
                          <FullscreenExitOutlined />
                        );
                        return (
                          <div class={`layout-header__action-item`} onClick={toggleFullscreen}>
                            <Icon class={`layout-header__action-icon`} />
                          </div>
                        );
                      },
                    }}
                  </Tooltip>
                )}
                <UserDropdown class={`layout-header__user-dropdown`} />
              </div>
              <LockAction onRegister={register} />
            </>
          )}
        </Layout.Header>
      );
    };
  },
});