king
2019-09-07 c2a10e60d47895a58007201fd1fa88e453ddd653
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import React, {Component} from 'react'
// import { Lifecycle } from 'react-router'
import { withRouter } from 'react-router-dom'
import PropTypes from 'prop-types'
import {connect} from 'react-redux'
import { is, fromJS } from 'immutable'
import { Menu, Icon } from 'antd'
import {modifyTabview} from '@/store/action'
import Api from '@/api'
import './sidemenu.scss'
 
const { SubMenu } = Menu
 
class Smenu extends Component {
  static propTypes = {
    collapse: PropTypes.bool,
    mainMenu: PropTypes.oneOfType([
      PropTypes.string,
      PropTypes.object
    ])
  }
 
  state = {
    subMenulist: null,
    rootSubmenuKeys: null,
    openKeys: null
  }
 
  async loadsubmenu (menu) {
    let result = await Api.getSubMenuData(menu.MenuID)
    if (result.status) {
      this.setState({
        subMenulist: result.data,
        rootSubmenuKeys: result.data.map(item => item.id),
        openKeys: this.props.collapse ? [] : [result.data[0].id]
      })
    }
  }
 
  changemenu(e) {
    let menu = JSON.parse(e.target.dataset.item)
    let tabs = JSON.parse(JSON.stringify(this.props.tabviews))
    tabs = tabs.filter(tab => {
      tab.selected = false
      return tab.MenuID !== menu.MenuID
    })
    menu.selected = true
    tabs.push(menu)
    this.props.modifyTabview(tabs)
    // this.props.history.push('/main')
    // this.props.history.replace('/main')
    e.preventDefault()
  }
 
  // mixins = [ Lifecycle ]
 
  routerWillLeave(nextLocation) {
    if (!this.state.isSaved)
      return 'Your work is not saved! Are you sure you want to leave?'
  }
 
  componentDidMount () {
 
  }
 
  UNSAFE_componentWillMount () {
 
  }
 
  UNSAFE_componentWillReceiveProps (nextProps) {
    if (nextProps.mainMenu && !is(fromJS(this.props.mainMenu), fromJS(nextProps.mainMenu))) {
      // 主菜单切换,请求2、3级菜单数据
      this.loadsubmenu(nextProps.mainMenu)
    } else if (nextProps.collapse && this.props.collapse !== nextProps.collapse) {
      // 展开合并时,关闭展开菜单
      this.setState({
        openKeys: []
      })
    }
  }
 
  shouldComponentUpdate(nextProps, nextState) {
    if (!is(fromJS(this.props.mainMenu), fromJS(nextProps.mainMenu)) || (!this.props.collapse && !is(fromJS(this.props.tabviews), fromJS(nextProps.tabviews)))) {
      // 主菜单切换,或菜单展开下的tab页变化,不会刷新
      return false
    } else {
      return !is(fromJS(this.props), fromJS(nextProps)) || !is(fromJS(this.state), fromJS(nextState))
    }
  }
 
  componentDidUpdate () {
    // console.log('componentDidUpdate')
  }
 
  onOpenChange = openKeys => {
    const latestOpenKey = openKeys.find(key => this.state.openKeys.indexOf(key) === -1)
    if (this.state.rootSubmenuKeys.indexOf(latestOpenKey) === -1) {
      this.setState({ openKeys })
    } else {
      this.setState({
        openKeys: latestOpenKey ? [latestOpenKey] : []
      })
    }
  }
  render () {
    return (
      <aside className={"side-menu ant-menu-dark" + (this.props.collapse ? ' side-menu-collapsed' : '')}>
        {this.state.subMenulist &&
          <Menu openKeys={this.state.openKeys} onOpenChange={this.onOpenChange} mode="inline" theme="dark" inlineCollapsed={this.props.collapse}>
          {this.state.subMenulist.map(item => {
            return (
              <SubMenu
                key={item.id}
                title={
                  <span>
                    {item.icon ? <Icon type={item.icon} /> : <Icon type="folder" />}
                    <span>{item.MenuName}</span>
                  </span>
                }
              >
                {item.children.map(cell => {
                  return (
                    <Menu.Item key={cell.id}>
                      <a href="#/main/0345" id={cell.MenuID} data-item={JSON.stringify(cell)} onClick={this.changemenu.bind(this)}>{cell.MenuName}</a>
                    </Menu.Item>
                  )
                })}
              </SubMenu>
            )
          })}
        </Menu>}
      </aside>
    )
  }
}
 
const mapStateToProps = (state) => {
  return {
    tabviews: state.tabviews,
    collapse: state.collapse,
    mainMenu: state.selectedMainMenu
  }
}
 
const mapDispatchToProps = (dispatch) => {
  return {
    modifyTabview: (tabviews) => dispatch(modifyTabview(tabviews))
  }
}
 
export default withRouter(connect(mapStateToProps, mapDispatchToProps)(Smenu))