toolbar.jsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import React from 'react';
  2. import { Tag, Icon, Modal, Button } from 'antd';
  3. import FilterBox from './filterBox';
  4. import { connect } from 'dva';
  5. import chartDesigner from '../../../models/chartDesigner';
  6. import './toolbar.less';
  7. class Toolbar extends React.Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. filters: props.chartDesigner.filters || [],
  12. visibleFilterBox: false
  13. }
  14. }
  15. filterUsingChange = (e) => {
  16. const key = e.target.dataset.key;
  17. const props = this.props;
  18. const chartDesigner = props.chartDesigner;
  19. const filters = chartDesigner.filters;
  20. props.dispatch({ type: 'chartDesigner/setModel', name: 'filters', value: filters.map( f => {
  21. if(f.key == key) {
  22. f = { ...f, using: !f.using }
  23. }
  24. return f;
  25. }) });
  26. }
  27. showFilterBox = (e) => {
  28. this.setState({
  29. visibleFilterBox: true
  30. });
  31. }
  32. hideFilterBox = (e) => {
  33. this.setState({
  34. visibleFilterBox: false,
  35. });
  36. }
  37. createFilters = (filters) => {
  38. this.props.dispatch({ type: 'chartDesigner/setModel', name: 'filters', value: filters });
  39. this.hideFilterBox()
  40. }
  41. /**
  42. * 生成过滤规则文本
  43. */
  44. createFilterLabel = (filter) => {
  45. let { label, name, operator, operatorLabel, type, using, value1, value2 } = filter;
  46. let filterLabel;
  47. if(type == 'string' || type == 'index') {
  48. if(operator == 'null') {
  49. filterLabel = `${label} ${operatorLabel}`;
  50. }else {
  51. filterLabel = `${label} ${operatorLabel} ${value1}`;
  52. }
  53. }else if(type == 'scale') {
  54. if(operator == 'null') {
  55. filterLabel = `${label} ${operatorLabel}`;
  56. }else if(operator == 'between') {
  57. filterLabel = `${label} ${operatorLabel} ${value1} ~ ${value2}`;
  58. }else {
  59. filterLabel = `${label} ${operatorLabel} ${value1}`;
  60. }
  61. }else if(type == 'time') {
  62. value1 = new Date(value1).format('yyyy/MM/dd');
  63. value2 = new Date(value2).format('yyyy/MM/dd');
  64. if(operator == 'null') {
  65. filterLabel = `${label} ${operatorLabel}`;
  66. }else if(operator == 'between') {
  67. filterLabel = `${label} ${operatorLabel} ${value1} ~ ${value2}`;
  68. }else {
  69. filterLabel = `${label} ${operatorLabel} ${value1}`;
  70. }
  71. }else if(type == 'categorical') {
  72. if(operator == 'null') {
  73. filterLabel = `${label} ${operatorLabel}`;
  74. }else {
  75. filterLabel = `${label} ${operatorLabel} ${value1}`;
  76. }
  77. }else {
  78. filterLabel = '错误条件';
  79. }
  80. return filterLabel;
  81. }
  82. render() {
  83. const filters = this.props.chartDesigner.filters;
  84. const columns = this.props.chartDesigner.columns;
  85. const { visibleFilterBox } = this.state;
  86. let tags = filters.map((f, i)=>{
  87. return {
  88. key: f.key,
  89. label: this.createFilterLabel(f),
  90. using: f.using
  91. }
  92. });
  93. return (
  94. <div className='toolbar'>
  95. <div className='filters'>
  96. <h6 style={{ marginRight: 8, display: 'inline' }}>筛选:</h6>
  97. {tags.map(tag => (
  98. <Tag
  99. className={`filter-tag ${tag.using?'filter-tag-using':''}`}
  100. key={tag.key}
  101. closable={false}
  102. onClick={this.filterUsingChange}
  103. data-key={tag.key}
  104. >
  105. {tag.label}
  106. </Tag>
  107. ))}
  108. <Tag
  109. onClick={this.showFilterBox}
  110. className={`filter-tag filter-tag-add`}
  111. >
  112. <Icon type="setting" />
  113. </Tag>
  114. </div>
  115. <div className='tools'>
  116. <Button className='tools-button' size='small'>按钮</Button>
  117. </div>
  118. <FilterBox key={Math.random()} columns={columns} filterData={filters} visibleFilterBox={visibleFilterBox} showFilterBox={this.showFilterBox} hideFilterBox={this.hideFilterBox} createFilters={this.createFilters} />
  119. </div>
  120. );
  121. }
  122. }
  123. function mapStateToProps({ present: { chartDesigner } }) {
  124. return { chartDesigner: chartDesigner }
  125. }
  126. export default connect(mapStateToProps)(Toolbar);