chooseChartBox.jsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import React from 'react'
  2. import '../../models/dashboardDesigner'
  3. import { Modal, Checkbox, Row, Col, Table, Input, message } from 'antd'
  4. import { connect } from 'dva'
  5. import { dateFormat } from '../../utils/baseUtils'
  6. import 'braft-editor/dist/braft.css'
  7. import './chooseChartBox.less'
  8. const { Search } = Input
  9. class ChooseChartBox extends React.Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. filterLabel: '',
  14. selectedRecord: []
  15. };
  16. }
  17. componentDidMount() {
  18. const { dispatch } = this.props;
  19. dispatch({ type: 'chart/fetchList' });
  20. }
  21. changeSelected = (record) => {
  22. let arr = this.state.selectedRecord;
  23. let findIndex = arr.findIndex(a => a.code === record.code);
  24. if(findIndex !== -1) {
  25. arr.splice(findIndex, 1)
  26. }else {
  27. arr.push(record);
  28. }
  29. this.setState({
  30. selectedRecord: arr
  31. });
  32. }
  33. okHandler = (model) => {
  34. const { selectedRecord } = this.state;
  35. const { dispatch, hideBox } = this.props;
  36. if(selectedRecord) {
  37. dispatch({ type: 'dashboardDesigner/addCharts', charts: selectedRecord });
  38. hideBox();
  39. }else {
  40. message.warning('未选中图表');
  41. }
  42. }
  43. handleChange = (content) => {
  44. }
  45. handleRawChange = (rawContent) => {
  46. }
  47. static editorProps = {
  48. height: 300,
  49. contentFormat: 'raw',
  50. initialContent: '<p>Hello World!</p>',
  51. onChange: () => {},
  52. onRawChange: () => {},
  53. image: true, // 开启图片插入功能?
  54. }
  55. render() {
  56. const { visibleBox, hideBox, dashboardDesigner, chart } = this.props;
  57. const { selectedRecord } = this.state;
  58. const reg = new RegExp('([+ \\- & | ! ( ) { } \\[ \\] ^ \" ~ * ? : ( ) \/])', 'g'); // 需要转义的字符
  59. let filterLabel = this.state.filterLabel.replace(new RegExp('(\\\\)', 'g'), '\\$1').replace(reg, '\\$1'); // 添加转义符号
  60. const columns = [{
  61. title: '选择',
  62. key: 'selected',
  63. width: 50,
  64. render: (text, record) => {
  65. return <Checkbox checked={dashboardDesigner.items.findIndex(i => i.chartCode===record.code)!==-1 || selectedRecord.findIndex(s => s.code === record.code) !== -1 } disabled={dashboardDesigner.items.findIndex(i => i.chartCode===record.code)!==-1}/>
  66. }
  67. }, {
  68. title: '名称',
  69. dataIndex: 'name',
  70. key: 'name',
  71. width: 100,
  72. render: (text, record) => {
  73. return <div>
  74. <div>
  75. <span>
  76. { filterLabel ?
  77. (text.split(new RegExp(`(${filterLabel})`, 'i')).map((fragment, i) => {
  78. return (
  79. fragment.toLowerCase().replace(new RegExp('(\\\\)', 'g'), '\\$1').replace(reg, '\\$1') === filterLabel.toLowerCase() ?
  80. <span key={i} style={{fontWeight: 'bold', color: 'red'}} className="highlight">{fragment}</span> :
  81. fragment
  82. )
  83. }
  84. )) : text
  85. }
  86. </span>
  87. </div>
  88. </div>
  89. }
  90. }, {
  91. title: '创建人',
  92. dataIndex: 'creator',
  93. key: 'creator',
  94. width: 100
  95. }, {
  96. title: '创建时间',
  97. dataIndex: 'createTime',
  98. key: 'createTime',
  99. width: 120,
  100. render: (text) => dateFormat(new Date(text), 'yyyy-MM-dd')
  101. }, {
  102. title: '说明',
  103. dataIndex: 'description',
  104. key: 'description',
  105. width: 200,
  106. render: (text, record) => {
  107. return (
  108. <span>
  109. { filterLabel ?
  110. ((text || '').split(new RegExp(`(${filterLabel})`, 'i')).map((fragment, i) => {
  111. return (
  112. fragment.toLowerCase().replace(new RegExp('(\\\\)', 'g'), '\\$1').replace(reg, '\\$1') === filterLabel.toLowerCase() ?
  113. <span key={i} style={{fontWeight: 'bold', color: 'red'}} className="highlight">{fragment}</span> :
  114. fragment
  115. )
  116. }
  117. )) : text
  118. }
  119. </span>
  120. )
  121. }
  122. }]
  123. return (
  124. <Modal
  125. className='choosechart-box'
  126. title={
  127. <Row>
  128. <Col span={14}>选择图表</Col>
  129. <Col span={8}><Search
  130. placeholder="请输入关键字"
  131. value={this.state.filterLabel}
  132. onChange={e => {
  133. this.setState({
  134. filterLabel: e.target.value
  135. });
  136. }}
  137. /></Col>
  138. </Row>
  139. }
  140. visible={visibleBox}
  141. onOk={() => {this.setState({ filterLabel: '', selectedRecord: [] });this.okHandler()}}
  142. onCancel={() => {this.setState({ filterLabel: '', selectedRecord: [] });hideBox()}}
  143. maskClosable={false}
  144. destroyOnClose={true}
  145. >
  146. <Table
  147. className='choosechart-table'
  148. columns={columns}
  149. dataSource={chart.list.map((l, i) => ({ ...l, key: i})).filter(l => {
  150. let regLabel = new RegExp('(' + filterLabel + '){1}', 'ig');
  151. return (l.name || '').search(regLabel) !== -1 || (l.description || '').search(regLabel) !== -1;
  152. })}
  153. size='small'
  154. scroll={{x: false, y: 471}}
  155. pagination={false}
  156. onRow={(record) => {
  157. return {
  158. onClick: () => {
  159. if(dashboardDesigner.items.findIndex(i => i.chartCode===record.code)===-1) {
  160. this.changeSelected(record);
  161. }
  162. }
  163. };
  164. }}
  165. />
  166. </Modal>
  167. )
  168. // return (
  169. // <Modal
  170. // className='element-config'
  171. // title={`${operation==='create'?'新增':'修改'}元素`}
  172. // visible={visibleBox}
  173. // onOk={() => {this.okHandler()}}
  174. // onCancel={hideElementConfigBox}
  175. // maskClosable={false}
  176. // destroyOnClose={true}
  177. // >
  178. // <div>
  179. // <Select defaultValue={dashboardDesigner.configBoxForm.type} style={{ width: 120 }}
  180. // onChange={(value) => { dispatch( {type: 'dashboardDesigner/handleFieldChange', name: 'type', value: value})}}>
  181. // <Option value="chart">图表</Option>
  182. // <Option value="simple">基础元素</Option>
  183. // </Select>
  184. // </div>
  185. // <div style={{display: `${dashboardDesigner.configBoxForm.type === "simple" ? "inline":"none"}`}}>
  186. // <BraftEditor {...this.editorProps}/>
  187. // </div>
  188. // </Modal>
  189. // )
  190. }
  191. }
  192. function mapStateToProps({ present: { dashboardDesigner, chart } }) {
  193. return { dashboardDesigner, chart };
  194. }
  195. export default connect(mapStateToProps)(ChooseChartBox)