cms/app-client/app/serializers/transcript.js
changeset 257 eba9edbd8f46
parent 256 18f0c3ee9aa5
child 258 12c694392e8e
--- a/cms/app-client/app/serializers/transcript.js	Thu Aug 18 17:02:02 2016 +0200
+++ b/cms/app-client/app/serializers/transcript.js	Sun Aug 21 13:51:22 2016 +0200
@@ -2,34 +2,60 @@
 
 export default JSONAPISerializer.extend({
 
-    normalizeResponse: function(store, primaryModelClass, payload, id, requestType) {
-        var lang = false;
+    normalizeResponse: function(store, primaryModelClass, payload, id) {
+        var speakers = payload['resources'].find(resource => resource['id'] === 'speakers');
+        var translationISO = false;
+
+        var buildFragment = function(annotation) {
+            var fragment = {
+                'original': annotation['content']['data']['content']
+            };
+            if(annotation['content']['data']['transl']) {
+                fragment['translation'] = annotation['content']['data']['transl']['@value'];
+            }
+            if(annotation['content']['data']['words']) {
+                var words = [];
+                annotation['content']['data']['words'].forEach(function(word) {
+                    words.push({
+                        'original': word['content'],
+                        'translation': word['transl']['@value']
+                    });
+                });
+                fragment['literal'] = words;
+            }
+            if(annotation['content']['data']['speaker']) {
+                if(typeof annotation['content']['data']['speaker'] === 'object') {
+                    var speaker = speakers['content']['data'].find(speaker => speaker['id'] === annotation['content']['data']['speaker']['id-ref']);
+                    if(speaker) {
+                        fragment['speaker'] = speaker['name'];
+                    }
+                } else {
+                    fragment['speaker'] = annotation['content']['data']['speaker'];
+                }
+            }
+            return fragment;
+        };
 
         var annotations = [];
-        payload.annotations.forEach(function(annotation) {
-            var annotationObject = {
-                'content': annotation.content.data.content,
-                'start': annotation.begin,
-                'end': annotation.end
-            };
-            if(annotation.content.data.transl) {
-                annotationObject.translation = annotation.content.data.transl['@value'];
-                if (!lang) {
-                    lang = annotation.content.data.transl['@language'];    
+        payload['annotations'].forEach(function(annotation) {
+            var previous = annotations[annotations.length - 1];
+            if(previous && annotation['begin'] === previous.begin && annotation['end'] === previous.end) {
+                previous.fragments.push(buildFragment(annotation));
+            } else {
+                var object = {
+                    'fragments': [buildFragment(annotation)],
+                    'begin': annotation['begin'],
+                    'end': annotation['end'],
+                };
+                var information = payload['annotation-types'].find(function(t) { return t['corpus:begin'] === annotation['begin'] && t['corpus:end'] === annotation['end']; });
+                if(information) {
+                    object.title = information['dc:title'];
                 }
+                annotations.push(object);
             }
-            if(annotation.content.data.words) {
-                var words = [];
-                annotation.content.data.words.forEach(function(word) {
-                    var wordObject = {
-                        'content': word.content,
-                        'translation': word.transl['@value']
-                    };
-                    words.push(wordObject);
-                })
-                annotationObject.words = words;
+            if(!translationISO && annotation['content']['data']['transl']) {
+                translationISO = annotation['content']['data']['transl']['@language'];    
             }
-            annotations.push(annotationObject);
         });
 
         var response = {
@@ -43,17 +69,17 @@
             }
         };
 
-        if(Array.isArray(payload.meta['dc:title'])) {
-            var original = payload.meta['dc:title'].find(function(title) { return title['@language'] !== lang; });
-            var translation = payload.meta['dc:title'].find(function(title) { return title['@language'] === lang; });
+        if(Array.isArray(payload['meta']['dc:title'])) {
+            var original = payload['meta']['dc:title'].find(function(title) { return title['@language'] !== translationISO; });
             if(original) {
                 response.data.attributes.title.original = original['@value'];
             }
+            var translation = payload['meta']['dc:title'].find(function(title) { return title['@language'] === translationISO; });
             if(translation) {
                 response.data.attributes.title.translation = translation['@value'];
             }            
         } else {
-            response.data.attributes.title.original = payload.meta['dc:title']['@value'];
+            response.data.attributes.title.original = payload['meta']['dc:title']['@value'];
         }