-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathViewpoint.jsx
More file actions
220 lines (206 loc) · 6.47 KB
/
Viewpoint.jsx
File metadata and controls
220 lines (206 loc) · 6.47 KB
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
import React from 'react';
import Hypertopic from 'hypertopic';
import conf from '../../config.js';
import {Topics} from '../../model.js';
import TopicPath from './TopicPath.jsx';
import TopicTree from '../../TopicTree.js';
import InputWithSuggestions from '../InputWithSuggestions.jsx';
import { t } from '@lingui/macro';
class Viewpoint extends React.Component {
constructor(props) {
super();
this.state = {
topics: {},
topicInputvalue: '',
currentSelection: '',
};
}
componentDidUpdate(prevProps) {
if (this.props.update_seq !== prevProps.update_seq) {
this._fetchViewpoint();
}
}
onTopicInputFocus = (event) => {
if (this.blurTimeout) {
this.blurTimeout = clearTimeout(this.blurTimeout);
}
this.setState({ hasFocus: true });
}
onTopicInputBlur = (event) => {
this.blurTimeout = setTimeout(() => {
this.setState({ hasFocus: false });
}, 200);
}
onTopicInputkeyDown = (event) => {
if (event.key === 'Escape') {
this.clearInput();
}
};
onTopicInputChange = (event, { newValue }) => {
if (this.state.currentSelection) {
newValue = '';
}
this.setState({
topicInputvalue: newValue,
currentSelection: ''
});
};
onSuggestionSelected = (event, { suggestion }) => {
this.setState({ currentSelection: suggestion });
};
clearInput = () => {
this.setState({
topicInputvalue: '',
currentSelection: '',
newTopic: ''
});
};
updatingTopicList = (topicToAssign, viewpointId) => {
return this.props
.assignTopic(topicToAssign, viewpointId)
.catch(error => console.error(error));
};
render() {
const paths = this._getPaths();
const inputProps = {
placeholder: this.state.newTopic ? t`Choisir une rubrique parent...` : t`Ajouter une rubrique...`,
value: this.state.topicInputvalue,
onFocus: this.onTopicInputFocus,
onBlur: this.onTopicInputBlur,
onChange: this.onTopicInputChange,
onKeyDown: this.onTopicInputKeyDown,
};
let classes = ['TopicAdding', 'input-group'];
if (!this.state.hasFocus) {
classes.push('inactive');
}
var newTopic;
if (this.state.newTopic) {
newTopic = (
<div className="newTopic">Ajouter nouveau : > {this.state.newTopic}
<button type="button" className="btn btn-xs ml-3 float-right DeleteButton"
onClick={ () => this.setState({newTopic: ''}) } id="deleteButton-newTopic">
<span className="oi oi-x"> </span>
</button>
</div>
);
}
const canValidateTopic = this.state.currentSelection || this.state.newTopic || this.state.topicInputvalue.length > 2;
let alreadyAssigned = this.props.topics.map(x => x.id);
let candidates = new Topics(this.state.topics).getAllPaths()
.filter(x => !alreadyAssigned.includes(x.id));
return (
<div className="Viewpoint">
<h3 className="h4">{this.state.name}</h3>
<hr/>
<div className="Topics">
{paths}
<div className={classes.join(' ')}>
<div className="d-none d-sm-block">
<InputWithSuggestions candidates={candidates}
onSuggestionSelected={this.onSuggestionSelected}
inputProps={inputProps}
id={this.props.id}
/>
</div>
<div className="input-group-append">
<button type="button" className="btn btn-sm ValidateButton btn" onClick={() => {
if (this.state.newTopic && (this.state.currentSelection || !this.state.topicInputvalue)) {
var parentId;
if (this.state.currentSelection)
parentId = this.state.currentSelection.id;
this.createTopic(this.state.newTopic, parentId)
.then(newId => {
this.updatingTopicList(
newId,
this.props.id
);
})
.then(this.clearInput);
} else {
if (this.state.currentSelection) {
this.updatingTopicList(
this.state.currentSelection.id,
this.props.id
).then(this.clearInput);
} else {
this.setState({
newTopic: this.state.topicInputvalue,
topicInputvalue: ''
});
}
}
}}
onFocus={this.onTopicInputFocus} onBlur={this.onTopicInputBlur}
disabled={!canValidateTopic}
id={`validateButton-${this.state.name}`}>
<span className="oi oi-check"> </span>
</button>
</div>
{newTopic}
</div>
</div>
</div>
);
}
_getPaths() {
return this.props.topics.map(t => (
<TopicPath
key={t.id}
id={t.id}
topics={this.state.topics}
removeTopic={() => this.props.removeTopic(t)}
/>
));
}
componentDidMount() {
this._fetchViewpoint();
}
async _fetchViewpoint() {
this.setState(await _fetchViewpointData(this.props.id));
}
async createTopic(name, parent) {
var newId;
let hypertopic = new Hypertopic((await conf).services);
return hypertopic.get({ _id: this.props.id })
.then(x => {
let topicTree = new TopicTree(x.topics);
let newParent = parent || 'root';
let newTopic = topicTree.newChildren(newParent);
newTopic.name = name;
newId = newTopic.id;
delete newTopic.id;
x.topics = topicTree.topics;
return x;
})
.then(hypertopic.post)
.then(_ => {
this.setState(previousState => {
let newTopic = {
id: newId,
name: [name]
};
if (parent) {
newTopic.broader = [{
id: parent,
name: previousState.topics[parent].name
}];
}
previousState.topics[newId] = newTopic;
return previousState;
});
})
.then(() => newId);
}
}
export const _fetchViewpointData = async(id) => {
return new Hypertopic((await conf).services).getView(`/viewpoint/${id}`).then((data) => {
let topics = data[id];
const name = topics.name;
delete topics.user;
delete topics.name;
delete topics.upper;
return ({name, topics});
});
};
export default Viewpoint;