/
SideBarTags.js
247 lines (209 loc) · 7.93 KB
/
SideBarTags.js
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import '../../../css/browser/side-bar-tags'
import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
import { FormattedMessage as T, intlShape } from 'react-intl'
import cx from 'classnames'
import { Creatable } from 'react-select'
import partition from 'lodash.partition'
import uniq from 'lodash.uniq'
import difference from 'lodash.difference'
import { TAGS_NS } from '../../../constants'
import Button from '../../Button'
import { addTagsCategory, addTag, removeTag, fetchTags } from '../../../actions/tags'
import { toggleCategories } from '../../../actions/browser'
class SideBarTags extends React.Component {
constructor (props) {
super(props)
this.state = {
newCategory: '',
// to avoid disappearing categories if new categories come from corpus-watcher just after creation
categories: [],
}
// prepopulate inputs
const userTags = props.webentity.tags[TAGS_NS]
if (userTags) {
Object.keys(userTags).forEach(k => {
this.state[`values/${k}`] = userTags[k].map(toOption)
})
}
// Force-bind methods used like `onEvent={ this.method }`
this.addCategory = this.addCategory.bind(this)
this.onChangeNewCategory = this.onChangeNewCategory.bind(this)
this.renderTagsCategory = this.renderTagsCategory.bind(this)
this._onKeyUp = this.onKeyUp.bind(this)
}
repopulate (webentity) {
Object.keys(this.state).filter(k => ~k.indexOf('values/'))
.forEach(k => {
delete this.state[k]
})
const userTags = webentity.tags[TAGS_NS]
if (userTags) {
Object.keys(userTags).forEach(k => {
this.state[`values/${k}`] = userTags[k].map(toOption)
})
}
}
onKeyUp (e) {
if (e.keyCode === 27) { // ESCAPE
e.target.blur()
}
}
componentWillMount () {
this.updateFullSuggestions(this.props.categories)
}
componentWillReceiveProps ({ categories, webentity }) {
if (webentity && this.props.webentity && webentity.id !== this.props.webentity.id) {
this.repopulate(webentity)
}
if (JSON.stringify(categories) !== JSON.stringify(this.props.categories)) {
this.updateFullSuggestions(categories)
}
}
updateFullSuggestions (categories) {
// Updated categories, fetch suggestions
const { serverUrl, corpusId, fetchTags } = this.props
fetchTags(serverUrl, corpusId).then((tags) => {
categories.forEach((category) => {
this.setState({ ['suggestions/' + category]: Object.keys(tags[category] || {}) })
})
})
}
onChangeNewCategory ({ target }) {
this.setState({ newCategory: target.value })
}
addCategory (e) {
e.preventDefault()
const { newCategory, categories } = this.state
if (newCategory && !~this.props.categories.concat(categories).indexOf(newCategory)) {
const { serverUrl, corpusId, webentity, addTagsCategory } = this.props
addTagsCategory(serverUrl, corpusId, webentity.id, newCategory)
this.setState({ newCategory: '', categories: this.state.categories.concat(newCategory) })
}
}
onChangeCreatable (options, category) {
const { serverUrl, corpusId, webentity, addTag, removeTag } = this.props
const key = `values/${category}`
const previousTags = (this.state[key] || []).map(o => o.value)
const nextTags = options.filter(o => o).map(o => o.value)
const addedTags = difference(nextTags, previousTags)
const removedTags = difference(previousTags, nextTags)
addedTags.map(tag => addTag(serverUrl, corpusId, category, webentity.id, tag, tag))
removedTags.map(tag => removeTag(serverUrl, corpusId, category, webentity.id, tag))
this.setState({ [key]: options })
}
// big textarea-like with many tags
renderFreeTagsCategory (category) {
const { formatMessage } = this.context.intl
const suggestions = this.state[`suggestions/${category}`] || []
const values = this.state[`values/${category}`] || []
return (
<div className="browser-side-bar-tags-free-tags" key={ category } onKeyUp={ this._onKeyUp } >
<h3><span>{ formatMessage({ id: 'sidebar.freetags' }) }</span></h3>
<Creatable
autoBlur ignoreCase multi
clearable={ false }
newOptionCreator={ ({ label }) => toOption(label) }
noResultsText=''
options={ suggestions.map(toOption) }
onChange={ (options) => this.onChangeCreatable(options, category) }
placeholder={ formatMessage({ id: 'sidebar.select-tags' }) }
promptTextCreator={ (tag) => `${formatMessage({ id: 'sidebar.create-tag' })}"${tag}"` }
value={ values } />
</div>
)
}
// simpler input with only one tag to fill
renderTagsCategory (category) {
const { formatMessage } = this.context.intl
const suggestions = this.state[`suggestions/${category}`] || []
const values = this.state[`values/${category}`] || []
return (
<div className="browser-side-bar-tags-category" key={ category }>
<h4 className="category-name">{ category }</h4>
<div className="category-tag" onKeyUp={ this._onKeyUp } >
<Creatable
autoBlur ignoreCase
multi={ false }
clearValueText={ formatMessage({ id: 'tags-clear-category-value' }) }
newOptionCreator={ ({ label }) => toOption(label) }
noResultsText=''
options={ suggestions.map(toOption) }
onChange={ (option) => this.onChangeCreatable([option], category) }
placeholder=''
promptTextCreator={ (tag) => tag+' ' }
value={ values[0] || '' } />
</div>
</div>
)
}
// free tags should be first, then other categories, then add category field
render () {
const { formatMessage } = this.context.intl
const { showCategories, toggleCategories } = this.props
const [freeTags, cats] = partition(this.props.categories, isFreeTags)
const categories = uniq((cats || []).concat(this.state.categories).filter(x => x))
return (
<div className="browser-side-bar-tags">
{ this.renderFreeTagsCategory(freeTags[0]) }
<div>
<h3 onClick={ () => toggleCategories() }>
<T id="sidebar.categories" />
<span className={ cx({
'ti-angle-up': showCategories,
'ti-angle-down': !showCategories
}) }></span>
</h3>
{ showCategories && <div>
{ categories.sort().map(this.renderTagsCategory) }
<form className="browser-side-bar-tags-new-category" onSubmit={ this.addCategory }>
<input placeholder={ formatMessage({ id: 'sidebar.add-tags-category' }) }
value={ this.state.newCategory }
onKeyUp={ this._onKeyUp }
onInput={ this.onChangeNewCategory } />
<Button icon="plus" title={ formatMessage({ id: 'sidebar.add-tags-category' }) } />
</form>
</div> }
</div>
</div>
)
}
}
function isFreeTags (category) {
return category === 'FREETAGS'
}
function toOption (tag) {
return tag
? { label: tag, value: tag }
: { label: '', value: '' }
}
SideBarTags.contextTypes = {
intl: intlShape
}
SideBarTags.propTypes = {
serverUrl: PropTypes.string.isRequired,
corpusId: PropTypes.string.isRequired,
webentity: PropTypes.object.isRequired,
categories: PropTypes.arrayOf(PropTypes.string).isRequired,
locale: PropTypes.string.isRequired,
showCategories: PropTypes.bool.isRequired,
// actions
addTag: PropTypes.func.isRequired,
removeTag: PropTypes.func.isRequired,
addTagsCategory: PropTypes.func.isRequired,
fetchTags: PropTypes.func.isRequired,
toggleCategories: PropTypes.func.isRequired
}
const mapStateToProps = ({ corpora, intl: { locale }, ui }, props) => ({
...props,
categories: corpora.list[props.corpusId].tagsCategories || [],
showCategories: ui.showCategories,
locale
})
export default connect(mapStateToProps, {
addTag,
removeTag,
addTagsCategory,
fetchTags,
toggleCategories
})(SideBarTags)