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.
 
 
 
 

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