src/js/serializers/ldt_localstorage.js
changeset 1033 c20df1c080e6
child 1068 7623f9af9272
equal deleted inserted replaced
1032:74ac0be7655c 1033:c20df1c080e6
       
     1 /* ldt_localstorage serializer: Used to store personal annotations in local storage */
       
     2 
       
     3 if (typeof IriSP.serializers === "undefined") {
       
     4     IriSP.serializers = {};
       
     5 }
       
     6 
       
     7 IriSP.serializers.ldt_localstorage = {
       
     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         var _this = this;
       
    64         return JSON.stringify(_source.getAnnotations().map(function (a) { return _this.serializeAnnotation(a, _source); }));
       
    65     },
       
    66     deSerialize : function(_data, _source) {
       
    67         var _this = this;
       
    68         if (typeof _data == "string") {
       
    69             _data = JSON.parse(_data);
       
    70         }
       
    71 
       
    72         _source.addList('tag', new IriSP.Model.List(_source.directory));
       
    73         _source.addList('annotationType', new IriSP.Model.List(_source.directory));
       
    74         _source.addList('annotation', new IriSP.Model.List(_source.directory));
       
    75         _data.map( function (a) { _this.deserializeAnnotation(a, _source); });
       
    76     }
       
    77 };
       
    78 
       
    79 /* End ldt_localstorage serializer */