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.

217 lines
7.5 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,
  22. icon: PropTypes.string,
  23. active: PropTypes.bool,
  24. localSettings : ImmutablePropTypes.map,
  25. multiColumn: PropTypes.bool,
  26. extraButton: PropTypes.node,
  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. state = {
  39. collapsed: true,
  40. animating: false,
  41. animatingNCD: false,
  42. };
  43. handleToggleClick = (e) => {
  44. e.stopPropagation();
  45. this.setState({ collapsed: !this.state.collapsed, animating: true });
  46. }
  47. handleTitleClick = () => {
  48. this.props.onClick();
  49. }
  50. handleMoveLeft = () => {
  51. this.props.onMove(-1);
  52. }
  53. handleMoveRight = () => {
  54. this.props.onMove(1);
  55. }
  56. handleBackClick = () => {
  57. // if history is exhausted, or we would leave mastodon, just go to root.
  58. if (window.history && (window.history.length === 1 || window.history.length === window._mastoInitialHistoryLen)) {
  59. this.context.router.history.push('/');
  60. } else {
  61. this.context.router.history.goBack();
  62. }
  63. }
  64. handleTransitionEnd = () => {
  65. this.setState({ animating: false });
  66. }
  67. handleTransitionEndNCD = () => {
  68. this.setState({ animatingNCD: false });
  69. }
  70. onEnterCleaningMode = () => {
  71. this.setState({ animatingNCD: true });
  72. this.props.onEnterCleaningMode(!this.props.notifCleaningActive);
  73. }
  74. render () {
  75. const { intl, icon, active, children, pinned, onPin, multiColumn, extraButton, showBackButton, intl: { formatMessage }, notifCleaning, notifCleaningActive } = this.props;
  76. const { collapsed, animating, animatingNCD } = this.state;
  77. let title = this.props.title;
  78. const wrapperClassName = classNames('column-header__wrapper', {
  79. 'active': active,
  80. });
  81. const buttonClassName = classNames('column-header', {
  82. 'active': active,
  83. });
  84. const collapsibleClassName = classNames('column-header__collapsible', {
  85. 'collapsed': collapsed,
  86. 'animating': animating,
  87. });
  88. const collapsibleButtonClassName = classNames('column-header__button', {
  89. 'active': !collapsed,
  90. });
  91. const notifCleaningButtonClassName = classNames('column-header__button', {
  92. 'active': notifCleaningActive,
  93. });
  94. const notifCleaningDrawerClassName = classNames('ncd column-header__collapsible', {
  95. 'collapsed': !notifCleaningActive,
  96. 'animating': animatingNCD,
  97. });
  98. let extraContent, pinButton, moveButtons, backButton, collapseButton;
  99. //*glitch
  100. const msgEnterNotifCleaning = intl.formatMessage(messages.enterNotifCleaning);
  101. if (children) {
  102. extraContent = (
  103. <div key='extra-content' className='column-header__collapsible__extra'>
  104. {children}
  105. </div>
  106. );
  107. }
  108. if (multiColumn && pinned) {
  109. 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>;
  110. moveButtons = (
  111. <div key='move-buttons' className='column-header__setting-arrows'>
  112. <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>
  113. <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>
  114. </div>
  115. );
  116. } else if (multiColumn) {
  117. 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>;
  118. }
  119. if (!pinned && (multiColumn || showBackButton)) {
  120. backButton = (
  121. <button onClick={this.handleBackClick} className='column-header__back-button'>
  122. <i className='fa fa-fw fa-chevron-left column-back-button__icon' />
  123. <FormattedMessage id='column_back_button.label' defaultMessage='Back' />
  124. </button>
  125. );
  126. }
  127. const collapsedContent = [
  128. extraContent,
  129. ];
  130. if (multiColumn) {
  131. collapsedContent.push(moveButtons);
  132. collapsedContent.push(pinButton);
  133. }
  134. if (children || multiColumn) {
  135. collapseButton = <button className={collapsibleButtonClassName} title={formatMessage(collapsed ? messages.show : messages.hide)} aria-label={formatMessage(collapsed ? messages.show : messages.hide)} aria-pressed={collapsed ? 'false' : 'true'} onClick={this.handleToggleClick}><i className='fa fa-sliders' /></button>;
  136. }
  137. const hasTitle = icon && title;
  138. return (
  139. <div className={wrapperClassName}>
  140. <h1 className={buttonClassName}>
  141. {hasTitle && (
  142. <button onClick={this.handleTitleClick}>
  143. <i className={`fa fa-fw fa-${icon} column-header__icon`} />
  144. {title}
  145. </button>
  146. )}
  147. {!hasTitle && backButton}
  148. <div className='column-header__buttons'>
  149. {hasTitle && backButton}
  150. {extraButton}
  151. { notifCleaning ? (
  152. <button
  153. aria-label={msgEnterNotifCleaning}
  154. title={msgEnterNotifCleaning}
  155. onClick={this.onEnterCleaningMode}
  156. className={notifCleaningButtonClassName}
  157. >
  158. <i className='fa fa-eraser' />
  159. </button>
  160. ) : null}
  161. {collapseButton}
  162. </div>
  163. </h1>
  164. { notifCleaning ? (
  165. <div className={notifCleaningDrawerClassName} onTransitionEnd={this.handleTransitionEndNCD}>
  166. <div className='column-header__collapsible-inner nopad-drawer'>
  167. {(notifCleaningActive || animatingNCD) ? (<NotificationPurgeButtonsContainer />) : null }
  168. </div>
  169. </div>
  170. ) : null}
  171. <div className={collapsibleClassName} tabIndex={collapsed ? -1 : null} onTransitionEnd={this.handleTransitionEnd}>
  172. <div className='column-header__collapsible-inner'>
  173. {(!collapsed || animating) && collapsedContent}
  174. </div>
  175. </div>
  176. </div>
  177. );
  178. }
  179. }