/
TranslationModal.js
250 lines (219 loc) · 10.5 KB
/
TranslationModal.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
248
249
250
import React from 'react';
import FocusTrap from 'focus-trap-react';
import {connectToStores} from 'fluxible-addons-react';
import ISO6391 from 'iso-639-1';
import {navigateAction} from 'fluxible-router';
import translateDeckRevision from '../../../actions/translation/translateDeckRevision.js';
import translateSlideRevision from '../../../actions/translation/translateSlideRevision.js';
import { Dropdown, Button, Modal, Icon, Header, Divider} from 'semantic-ui-react';
import TranslationStore from '../../../stores/TranslationStore';
import UserProfileStore from '../../../stores/UserProfileStore';
import DeckTreeStore from '../../../stores/DeckTreeStore';
import loadSlidePreview from '../../../actions/slide/loadSlidePreview';
import SlidePreviewModal from './SlidePreviewModal';
class TranslationModal extends React.Component {
constructor(props) {
super(props);
this.state = {language: null, error:false, previewModal:false, previewLanguage: null};
}
handleClose(){
this.setState({language: null, error: false, previewLanguage: null});
this.props.handleClose();
}
handleTranslateToClick(mode, selector = null){
//$(document).find('#deckViewPanel').prepend('<div className="ui active dimmer"><div className="ui text loader">Loading</div></div>');
let code = this.state.language;
if (code){
switch (mode){
case 'deck':
this.context.executeAction(translateDeckRevision, {
// TODO this is wrong, the second part for a lanugage code is the COUNTRY not the language, so for greek the el_EL is invalid
language: code+'_'+code.toUpperCase(),
mode: 'deck'
});
this.props.handleClose();
break;
case 'slide':
this.context.executeAction(translateSlideRevision, {
// TODO this is wrong, the second part for a lanugage code is the COUNTRY not the language, so for greek the el_EL is invalid
language: code+'_'+code.toUpperCase(),
selector: selector
});
this.setState({previewModal: true});
break;
case 'subdeck':
this.context.executeAction(translateDeckRevision, {
// TODO this is wrong, the second part for a lanugage code is the COUNTRY not the language, so for greek the el_EL is invalid
language: code+'_'+code.toUpperCase(),
selector: selector,
mode: 'subdeck'
});
this.props.handleClose();
break;
}
}else{
this.setState({error: true});
}
}
handleOptionChange(event, data) {
this.setState({language: data.value, error: false});
}
renderTranslateTo(supported) {
return (
{value:supported.code , key: supported.code, text: supported.name}
);
}
showSlidePreview(id, language){
this.context.executeAction(loadSlidePreview, {params: {'sid' : id}});
this.setState({previewModal: true, previewLanguage: language});
}
render() {
let current = '';
let selector = this.props.selector;
let mode = this.props.mode;
if (mode !== 'deck'){ //this is content item modal
if (this.props.selector.stype === 'deck'){ //a deck is selected
mode = 'subdeck';
}else{
mode = 'slide'; //a slide is selected
}
}
let translations = [];
let existing_codes = [];
let languages_string = '';
let languages_array = [];
let available_desc = '';
let available_array = [];
let languageOptions = [];
let currentLanguage = 'en_GB';
switch (mode) {
case 'deck': //root deck, subdecks
currentLanguage = this.props.DeckTreeStore.currentLang.language;
if (currentLanguage){
current = ISO6391.getName(currentLanguage.toLowerCase().substr(0,2));
}else{
current = '';
}
translations = this.props.DeckTreeStore.translations; //for the root deck
break;
case 'slide': //slide, subdeck
currentLanguage = this.props.TranslationStore.currentLang.language;
if (currentLanguage){
current = ISO6391.getName(currentLanguage.toLowerCase().substr(0,2));
}else{
current = '';
}
translations = this.props.TranslationStore.translations;
break;
case 'subdeck': //slide, subdeck
currentLanguage = this.props.TranslationStore.currentLang.language;
if (currentLanguage){
current = ISO6391.getName(currentLanguage.toLowerCase().substr(0,2));
}else{
current = '';
}
translations = this.props.TranslationStore.translations;
break;
default:
currentLanguage = this.props.DeckTreeStore.currentLang.language;
if (currentLanguage){
current = ISO6391.getName(currentLanguage.toLowerCase().substr(0,2));
}else{
current = '';
}
translations = this.props.DeckTreeStore.translations; //for the root deck
break;
}
if (translations){
existing_codes = translations.map((el) => { //getting all translations codes
return el.language.split('_')[0];
});
available_array = translations.map((translation) => {
let languageName = ISO6391.getName(translation.language.toLowerCase().substr(0,2));
let link = '/deck/';
if (languageName){
if (translation.language !== currentLanguage){
switch (mode){
case 'deck':
link+= translation.deck_id;
return (
<a href={link} key={languageName}>{languageName}, </a>
);
break;
case 'slide':
return (
<a as='a' onClick={ this.showSlidePreview.bind(this, translation.slide_id, languageName) } key ={languageName}>{languageName}, </a>
);
break;
case 'subdeck':
link+= translation.deck_id;
return (
<a href={link} key={languageName}>{languageName}, </a>
);
break;
default: //subdeck
link+= translation.deck_id;
return (
<a href={link} key={languageName}>{languageName}, </a>
);
break;
}
}
}else{
return null;
}
});
if (currentLanguage){
available_desc = <p>This {mode} is already available in {available_array} and <b>{current}</b>.</p>;
}
}
const supported = this.props.TranslationStore.supportedLangs.filter((el) => { //removing existing translations from supported
return !existing_codes.includes(el.code);
});
if (supported){
languageOptions = supported.map(this.renderTranslateTo, this);
}
return (
<Modal dimmer='blurring' size='small' role='dialog' aria-labelledby='translationModalHeader'
aria-describedby='translationModalDesc' open={this.props.isOpen}
onClose={this.props.handleClose}>
<SlidePreviewModal languageName={this.state.previewLanguage} slide={this.props.TranslationStore.slideToPreview} isOpen={this.state.previewModal} handleClose={() => this.setState({previewModal: false})} />
<Header icon='translate' content={'Translate the ' +mode} id='translationModalHeader'/>
<Modal.Content>
<FocusTrap focusTrapOptions={{clickOutsideDeactivates: true}} active={this.props.isOpen}>
{available_desc}
<p id='translationModalDesc'>You are about to translate the {mode}. Please choose the target language from the list below:</p>
<Dropdown
placeholder='Choose a target language...'
scrolling
selection
search
className={this.state.error ? 'error' : ''}
options={languageOptions}
onChange={this.handleOptionChange.bind(this)}
ref = {(dropDown) => {this.dropDown = dropDown;}}
value = {this.state.language}
/>
<p><b>!Please note that this is an experimental service! <br/> We use Microsoft Bing translation service, which may not be accurate.</b></p>
<Divider/>
<p>
<Button as='button' primary onClick={this.handleTranslateToClick.bind(this, mode, this.props.selector)}><Icon name='translate'/> Translate</Button>
<Button as='button' onClick={this.handleClose.bind(this)}><Icon name='close'/> Close</Button>
</p>
</FocusTrap>
</Modal.Content>
</Modal>
);
}
}
TranslationModal.contextTypes = {
executeAction: React.PropTypes.func.isRequired
};
TranslationModal = connectToStores(TranslationModal, [TranslationStore, UserProfileStore, DeckTreeStore], (context, props) => {
return {
TranslationStore: context.getStore(TranslationStore).getState(),
UserProfileStore: context.getStore(UserProfileStore).getState(),
DeckTreeStore: context.getStore(DeckTreeStore).getState(),
};
});
export default TranslationModal;