header.jsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import React from 'react';
  2. import { Input, Icon, Button, Popconfirm, Switch, Form } from 'antd';
  3. import { connect } from 'dva';
  4. import './header.less';
  5. import Loadable from 'utils/loadable';
  6. const DeleteBox = Loadable(() => import('components/common/deleteBox/deleteBox'), true);
  7. const FormItem = Form.Item;
  8. class Header extends React.Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. visibleConfirm: false,
  13. visibleSettingBox: false,
  14. visibleDeleteBox: false,
  15. validInfo: {
  16. name: { status: 'success', help: '' }
  17. },
  18. }
  19. }
  20. handleVisibleChange = (visible) => {
  21. this.setState({ visibleConfirm: visible });
  22. }
  23. isOwner = () => {
  24. const { dashboardDesigner, main } = this.props;
  25. const { creatorCode } = dashboardDesigner;
  26. const { currentUser } = main;
  27. return currentUser.code === creatorCode || currentUser.role === 'superAdmin';
  28. }
  29. isValid = () => {
  30. const { dashboardDesigner } = this.props;
  31. const { name } = dashboardDesigner;
  32. return !!name && name.trim().length > 0 && name.length <= 50;
  33. }
  34. render() {
  35. const { dashboardDesigner, dispatch } = this.props;
  36. const { visibleDeleteBox, validInfo } = this.state;
  37. const { editMode } = dashboardDesigner;
  38. return (
  39. <div className='dashboarddesigner-header-content'>
  40. <div className='header-item toolbar-back'>
  41. {this.isOwner() && <Popconfirm
  42. overlayClassName={`close-popconfirm${this.isValid() ? '' : ' confirm-disabled'}`}
  43. placement="bottomLeft"
  44. title="离开前保存修改吗?"
  45. visible={this.state.visibleConfirm}
  46. onVisibleChange={this.handleVisibleChange}
  47. onConfirm={async () => {
  48. if(this.isValid()) {
  49. dispatch({ type: 'dashboard/remoteModify' });
  50. dispatch({ type: 'main/goBack', path: '/workshop/dashboard' });
  51. }
  52. this.setState({
  53. visibleConfirm: false
  54. });
  55. }}
  56. onCancel={() => {
  57. this.setState({
  58. visibleConfirm: false
  59. });
  60. dispatch({ type: 'main/redirect', path: '/workshop/dashboard' });
  61. }}
  62. okText="保存"
  63. cancelText="不保存"
  64. >
  65. <Button onClick={() => {
  66. if(!dashboardDesigner.dirty) {
  67. dispatch({ type: 'main/goBack', path: '/workshop/dashboard' });
  68. dispatch({ type: 'dashboardDesigner/reset' });
  69. }
  70. }}>
  71. <Icon type='left' />返回
  72. </Button>
  73. </Popconfirm>}
  74. {!this.isOwner() && <Button onClick={(e) => {
  75. dispatch({ type: 'main/goBack', path: '/workshop/chart' });
  76. dispatch({ type: 'dashboardDesigner/reset' });
  77. }}>
  78. <Icon type='left' />返回
  79. </Button>}
  80. </div>
  81. <div className='header-item toolbar-title'>
  82. <FormItem
  83. validateStatus={validInfo.name.status}
  84. help={validInfo.name.help}
  85. >
  86. <Input
  87. key={dashboardDesigner.name}
  88. className='input-title'
  89. defaultValue={dashboardDesigner.name}
  90. readOnly={!this.isOwner()}
  91. addonAfter={this.isOwner() ? <Icon type="edit"
  92. onClick={(e) => {
  93. const input = e.currentTarget.parentElement.parentElement.getElementsByTagName('input')[0];
  94. input && input.focus()
  95. }}
  96. /> : null}
  97. onChange={e => {
  98. let val = e.target.value + '';
  99. let status, help;
  100. if(val.trim().length === 0) {
  101. status = 'error';
  102. help = '报表名称不能为空';
  103. }else if(val.trim().length > 50) {
  104. status = 'error';
  105. help = '报表名称不能超过50个字符';
  106. }else {
  107. status = 'success';
  108. help = '';
  109. }
  110. window.clearTimeout(this.headerTimeout);
  111. this.headerTimeout = window.setTimeout(() => {
  112. this.setState({
  113. validInfo: { ...validInfo, name: { status, help } }
  114. });
  115. }, 100);
  116. }}
  117. onBlur={(e) => {
  118. dispatch({ type: 'dashboardDesigner/setField', name: 'name', value: e.target.value });
  119. }}
  120. onPressEnter={(e) => {
  121. dispatch({ type: 'dashboardDesigner/setField', name: 'name', value: e.target.value });
  122. }}
  123. />
  124. </FormItem>
  125. </div>
  126. <div className='header-item toolbar-viewswitch'>
  127. {this.isOwner() && <Switch
  128. className={`mode-switch ${editMode ? 'edit-mode-switch' : 'view-mode-switch'}`}
  129. checked={editMode}
  130. checkedChildren="编辑"
  131. unCheckedChildren="浏览"
  132. onChange={(checked) => {
  133. dispatch({ type: 'dashboardDesigner/setEditMode', checked });
  134. // 主动触发一次window的resize事件
  135. window.setTimeout(() => {
  136. var e = document.createEvent("Event");
  137. e.initEvent("resize", true, true);
  138. window.dispatchEvent(e);
  139. }, 500);
  140. }}
  141. />}
  142. {this.isOwner() && <Button disabled={!this.isValid()} style={{ marginLeft: '8px' }} onClick={() => {
  143. if(dashboardDesigner.code && dashboardDesigner.code !== -1) {
  144. dispatch({ type: 'dashboard/remoteModify' });
  145. }else {
  146. dispatch({ type: 'dashboard/remoteAdd' });
  147. }
  148. }}>保存</Button>}
  149. {this.isOwner() && <Button style={{ marginLeft: '8px' }} onClick={() => {
  150. this.setState({
  151. visibleDeleteBox: true
  152. });
  153. }}>删除</Button>}
  154. {visibleDeleteBox && <DeleteBox
  155. visibleBox={visibleDeleteBox}
  156. text={<div><span>确定要删除报表【{dashboardDesigner.name}】吗?</span></div>}
  157. hideBox={() => {
  158. this.setState({
  159. visibleDeleteBox: false
  160. })
  161. }}
  162. okHandler={() => {
  163. dispatch({ type: 'dashboard/remoteDelete', code: dashboardDesigner.code })
  164. dispatch({ type: 'main/redirect', path: '/workshop/dashboard' });
  165. }} />}
  166. </div>
  167. </div>
  168. );
  169. }
  170. }
  171. export default connect(({ main, dashboardDesigner }) => ({ main, dashboardDesigner }))(Header);