king
2024-08-25 326aa6b3effaccc71cfe0775d47b0f29eb3695a6
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Modal } from 'antd'
 
import MKEmitter from '@/utils/events.js'
import Utils from '@/utils/utils.js'
import ElementForm from './menuform'
import DragElement from './drags'
// import './index.scss'
 
const { confirm } = Modal
 
class MenusComponent extends Component {
  static propTpyes = {
    menus: PropTypes.array,          // 菜单列表
    columns: PropTypes.array,        // 字段集
    updateConfig: PropTypes.func     // 菜单配置更新
  }
 
  state = {
    menus: [],
    columns: [],
    visible: false
  }
 
  /**
   * @description 初始化
   */
  UNSAFE_componentWillMount () {
    const { menus } = this.props
 
    this.setState({
      menus: fromJS(menus).toJS()
    })
  }
 
  componentDidMount () {
    MKEmitter.addListener('addmobmenu', this.addmobmenu)
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.state), fromJS(nextState))
  }
 
  /**
   * @description 组件销毁,清除state更新,清除快捷键设置
   */
  componentWillUnmount () {
    this.setState = () => {
      return
    }
    MKEmitter.removeListener('addmobmenu', this.addmobmenu)
  }
 
  addmobmenu = () => {
    let _menu = {
      name: '菜单',
      property: 'menu'
    }
 
    this.setState({editMenu: _menu, visible: true})
  }
 
  /**
   * @description 顺序调整
   */
  handleList = (list) => {
    this.setState({menus: list}, () => {
      this.props.updateConfig(list)
    })
  }
 
  /**
   * @description 元素编辑
   */
  handleElement = (menu) => {
    this.setState({
      visible: true,
      editMenu: menu
    })
  }
 
  /**
   * @description 取消保存,如果元素为新添元素,则从序列中删除
   */
  editModalCancel = () => {
    this.setState({
      editMenu: null,
      visible: false
    })
  }
 
  /**
   * @description 元素修改后提交保存
   */
  handleSubmit = () => {
    const { menus, editMenu } = this.state
 
    this.menuFormRef.handleConfirm().then(res => {
      let _menus = fromJS(menus).toJS()
 
      if (editMenu.MenuID) {
        res.MenuID = editMenu.MenuID
 
        _menus = _menus.map(item => {
          if (item.MenuID === res.MenuID) return res
          return item
        })
      } else {
        res.MenuID = Utils.getuuid()
 
        _menus.push(res)
      }
 
      if (editMenu.MenuID && editMenu.property === 'menu' && res.property !== 'menu') {
        const that = this
        confirm({
          content: '菜单将被重置,确定修改吗?',
          onOk() {
            _menus = _menus.map(item => {
              if (item.MenuID === res.MenuID) {
                item.MenuID = Utils.getuuid()
              }
              return item
            })
            that.setState({menus: _menus, editMenu: null, visible: false}, () => {
              that.props.updateConfig(_menus)
            })
          },
          onCancel() {}
        })
      } else {
        this.setState({menus: _menus, editMenu: null, visible: false}, () => {
          this.props.updateConfig(_menus)
        })
      }
    })
  }
 
  /**
   * @description 按钮删除
   */
  deleteElement = (card) => {
    const { menus } = this.state
    let that = this
 
    confirm({
      content: `确定删除《${card.name}》吗?`,
      onOk() {
        let _menus = menus.filter(item => item.MenuID !== card.MenuID)
 
        that.setState({
          menus: _menus
        }, () => {
          that.props.updateConfig(_menus)
        })
      },
      onCancel() {}
    })
  }
 
  render() {
    const { columns } = this.props
    const { menus, visible, editMenu } = this.state
 
    return (
      <>
        <DragElement
          list={menus}
          handleList={this.handleList}
          handleMenu={this.handleElement}
          deleteMenu={this.deleteElement}
        />
        <Modal
          title={editMenu && !editMenu.MenuID ? '添加菜单' : '编辑菜单'}
          visible={visible}
          width={800}
          maskClosable={false}
          onCancel={this.editModalCancel}
          onOk={this.handleSubmit}
          destroyOnClose
        >
          <ElementForm
            menu={editMenu}
            cols={columns}
            inputSubmit={this.handleSubmit}
            wrappedComponentRef={(inst) => this.menuFormRef = inst}
          />
        </Modal>
      </>
    )
  }
}
 
export default MenusComponent