src/js/serializers/ldt_annotate.js
author veltr
Fri, 26 Oct 2012 15:51:05 +0200
branchplatform-restapi
changeset 974 560afb14296d
parent 924 64c2eaafe5e2
child 975 35aadec4131b
permissions -rw-r--r--
Annotations now fit the new rest api
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
904
510ebab76fa3 Work on CreateAnnotation Widget
veltr
parents:
diff changeset
     1
/* Used when Putting annotations on the platform */
510ebab76fa3 Work on CreateAnnotation Widget
veltr
parents:
diff changeset
     2
510ebab76fa3 Work on CreateAnnotation Widget
veltr
parents:
diff changeset
     3
if (typeof IriSP.serializers === "undefined") {
510ebab76fa3 Work on CreateAnnotation Widget
veltr
parents:
diff changeset
     4
    IriSP.serializers = {}
510ebab76fa3 Work on CreateAnnotation Widget
veltr
parents:
diff changeset
     5
}
510ebab76fa3 Work on CreateAnnotation Widget
veltr
parents:
diff changeset
     6
510ebab76fa3 Work on CreateAnnotation Widget
veltr
parents:
diff changeset
     7
IriSP.serializers.ldt_annotate = {
974
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
     8
    serializeAnnotation : function(_data, _source) {
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
     9
        var _annType = _data.getAnnotationType();
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    10
        return {
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    11
            begin: _data.begin.milliseconds,
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    12
            end: _data.end.milliseconds,
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    13
            content: {
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    14
                data: _data.description,
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    15
                audio: _data.audio
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    16
            },
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    17
            tags: _data.getTagTexts(),
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    18
            media: _data.getMedia().id,
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    19
            type_title: _annType.title,
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    20
            type: ( typeof _annType.dont_send_id !== "undefined" && _annType.dont_send_id ? "" : _annType.id ),
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    21
            meta: {
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    22
                created: _data.created,
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    23
                creator: _data.creator
904
510ebab76fa3 Work on CreateAnnotation Widget
veltr
parents:
diff changeset
    24
            }
510ebab76fa3 Work on CreateAnnotation Widget
veltr
parents:
diff changeset
    25
        }
510ebab76fa3 Work on CreateAnnotation Widget
veltr
parents:
diff changeset
    26
    },
510ebab76fa3 Work on CreateAnnotation Widget
veltr
parents:
diff changeset
    27
    serialize : function(_source) {
974
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    28
        var _this = this
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    29
            _res = {
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    30
                "objects": _source.getAnnotations().map(function(_annotation) {
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    31
                    return _this.serializeAnnotation(_annotation, _source);
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    32
                })
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    33
            };
904
510ebab76fa3 Work on CreateAnnotation Widget
veltr
parents:
diff changeset
    34
        return JSON.stringify(_res);
908
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    35
    },
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    36
    deSerialize : function(_data, _source) {
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    37
        if (typeof _data == "string") {
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    38
            _data = JSON.parse(_data);
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    39
        }
974
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    40
        
908
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    41
        _source.addList('tag', new IriSP.Model.List(_source.directory));
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    42
        _source.addList('annotationType', new IriSP.Model.List(_source.directory));
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    43
        _source.addList('annotation', new IriSP.Model.List(_source.directory));
974
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    44
        IriSP._(_data.objects).each(function(_anndata) {
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    45
            var _ann = new IriSP.Model.Annotation(_anndata.id, _source);
908
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    46
            _ann.description = _anndata.content.data || "";
974
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    47
            _ann.title = "";
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    48
            _ann.creator = _anndata.meta.creator || "";
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    49
            _ann.created = new Date(_anndata.meta.created);
908
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    50
            _ann.setMedia(_anndata.media, _source);
974
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    51
            var _anntype = _source.getElement(_anndata.type);
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    52
            if (!_anntype) {
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    53
                _anntype = new IriSP.Model.AnnotationType(_anndata.type, _source);
908
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    54
                _anntype.title = _anndata.type_title;
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    55
                _source.getAnnotationTypes().push(_anntype);
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    56
            }
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    57
            _ann.setAnnotationType(_anntype.id);
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    58
            var _tagIds = IriSP._(_anndata.tags).map(function(_title) {
974
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    59
                var _tags = _source.getTags(true).searchByTitle(_title, true);
908
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    60
                if (_tags.length) {
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    61
                    var _tag = _tags[0];
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    62
                }
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    63
                else {
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    64
                    _tag = new IriSP.Model.Tag(_title.replace(/\W/g,'_'),_source);
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    65
                    _tag.title = _title;
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    66
                    _source.getTags().push(_tag);
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    67
                }
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    68
                return _tag.id;
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    69
            });
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    70
            _ann.setTags(_tagIds);
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    71
            _ann.setBegin(_anndata.begin);
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    72
            _ann.setEnd(_anndata.end);
915
ba7aab923d08 corrections
veltr
parents: 910
diff changeset
    73
            if (typeof _anndata.content.audio !== "undefined" && _anndata.content.audio.href) {
ba7aab923d08 corrections
veltr
parents: 910
diff changeset
    74
                _ann.audio = _anndata.content.audio;
ba7aab923d08 corrections
veltr
parents: 910
diff changeset
    75
            }
908
f56199193fad CreateAnnotation widget now posts annotations, Tagcloud can be made segment-dependent
veltr
parents: 904
diff changeset
    76
            _source.getAnnotations().push(_ann);
974
560afb14296d Annotations now fit the new rest api
veltr
parents: 924
diff changeset
    77
        });
904
510ebab76fa3 Work on CreateAnnotation Widget
veltr
parents:
diff changeset
    78
    }
510ebab76fa3 Work on CreateAnnotation Widget
veltr
parents:
diff changeset
    79
}