chooseChartBox.jsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import React from 'react'
  2. import '../../models/dashboardDesigner'
  3. import { Modal, Radio, Row, Col, Table, Input, Select, message, DatePicker } from 'antd'
  4. import { connect } from 'dva'
  5. import moment from 'moment'
  6. import 'braft-editor/dist/braft.css'
  7. import './chooseChartBox.less'
  8. const { Search } = Input
  9. const { RangePicker } = DatePicker
  10. class ChooseChartBox extends React.Component {
  11. constructor(props) {
  12. super(props);
  13. this.state = {
  14. filterLabel: '',
  15. selectedRecord: -1,
  16. screenWidth: document.documentElement.clientWidth || document.body.clientWidth,
  17. screenHeight: document.documentElement.clientHeight || document.body.clientHeight
  18. };
  19. }
  20. componentDidMount() {
  21. const { dispatch } = this.props;
  22. dispatch({ type: 'chart/fetchList' });
  23. window.addEventListener('resize', this.onWindowResize);
  24. }
  25. componentWillUnmount() {
  26. window.removeEventListener('resize', this.onWindowResize);
  27. }
  28. onWindowResize = () => {
  29. this.setState({
  30. screenWidth: document.documentElement.clientWidth || document.body.clientWidth,
  31. screenHeight: document.documentElement.clientHeight || document.body.clientHeight
  32. });
  33. }
  34. changeSelected = (record) => {
  35. this.setState({
  36. selectedRecord: record
  37. });
  38. }
  39. okHandler = (model) => {
  40. const { selectedRecord } = this.state;
  41. const { dispatch, hideBox } = this.props;
  42. if(selectedRecord) {
  43. dispatch({ type: 'dashboardDesigner/addChart', chart: selectedRecord });
  44. hideBox();
  45. }else {
  46. message.warning('未选中图表');
  47. }
  48. }
  49. generateFilterItems = () => {
  50. const { filterItems } = this.props.chart;
  51. return filterItems.map(t => <Select.Option key={t.name} value={t.name}>{t.label}</Select.Option>);
  52. }
  53. render() {
  54. const { main, visibleBox, hideBox, dashboardDesigner, chart, dispatch } = this.props;
  55. const { selectedRecord, screenWidth, screenHeight } = this.state;
  56. const tableBodyWidth = screenWidth * 0.8 - 10 - 10 - 18;
  57. const tableBodyHeight = screenHeight * 0.8 - 65 - 53 - 38 - 130;
  58. const tableRowHeight = 38;
  59. const { filterItem } = dashboardDesigner;
  60. const reg = new RegExp('([+ \\- & | ! ( ) { } \\[ \\] ^ \" ~ * ? : ( ) \/])', 'g'); // 需要转义的字符
  61. let filterLabel = this.state.filterLabel.replace(new RegExp('(\\\\)', 'g'), '\\$1').replace(reg, '\\$1'); // 添加转义符号
  62. const columns = [{
  63. title: '选择',
  64. key: 'selected',
  65. width: 50,
  66. render: (text, record) => {
  67. // 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}/>
  68. return <Radio
  69. checked={dashboardDesigner.items.findIndex(i => i.chartCode===record.code)!==-1 || selectedRecord.code === record.code }
  70. disabled={dashboardDesigner.items.findIndex(i => i.chartCode===record.code)!==-1}
  71. />
  72. }
  73. }, {
  74. title: '名称',
  75. dataIndex: 'name',
  76. key: 'name',
  77. width: 100,
  78. render: (text, record) => {
  79. return <div>
  80. <div>
  81. <span>
  82. { filterLabel && filterItem.name === 'name' ?
  83. (text.split(new RegExp(`(${filterLabel})`, 'i')).map((fragment, i) => {
  84. return (
  85. fragment.toLowerCase().replace(new RegExp('(\\\\)', 'g'), '\\$1').replace(reg, '\\$1') === filterLabel.toLowerCase() ?
  86. <span key={i} style={{fontWeight: 'bold', color: 'red'}} className="highlight">{fragment}</span> :
  87. fragment
  88. )
  89. }
  90. )) : text
  91. }
  92. </span>
  93. </div>
  94. </div>
  95. }
  96. }, {
  97. title: '创建人',
  98. dataIndex: 'creatorName',
  99. key: 'creatorName',
  100. width: 100,
  101. render: (text, record) => {
  102. return <div>
  103. <div>
  104. <span>
  105. { filterLabel && filterItem.name === 'creatorName' ?
  106. (text.split(new RegExp(`(${filterLabel})`, 'i')).map((fragment, i) => {
  107. return (
  108. fragment.toLowerCase().replace(new RegExp('(\\\\)', 'g'), '\\$1').replace(reg, '\\$1') === filterLabel.toLowerCase() ?
  109. <span key={i} style={{fontWeight: 'bold', color: 'red'}} className="highlight">{fragment}</span> :
  110. fragment
  111. )
  112. }
  113. )) : text
  114. }
  115. </span>
  116. </div>
  117. </div>
  118. }
  119. }, {
  120. title: '创建时间',
  121. dataIndex: 'createTime',
  122. key: 'createTime',
  123. width: 120,
  124. render: (text) => moment(text).format('YYYY-MM-DD')
  125. }, {
  126. title: '说明',
  127. dataIndex: 'description',
  128. key: 'description',
  129. width: 200,
  130. render: (text, record) => {
  131. return (
  132. <span>
  133. { filterLabel && filterItem.name === 'description' ?
  134. ((text || '').split(new RegExp(`(${filterLabel})`, 'i')).map((fragment, i) => {
  135. return (
  136. fragment.toLowerCase().replace(new RegExp('(\\\\)', 'g'), '\\$1').replace(reg, '\\$1') === filterLabel.toLowerCase() ?
  137. <span key={i} style={{fontWeight: 'bold', color: 'red'}} className="highlight">{fragment}</span> :
  138. fragment
  139. )
  140. }
  141. )) : text
  142. }
  143. </span>
  144. )
  145. }
  146. }]
  147. return (
  148. <Modal
  149. className='choosechart-box'
  150. width='80%'
  151. height='80%'
  152. title={
  153. <Row>
  154. <Row>
  155. <Col >选择图表</Col>
  156. </Row>
  157. <Row type='flex' justify='end'>
  158. <Col style={{ padding: '0 1px', margin: '0 -5px' }}>
  159. <Select
  160. value={filterItem.name}
  161. style={{ width: 120 }}
  162. onChange={value => {
  163. let item = chart.filterItems.find(i => i.name === value);
  164. dispatch({ type: 'dashboardDesigner/setFilterItem', item });
  165. }}
  166. >
  167. {this.generateFilterItems()}
  168. </Select>
  169. </Col>
  170. <Col style={{ padding: '0 5px' }}>
  171. {filterItem.type === 'date' ?
  172. <RangePicker
  173. ranges={{
  174. '今天': [moment().startOf('day'), moment().endOf('day')],
  175. '昨天': [moment().startOf('day').add(-1,'days'), moment().endOf('day')],
  176. '近七天': [moment().startOf('day'),moment().endOf('day').add(6,'days')],
  177. '本月': [moment().startOf('month'), moment().endOf('month')],
  178. '本年': [moment().startOf('year'), moment().endOf('year')]
  179. }}
  180. showTime={{ format: 'HH:mm' }}
  181. format="YYYY-MM-DD HH:mm:ss"
  182. onChange={e => {
  183. //清空时间时
  184. if(e.length === 0){
  185. this.setState({
  186. filterLabel: ''
  187. });
  188. }
  189. }}
  190. onOk={e => {
  191. //解析时间格式
  192. let start = e[0]
  193. let end = e[1]
  194. let time = start._d.getTime() + "#" + end._d.getTime()
  195. this.setState({
  196. filterLabel: time
  197. });
  198. }}
  199. >
  200. </RangePicker>
  201. :
  202. <Search
  203. placeholder="请输入关键字"
  204. value={this.state.filterLabel}
  205. onChange={e => {
  206. this.setState({
  207. filterLabel: e.target.value
  208. });
  209. }}
  210. />
  211. }
  212. </Col>
  213. </Row>
  214. </Row>
  215. }
  216. visible={visibleBox}
  217. onOk={() => {this.setState({ filterLabel: '', selectedRecord: -1 });this.okHandler()}}
  218. onCancel={() => {this.setState({ filterLabel: '', selectedRecord: -1 });hideBox()}}
  219. maskClosable={false}
  220. destroyOnClose={true}
  221. >
  222. <Table
  223. className='choosechart-table'
  224. columns={columns.map(c => ({
  225. ...c,
  226. width: 100
  227. }))}
  228. dataSource={chart.list.map((l, i) => {
  229. let o = Object.assign({}, l);
  230. if(filterItem.type === 'date') {
  231. if(filterLabel===""){
  232. return { ...o, key: i };
  233. }else if(filterLabel.indexOf('#')>-1){
  234. let start = filterLabel.split('#')[0]
  235. let end = filterLabel.split('#')[1]
  236. let nowTime = new Date(o[filterItem.name]).getTime();
  237. if(nowTime>=start && nowTime<=end){
  238. return { ...o, key: i};
  239. }
  240. return null
  241. }else{
  242. return null
  243. }
  244. }else {
  245. return ((o[filterItem.name] + '').search(new RegExp('(' + filterLabel + '){1}', 'ig')) > -1) ? { ...o, key: i } : null
  246. }
  247. }).filter(a => a!==null && a.creatorCode === main.currentUser.code)}
  248. size='small'
  249. scroll={{ x: columns ? columns.length * 100 : tableBodyWidth, y: tableBodyHeight }}
  250. pagination={{ defaultPageSize: Math.floor(tableBodyHeight/tableRowHeight) || 10 }}
  251. onRow={(record) => {
  252. return {
  253. onClick: () => {
  254. if(dashboardDesigner.items.findIndex(i => i.chartCode===record.code)===-1) {
  255. this.changeSelected(record);
  256. }
  257. }
  258. };
  259. }}
  260. />
  261. </Modal>
  262. )
  263. }
  264. }
  265. function mapStateToProps({ present: { main, dashboardDesigner, chart } }) {
  266. return { main, dashboardDesigner, chart };
  267. }
  268. export default connect(mapStateToProps)(ChooseChartBox)