-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecps.jsx
86 lines (74 loc) · 2.76 KB
/
recps.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import React from 'react'
import suggestBox from 'suggest-box'
import u from 'patchkit-util'
import social from 'patchkit-util/social'
import t from 'patchwork-translations'
export const RECP_LIMIT = 7
class ComposerRecp extends React.Component {
static contextTypes = {
users: React.PropTypes.object.isRequired
}
static propTypes = {
id: React.PropTypes.string.isRequired,
isReadOnly: React.PropTypes.bool
}
render() {
return <span className="recp">
{u.getName(this.context.users, this.props.id)}
{this.props.isReadOnly ? '' : <a onClick={() => this.props.onRemove(this.props.id)}><i className="fa fa-remove"/></a>}
</span>
}
}
export class ComposerRecps extends React.Component {
static contextTypes = {
users: React.PropTypes.object.isRequired,
user: React.PropTypes.object.isRequired
}
static propTypes = {
onAdd: React.PropTypes.func.isRequired,
onRemove: React.PropTypes.func.isRequired,
recps: React.PropTypes.array.isRequired,
suggestOptions: React.PropTypes.object,
isReadOnly: React.PropTypes.bool
}
constructor(props) {
super(props)
this.state = { inputText: '' }
}
componentDidMount() {
this.setupSuggest()
}
componentDidUpdate() {
this.setupSuggest()
}
setupSuggest() {
// setup the suggest-box
const input = this.refs && this.refs.input
if (!input || input.isSetup || !this.props.suggestOptions)
return
input.isSetup = true
suggestBox(input, { any: this.props.suggestOptions['@'] }, { cls: 'msg-recipients' })
input.addEventListener('suggestselect', this.onSuggestSelect.bind(this))
}
onChange(e) {
this.setState({ inputText: e.target.value })
}
onSuggestSelect(e) {
this.props.onAdd(e.detail.id)
this.setState({ inputText: '' })
}
render() {
const isAtLimit = (this.props.recps.length >= RECP_LIMIT)
const warnings = this.props.recps.filter((id) => (id !== this.context.user.id) && !social.follows(this.context.users, id, this.context.user.id))
return <div className="recps-inputs flex-fill">
<i className="fa fa-user" /> {t('thread.ToRecps')} {this.props.recps.map((r) => <ComposerRecp key={r} id={r} onRemove={this.props.onRemove} isReadOnly={this.props.isReadOnly} />)}
{ (!isAtLimit && !this.props.isReadOnly) ?
<input ref="input" type="text" placeholder={t('composer.AddRecipients')} value={this.state.inputText} onChange={this.onChange.bind(this)} {...this.props} /> :
'' }
{ isAtLimit ? <div className="warning">{t('composer.RecipientLimitReached')}</div> : '' }
{ warnings.length
? warnings.map((id, i) => <div key={'warning-'+i} className="warning">{t('composer.NotFollowedWarning', {name: u.getName(this.context.users, id)})}</div>)
: '' }
</div>
}
}