logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

privacy_dropdown.js (7194B)


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