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.

213 lines
7.4 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import classNames from 'classnames';
  4. import { defineMessages, FormattedMessage, injectIntl } from 'react-intl';
  5. import ImmutablePropTypes from 'react-immutable-proptypes';
  6. import NotificationPurgeButtonsContainer from 'flavours/glitch/containers/notification_purge_buttons_container';
  7. const messages = defineMessages({
  8. show: { id: 'column_header.show_settings', defaultMessage: 'Show settings' },
  9. hide: { id: 'column_header.hide_settings', defaultMessage: 'Hide settings' },
  10. moveLeft: { id: 'column_header.moveLeft_settings', defaultMessage: 'Move column to the left' },
  11. moveRight: { id: 'column_header.moveRight_settings', defaultMessage: 'Move column to the right' },
  12. enterNotifCleaning : { id: 'notification_purge.start', defaultMessage: 'Enter notification cleaning mode' },
  13. });
  14. @injectIntl
  15. export default class ColumnHeader extends React.PureComponent {
  16. static contextTypes = {
  17. router: PropTypes.object,
  18. };
  19. static propTypes = {
  20. intl: PropTypes.object.isRequired,
  21. title: PropTypes.node.isRequired,
  22. icon: PropTypes.string.isRequired,
  23. active: PropTypes.bool,
  24. localSettings : ImmutablePropTypes.map,
  25. multiColumn: PropTypes.bool,
  26. focusable: PropTypes.bool,
  27. showBackButton: PropTypes.bool,
  28. notifCleaning: PropTypes.bool, // true only for the notification column
  29. notifCleaningActive: PropTypes.bool,
  30. onEnterCleaningMode: PropTypes.func,
  31. children: PropTypes.node,
  32. pinned: PropTypes.bool,
  33. onPin: PropTypes.func,
  34. onMove: PropTypes.func,
  35. onClick: PropTypes.func,
  36. intl: PropTypes.object.isRequired,
  37. };
  38. static defaultProps = {
  39. focusable: true,
  40. }
  41. state = {
  42. collapsed: true,
  43. animating: false,
  44. animatingNCD: false,
  45. };
  46. handleToggleClick = (e) => {
  47. e.stopPropagation();
  48. this.setState({ collapsed: !this.state.collapsed, animating: true });
  49. }
  50. handleTitleClick = () => {
  51. this.props.onClick();
  52. }
  53. handleMoveLeft = () => {
  54. this.props.onMove(-1);
  55. }
  56. handleMoveRight = () => {
  57. this.props.onMove(1);
  58. }
  59. handleBackClick = () => {
  60. // if history is exhausted, or we would leave mastodon, just go to root.
  61. if (window.history && (window.history.length === 1 || window.history.length === window._mastoInitialHistoryLen)) {
  62. this.context.router.history.push('/');
  63. } else {
  64. this.context.router.history.goBack();
  65. }
  66. }
  67. handleTransitionEnd = () => {
  68. this.setState({ animating: false });
  69. }
  70. handleTransitionEndNCD = () => {
  71. this.setState({ animatingNCD: false });
  72. }
  73. onEnterCleaningMode = () => {
  74. this.setState({ animatingNCD: true });
  75. this.props.onEnterCleaningMode(!this.props.notifCleaningActive);
  76. }
  77. render () {
  78. const { intl, icon, active, children, pinned, onPin, multiColumn, focusable, showBackButton, intl: { formatMessage }, notifCleaning, notifCleaningActive } = this.props;
  79. const { collapsed, animating, animatingNCD } = this.state;
  80. let title = this.props.title;
  81. const wrapperClassName = classNames('column-header__wrapper', {
  82. 'active': active,
  83. });
  84. const buttonClassName = classNames('column-header', {
  85. 'active': active,
  86. });
  87. const collapsibleClassName = classNames('column-header__collapsible', {
  88. 'collapsed': collapsed,
  89. 'animating': animating,
  90. });
  91. const collapsibleButtonClassName = classNames('column-header__button', {
  92. 'active': !collapsed,
  93. });
  94. const notifCleaningButtonClassName = classNames('column-header__button', {
  95. 'active': notifCleaningActive,
  96. });
  97. const notifCleaningDrawerClassName = classNames('ncd column-header__collapsible', {
  98. 'collapsed': !notifCleaningActive,
  99. 'animating': animatingNCD,
  100. });
  101. let extraContent, pinButton, moveButtons, backButton, collapseButton;
  102. //*glitch
  103. const msgEnterNotifCleaning = intl.formatMessage(messages.enterNotifCleaning);
  104. if (children) {
  105. extraContent = (
  106. <div key='extra-content' className='column-header__collapsible__extra'>
  107. {children}
  108. </div>
  109. );
  110. }
  111. if (multiColumn && pinned) {
  112. 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>;
  113. moveButtons = (
  114. <div key='move-buttons' className='column-header__setting-arrows'>
  115. <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>
  116. <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>
  117. </div>
  118. );
  119. } else if (multiColumn) {
  120. 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>;
  121. }
  122. if (!pinned && (multiColumn || showBackButton)) {
  123. backButton = (
  124. <button onClick={this.handleBackClick} className='column-header__back-button'>
  125. <i className='fa fa-fw fa-chevron-left column-back-button__icon' />
  126. <FormattedMessage id='column_back_button.label' defaultMessage='Back' />
  127. </button>
  128. );
  129. }
  130. const collapsedContent = [
  131. extraContent,
  132. ];
  133. if (multiColumn) {
  134. collapsedContent.push(moveButtons);
  135. collapsedContent.push(pinButton);
  136. }
  137. if (children || multiColumn) {
  138. 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>;
  139. }
  140. return (
  141. <div className={wrapperClassName}>
  142. <h1 tabIndex={focusable ? 0 : null} role='button' className={buttonClassName} aria-label={title} onClick={this.handleTitleClick}>
  143. <i className={`fa fa-fw fa-${icon} column-header__icon`} />
  144. <span className='column-header__title'>
  145. {title}
  146. </span>
  147. <div className='column-header__buttons'>
  148. {backButton}
  149. { notifCleaning ? (
  150. <button
  151. aria-label={msgEnterNotifCleaning}
  152. title={msgEnterNotifCleaning}
  153. onClick={this.onEnterCleaningMode}
  154. className={notifCleaningButtonClassName}
  155. >
  156. <i className='fa fa-eraser' />
  157. </button>
  158. ) : null}
  159. {collapseButton}
  160. </div>
  161. </h1>
  162. { notifCleaning ? (
  163. <div className={notifCleaningDrawerClassName} onTransitionEnd={this.handleTransitionEndNCD}>
  164. <div className='column-header__collapsible-inner nopad-drawer'>
  165. {(notifCleaningActive || animatingNCD) ? (<NotificationPurgeButtonsContainer />) : null }
  166. </div>
  167. </div>
  168. ) : null}
  169. <div className={collapsibleClassName} tabIndex={collapsed ? -1 : null} onTransitionEnd={this.handleTransitionEnd}>
  170. <div className='column-header__collapsible-inner'>
  171. {(!collapsed || animating) && collapsedContent}
  172. </div>
  173. </div>
  174. </div>
  175. );
  176. }
  177. }