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.

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