cms/app-client/app/serializers/transcript.js
author Chloe Laisne <chloe.laisne@gmail.com>
Thu, 18 Aug 2016 17:02:02 +0200
changeset 256 18f0c3ee9aa5
parent 255 ed05b89e3299
child 257 eba9edbd8f46
permissions -rw-r--r--
Add title translation to transcript

import JSONAPISerializer from 'ember-data/serializers/json-api';

export default JSONAPISerializer.extend({

    normalizeResponse: function(store, primaryModelClass, payload, id, requestType) {
        var lang = false;

        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'];    
                }
            }
            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;
            }
            annotations.push(annotationObject);
        });

        var response = {
            'data': {
                'id': id,
                'type': 'transcript',
                'attributes': {
                    'title': {},
                    'annotations': annotations
                }
            }
        };

        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(original) {
                response.data.attributes.title.original = original['@value'];
            }
            if(translation) {
                response.data.attributes.title.translation = translation['@value'];
            }            
        } else {
            response.data.attributes.title.original = payload.meta['dc:title']['@value'];
        }


        return response;
    }

});