king
2020-10-13 8f6b3d26bde4e22773cc53386dfbae669a7472ed
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
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Button, Icon, Col, Tooltip } from 'antd'
 
import zhCN from '@/locales/zh-CN/model.js'
import enUS from '@/locales/en-US/model.js'
 
import './index.scss'
 
class CardCellComponent extends Component {
  static propTpyes = {
    cards: PropTypes.object,         // 菜单配置信息
    cardCell: PropTypes.object,
    data: PropTypes.object,
    elements: PropTypes.array,       // 元素集
  }
 
  state = {
    dict: localStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
    card: null,          // 编辑中元素
    elements: null,      // 按钮组
  }
 
  /**
   * @description 搜索条件初始化
   */
  UNSAFE_componentWillMount () {
    const { elements } = this.props
 
    this.setState({
      elements: fromJS(elements).toJS()
    })
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.state), fromJS(nextState))
  }
 
  /**
   * @description 组件销毁,清除state更新,清除快捷键设置
   */
  componentWillUnmount () {
    this.setState = () => {
      return
    }
  }
 
  getContent = (card) => {
    const { data } = this.props
 
    if (card.eleType === 'text' || card.eleType === 'number') {
      let val = ''
 
      if (card.datatype === 'static') {
        val = card.value
      } else if (data.hasOwnProperty(card.field)) {
        val = data[card.field]
      }
 
      if (val !== '') {
        val = `${card.prefix || ''}${val}${card.postfix || ''}`
      }
 
      return (
        <Col key={card.uuid} span={card.width}>
          <div style={card.style}>{val}</div>
        </Col>
      )
    } else if (card.eleType === 'icon') {
      let val = ''
 
      if (card.datatype === 'static') {
        val = card.tooltip
      } else if (data.hasOwnProperty(card.field)) {
        val = data[card.field]
      }
 
      return (
        <Col key={card.uuid} span={card.width}>
          <div style={card.style}>
            {val ? <Tooltip title={val}>
              <Icon type={card.icon}/>
            </Tooltip> : <Icon type={card.icon}/>}
          </div>
        </Col>
      )
    } else if (card.eleType === 'slider') {
      let val = 0
 
      if (data.hasOwnProperty(card.field)) {
        val = parseFloat(data[card.field])
        if (isNaN(val)) {
          val = 0
        }
      }
 
      val = val / card.maxValue * 100
      val = parseInt(val * 100) / 100
 
      let _val = val
      if (val > 100) {
        _val = '100%'
      } else {
        _val = `${val}%`
      }
 
      return (
        <Col key={card.uuid} span={card.width}>
          <div style={card.style}>
            <div className="ant-mk-slider">
              <div className="ant-mk-slider-rail"></div>
              <div className="ant-mk-slider-track" style={{width: _val, backgroundColor: card.color}}></div>
              <Tooltip title={val}>
                <div className="ant-mk-slider-handle" style={{left: _val, borderColor: card.color}}></div>
              </Tooltip>
            </div>
          </div>
        </Col>
      )
    } else if (card.eleType === 'picture') {
      let _imagestyle = {}
 
      if (card.url) {
        _imagestyle = {backgroundImage: `url('${card.url}')`}
      } else {
        _imagestyle = {backgroundImage: `url('')`}
      }
 
      if (card.radius === 'true') {
        _imagestyle.borderRadius = '50%'
      }
 
      if (card.lenWidRadio === '16:9') {
        _imagestyle.paddingTop = '56.25%'
      } else if (card.lenWidRadio === '3:2') {
        _imagestyle.paddingTop = '66.67%'
      } else if (card.lenWidRadio === '4:3') {
        _imagestyle.paddingTop = '75%'
      } else {
        _imagestyle.paddingTop = '100%'
      }
 
      return (
        <Col key={card.uuid} span={card.width}>
          <div style={card.style}>
            <div className="ant-mk-picture" style={_imagestyle}></div>
          </div>
        </Col>
      )
    } else if (card.eleType === 'splitline') {
      return (
        <Col key={card.uuid} span={card.width}>
          <div style={card.style}>
            <div className="ant-mk-splitline" style={{backgroundColor: card.color}}></div>
          </div>
        </Col>
      )
    } else if (card.eleType === 'button') {
      if (card.show === 'icon') {
        return (
          <Col key={card.uuid} span={card.width}>
            <div style={card.style}>
              <Button className={'mk-link mk-' + card.class} style={card.btnstyle} type="link"><Icon type={card.icon}/></Button>
            </div>
          </Col>
        )
      } else if (card.show === 'link') {
        return (
          <Col key={card.uuid} span={card.width}>
            <div style={card.style}>
              <Button className={'mk-link mk-' + card.class} style={card.btnstyle} type="link">{card.label}{card.icon ? <Icon type={card.icon}/> : null}</Button>
            </div>
          </Col>
        )
      } else {
        return (
          <Col key={card.uuid} span={card.width}>
            <div style={card.style}>
              <Button
                className={'mk-btn mk-' + card.class}
                icon={card.icon}
                style={card.btnstyle}
              >
                {card.label}
              </Button>
            </div>
          </Col>
        )
      }
    }
  }
 
  render() {
    const { elements } = this.state
 
    return (
      <div className="card-cell-list">
        {elements.map(item => this.getContent(item))}
      </div>
    )
  }
}
 
export default CardCellComponent