diff -r 1059a7ae018a -r 710a2ae08a74 cms/app-client/app/serializers/transcript.js --- a/cms/app-client/app/serializers/transcript.js Mon Nov 28 23:11:54 2016 +0100 +++ b/cms/app-client/app/serializers/transcript.js Fri Dec 02 00:22:31 2016 +0100 @@ -1,109 +1,103 @@ import JSONAPISerializer from 'ember-data/serializers/json-api'; +import _ from 'lodash/lodash'; export default JSONAPISerializer.extend({ normalizeResponse: function(store, primaryModelClass, payload, id) { - var speakers = payload['resources'].find(resource => resource['id'] === 'speakers'); - var topics = payload['resources'].find(resource => resource['id'] === 'topics'); + var speakerResources = payload['resources'].find(resource => resource['id'] === 'speakers') || {'content': { 'data': []}}; + var speakers = _.reduce( + speakerResources['content']['data'], + function(res, s) { res[s.id] = s; return res;}, + {}); + var topicsResources = payload['resources'].find(resource => resource['id'] === 'topics') || {'content': { 'data': []}}; + var topics = _.reduce( + topicsResources['content']['data'], + function(res, r) { res[r.id] = r; return res;}, + {}); + var translationISO = false; - var buildFragment = function(annotation) { - var fragment = { - 'original': annotation['content']['data']['content'] + var turns = _.reduce(payload['annotation-types'] || [], function(res, t) { + res[t['id']] = { + title: t['dc:title'], + begin: t['corpus:begin'], + end: t['corpus:end'], + annotations: [] + }; + return res; + },{}); + + var sections = _.map(payload['lists'] || [], function(list){ + var topic = topics[list['meta']['corpus:topic']['id-ref']]; + return { + title: (topic && topic['desc'])?topic['desc'] : null, + begin: list['meta']['corpus:begin'], + end: list['meta']['corpus:end'], + turns: _.reduce((list['items'] || []), function(res,item) { + if(item['id-ref'] && turns[item['id-ref']]) { + res.push(turns[item['id-ref']]); + } + return res; + }, []) + }; + }); + + var currentSpeaker = null; + var annotations = _.map(payload['annotations'] || [], function(annotation) { + var annot = { + original: annotation['content']['data']['content'], + begin: annotation['begin'], + end: annotation['end'] }; if(annotation['content']['data']['transl']) { - fragment['translation'] = annotation['content']['data']['transl']['@value']; + annot['translation'] = annotation['content']['data']['transl']['@value']; } if(annotation['content']['data']['words']) { - var words = []; - annotation['content']['data']['words'].forEach(function(word) { - words.push({ + var words = _.map(annotation['content']['data']['words'], function(word) { + return { 'original': word['content'], 'translation': word['transl']['@value'] - }); + }; }); - fragment['literal'] = words; + annot['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']); + var speaker = speakers[annotation['content']['data']['speaker']['id-ref']]; if(speaker) { - fragment['speaker'] = speaker['name']; + annot['speaker'] = speaker['name']; } } else { - fragment['speaker'] = annotation['content']['data']['speaker']; + annot['speaker'] = annotation['content']['data']['speaker']; } + annot['showSpeaker'] = (annot['speaker'] === currentSpeaker); + currentSpeaker = annot['speaker']; } - return fragment; - }; - - var annotations = []; - 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'], - }; - annotations.push(object); + if(annotation['type'] && turns[annotation['type']]) { + var type = turns[annotation['type']]; + annot['type'] = type; + type.annotations.push(annot); } if(!translationISO && annotation['content']['data']['transl']) { translationISO = annotation['content']['data']['transl']['@language']; } + + return annot; }); - if(payload['annotation-types'].length) { - var types = []; - payload['annotation-types'].forEach(function(t) { - var object = { - 'title': t['dc:title'], - 'begin': t['corpus:begin'], - 'end': t['corpus:end'] - }; - var fragments = annotations.find(function(annotation) { return annotation['begin'] === t['corpus:begin'] && annotation['end'] === t['corpus:end']; }); - if(fragments) { - object['fragments'] = fragments['fragments']; - } - types.push(object); - }); - annotations = types; - } - - var sections = []; - if(payload['lists'].length) { - var lists = []; - payload['lists'].forEach(function(list) { - var topic = topics['content']['data'].find(topic => topic.id === list['meta']['corpus:topic']['id-ref']); - sections.push({ - 'title': topic['desc'], - 'begin': list['meta']['corpus:begin'], - 'end': list['meta']['corpus:end'] - }); - lists.push(annotations.filter(annotation => annotation['begin'] >= list['meta']['corpus:begin'] && annotation['end'] <= list['meta']['corpus:end'])); - }); - annotations = lists; - } else { - annotations = [annotations]; - } - var response = { 'data': { - 'id': id, - 'type': 'transcript', - 'attributes': { - 'title': {}, - 'annotations': annotations + id: id, + type: 'transcript', + attributes: { + title: {}, + annotations: annotations, + sections: sections, + turns: turns } } }; - if(sections.length) { - response.data.attributes.sections = sections; - } - if(Array.isArray(payload['meta']['dc:title'])) { var original = payload['meta']['dc:title'].find(function(title) { return title['@language'] !== translationISO; }); if(original) {