integration/js/ldt-annotate.js
changeset 50 9cc1b66d0880
equal deleted inserted replaced
49:cb8403125d4d 50:9cc1b66d0880
       
     1 /* Used when Putting annotations on the platform */
       
     2 
       
     3 if (typeof IriSP.serializers === "undefined") {
       
     4     IriSP.serializers = {}
       
     5 }
       
     6 
       
     7 IriSP.serializers.ldt_annotate = {
       
     8     serializeAnnotation : function(_data, _source) {
       
     9         var _annType = _data.getAnnotationType();
       
    10         return {
       
    11             begin: _data.begin.milliseconds,
       
    12             end: _data.end.milliseconds,
       
    13             content: {
       
    14                 description: _data.description,
       
    15                 title: _data.title,
       
    16                 audio: _data.audio
       
    17             },
       
    18             tags: _data.getTagTexts(),
       
    19             media: _data.getMedia().id,
       
    20             type_title: _annType.title,
       
    21             type: ( typeof _annType.dont_send_id !== "undefined" && _annType.dont_send_id ? "" : _annType.id ),
       
    22             meta: {
       
    23                 created: _data.created,
       
    24                 creator: _data.creator
       
    25             }
       
    26         }
       
    27     },
       
    28     deserializeAnnotation : function(_anndata, _source) {
       
    29         var _ann = new IriSP.Model.Annotation(_anndata.id, _source);
       
    30         _ann.description = _anndata.content.description || "";
       
    31         _ann.title = _anndata.content.title || "";
       
    32         _ann.creator = _anndata.meta.creator || "";
       
    33         _ann.created = new Date(_anndata.meta.created);
       
    34         _ann.setMedia(_anndata.media, _source);
       
    35         var _anntype = _source.getElement(_anndata.type);
       
    36         if (!_anntype) {
       
    37             _anntype = new IriSP.Model.AnnotationType(_anndata.type, _source);
       
    38             _anntype.title = _anndata.type_title;
       
    39             _source.getAnnotationTypes().push(_anntype);
       
    40         }
       
    41         _ann.setAnnotationType(_anntype.id);
       
    42         var _tagIds = IriSP._(_anndata.tags).map(function(_title) {
       
    43             var _tags = _source.getTags(true).searchByTitle(_title, true);
       
    44             if (_tags.length) {
       
    45                 var _tag = _tags[0];
       
    46             }
       
    47             else {
       
    48                 _tag = new IriSP.Model.Tag(_title.replace(/\W/g,'_'),_source);
       
    49                 _tag.title = _title;
       
    50                 _source.getTags().push(_tag);
       
    51             }
       
    52             return _tag.id;
       
    53         });
       
    54         _ann.setTags(_tagIds);
       
    55         _ann.setBegin(_anndata.begin);
       
    56         _ann.setEnd(_anndata.end);
       
    57         if (typeof _anndata.content.audio !== "undefined" && _anndata.content.audio.href) {
       
    58             _ann.audio = _anndata.content.audio;
       
    59         }
       
    60         _source.getAnnotations().push(_ann);
       
    61     },
       
    62     serialize : function(_source) {
       
    63         return JSON.stringify(this.serializeAnnotation(_source.getAnnotations()[0], _source));
       
    64     },
       
    65     deSerialize : function(_data, _source) {
       
    66         if (typeof _data == "string") {
       
    67             _data = JSON.parse(_data);
       
    68         }
       
    69         
       
    70         _source.addList('tag', new IriSP.Model.List(_source.directory));
       
    71         _source.addList('annotationType', new IriSP.Model.List(_source.directory));
       
    72         _source.addList('annotation', new IriSP.Model.List(_source.directory));
       
    73         this.deserializeAnnotation(_data, _source);
       
    74     }
       
    75 }