闭社主体 forked from https://github.com/tootsuite/mastodon
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.

99 lines
3.8 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. const messages = defineMessages({
  6. public_short: { id: 'privacy.public.short', defaultMessage: 'Public' },
  7. public_long: { id: 'privacy.public.long', defaultMessage: 'Post to public timelines' },
  8. unlisted_short: { id: 'privacy.unlisted.short', defaultMessage: 'Unlisted' },
  9. unlisted_long: { id: 'privacy.unlisted.long', defaultMessage: 'Do not show in public timelines' },
  10. private_short: { id: 'privacy.private.short', defaultMessage: 'Followers-only' },
  11. private_long: { id: 'privacy.private.long', defaultMessage: 'Post to followers only' },
  12. direct_short: { id: 'privacy.direct.short', defaultMessage: 'Direct' },
  13. direct_long: { id: 'privacy.direct.long', defaultMessage: 'Post to mentioned users only' },
  14. change_privacy: { id: 'privacy.change', defaultMessage: 'Adjust status privacy' },
  15. });
  16. const iconStyle = {
  17. height: null,
  18. lineHeight: '27px',
  19. };
  20. class PrivacyDropdown extends React.PureComponent {
  21. static propTypes = {
  22. value: PropTypes.string.isRequired,
  23. onChange: PropTypes.func.isRequired,
  24. intl: PropTypes.object.isRequired,
  25. };
  26. state = {
  27. open: false,
  28. };
  29. handleToggle = () => {
  30. this.setState({ open: !this.state.open });
  31. }
  32. handleClick = (e) => {
  33. const value = e.currentTarget.getAttribute('data-index');
  34. e.preventDefault();
  35. this.setState({ open: false });
  36. this.props.onChange(value);
  37. }
  38. onGlobalClick = (e) => {
  39. if (e.target !== this.node && !this.node.contains(e.target) && this.state.open) {
  40. this.setState({ open: false });
  41. }
  42. }
  43. componentDidMount () {
  44. window.addEventListener('click', this.onGlobalClick);
  45. window.addEventListener('touchstart', this.onGlobalClick);
  46. }
  47. componentWillUnmount () {
  48. window.removeEventListener('click', this.onGlobalClick);
  49. window.removeEventListener('touchstart', this.onGlobalClick);
  50. }
  51. setRef = (c) => {
  52. this.node = c;
  53. }
  54. render () {
  55. const { value, onChange, intl } = this.props;
  56. const { open } = this.state;
  57. const options = [
  58. { icon: 'globe', value: 'public', shortText: intl.formatMessage(messages.public_short), longText: intl.formatMessage(messages.public_long) },
  59. { icon: 'unlock-alt', value: 'unlisted', shortText: intl.formatMessage(messages.unlisted_short), longText: intl.formatMessage(messages.unlisted_long) },
  60. { icon: 'lock', value: 'private', shortText: intl.formatMessage(messages.private_short), longText: intl.formatMessage(messages.private_long) },
  61. { icon: 'envelope', value: 'direct', shortText: intl.formatMessage(messages.direct_short), longText: intl.formatMessage(messages.direct_long) },
  62. ];
  63. const valueOption = options.find(item => item.value === value);
  64. return (
  65. <div ref={this.setRef} className={`privacy-dropdown ${open ? 'active' : ''}`}>
  66. <div className='privacy-dropdown__value'><IconButton className='privacy-dropdown__value-icon' icon={valueOption.icon} title={intl.formatMessage(messages.change_privacy)} size={18} active={open} inverted onClick={this.handleToggle} style={iconStyle} /></div>
  67. <div className='privacy-dropdown__dropdown'>
  68. {open && options.map(item =>
  69. <div role='button' tabIndex='0' key={item.value} data-index={item.value} onClick={this.handleClick} className={`privacy-dropdown__option ${item.value === value ? 'active' : ''}`}>
  70. <div className='privacy-dropdown__option__icon'><i className={`fa fa-fw fa-${item.icon}`} /></div>
  71. <div className='privacy-dropdown__option__content'>
  72. <strong>{item.shortText}</strong>
  73. {item.longText}
  74. </div>
  75. </div>
  76. )}
  77. </div>
  78. </div>
  79. );
  80. }
  81. }
  82. export default injectIntl(PrivacyDropdown);