闭社主体 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.

98 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. @injectIntl
  21. export default class PrivacyDropdown extends React.PureComponent {
  22. static propTypes = {
  23. value: PropTypes.string.isRequired,
  24. onChange: PropTypes.func.isRequired,
  25. intl: PropTypes.object.isRequired,
  26. };
  27. state = {
  28. open: false,
  29. };
  30. handleToggle = () => {
  31. this.setState({ open: !this.state.open });
  32. }
  33. handleClick = (e) => {
  34. const value = e.currentTarget.getAttribute('data-index');
  35. e.preventDefault();
  36. this.setState({ open: false });
  37. this.props.onChange(value);
  38. }
  39. onGlobalClick = (e) => {
  40. if (e.target !== this.node && !this.node.contains(e.target) && this.state.open) {
  41. this.setState({ open: false });
  42. }
  43. }
  44. componentDidMount () {
  45. window.addEventListener('click', this.onGlobalClick);
  46. window.addEventListener('touchstart', this.onGlobalClick);
  47. }
  48. componentWillUnmount () {
  49. window.removeEventListener('click', this.onGlobalClick);
  50. window.removeEventListener('touchstart', this.onGlobalClick);
  51. }
  52. setRef = (c) => {
  53. this.node = c;
  54. }
  55. render () {
  56. const { value, intl } = this.props;
  57. const { open } = this.state;
  58. const options = [
  59. { icon: 'globe', value: 'public', shortText: intl.formatMessage(messages.public_short), longText: intl.formatMessage(messages.public_long) },
  60. { icon: 'unlock-alt', value: 'unlisted', shortText: intl.formatMessage(messages.unlisted_short), longText: intl.formatMessage(messages.unlisted_long) },
  61. { icon: 'lock', value: 'private', shortText: intl.formatMessage(messages.private_short), longText: intl.formatMessage(messages.private_long) },
  62. { icon: 'envelope', value: 'direct', shortText: intl.formatMessage(messages.direct_short), longText: intl.formatMessage(messages.direct_long) },
  63. ];
  64. const valueOption = options.find(item => item.value === value);
  65. return (
  66. <div ref={this.setRef} className={`privacy-dropdown ${open ? 'active' : ''}`}>
  67. <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>
  68. <div className='privacy-dropdown__dropdown'>
  69. {open && options.map(item =>
  70. <div role='button' tabIndex='0' key={item.value} data-index={item.value} onClick={this.handleClick} className={`privacy-dropdown__option ${item.value === value ? 'active' : ''}`}>
  71. <div className='privacy-dropdown__option__icon'><i className={`fa fa-fw fa-${item.icon}`} /></div>
  72. <div className='privacy-dropdown__option__content'>
  73. <strong>{item.shortText}</strong>
  74. {item.longText}
  75. </div>
  76. </div>
  77. )}
  78. </div>
  79. </div>
  80. );
  81. }
  82. }