Header.js 3.77 KB
Newer Older
jim's avatar
jim committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
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,
  };
  componentDidMount() {
    document.getElementById('root').addEventListener('scroll', this.handScroll);
  }
  componentWillUnmount() {
jim's avatar
jim committed
21
    document.getElementById('root').removeEventListener('scroll', this.handScroll);
jim's avatar
jim committed
22 23
  }
  getHeadWidth = () => {
jim's avatar
jim committed
24 25
    const { fixedHeader, layout, fixSiderbar } = this.props.setting;
    if (!fixedHeader || layout === 'topmenu' || fixSiderbar) {
jim's avatar
jim committed
26 27 28 29 30 31 32 33 34
      return '100%';
    }
    if (!this.props.collapsed) {
      return 'calc(100% - 256px)';
    }
    if (this.props.collapsed) {
      return 'calc(100% - 80px)';
    }
  };
jim's avatar
jim committed
35
  handleNoticeClear = type => {
jim's avatar
jim committed
36 37 38 39 40 41 42 43
    message.success(`ζΈ…η©ΊδΊ†${type}`);
    this.props.dispatch({
      type: 'global/clearNotices',
      payload: type,
    });
  };
  handleMenuClick = ({ key }) => {
    if (key === 'userCenter') {
jim's avatar
jim committed
44
      this.props.dispatch(routerRedux.push('/account/center'));
jim's avatar
jim committed
45 46 47 48 49 50 51
      return;
    }
    if (key === 'triggerError') {
      this.props.dispatch(routerRedux.push('/exception/trigger'));
      return;
    }
    if (key === 'userinfo') {
jim's avatar
jim committed
52
      this.props.dispatch(routerRedux.push('/account/settings/base'));
jim's avatar
jim committed
53 54 55 56 57 58 59 60
      return;
    }
    if (key === 'logout') {
      this.props.dispatch({
        type: 'login/logout',
      });
    }
  };
jim's avatar
jim committed
61
  handleNoticeVisibleChange = visible => {
jim's avatar
jim committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    if (visible) {
      this.props.dispatch({
        type: 'global/fetchNotices',
      });
    }
  };
  handScroll = () => {
    if (!this.props.autoHideHeader) {
      return;
    }
    const { scrollTop } = document.getElementById('root');
    if (!this.ticking) {
      this.ticking = false;
      requestAnimationFrame(() => {
        if (scrollTop > 400 && this.state.visible) {
          this.setState({
            visible: false,
          });
        }
        if (scrollTop < 400 && !this.state.visible) {
          this.setState({
            visible: true,
          });
        }
        this.ticking = false;
      });
    }
  };
  render() {
jim's avatar
jim committed
91 92
    const { isMobile, handleMenuCollapse } = this.props;
    const { silderTheme, layout, fixedHeader } = this.props.setting;
jim's avatar
jim committed
93 94 95 96 97 98 99 100 101 102 103 104
    const isTop = layout === 'topmenu';
    const HeaderDom = this.state.visible ? (
      <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
105
            onNoticeClear={this.handleNoticeClear}
jim's avatar
jim committed
106 107 108 109 110 111 112
            onMenuClick={this.handleMenuClick}
            onNoticeVisibleChange={this.handleNoticeVisibleChange}
            {...this.props}
          />
        ) : (
          <GlobalHeader
            onCollapse={handleMenuCollapse}
jim's avatar
jim committed
113
            onNoticeClear={this.handleNoticeClear}
jim's avatar
jim committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
            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
134
  setting,
jim's avatar
jim committed
135
}))(HeaderView);