]> cat aescling's git repositories - mastodon.git/blob - app/javascript/mastodon/features/compose/components/privacy_dropdown.js
Upgrade ESLint to version 4.x (#6276)
[mastodon.git] / app / javascript / mastodon / features / compose / components / privacy_dropdown.js
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
11 const messages = defineMessages({
12 public_short: { id: 'privacy.public.short', defaultMessage: 'Public' },
13 public_long: { id: 'privacy.public.long', defaultMessage: 'Post to public timelines' },
14 unlisted_short: { id: 'privacy.unlisted.short', defaultMessage: 'Unlisted' },
15 unlisted_long: { id: 'privacy.unlisted.long', defaultMessage: 'Do not show in public timelines' },
16 private_short: { id: 'privacy.private.short', defaultMessage: 'Followers-only' },
17 private_long: { id: 'privacy.private.long', defaultMessage: 'Post to followers only' },
18 direct_short: { id: 'privacy.direct.short', defaultMessage: 'Direct' },
19 direct_long: { id: 'privacy.direct.long', defaultMessage: 'Post to mentioned users only' },
20 change_privacy: { id: 'privacy.change', defaultMessage: 'Adjust status privacy' },
21 });
22
23 const listenerOptions = detectPassiveEvents.hasSupport ? { passive: true } : false;
24
25 class PrivacyDropdownMenu extends React.PureComponent {
26
27 static propTypes = {
28 style: PropTypes.object,
29 items: PropTypes.array.isRequired,
30 value: PropTypes.string.isRequired,
31 onClose: PropTypes.func.isRequired,
32 onChange: PropTypes.func.isRequired,
33 };
34
35 handleDocumentClick = e => {
36 if (this.node && !this.node.contains(e.target)) {
37 this.props.onClose();
38 }
39 }
40
41 handleClick = e => {
42 if (e.key === 'Escape') {
43 this.props.onClose();
44 } else if (!e.key || e.key === 'Enter') {
45 const value = e.currentTarget.getAttribute('data-index');
46
47 e.preventDefault();
48
49 this.props.onClose();
50 this.props.onChange(value);
51 }
52 }
53
54 componentDidMount () {
55 document.addEventListener('click', this.handleDocumentClick, false);
56 document.addEventListener('touchend', this.handleDocumentClick, listenerOptions);
57 }
58
59 componentWillUnmount () {
60 document.removeEventListener('click', this.handleDocumentClick, false);
61 document.removeEventListener('touchend', this.handleDocumentClick, listenerOptions);
62 }
63
64 setRef = c => {
65 this.node = c;
66 }
67
68 render () {
69 const { style, items, value } = this.props;
70
71 return (
72 <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 }) }}>
73 {({ opacity, scaleX, scaleY }) => (
74 <div className='privacy-dropdown__dropdown' style={{ ...style, opacity: opacity, transform: `scale(${scaleX}, ${scaleY})` }} ref={this.setRef}>
75 {items.map(item => (
76 <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 })}>
77 <div className='privacy-dropdown__option__icon'>
78 <i className={`fa fa-fw fa-${item.icon}`} />
79 </div>
80
81 <div className='privacy-dropdown__option__content'>
82 <strong>{item.text}</strong>
83 {item.meta}
84 </div>
85 </div>
86 ))}
87 </div>
88 )}
89 </Motion>
90 );
91 }
92
93 }
94
95 @injectIntl
96 export default class PrivacyDropdown extends React.PureComponent {
97
98 static propTypes = {
99 isUserTouching: PropTypes.func,
100 isModalOpen: PropTypes.bool.isRequired,
101 onModalOpen: PropTypes.func,
102 onModalClose: PropTypes.func,
103 value: PropTypes.string.isRequired,
104 onChange: PropTypes.func.isRequired,
105 intl: PropTypes.object.isRequired,
106 };
107
108 state = {
109 open: false,
110 };
111
112 handleToggle = () => {
113 if (this.props.isUserTouching()) {
114 if (this.state.open) {
115 this.props.onModalClose();
116 } else {
117 this.props.onModalOpen({
118 actions: this.options.map(option => ({ ...option, active: option.value === this.props.value })),
119 onClick: this.handleModalActionClick,
120 });
121 }
122 } else {
123 this.setState({ open: !this.state.open });
124 }
125 }
126
127 handleModalActionClick = (e) => {
128 e.preventDefault();
129
130 const { value } = this.options[e.currentTarget.getAttribute('data-index')];
131
132 this.props.onModalClose();
133 this.props.onChange(value);
134 }
135
136 handleKeyDown = e => {
137 switch(e.key) {
138 case 'Enter':
139 this.handleToggle();
140 break;
141 case 'Escape':
142 this.handleClose();
143 break;
144 }
145 }
146
147 handleClose = () => {
148 this.setState({ open: false });
149 }
150
151 handleChange = value => {
152 this.props.onChange(value);
153 }
154
155 componentWillMount () {
156 const { intl: { formatMessage } } = this.props;
157
158 this.options = [
159 { icon: 'globe', value: 'public', text: formatMessage(messages.public_short), meta: formatMessage(messages.public_long) },
160 { icon: 'unlock-alt', value: 'unlisted', text: formatMessage(messages.unlisted_short), meta: formatMessage(messages.unlisted_long) },
161 { icon: 'lock', value: 'private', text: formatMessage(messages.private_short), meta: formatMessage(messages.private_long) },
162 { icon: 'envelope', value: 'direct', text: formatMessage(messages.direct_short), meta: formatMessage(messages.direct_long) },
163 ];
164 }
165
166 render () {
167 const { value, intl } = this.props;
168 const { open } = this.state;
169
170 const valueOption = this.options.find(item => item.value === value);
171
172 return (
173 <div className={classNames('privacy-dropdown', { active: open })} onKeyDown={this.handleKeyDown}>
174 <div className={classNames('privacy-dropdown__value', { active: this.options.indexOf(valueOption) === 0 })}>
175 <IconButton
176 className='privacy-dropdown__value-icon'
177 icon={valueOption.icon}
178 title={intl.formatMessage(messages.change_privacy)}
179 size={18}
180 expanded={open}
181 active={open}
182 inverted
183 onClick={this.handleToggle}
184 style={{ height: null, lineHeight: '27px' }}
185 />
186 </div>
187
188 <Overlay show={open} placement='bottom' target={this}>
189 <PrivacyDropdownMenu
190 items={this.options}
191 value={value}
192 onClose={this.handleClose}
193 onChange={this.handleChange}
194 />
195 </Overlay>
196 </div>
197 );
198 }
199
200 }
This page took 0.100887 seconds and 4 git commands to generate.