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

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 = () => {
柴茂源's avatar
柴茂源 committed
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%';
    }
柴茂源's avatar
柴茂源 committed
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);