Header.js 4.29 KB
Newer Older
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
1
import React, { Component } from 'react';
2
import { formatMessage } from 'umi-plugin-react/locale';
jim's avatar
jim committed
3 4 5
import { Layout, message } from 'antd';
import Animate from 'rc-animate';
import { connect } from 'dva';
zinkey's avatar
zinkey committed
6
import router from 'umi/router';
7 8
import GlobalHeader from '@/components/GlobalHeader';
import TopNavHeader from '@/components/TopNavHeader';
jim's avatar
jim committed
9 10 11 12
import styles from './Header.less';

const { Header } = Layout;

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
13
class HeaderView extends Component {
jim's avatar
jim committed
14 15 16
  state = {
    visible: true,
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
17

18 19 20 21 22 23 24 25 26
  static getDerivedStateFromProps(props, state) {
    if (!props.autoHideHeader && !state.visible) {
      return {
        visible: true,
      };
    }
    return null;
  }

jim's avatar
jim committed
27
  componentDidMount() {
28
    document.addEventListener('scroll', this.handScroll, { passive: true });
jim's avatar
jim committed
29
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
30

jim's avatar
jim committed
31
  componentWillUnmount() {
32
    document.removeEventListener('scroll', this.handScroll);
jim's avatar
jim committed
33
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
34

jim's avatar
jim committed
35
  getHeadWidth = () => {
36 37 38
    const { isMobile, collapsed, setting } = this.props;
    const { fixedHeader, layout } = setting;
    if (isMobile || !fixedHeader || layout === 'topmenu') {
jim's avatar
jim committed
39 40
      return '100%';
    }
41
    return collapsed ? 'calc(100% - 80px)' : 'calc(100% - 256px)';
jim's avatar
jim committed
42
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
43

jim's avatar
jim committed
44
  handleNoticeClear = type => {
Rayron Victor's avatar
Rayron Victor committed
45 46 47 48 49
    message.success(
      `${formatMessage({ id: 'component.noticeIcon.cleared' })} ${formatMessage({
        id: `component.globalHeader.${type}`,
      })}`
    );
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
50 51
    const { dispatch } = this.props;
    dispatch({
jim's avatar
jim committed
52 53 54 55
      type: 'global/clearNotices',
      payload: type,
    });
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
56

jim's avatar
jim committed
57
  handleMenuClick = ({ key }) => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
58
    const { dispatch } = this.props;
jim's avatar
jim committed
59
    if (key === 'userCenter') {
zinkey's avatar
zinkey committed
60
      router.push('/account/center');
jim's avatar
jim committed
61 62 63
      return;
    }
    if (key === 'triggerError') {
zinkey's avatar
zinkey committed
64
      router.push('/exception/trigger');
jim's avatar
jim committed
65 66 67
      return;
    }
    if (key === 'userinfo') {
zinkey's avatar
zinkey committed
68
      router.push('/account/settings/base');
jim's avatar
jim committed
69 70 71
      return;
    }
    if (key === 'logout') {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
72
      dispatch({
jim's avatar
jim committed
73 74 75 76
        type: 'login/logout',
      });
    }
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
77

jim's avatar
jim committed
78
  handleNoticeVisibleChange = visible => {
jim's avatar
jim committed
79
    if (visible) {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
80 81
      const { dispatch } = this.props;
      dispatch({
jim's avatar
jim committed
82 83 84 85
        type: 'global/fetchNotices',
      });
    }
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
86

87
  handScroll = () => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
88 89 90
    const { autoHideHeader } = this.props;
    const { visible } = this.state;
    if (!autoHideHeader) {
jim's avatar
jim committed
91 92
      return;
    }
93
    const scrollTop = document.body.scrollTop + document.documentElement.scrollTop;
jim's avatar
jim committed
94
    if (!this.ticking) {
95
      this.ticking = true;
jim's avatar
jim committed
96
      requestAnimationFrame(() => {
97 98 99 100
        if (this.oldScrollTop > scrollTop) {
          this.setState({
            visible: true,
          });
101
        } else if (scrollTop > 300 && visible) {
jim's avatar
jim committed
102 103 104
          this.setState({
            visible: false,
          });
105
        } else if (scrollTop < 300 && !visible) {
jim's avatar
jim committed
106 107 108 109
          this.setState({
            visible: true,
          });
        }
110
        this.oldScrollTop = scrollTop;
jim's avatar
jim committed
111 112 113 114
        this.ticking = false;
      });
    }
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
115

jim's avatar
jim committed
116
  render() {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
117
    const { isMobile, handleMenuCollapse, setting } = this.props;
afc163's avatar
afc163 committed
118
    const { navTheme, layout, fixedHeader } = setting;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
119
    const { visible } = this.state;
jim's avatar
jim committed
120
    const isTop = layout === 'topmenu';
121
    const width = this.getHeadWidth();
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
122
    const HeaderDom = visible ? (
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
123 124 125 126
      <Header
        style={{ padding: 0, width, zIndex: 2 }}
        className={fixedHeader ? styles.fixedHeader : ''}
      >
jim's avatar
jim committed
127 128
        {isTop && !isMobile ? (
          <TopNavHeader
afc163's avatar
afc163 committed
129
            theme={navTheme}
jim's avatar
jim committed
130 131
            mode="horizontal"
            onCollapse={handleMenuCollapse}
jim's avatar
jim committed
132
            onNoticeClear={this.handleNoticeClear}
jim's avatar
jim committed
133 134 135 136 137 138 139
            onMenuClick={this.handleMenuClick}
            onNoticeVisibleChange={this.handleNoticeVisibleChange}
            {...this.props}
          />
        ) : (
          <GlobalHeader
            onCollapse={handleMenuCollapse}
jim's avatar
jim committed
140
            onNoticeClear={this.handleNoticeClear}
jim's avatar
jim committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
            onMenuClick={this.handleMenuClick}
            onNoticeVisibleChange={this.handleNoticeVisibleChange}
            {...this.props}
          />
        )}
      </Header>
    ) : null;
    return (
      <Animate component="" transitionName="fade">
        {HeaderDom}
      </Animate>
    );
  }
}

export default connect(({ user, global, setting, loading }) => ({
  currentUser: user.currentUser,
  collapsed: global.collapsed,
何乐's avatar
何乐 committed
159
  fetchingMoreNotices: loading.effects['global/fetchMoreNotices'],
jim's avatar
jim committed
160 161
  fetchingNotices: loading.effects['global/fetchNotices'],
  notices: global.notices,
jim's avatar
jim committed
162
  setting,
jim's avatar
jim committed
163
}))(HeaderView);