The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

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