Header.js 3.85 KB
Newer Older
jim's avatar
jim committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
import React, { PureComponent } from 'react';
import { Layout, message } from 'antd';
import Animate from 'rc-animate';
import { connect } from 'dva';
import { routerRedux } from 'dva/router';
import GlobalHeader from '../components/GlobalHeader';
import TopNavHeader from '../components/TopNavHeader';
import styles from './Header.less';
import Authorized from '../utils/Authorized';

const { Header } = Layout;

class HeaderView extends PureComponent {
  state = {
    visible: true,
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
17

jim's avatar
jim committed
18 19 20
  componentDidMount() {
    document.getElementById('root').addEventListener('scroll', this.handScroll);
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
21

jim's avatar
jim committed
22
  componentWillUnmount() {
jim's avatar
jim committed
23
    document.getElementById('root').removeEventListener('scroll', this.handScroll);
jim's avatar
jim committed
24
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
25

jim's avatar
jim committed
26
  getHeadWidth = () => {
27 28 29
    const { isMobile, collapsed, setting } = this.props;
    const { fixedHeader, layout } = setting;
    if (isMobile || !fixedHeader || layout === 'topmenu') {
jim's avatar
jim committed
30 31
      return '100%';
    }
32
    return collapsed ? 'calc(100% - 80px)' : 'calc(100% - 256px)';
jim's avatar
jim committed
33
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
34

jim's avatar
jim committed
35
  handleNoticeClear = type => {
jim's avatar
jim committed
36
    message.success(`ζΈ…η©ΊδΊ†${type}`);
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
37 38
    const { dispatch } = this.props;
    dispatch({
jim's avatar
jim committed
39 40 41 42
      type: 'global/clearNotices',
      payload: type,
    });
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
43

jim's avatar
jim committed
44
  handleMenuClick = ({ key }) => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
45
    const { dispatch } = this.props;
jim's avatar
jim committed
46
    if (key === 'userCenter') {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
47
      dispatch(routerRedux.push('/account/center'));
jim's avatar
jim committed
48 49 50
      return;
    }
    if (key === 'triggerError') {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
51
      dispatch(routerRedux.push('/exception/trigger'));
jim's avatar
jim committed
52 53 54
      return;
    }
    if (key === 'userinfo') {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
55
      dispatch(routerRedux.push('/account/settings/base'));
jim's avatar
jim committed
56 57 58
      return;
    }
    if (key === 'logout') {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
59
      dispatch({
jim's avatar
jim committed
60 61 62 63
        type: 'login/logout',
      });
    }
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
64

jim's avatar
jim committed
65
  handleNoticeVisibleChange = visible => {
jim's avatar
jim committed
66
    if (visible) {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
67 68
      const { dispatch } = this.props;
      dispatch({
jim's avatar
jim committed
69 70 71 72
        type: 'global/fetchNotices',
      });
    }
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
73

jim's avatar
jim committed
74
  handScroll = () => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
75 76 77
    const { autoHideHeader } = this.props;
    const { visible } = this.state;
    if (!autoHideHeader) {
jim's avatar
jim committed
78 79 80 81 82 83
      return;
    }
    const { scrollTop } = document.getElementById('root');
    if (!this.ticking) {
      this.ticking = false;
      requestAnimationFrame(() => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
84
        if (scrollTop > 400 && visible) {
jim's avatar
jim committed
85 86 87 88
          this.setState({
            visible: false,
          });
        }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
89
        if (scrollTop < 400 && !visible) {
jim's avatar
jim committed
90 91 92 93 94 95 96 97
          this.setState({
            visible: true,
          });
        }
        this.ticking = false;
      });
    }
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
98

jim's avatar
jim committed
99
  render() {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
100 101 102
    const { isMobile, handleMenuCollapse, setting } = this.props;
    const { silderTheme, layout, fixedHeader } = setting;
    const { visible } = this.state;
jim's avatar
jim committed
103
    const isTop = layout === 'topmenu';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
104
    const HeaderDom = visible ? (
jim's avatar
jim committed
105 106 107 108 109 110 111 112 113 114
      <Header
        style={{ padding: 0, width: this.getHeadWidth() }}
        className={fixedHeader ? styles.fixedHeader : ''}
      >
        {isTop && !isMobile ? (
          <TopNavHeader
            theme={silderTheme}
            mode="horizontal"
            Authorized={Authorized}
            onCollapse={handleMenuCollapse}
jim's avatar
jim committed
115
            onNoticeClear={this.handleNoticeClear}
jim's avatar
jim committed
116 117 118 119 120 121 122
            onMenuClick={this.handleMenuClick}
            onNoticeVisibleChange={this.handleNoticeVisibleChange}
            {...this.props}
          />
        ) : (
          <GlobalHeader
            onCollapse={handleMenuCollapse}
jim's avatar
jim committed
123
            onNoticeClear={this.handleNoticeClear}
jim's avatar
jim committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
            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
144
  setting,
jim's avatar
jim committed
145
}))(HeaderView);