You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
5.1 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import classNames from 'classnames';
  4. import { FormattedMessage, injectIntl, defineMessages } from 'react-intl';
  5. const messages = defineMessages({
  6. show: { id: 'column_header.show_settings', defaultMessage: 'Show settings' },
  7. hide: { id: 'column_header.hide_settings', defaultMessage: 'Hide settings' },
  8. moveLeft: { id: 'column_header.moveLeft_settings', defaultMessage: 'Move column to the left' },
  9. moveRight: { id: 'column_header.moveRight_settings', defaultMessage: 'Move column to the right' },
  10. });
  11. @injectIntl
  12. export default class ColumnHeader extends React.PureComponent {
  13. static contextTypes = {
  14. router: PropTypes.object,
  15. };
  16. static propTypes = {
  17. intl: PropTypes.object.isRequired,
  18. title: PropTypes.node.isRequired,
  19. icon: PropTypes.string.isRequired,
  20. active: PropTypes.bool,
  21. multiColumn: PropTypes.bool,
  22. showBackButton: PropTypes.bool,
  23. children: PropTypes.node,
  24. pinned: PropTypes.bool,
  25. onPin: PropTypes.func,
  26. onMove: PropTypes.func,
  27. onClick: PropTypes.func,
  28. };
  29. state = {
  30. collapsed: true,
  31. animating: false,
  32. };
  33. handleToggleClick = (e) => {
  34. e.stopPropagation();
  35. this.setState({ collapsed: !this.state.collapsed, animating: true });
  36. }
  37. handleTitleClick = () => {
  38. this.props.onClick();
  39. }
  40. handleMoveLeft = () => {
  41. this.props.onMove(-1);
  42. }
  43. handleMoveRight = () => {
  44. this.props.onMove(1);
  45. }
  46. handleBackClick = () => {
  47. if (window.history && window.history.length === 1) this.context.router.history.push('/');
  48. else this.context.router.history.goBack();
  49. }
  50. handleTransitionEnd = () => {
  51. this.setState({ animating: false });
  52. }
  53. render () {
  54. const { title, icon, active, children, pinned, onPin, multiColumn, showBackButton, intl: { formatMessage } } = this.props;
  55. const { collapsed, animating } = this.state;
  56. const wrapperClassName = classNames('column-header__wrapper', {
  57. 'active': active,
  58. });
  59. const buttonClassName = classNames('column-header', {
  60. 'active': active,
  61. });
  62. const collapsibleClassName = classNames('column-header__collapsible', {
  63. 'collapsed': collapsed,
  64. 'animating': animating,
  65. });
  66. const collapsibleButtonClassName = classNames('column-header__button', {
  67. 'active': !collapsed,
  68. });
  69. let extraContent, pinButton, moveButtons, backButton, collapseButton;
  70. if (children) {
  71. extraContent = (
  72. <div key='extra-content' className='column-header__collapsible__extra'>
  73. {children}
  74. </div>
  75. );
  76. }
  77. if (multiColumn && pinned) {
  78. pinButton = <button key='pin-button' className='text-btn column-header__setting-btn' onClick={onPin}><i className='fa fa fa-times' /> <FormattedMessage id='column_header.unpin' defaultMessage='Unpin' /></button>;
  79. moveButtons = (
  80. <div key='move-buttons' className='column-header__setting-arrows'>
  81. <button title={formatMessage(messages.moveLeft)} aria-label={formatMessage(messages.moveLeft)} className='text-btn column-header__setting-btn' onClick={this.handleMoveLeft}><i className='fa fa-chevron-left' /></button>
  82. <button title={formatMessage(messages.moveRight)} aria-label={formatMessage(messages.moveRight)} className='text-btn column-header__setting-btn' onClick={this.handleMoveRight}><i className='fa fa-chevron-right' /></button>
  83. </div>
  84. );
  85. } else if (multiColumn) {
  86. pinButton = <button key='pin-button' className='text-btn column-header__setting-btn' onClick={onPin}><i className='fa fa fa-plus' /> <FormattedMessage id='column_header.pin' defaultMessage='Pin' /></button>;
  87. }
  88. if (!pinned && (multiColumn || showBackButton)) {
  89. backButton = (
  90. <button onClick={this.handleBackClick} className='column-header__back-button'>
  91. <i className='fa fa-fw fa-chevron-left column-back-button__icon' />
  92. <FormattedMessage id='column_back_button.label' defaultMessage='Back' />
  93. </button>
  94. );
  95. }
  96. const collapsedContent = [
  97. extraContent,
  98. ];
  99. if (multiColumn) {
  100. collapsedContent.push(moveButtons);
  101. collapsedContent.push(pinButton);
  102. }
  103. if (children || multiColumn) {
  104. collapseButton = <button className={collapsibleButtonClassName} aria-label={formatMessage(collapsed ? messages.show : messages.hide)} aria-pressed={collapsed ? 'false' : 'true'} onClick={this.handleToggleClick}><i className='fa fa-sliders' /></button>;
  105. }
  106. return (
  107. <div className={wrapperClassName}>
  108. <h1 className={buttonClassName}>
  109. <button onClick={this.handleTitleClick}>
  110. <i className={`fa fa-fw fa-${icon} column-header__icon`} />
  111. {title}
  112. </button>
  113. <div className='column-header__buttons'>
  114. {backButton}
  115. {collapseButton}
  116. </div>
  117. </h1>
  118. <div className={collapsibleClassName} tabIndex={collapsed ? -1 : null} onTransitionEnd={this.handleTransitionEnd}>
  119. <div className='column-header__collapsible-inner'>
  120. {(!collapsed || animating) && collapsedContent}
  121. </div>
  122. </div>
  123. </div>
  124. );
  125. }
  126. }