king
2020-09-16 c34bcb0a3054bdab29fbaff17e587c19d7b5de28
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Tabs, Icon, Popover, Modal } from 'antd'
 
import MKEmitter from '@/utils/events.js'
import asyncComponent from '@/utils/asyncComponent'
 
import Utils from '@/utils/utils.js'
import zhCN from '@/locales/zh-CN/model.js'
import enUS from '@/locales/en-US/model.js'
import './index.scss'
 
const SettingComponent = asyncComponent(() => import('../tabsetting'))
const TabLabelComponent = asyncComponent(() => import('../tablabelform'))
const TabComponents = asyncComponent(() => import('../tabcomponents'))
 
const { TabPane } = Tabs
const { confirm } = Modal
 
class antvBarLineChart extends Component {
  static propTpyes = {
    menu: PropTypes.object,
    tabs: PropTypes.object,
    updateConfig: PropTypes.func,
  }
 
  state = {
    dict: localStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
    tabs: null,
    editab: null,
    labelvisible: false
  }
 
  UNSAFE_componentWillMount () {
    const { tabs } = this.props
 
    if (tabs.isNew) {
      let _tabs = {
        uuid: tabs.uuid,
        type: tabs.type,
        floor: tabs.floor,
        subtype: tabs.subtype,
        setting: {span: 12, position: 'top', tabStyle: 'line', name: tabs.name},
        subtabs: [
          { uuid: Utils.getuuid(), label: 'Tab 1', icon: '', components: [] },
          { uuid: Utils.getuuid(), label: 'Tab 2', icon: '', components: [] },
          { uuid: Utils.getuuid(), label: 'Tab 3', icon: '', components: [] }
        ]
      }
      this.setState({
        tabs: _tabs
      })
      this.props.updateConfig(_tabs)
    } else {
      this.setState({
        tabs: fromJS(tabs).toJS()
      })
    }
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.state), fromJS(nextState))
  }
 
  updateComponent = (component) => {
    const { tabs } = this.state
 
    if (!is(fromJS(tabs.setting), fromJS(component.setting))) {
      // 注册事件-标签变化,通知标签内元素
      MKEmitter.emit('tabsChange', tabs.uuid)
    }
 
    this.setState({
      tabs: component
    })
    this.props.updateConfig(component)
  }
 
  updateTabComponent = (tab) => {
    let tabs = fromJS(this.state.tabs).toJS()
 
    tabs.subtabs = tabs.subtabs.map(t => {
      if (t.uuid === tab.uuid) {
        return tab
      } else {
        return t
      }
    })
 
    this.setState({tabs})
    this.props.updateConfig(tabs)
  }
 
  tabAdd = (e) => {
    e.stopPropagation()
    this.setState({
      editab: {
        uuid: '',
        label: '',
        icon: '',
        components: []
      },
      labelvisible: true
    })
  }
 
  editTab = (tab) => {
    this.setState({
      editab: tab,
      labelvisible: true
    })
  }
 
  tabLabelSubmit = () => {
    let tabs = fromJS(this.state.tabs).toJS()
    let editab = fromJS(this.state.editab).toJS()
 
    this.tabLabelRef.handleConfirm().then(res => {
      editab.label = res.label
      editab.icon = res.icon
 
      if (editab.uuid) {
        tabs.subtabs = tabs.subtabs.map(t => {
          if (t.uuid === editab.uuid) {
            return editab
          } else {
            return t
          }
        })
      } else {
        editab.uuid = Utils.getuuid()
        tabs.subtabs.push(editab)
      }
 
      this.setState({
        editab: null,
        labelvisible: false,
        tabs
      })
      this.props.updateConfig(tabs)
    })
  }
 
  delTab = (tab) => {
    let tabs = fromJS(this.state.tabs).toJS()
    const _this = this
 
    tabs.subtabs = tabs.subtabs.filter(t => t.uuid !== tab.uuid)
 
    confirm({
      title: '确定删除标签?',
      content: '',
      onOk() {
        _this.setState({tabs})
        _this.props.updateConfig(tabs)
      },
      onCancel() {}
    })
  }
 
  moveSwitch = (index, index1) => {
    let tabs = fromJS(this.state.tabs).toJS()
    tabs.subtabs[index] = tabs.subtabs.splice(index1, 1, tabs.subtabs[index])[0]
 
    this.setState({tabs})
    this.props.updateConfig(tabs)
  }
 
  render() {
    const { menu } = this.props
    const { tabs, dict, labelvisible, editab } = this.state
 
    return (
      <div className="menu-tabs-edit-box">
        <SettingComponent config={tabs} updateConfig={this.updateComponent} />
        <Tabs defaultActiveKey="1" tabPosition={tabs.setting.position} type={tabs.setting.tabStyle}>
          {tabs.subtabs.map((tab, index) => (
            <TabPane tab={
              <Popover overlayClassName="mk-popover-control-wrap" mouseLeaveDelay={0.2} mouseEnterDelay={0.2} content={
                <div className="mk-popover-control">
                  <Icon className="edit" title="edit" type="edit" onClick={() => this.editTab(tab)} />
                  {index !== 0 ? <Icon className="edit" type="arrow-left" onClick={() => this.moveSwitch(index, index - 1)} /> : null}
                  {(index + 1) !== tabs.subtabs.length ? <Icon className="edit" type="arrow-right" onClick={() => this.moveSwitch(index, index + 1)} /> : null}
                  <Icon className="close" title="delete" type="close" onClick={() => this.delTab(tab)} />
                </div>
              } trigger="hover">
                <span>{tab.icon ? <Icon type={tab.icon} /> : null}{tab.label}</span>
              </Popover>
            } key={tab.uuid}>
              <TabComponents menu={menu} parentId={tabs.uuid} config={tab} handleList={this.updateTabComponent} deleteCard={this.deleteCard} />
            </TabPane>
          ))}
          <TabPane className="tab-add" disabled tab={<Icon onClick={this.tabAdd} type="plus" />} key="add"></TabPane>
        </Tabs>
        <Modal
          wrapClassName="popview-modal"
          title={'标签编辑'}
          visible={labelvisible}
          width={600}
          maskClosable={false}
          okText={dict['model.submit']}
          onOk={this.tabLabelSubmit}
          onCancel={() => { this.setState({ labelvisible: false }) }}
          destroyOnClose
        >
          <TabLabelComponent
            dict={dict}
            tab={editab}
            wrappedComponentRef={(inst) => this.tabLabelRef = inst}
          />
        </Modal>
      </div>
    )
  }
}
 
export default antvBarLineChart