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
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Table, Button, Modal } from 'antd'
import { ArrowUpOutlined, ArrowDownOutlined } from '@ant-design/icons'
 
import LinkForm from '../linkform'
import Utils from '@/utils/utils.js'
import './index.scss'
 
const { confirm } = Modal
 
class LinkTable extends Component {
  static propTpyes = {
    links: PropTypes.object,    // 卡片行信息
  }
 
  state = {
    data: [],
    editMenu: null,
    columns: [
      { title: '链接名称', dataIndex: 'name', key: 'name' },
      { title: '链接属性', dataIndex: 'property', key: 'property',  render: text => {
        const trans = {link: '链接', linkmenu: '关联菜单'}
 
        return trans[text]
      }},
      { title: '链接地址', dataIndex: 'link', key: 'link'},
      { title: '打开方式', dataIndex: 'open', key: 'open',  render: (text, record) => {
        const trans = {blank: '新窗口', self: '当前窗口'}
 
        return trans[text]
      }},
      { title: '操作', key: 'operation', align: 'center', width: '190px', render: (text, record) =>
        (<div>
          <Button type="link" style={{padding: '0 5px', marginRight: '5px'}} onClick={() => this.editMenu(record)}>编辑</Button>
          <Button type="link" style={{color: '#ff4d4f', padding: '0 5px', marginRight: '5px'}} onClick={() => this.delMenu(record)}>删除</Button>
          <ArrowUpOutlined style={{color: '#26C281', cursor: 'pointer', padding: '0 5px', marginRight: '5px'}} onClick={() => this.moveUp(record)}/>
          <ArrowDownOutlined style={{color: '#ff4d4f', cursor: 'pointer', padding: '0 5px'}} onClick={() => this.moveDown(record)}/>
        </div>)
      }
    ]
  }
 
  UNSAFE_componentWillMount () {
    const { links } = this.props
 
    this.setState({data: fromJS(links).toJS()})
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.state), fromJS(nextState))
  }
 
  moveUp = (record) => {
    let data = fromJS(this.state.data).toJS()
 
    let dragIndex = data.findIndex(c => c.MenuID === record.MenuID)
    let hoverIndex = dragIndex - 1
 
    if (hoverIndex === -1) return
 
    data.splice(hoverIndex, 0, ...data.splice(dragIndex, 1))
    this.setState({data})
  }
 
  moveDown = (record) => {
    let data = fromJS(this.state.data).toJS()
 
    let dragIndex = data.findIndex(c => c.MenuID === record.MenuID)
    let hoverIndex = dragIndex + 1
 
    if (hoverIndex === data.length) return
 
    data.splice(hoverIndex, 0, ...data.splice(dragIndex, 1))
    this.setState({data})
  }
 
  delMenu = (record) => {
    const { data } = this.state
    const that = this
 
    confirm({
      title: '确定删除吗?',
      content: '',
      onOk() {
        that.setState({data: data.filter(item => item.MenuID !== record.MenuID)})
      },
      onCancel() {}
    })
  }
 
  editMenu = (record) => {
    this.setState({editMenu: record, visible: true})
  }
 
  plusMenu = () => {
    let _menu = {
      name: '链接'
    }
 
    this.setState({editMenu: _menu, visible: true})
  }
 
  menuSubmit = () => {
    const { editMenu, data } = this.state
 
    this.menuRef.handleConfirm().then(res => {
      let _menu = {...editMenu, ...res}
      if (!_menu.MenuID) {
        _menu.MenuID = Utils.getuuid()
        this.setState({data: [...data, _menu], editMenu: null, visible: false})
      } else {
        this.setState({
          editMenu: null,
          visible: false,
          data: data.map(item => {
            if (item.MenuID === _menu.MenuID) {
              return _menu
            } else {
              return item
            }
          })
        })
      }
    })
  }
 
  render() {
    const { columns, data, visible, editMenu } = this.state
 
    return (
      <div className="link-control-wrap">
        <Button className="link-plus mk-green" onClick={this.plusMenu}>添加</Button>
        <Table
          rowKey="MenuID"
          columns={columns}
          dataSource={data}
          pagination={false}
        />
        <Modal
          title="编辑"
          visible={visible}
          width={600}
          maskClosable={false}
          onOk={this.menuSubmit}
          onCancel={() => { this.setState({ visible: false }) }}
          destroyOnClose
        >
          <LinkForm
            menu={editMenu}
            inputSubmit={this.menuSubmit}
            wrappedComponentRef={(inst) => this.menuRef = inst}
          />
        </Modal>
      </div>
    )
  }
}
 
export default LinkTable