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.
 
 
 
 

129 lines
4.7 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. isUserTouching: PropTypes.func,
  24. isModalOpen: PropTypes.bool.isRequired,
  25. onModalOpen: PropTypes.func,
  26. onModalClose: PropTypes.func,
  27. value: PropTypes.string.isRequired,
  28. onChange: PropTypes.func.isRequired,
  29. intl: PropTypes.object.isRequired,
  30. };
  31. state = {
  32. open: false,
  33. };
  34. handleToggle = () => {
  35. if (this.props.isUserTouching()) {
  36. if (this.state.open) {
  37. this.props.onModalClose();
  38. } else {
  39. this.props.onModalOpen({
  40. actions: this.options.map(option => ({ ...option, active: option.value === this.props.value })),
  41. onClick: this.handleModalActionClick,
  42. });
  43. }
  44. } else {
  45. this.setState({ open: !this.state.open });
  46. }
  47. }
  48. handleModalActionClick = (e) => {
  49. e.preventDefault();
  50. const { value } = this.options[e.currentTarget.getAttribute('data-index')];
  51. this.props.onModalClose();
  52. this.props.onChange(value);
  53. }
  54. handleClick = (e) => {
  55. if (e.key === 'Escape') {
  56. this.setState({ open: false });
  57. } else if (!e.key || e.key === 'Enter') {
  58. const value = e.currentTarget.getAttribute('data-index');
  59. e.preventDefault();
  60. this.setState({ open: false });
  61. this.props.onChange(value);
  62. }
  63. }
  64. onGlobalClick = (e) => {
  65. if (e.target !== this.node && !this.node.contains(e.target) && this.state.open) {
  66. this.setState({ open: false });
  67. }
  68. }
  69. componentWillMount () {
  70. const { intl: { formatMessage } } = this.props;
  71. this.options = [
  72. { icon: 'globe', value: 'public', text: formatMessage(messages.public_short), meta: formatMessage(messages.public_long) },
  73. { icon: 'unlock-alt', value: 'unlisted', text: formatMessage(messages.unlisted_short), meta: formatMessage(messages.unlisted_long) },
  74. { icon: 'lock', value: 'private', text: formatMessage(messages.private_short), meta: formatMessage(messages.private_long) },
  75. { icon: 'envelope', value: 'direct', text: formatMessage(messages.direct_short), meta: formatMessage(messages.direct_long) },
  76. ];
  77. }
  78. componentDidMount () {
  79. window.addEventListener('click', this.onGlobalClick);
  80. window.addEventListener('touchstart', this.onGlobalClick);
  81. }
  82. componentWillUnmount () {
  83. window.removeEventListener('click', this.onGlobalClick);
  84. window.removeEventListener('touchstart', this.onGlobalClick);
  85. }
  86. setRef = (c) => {
  87. this.node = c;
  88. }
  89. render () {
  90. const { value, intl } = this.props;
  91. const { open } = this.state;
  92. const valueOption = this.options.find(item => item.value === value);
  93. return (
  94. <div ref={this.setRef} className={`privacy-dropdown ${open ? 'active' : ''}`}>
  95. <div className='privacy-dropdown__value'><IconButton className='privacy-dropdown__value-icon' icon={valueOption.icon} title={intl.formatMessage(messages.change_privacy)} size={18} pressed={open} active={open} inverted onClick={this.handleToggle} style={iconStyle} /></div>
  96. <div className='privacy-dropdown__dropdown'>
  97. {open && this.options.map(item =>
  98. <div role='button' tabIndex='0' key={item.value} data-index={item.value} onKeyDown={this.handleClick} onClick={this.handleClick} className={`privacy-dropdown__option ${item.value === value ? 'active' : ''}`}>
  99. <div className='privacy-dropdown__option__icon'><i className={`fa fa-fw fa-${item.icon}`} /></div>
  100. <div className='privacy-dropdown__option__content'>
  101. <strong>{item.text}</strong>
  102. {item.meta}
  103. </div>
  104. </div>
  105. )}
  106. </div>
  107. </div>
  108. );
  109. }
  110. }