author | Chloe Laisne <chloe.laisne@gmail.com> |
Sun, 21 Aug 2016 13:51:22 +0200 | |
changeset 257 | eba9edbd8f46 |
parent 256 | 18f0c3ee9aa5 |
child 258 | 12c694392e8e |
permissions | -rw-r--r-- |
245
c9dd78a43b07
Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff
changeset
|
1 |
import JSONAPISerializer from 'ember-data/serializers/json-api'; |
c9dd78a43b07
Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff
changeset
|
2 |
|
c9dd78a43b07
Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff
changeset
|
3 |
export default JSONAPISerializer.extend({ |
c9dd78a43b07
Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff
changeset
|
4 |
|
257
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
5 |
normalizeResponse: function(store, primaryModelClass, payload, id) { |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
6 |
var speakers = payload['resources'].find(resource => resource['id'] === 'speakers'); |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
7 |
var translationISO = false; |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
8 |
|
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
9 |
var buildFragment = function(annotation) { |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
10 |
var fragment = { |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
11 |
'original': annotation['content']['data']['content'] |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
12 |
}; |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
13 |
if(annotation['content']['data']['transl']) { |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
14 |
fragment['translation'] = annotation['content']['data']['transl']['@value']; |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
15 |
} |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
16 |
if(annotation['content']['data']['words']) { |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
17 |
var words = []; |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
18 |
annotation['content']['data']['words'].forEach(function(word) { |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
19 |
words.push({ |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
20 |
'original': word['content'], |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
21 |
'translation': word['transl']['@value'] |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
22 |
}); |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
23 |
}); |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
24 |
fragment['literal'] = words; |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
25 |
} |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
26 |
if(annotation['content']['data']['speaker']) { |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
27 |
if(typeof annotation['content']['data']['speaker'] === 'object') { |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
28 |
var speaker = speakers['content']['data'].find(speaker => speaker['id'] === annotation['content']['data']['speaker']['id-ref']); |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
29 |
if(speaker) { |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
30 |
fragment['speaker'] = speaker['name']; |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
31 |
} |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
32 |
} else { |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
33 |
fragment['speaker'] = annotation['content']['data']['speaker']; |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
34 |
} |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
35 |
} |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
36 |
return fragment; |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
37 |
}; |
256
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
38 |
|
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
39 |
var annotations = []; |
257
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
40 |
payload['annotations'].forEach(function(annotation) { |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
41 |
var previous = annotations[annotations.length - 1]; |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
42 |
if(previous && annotation['begin'] === previous.begin && annotation['end'] === previous.end) { |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
43 |
previous.fragments.push(buildFragment(annotation)); |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
44 |
} else { |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
45 |
var object = { |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
46 |
'fragments': [buildFragment(annotation)], |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
47 |
'begin': annotation['begin'], |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
48 |
'end': annotation['end'], |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
49 |
}; |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
50 |
var information = payload['annotation-types'].find(function(t) { return t['corpus:begin'] === annotation['begin'] && t['corpus:end'] === annotation['end']; }); |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
51 |
if(information) { |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
52 |
object.title = information['dc:title']; |
256
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
53 |
} |
257
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
54 |
annotations.push(object); |
256
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
55 |
} |
257
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
56 |
if(!translationISO && annotation['content']['data']['transl']) { |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
57 |
translationISO = annotation['content']['data']['transl']['@language']; |
256
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
58 |
} |
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
59 |
}); |
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
60 |
|
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
61 |
var response = { |
245
c9dd78a43b07
Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff
changeset
|
62 |
'data': { |
256
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
63 |
'id': id, |
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
64 |
'type': 'transcript', |
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
65 |
'attributes': { |
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
66 |
'title': {}, |
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
67 |
'annotations': annotations |
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
68 |
} |
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
69 |
} |
245
c9dd78a43b07
Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff
changeset
|
70 |
}; |
256
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
71 |
|
257
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
72 |
if(Array.isArray(payload['meta']['dc:title'])) { |
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
73 |
var original = payload['meta']['dc:title'].find(function(title) { return title['@language'] !== translationISO; }); |
256
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
74 |
if(original) { |
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
75 |
response.data.attributes.title.original = original['@value']; |
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
76 |
} |
257
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
77 |
var translation = payload['meta']['dc:title'].find(function(title) { return title['@language'] === translationISO; }); |
256
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
78 |
if(translation) { |
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
79 |
response.data.attributes.title.translation = translation['@value']; |
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
80 |
} |
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
81 |
} else { |
257
eba9edbd8f46
Add title to annotation
Chloe Laisne <chloe.laisne@gmail.com>
parents:
256
diff
changeset
|
82 |
response.data.attributes.title.original = payload['meta']['dc:title']['@value']; |
256
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
83 |
} |
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
84 |
|
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
85 |
|
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
86 |
return response; |
18f0c3ee9aa5
Add title translation to transcript
Chloe Laisne <chloe.laisne@gmail.com>
parents:
255
diff
changeset
|
87 |
} |
245
c9dd78a43b07
Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff
changeset
|
88 |
|
c9dd78a43b07
Transcript model and erializer
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff
changeset
|
89 |
}); |