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.

256 lines
8.2 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { injectIntl, defineMessages } from 'react-intl';
  4. import IconButton from '../../../components/icon_button';
  5. import Overlay from 'react-overlays/lib/Overlay';
  6. import Motion from '../../ui/util/optional_motion';
  7. import spring from 'react-motion/lib/spring';
  8. import detectPassiveEvents from 'detect-passive-events';
  9. import classNames from 'classnames';
  10. const messages = defineMessages({
  11. public_short: { id: 'privacy.public.short', defaultMessage: 'Public' },
  12. public_long: { id: 'privacy.public.long', defaultMessage: 'Post to public timelines' },
  13. unlisted_short: { id: 'privacy.unlisted.short', defaultMessage: 'Unlisted' },
  14. unlisted_long: { id: 'privacy.unlisted.long', defaultMessage: 'Do not show in public timelines' },
  15. private_short: { id: 'privacy.private.short', defaultMessage: 'Followers-only' },
  16. private_long: { id: 'privacy.private.long', defaultMessage: 'Post to followers only' },
  17. direct_short: { id: 'privacy.direct.short', defaultMessage: 'Direct' },
  18. direct_long: { id: 'privacy.direct.long', defaultMessage: 'Post to mentioned users only' },
  19. change_privacy: { id: 'privacy.change', defaultMessage: 'Adjust status privacy' },
  20. });
  21. const listenerOptions = detectPassiveEvents.hasSupport ? { passive: true } : false;
  22. class PrivacyDropdownMenu extends React.PureComponent {
  23. static propTypes = {
  24. style: PropTypes.object,
  25. items: PropTypes.array.isRequired,
  26. value: PropTypes.string.isRequired,
  27. onClose: PropTypes.func.isRequired,
  28. onChange: PropTypes.func.isRequired,
  29. };
  30. state = {
  31. mounted: false,
  32. };
  33. handleDocumentClick = e => {
  34. if (this.node && !this.node.contains(e.target)) {
  35. this.props.onClose();
  36. }
  37. }
  38. handleKeyDown = e => {
  39. const { items } = this.props;
  40. const value = e.currentTarget.getAttribute('data-index');
  41. const index = items.findIndex(item => {
  42. return (item.value === value);
  43. });
  44. let element;
  45. switch(e.key) {
  46. case 'Escape':
  47. this.props.onClose();
  48. break;
  49. case 'Enter':
  50. this.handleClick(e);
  51. break;
  52. case 'ArrowDown':
  53. element = this.node.childNodes[index + 1];
  54. if (element) {
  55. element.focus();
  56. this.props.onChange(element.getAttribute('data-index'));
  57. }
  58. break;
  59. case 'ArrowUp':
  60. element = this.node.childNodes[index - 1];
  61. if (element) {
  62. element.focus();
  63. this.props.onChange(element.getAttribute('data-index'));
  64. }
  65. break;
  66. case 'Home':
  67. element = this.node.firstChild;
  68. if (element) {
  69. element.focus();
  70. this.props.onChange(element.getAttribute('data-index'));
  71. }
  72. break;
  73. case 'End':
  74. element = this.node.lastChild;
  75. if (element) {
  76. element.focus();
  77. this.props.onChange(element.getAttribute('data-index'));
  78. }
  79. break;
  80. }
  81. }
  82. handleClick = e => {
  83. const value = e.currentTarget.getAttribute('data-index');
  84. e.preventDefault();
  85. this.props.onClose();
  86. this.props.onChange(value);
  87. }
  88. componentDidMount () {
  89. document.addEventListener('click', this.handleDocumentClick, false);
  90. document.addEventListener('touchend', this.handleDocumentClick, listenerOptions);
  91. if (this.focusedItem) this.focusedItem.focus();
  92. this.setState({ mounted: true });
  93. }
  94. componentWillUnmount () {
  95. document.removeEventListener('click', this.handleDocumentClick, false);
  96. document.removeEventListener('touchend', this.handleDocumentClick, listenerOptions);
  97. }
  98. setRef = c => {
  99. this.node = c;
  100. }
  101. setFocusRef = c => {
  102. this.focusedItem = c;
  103. }
  104. render () {
  105. const { mounted } = this.state;
  106. const { style, items, value } = this.props;
  107. return (
  108. <Motion defaultStyle={{ opacity: 0, scaleX: 0.85, scaleY: 0.75 }} style={{ opacity: spring(1, { damping: 35, stiffness: 400 }), scaleX: spring(1, { damping: 35, stiffness: 400 }), scaleY: spring(1, { damping: 35, stiffness: 400 }) }}>
  109. {({ opacity, scaleX, scaleY }) => (
  110. // It should not be transformed when mounting because the resulting
  111. // size will be used to determine the coordinate of the menu by
  112. // react-overlays
  113. <div className='privacy-dropdown__dropdown' style={{ ...style, opacity: opacity, transform: mounted ? `scale(${scaleX}, ${scaleY})` : null }} role='listbox' ref={this.setRef}>
  114. {items.map(item => (
  115. <div role='option' tabIndex='0' key={item.value} data-index={item.value} onKeyDown={this.handleKeyDown} onClick={this.handleClick} className={classNames('privacy-dropdown__option', { active: item.value === value })} aria-selected={item.value === value} ref={item.value === value ? this.setFocusRef : null}>
  116. <div className='privacy-dropdown__option__icon'>
  117. <i className={`fa fa-fw fa-${item.icon}`} />
  118. </div>
  119. <div className='privacy-dropdown__option__content'>
  120. <strong>{item.text}</strong>
  121. {item.meta}
  122. </div>
  123. </div>
  124. ))}
  125. </div>
  126. )}
  127. </Motion>
  128. );
  129. }
  130. }
  131. @injectIntl
  132. export default class PrivacyDropdown extends React.PureComponent {
  133. static propTypes = {
  134. isUserTouching: PropTypes.func,
  135. isModalOpen: PropTypes.bool.isRequired,
  136. onModalOpen: PropTypes.func,
  137. onModalClose: PropTypes.func,
  138. value: PropTypes.string.isRequired,
  139. onChange: PropTypes.func.isRequired,
  140. intl: PropTypes.object.isRequired,
  141. };
  142. state = {
  143. open: false,
  144. placement: null,
  145. };
  146. handleToggle = ({ target }) => {
  147. if (this.props.isUserTouching()) {
  148. if (this.state.open) {
  149. this.props.onModalClose();
  150. } else {
  151. this.props.onModalOpen({
  152. actions: this.options.map(option => ({ ...option, active: option.value === this.props.value })),
  153. onClick: this.handleModalActionClick,
  154. });
  155. }
  156. } else {
  157. const { top } = target.getBoundingClientRect();
  158. this.setState({ placement: top * 2 < innerHeight ? 'bottom' : 'top' });
  159. this.setState({ open: !this.state.open });
  160. }
  161. }
  162. handleModalActionClick = (e) => {
  163. e.preventDefault();
  164. const { value } = this.options[e.currentTarget.getAttribute('data-index')];
  165. this.props.onModalClose();
  166. this.props.onChange(value);
  167. }
  168. handleKeyDown = e => {
  169. switch(e.key) {
  170. case 'Escape':
  171. this.handleClose();
  172. break;
  173. }
  174. }
  175. handleClose = () => {
  176. this.setState({ open: false });
  177. }
  178. handleChange = value => {
  179. this.props.onChange(value);
  180. }
  181. componentWillMount () {
  182. const { intl: { formatMessage } } = this.props;
  183. this.options = [
  184. { icon: 'globe', value: 'public', text: formatMessage(messages.public_short), meta: formatMessage(messages.public_long) },
  185. { icon: 'unlock-alt', value: 'unlisted', text: formatMessage(messages.unlisted_short), meta: formatMessage(messages.unlisted_long) },
  186. { icon: 'lock', value: 'private', text: formatMessage(messages.private_short), meta: formatMessage(messages.private_long) },
  187. { icon: 'envelope', value: 'direct', text: formatMessage(messages.direct_short), meta: formatMessage(messages.direct_long) },
  188. ];
  189. }
  190. render () {
  191. const { value, intl } = this.props;
  192. const { open, placement } = this.state;
  193. const valueOption = this.options.find(item => item.value === value);
  194. return (
  195. <div className={classNames('privacy-dropdown', { active: open })} onKeyDown={this.handleKeyDown}>
  196. <div className={classNames('privacy-dropdown__value', { active: this.options.indexOf(valueOption) === 0 })}>
  197. <IconButton
  198. className='privacy-dropdown__value-icon'
  199. icon={valueOption.icon}
  200. title={intl.formatMessage(messages.change_privacy)}
  201. size={18}
  202. expanded={open}
  203. active={open}
  204. inverted
  205. onClick={this.handleToggle}
  206. style={{ height: null, lineHeight: '27px' }}
  207. />
  208. </div>
  209. <Overlay show={open} placement={placement} target={this}>
  210. <PrivacyDropdownMenu
  211. items={this.options}
  212. value={value}
  213. onClose={this.handleClose}
  214. onChange={this.handleChange}
  215. />
  216. </Overlay>
  217. </div>
  218. );
  219. }
  220. }