header.jsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import React from 'react'
  2. import { Input, Icon, Button, Popconfirm, Switch } from 'antd'
  3. import { connect } from 'dva'
  4. import './header.less'
  5. import SettingBox from './settingBox';
  6. class Header extends React.Component {
  7. constructor(props) {
  8. super(props);
  9. this.state = {
  10. visibleConfirm: false,
  11. visibleSettingBox: false
  12. }
  13. }
  14. handleVisibleChange = (visible) => {
  15. this.setState({ visibleConfirm: visible });
  16. }
  17. isOwner = () => {
  18. const { dashboardDesigner, main } = this.props;
  19. const { creatorCode } = dashboardDesigner;
  20. const { currentUser } = main;
  21. return currentUser.code === creatorCode;
  22. }
  23. render() {
  24. const { dashboardDesigner, dispatch } = this.props;
  25. const { editMode } = dashboardDesigner;
  26. return (
  27. <div className='dashboarddesigner-header'>
  28. <div className='header-item toolbar-back'>
  29. {this.isOwner() && <Popconfirm
  30. placement="bottomLeft"
  31. title="离开前保存修改吗?"
  32. visible={this.state.visibleConfirm}
  33. onVisibleChange={this.handleVisibleChange}
  34. onConfirm={async () => {
  35. this.setState({
  36. visibleConfirm: false
  37. });
  38. try {
  39. await dispatch({ type: 'dashboardDesigner/saveWithThumbnail' });
  40. }catch(e){
  41. return e
  42. }
  43. dispatch({ type: 'main/redirect', path: '/dashboard' });
  44. }}
  45. onCancel={() => {
  46. this.setState({
  47. visibleConfirm: false
  48. });
  49. dispatch({ type: 'main/redirect', path: '/dashboard' });
  50. }}
  51. okText="保存"
  52. cancelText="不保存"
  53. >
  54. <Button onClick={() => {
  55. console.log(dashboardDesigner.dirty);
  56. if(!dashboardDesigner.dirty) {
  57. dispatch({ type: 'main/goBack', path: '/chart' });
  58. }
  59. }}>
  60. <Icon type='left' />返回
  61. </Button>
  62. </Popconfirm>}
  63. {!this.isOwner() && <Button onClick={(e) => {
  64. dispatch({ type: 'main/goBack', path: '/chart' });
  65. }}>
  66. <Icon type='left' />返回
  67. </Button>}
  68. {this.isOwner() && <Button onClick={() => {
  69. if(dashboardDesigner.code && dashboardDesigner.code !== -1) {
  70. dispatch({ type: 'dashboardDesigner/saveWithThumbnail' });
  71. }else {
  72. dispatch({ type: 'dashboard/remoteAdd' });
  73. }
  74. }}><Icon type='save' />保存</Button>}
  75. </div>
  76. <div className='header-item toolbar-title'>
  77. <Input
  78. className='input-title'
  79. value={dashboardDesigner.name}
  80. disabled={!this.isOwner()}
  81. addonAfter={this.isOwner() ? <Icon type="edit"
  82. onClick={(e) => {
  83. const input = e.currentTarget.parentElement.parentElement.getElementsByTagName('input')[0];
  84. input && input.focus()
  85. }}
  86. /> : null}
  87. onChange={(e) => {
  88. dispatch({ type: 'dashboardDesigner/setField', name: 'name', value: e.target.value });
  89. }}
  90. />
  91. </div>
  92. <div className='header-item toolbar-viewswitch'>
  93. {this.isOwner() && <Switch
  94. className={`mode-switch ${editMode ? 'edit-mode-switch' : 'view-mode-switch'}`}
  95. checked={editMode}
  96. checkedChildren="编辑"
  97. unCheckedChildren="浏览"
  98. onChange={(checked) => {
  99. dispatch({ type: 'dashboardDesigner/setEditMode', checked });
  100. // 主动触发一次window的resize事件
  101. window.setTimeout(() => {
  102. var e = document.createEvent("Event");
  103. e.initEvent("resize", true, true);
  104. window.dispatchEvent(e);
  105. }, 300);
  106. }}
  107. />}
  108. </div>
  109. </div>
  110. );
  111. }
  112. }
  113. export default connect(({ present: { main, dashboardDesigner } }) => ({ main, dashboardDesigner }))(Header);