This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
GlossaryStore.js
483 lines (431 loc) · 13.1 KB
/
GlossaryStore.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import Dispatcher from '../dispatchers/GlossaryDispatcher';
import assign from 'object-assign';
import {EventEmitter} from 'events';
import Configs from '../constants/Configs';
import {GlossaryActionTypes} from '../constants/ActionTypes';
import GlossaryHelper from '../utils/GlossaryHelper'
import StringUtils from '../utils/StringUtils'
import DateHelpers from '../utils/DateHelper'
import _ from 'lodash';
import GlossaryAPIStore from './GlossaryAPIStore'
var PAGE_SIZE = 1000,
CHANGE_EVENT = "change",
MAX_LISTENERS = 100, //overriding number of listener for GlossaryStore. (default is 11)
SEVERITY = {
INFO : 'info',
WARN : 'warn',
ERROR: 'error'
};
EventEmitter.prototype.setMaxListeners(MAX_LISTENERS);
var _state = {
canAddNewEntry: false,
canUpdateEntry: false,
canDeleteEntry: false,
localeOptions: [],
srcLocale: null,
selectedTransLocale: null,
locales: null,
glossary: {},
original_glossary: {},
glossaryIds: [],
page:1,
filter: '',
focusedRow: {
rowIndex: -1
},
uploadFile: {
show: false,
status : -1,
file: null,
transLocale: null
},
sort: {
src_content: true
},
totalCount: 0,
newEntry: {
term: '',
pos: '',
description: '',
isSaving: false,
show: false,
isAllowSave: false
},
loadingEntries: true
};
function processLocalesStatistic(res) {
if(res.error) {
setNotification(SEVERITY.ERROR,
'We are sorry',
'Error during processing glossary info from server.',
res, true);
} else {
var body = res.body;
_state.locales = {};
_state.localeOptions = [];
_state.srcLocale = body['srcLocale'];
resetCache();
for (var i = 0; i < _state.srcLocale.numberOfTerms; i++) {
_state.glossaryIds[i] = null;
}
_.forEach(body.transLocale, function(transLocale) {
_state.locales[transLocale.locale.localeId] = transLocale;
_state.localeOptions.push({
value: transLocale.locale.localeId,
label: `${transLocale.locale.displayName} - (${transLocale.numberOfTerms})`
});
});
}
return _state;
}
function loadGlossaryByLocale() {
_state.loadingEntries = true;
return GlossaryAPIStore.loadGlossaryByLocale(_state.srcLocale,
_state.selectedTransLocale, _state.filter, _state.sort, _state.page,
PAGE_SIZE);
}
function canAddNewEntry () {
return Configs.data.permission.insertGlossary;
}
function canUpdateEntry() {
return Configs.data.permission.updateGlossary;
}
function canDeleteEntry() {
return Configs.data.permission.deleteGlossary;
}
function processGlossaryList(res) {
if(res.error) {
setNotification(SEVERITY.ERROR,
'We are sorry',
'Error during processing glossary response from server.',
res, true);
} else {
var transLocaleId = _state.selectedTransLocale,
page = _state.page,
body = res.body;
_state.totalCount = body.totalCount;
var startIndex = ((page -1) * PAGE_SIZE);
_.forOwn(body.results, function(entry) {
_state.glossary[entry.id] = GlossaryHelper.generateEntry(entry, transLocaleId);
_state.glossary[entry.id].status['isSrcValid'] = GlossaryHelper.isSourceValid( _state.glossary[entry.id]);
_state.glossary[entry.id].status['canUpdateTransComment'] = GlossaryHelper.canUpdateTransComment( _state.glossary[entry.id]);
_state.glossaryIds[startIndex] = [entry.id];
startIndex++;
});
_state.original_glossary = _.cloneDeep(_state.glossary);
_state.loadingEntries = false;
}
return _state;
}
function saveGlossary(data) {
var entry = {
id: '', pos: data.pos, description: data.description,
srcTerm: GlossaryHelper.generateSrcTerm(data.srcLocaleId)
};
entry.srcTerm.content = data.term;
var glossaryEntry = GlossaryHelper.generateGlossaryEntryDTO(entry);
return GlossaryAPIStore.saveOrUpdateGlossary([glossaryEntry]);
}
function updateGlossary(entry) {
entry.status.isSaving = true;
var glossaryEntry = GlossaryHelper.generateGlossaryEntryDTO(entry);
return GlossaryAPIStore.saveOrUpdateGlossary([glossaryEntry]);
}
function onUploadFile(percentCompleted) {
_state.uploadFile.status = percentCompleted;
}
function processUploadFile(res) {
_state.uploadFile.status = -1;
_state.uploadFile.show = false;
_state.uploadFile.transLocale = null;
_state.uploadFile.file = null;
if(res.error) {
setNotification(SEVERITY.ERROR,
'We are sorry',
'We were unable to import your file. Please refresh this page and try again.',
res, true);
} else {
var message;
if(res.body) {
var savedCount = _.size(res.body.glossaryEntries);
message = savedCount + ' terms imported.';
}
setNotification(SEVERITY.INFO, 'File imported successfully', message, '', true);
}
}
function processDelete(res) {
if(res.error) {
setNotification(SEVERITY.ERROR,
'We are sorry',
'We were unable to delete this glossary term. Please refresh this page and try again.',
res, true);
} else {
setNotification(SEVERITY.INFO, 'Glossary term deleted', '', '', false);
}
}
function processSave(res) {
_.assign(_state.newEntry, {
isSaving: false,
show: false,
isAllowSave: false,
pos: '',
term: '',
description: ''
});
if(res.error) {
setNotification(SEVERITY.ERROR, 'We are sorry', 'We were unable to create glossary term. Please refresh this page and try again.', res, true);
return;
} else if(!_.isEmpty(res.body.warnings)) {
setNotification(SEVERITY.WARN, 'Warning', 'Warning during save', res.body.warnings, true);
return;
}
setNotification(SEVERITY.INFO, 'Glossary term created', '', '', false);
}
function processUpdate(res, id) {
_state.glossary[id].status = GlossaryHelper.getDefaultEntryStatus();
if(res.error) {
setNotification(SEVERITY.ERROR, 'We are sorry', 'We were unable to update your changes. Please refresh this page and try again.', res, true);
return;
} else if(!_.isEmpty(res.body.warnings)) {
setNotification(SEVERITY.WARN, 'Warning', 'Warning during update', res.body.warnings, true);
return;
}
setNotification(SEVERITY.INFO, 'Glossary term updated', '', '', false);
}
/**
* Set notification message.
* Display model if showModel=true, otherwise, only display in console.
*
* @param severity
* @param subject
* @param msg
* @param details
* @param showModel
*/
function setNotification(severity, subject, msg, details, showModel) {
switch(severity) {
case SEVERITY.WARN:
console.warn(subject, msg, details);
break;
case SEVERITY.ERROR:
console.error(subject, msg, details);
break;
case SEVERITY.INFO:
default:
console.info(subject, msg, details);
break;
}
if(showModel) {
_state.notification = {
SEVERITY: severity,
SUBJECT: subject,
MESSAGE: msg,
DETAILS: details
};
}
}
function refreshGlossaryEntries() {
//load permission here
_state.canAddNewEntry = canAddNewEntry();
_state.canUpdateEntry = canUpdateEntry();
_state.canDeleteEntry = canDeleteEntry();
GlossaryAPIStore.loadLocalesStats()
.then(processLocalesStatistic)
.then(loadGlossaryByLocale)
.then(processGlossaryList)
.then(() => GlossaryStore.emitChange());
}
function resetCache() {
_.assign(_state, {
page: 1,
glossary: {},
glossaryIds: [],
sort:{
src_content: true
}
});
}
var GlossaryStore = assign({}, EventEmitter.prototype, {
init: function() {
if (_state.locales === null) {
refreshGlossaryEntries();
}
return _state;
}.bind(this),
getEntry: function(id) {
return _state.glossary[id];
},
getSort: function(field) {
return _state.sort[field];
},
getFocusedRow: function() {
return _state.focusedRow;
},
getNewEntryState: function() {
return _state.newEntry;
},
getUploadFileState: function() {
return _state.uploadFile;
},
emitChange: function() {
this.emit(CHANGE_EVENT);
},
/**
* @param {function} callback
*/
addChangeListener: function(callback) {
this.on(CHANGE_EVENT, callback);
},
/**
* @param {function} callback
*/
removeChangeListener: function(callback) {
this.removeListener(CHANGE_EVENT, callback);
},
_onTranslationLocaleChanged: function (data) {
console.debug('select language', data);
_state.selectedTransLocale = data;
resetCache();
loadGlossaryByLocale()
.then(processGlossaryList)
.then(() => GlossaryStore.emitChange());
},
_onSaveGlossary: function(data) {
console.debug('save glossary', data);
_state.newEntry.isSaving = true;
_state.newEntry.show = true;
GlossaryStore.emitChange();
saveGlossary(data)
.then(processSave)
.then(refreshGlossaryEntries);
},
_onUpdateGlossary: function(data) {
console.debug('update glossary', _state.glossary[data]);
updateGlossary(_state.glossary[data])
.then(function(res) {
processUpdate(res, data);
})
.then(refreshGlossaryEntries);
},
_onDeleteGlossary: function(data) {
console.debug('delete entry', data);
GlossaryAPIStore.deleteGlossary(data)
.then(processDelete)
.then(refreshGlossaryEntries);
},
_onUpdateFilter: function(data) {
console.debug('Update filter', data);
if(_state.filter !== data) {
_state.filter = data;
refreshGlossaryEntries();
}
},
_onLoadGlossary: function(data) {
console.debug('load glossary', data);
var pageSize = data % PAGE_SIZE === 0 ? PAGE_SIZE - 1 : PAGE_SIZE;
_state.page = Math.ceil(data/pageSize);
_state.page = Math.max(_state.page, 1);
loadGlossaryByLocale()
.then(processGlossaryList)
.then(() => GlossaryStore.emitChange());
},
_onUpdateSortOrder: function(data) {
console.debug('Update sort order', data);
_state.sort = {};
_state.sort[data.field] = data.ascending;
loadGlossaryByLocale()
.then(processGlossaryList)
.then(() => GlossaryStore.emitChange());
},
_onUploadFile: function(data) {
console.debug('Upload file', data);
GlossaryAPIStore.uploadFile(data, onUploadFile)
.then(processUploadFile)
.then(refreshGlossaryEntries)
.then(() => GlossaryStore.emitChange());
},
_onUpdateEntryField: function(data) {
_.set(_state.glossary[data.id], data.field, data.value);
_state.glossary[data.id]['status'] =
GlossaryHelper.getEntryStatus(_state.glossary[data.id], _state.original_glossary[data.id]);
GlossaryStore.emitChange();
},
_onUpdateComment: function(data) {
console.debug('Update comment', data);
_.set(_state.glossary[data.id], 'transTerm.comment', data.value);
updateGlossary(_state.glossary[data.id])
.then(function(res) {
processUpdate(res, data.id);
})
.then(refreshGlossaryEntries);
},
_onUpdateFocusRow: function(data) {
var previousId = _state.focusedRow.id,
previousRowIndex = _state.focusedRow.rowIndex;
//same index, ignore
if(previousRowIndex === data.rowIndex) {
return;
}
if(previousRowIndex !== -1 && previousId !== null) {
var entry = _state.glossary[previousId];
if(entry && (entry.status.isSrcModified || entry.status.isTransModified)) {
entry.status.isSaving = true;
GlossaryStore.emitChange();
GlossaryStore._onUpdateGlossary(previousId);
}
}
_state.focusedRow = data;
GlossaryStore.emitChange();
},
_onResetEntry: function(data) {
console.debug('reset entry', data);
_state.glossary[data] = _.cloneDeep(_state.original_glossary[data]);
GlossaryStore.emitChange();
},
dispatchToken: Dispatcher.register(function(payload) {
const action = payload.action;
switch (action.actionType) {
case GlossaryActionTypes.TRANS_LOCALE_SELECTED:
GlossaryStore._onTranslationLocaleChanged(action.data);
break;
case GlossaryActionTypes.INSERT_GLOSSARY:
GlossaryStore._onSaveGlossary(action.data);
break;
case GlossaryActionTypes.UPDATE_GLOSSARY:
GlossaryStore._onUpdateGlossary(action.data);
break;
case GlossaryActionTypes.DELETE_GLOSSARY:
GlossaryStore._onDeleteGlossary(action.data);
break;
case GlossaryActionTypes.UPDATE_FILTER:
GlossaryStore._onUpdateFilter(action.data);
break;
case GlossaryActionTypes.LOAD_GLOSSARY:
GlossaryStore._onLoadGlossary(action.data);
break;
case GlossaryActionTypes.UPDATE_SORT_ORDER:
GlossaryStore._onUpdateSortOrder(action.data);
break;
case GlossaryActionTypes.UPLOAD_FILE:
GlossaryStore._onUploadFile(action.data);
break;
case GlossaryActionTypes.UPDATE_ENTRY_FIELD:
GlossaryStore._onUpdateEntryField(action.data);
break;
case GlossaryActionTypes.UPDATE_COMMENT:
GlossaryStore._onUpdateComment(action.data);
break;
case GlossaryActionTypes.UPDATE_FOCUSED_ROW:
GlossaryStore._onUpdateFocusRow(action.data);
break;
case GlossaryActionTypes.RESET_ENTRY:
GlossaryStore._onResetEntry(action.data);
break;
case GlossaryActionTypes.CLEAR_MESSAGE:
_state.notification = null;
GlossaryStore.emitChange();
break;
}
})
});
export default GlossaryStore;