闭社主体 forked from https://github.com/tootsuite/mastodon
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.

145 lines
4.3 KiB

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