chooseChartBox.jsx 7.8 KB

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