1 import JSONAPISerializer from 'ember-data/serializers/json-api'; |
1 import JSONAPISerializer from 'ember-data/serializers/json-api'; |
2 |
2 |
3 export default JSONAPISerializer.extend({ |
3 export default JSONAPISerializer.extend({ |
4 |
4 |
5 normalizeResponse: function(store, primaryModelClass, payload, id, requestType) { |
5 normalizeResponse: function(store, primaryModelClass, payload, id) { |
6 var lang = false; |
6 var speakers = payload['resources'].find(resource => resource['id'] === 'speakers'); |
|
7 var translationISO = false; |
|
8 |
|
9 var buildFragment = function(annotation) { |
|
10 var fragment = { |
|
11 'original': annotation['content']['data']['content'] |
|
12 }; |
|
13 if(annotation['content']['data']['transl']) { |
|
14 fragment['translation'] = annotation['content']['data']['transl']['@value']; |
|
15 } |
|
16 if(annotation['content']['data']['words']) { |
|
17 var words = []; |
|
18 annotation['content']['data']['words'].forEach(function(word) { |
|
19 words.push({ |
|
20 'original': word['content'], |
|
21 'translation': word['transl']['@value'] |
|
22 }); |
|
23 }); |
|
24 fragment['literal'] = words; |
|
25 } |
|
26 if(annotation['content']['data']['speaker']) { |
|
27 if(typeof annotation['content']['data']['speaker'] === 'object') { |
|
28 var speaker = speakers['content']['data'].find(speaker => speaker['id'] === annotation['content']['data']['speaker']['id-ref']); |
|
29 if(speaker) { |
|
30 fragment['speaker'] = speaker['name']; |
|
31 } |
|
32 } else { |
|
33 fragment['speaker'] = annotation['content']['data']['speaker']; |
|
34 } |
|
35 } |
|
36 return fragment; |
|
37 }; |
7 |
38 |
8 var annotations = []; |
39 var annotations = []; |
9 payload.annotations.forEach(function(annotation) { |
40 payload['annotations'].forEach(function(annotation) { |
10 var annotationObject = { |
41 var previous = annotations[annotations.length - 1]; |
11 'content': annotation.content.data.content, |
42 if(previous && annotation['begin'] === previous.begin && annotation['end'] === previous.end) { |
12 'start': annotation.begin, |
43 previous.fragments.push(buildFragment(annotation)); |
13 'end': annotation.end |
44 } else { |
14 }; |
45 var object = { |
15 if(annotation.content.data.transl) { |
46 'fragments': [buildFragment(annotation)], |
16 annotationObject.translation = annotation.content.data.transl['@value']; |
47 'begin': annotation['begin'], |
17 if (!lang) { |
48 'end': annotation['end'], |
18 lang = annotation.content.data.transl['@language']; |
49 }; |
|
50 var information = payload['annotation-types'].find(function(t) { return t['corpus:begin'] === annotation['begin'] && t['corpus:end'] === annotation['end']; }); |
|
51 if(information) { |
|
52 object.title = information['dc:title']; |
19 } |
53 } |
|
54 annotations.push(object); |
20 } |
55 } |
21 if(annotation.content.data.words) { |
56 if(!translationISO && annotation['content']['data']['transl']) { |
22 var words = []; |
57 translationISO = annotation['content']['data']['transl']['@language']; |
23 annotation.content.data.words.forEach(function(word) { |
|
24 var wordObject = { |
|
25 'content': word.content, |
|
26 'translation': word.transl['@value'] |
|
27 }; |
|
28 words.push(wordObject); |
|
29 }) |
|
30 annotationObject.words = words; |
|
31 } |
58 } |
32 annotations.push(annotationObject); |
|
33 }); |
59 }); |
34 |
60 |
35 var response = { |
61 var response = { |
36 'data': { |
62 'data': { |
37 'id': id, |
63 'id': id, |
41 'annotations': annotations |
67 'annotations': annotations |
42 } |
68 } |
43 } |
69 } |
44 }; |
70 }; |
45 |
71 |
46 if(Array.isArray(payload.meta['dc:title'])) { |
72 if(Array.isArray(payload['meta']['dc:title'])) { |
47 var original = payload.meta['dc:title'].find(function(title) { return title['@language'] !== lang; }); |
73 var original = payload['meta']['dc:title'].find(function(title) { return title['@language'] !== translationISO; }); |
48 var translation = payload.meta['dc:title'].find(function(title) { return title['@language'] === lang; }); |
|
49 if(original) { |
74 if(original) { |
50 response.data.attributes.title.original = original['@value']; |
75 response.data.attributes.title.original = original['@value']; |
51 } |
76 } |
|
77 var translation = payload['meta']['dc:title'].find(function(title) { return title['@language'] === translationISO; }); |
52 if(translation) { |
78 if(translation) { |
53 response.data.attributes.title.translation = translation['@value']; |
79 response.data.attributes.title.translation = translation['@value']; |
54 } |
80 } |
55 } else { |
81 } else { |
56 response.data.attributes.title.original = payload.meta['dc:title']['@value']; |
82 response.data.attributes.title.original = payload['meta']['dc:title']['@value']; |
57 } |
83 } |
58 |
84 |
59 |
85 |
60 return response; |
86 return response; |
61 } |
87 } |