Header.js 4.2 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 = () => {
柴茂源's avatar
柴茂源 committed
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%';
    }
柴茂源's avatar
柴茂源 committed
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
        else if (scrollTop > 300 && visible) {
jim's avatar
jim committed
103 104 105 106
          this.setState({
            visible: false,
          });
        }
107
        else 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);