Header.js 4.19 KB
Newer Older
jim's avatar
jim committed
1
import React, { PureComponent } from 'react';
2
import { formatMessage } from 'umi/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 13 14 15 16
import styles from './Header.less';

const { Header } = Layout;

class HeaderView extends PureComponent {
  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 101
        if (this.oldScrollTop > scrollTop) {
          this.setState({
            visible: true,
          });
        }
102
        if (scrollTop > 300 && visible) {
jim's avatar
jim committed
103 104 105 106
          this.setState({
            visible: false,
          });
        }
107
        if (scrollTop < 300 && !visible) {
jim's avatar
jim committed
108 109 110 111
          this.setState({
            visible: true,
          });
        }
112
        this.oldScrollTop = scrollTop;
jim's avatar
jim committed
113 114 115 116
        this.ticking = false;
      });
    }
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
117

jim's avatar
jim committed
118
  render() {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
119
    const { isMobile, handleMenuCollapse, setting } = this.props;
afc163's avatar
afc163 committed
120
    const { navTheme, layout, fixedHeader } = setting;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
121
    const { visible } = this.state;
jim's avatar
jim committed
122
    const isTop = layout === 'topmenu';
123
    const width = this.getHeadWidth();
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
124
    const HeaderDom = visible ? (
125
      <Header style={{ padding: 0, width }} className={fixedHeader ? styles.fixedHeader : ''}>
jim's avatar
jim committed
126 127
        {isTop && !isMobile ? (
          <TopNavHeader
afc163's avatar
afc163 committed
128
            theme={navTheme}
jim's avatar
jim committed
129 130
            mode="horizontal"
            onCollapse={handleMenuCollapse}
jim's avatar
jim committed
131
            onNoticeClear={this.handleNoticeClear}
jim's avatar
jim committed
132 133 134 135 136 137 138
            onMenuClick={this.handleMenuClick}
            onNoticeVisibleChange={this.handleNoticeVisibleChange}
            {...this.props}
          />
        ) : (
          <GlobalHeader
            onCollapse={handleMenuCollapse}
jim's avatar
jim committed
139
            onNoticeClear={this.handleNoticeClear}
jim's avatar
jim committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
            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,
  fetchingNotices: loading.effects['global/fetchNotices'],
  notices: global.notices,
jim's avatar
jim committed
160
  setting,
jim's avatar
jim committed
161
}))(HeaderView);