src/js/serializers/ldt_localstorage.js
author ymh <ymh.work@gmail.com>
Fri, 02 Oct 2015 11:27:17 +0200
changeset 1068 7623f9af9272
parent 1033 c20df1c080e6
child 1072 ac1eacb3aa33
permissions -rw-r--r--
merge pull request #3 from O. Auber
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1033
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
/* ldt_localstorage serializer: Used to store personal annotations in local storage */
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
if (typeof IriSP.serializers === "undefined") {
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
    IriSP.serializers = {};
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
}
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
IriSP.serializers.ldt_localstorage = {
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
    serializeAnnotation : function(_data, _source) {
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
        var _annType = _data.getAnnotationType();
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
        return {
1068
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1033
diff changeset
    11
            id: _data.id,
1033
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
            begin: _data.begin.milliseconds,
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
            end: _data.end.milliseconds,
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
            content: {
1068
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1033
diff changeset
    15
                data: (_data.content ? _data.content.data || {} : {}),
1033
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
                description: _data.description,
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
                title: _data.title,
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
                audio: _data.audio
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
            },
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
            tags: _data.getTagTexts(),
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
            media: _data.getMedia().id,
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
            type_title: _annType.title,
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
            type: ( typeof _annType.dont_send_id !== "undefined" && _annType.dont_send_id ? "" : _annType.id ),
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
            meta: {
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
                created: _data.created,
1068
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1033
diff changeset
    26
                creator: _data.creator,
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1033
diff changeset
    27
                modified: _data.modified,
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1033
diff changeset
    28
                contributor: _data.contributor
1033
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
            }
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
        };
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
    },
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
    deserializeAnnotation : function(_anndata, _source) {
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
        var _ann = new IriSP.Model.Annotation(_anndata.id, _source);
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
        _ann.description = _anndata.content.description || "";
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
        _ann.title = _anndata.content.title || "";
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
        _ann.creator = _anndata.meta.creator || "";
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
        _ann.created = new Date(_anndata.meta.created);
1068
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1033
diff changeset
    38
        _ann.contributor = _anndata.meta.contributor || "";
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1033
diff changeset
    39
        _ann.modified = new Date(_anndata.meta.modified);
1033
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
        _ann.setMedia(_anndata.media, _source);
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
        var _anntype = _source.getElement(_anndata.type);
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
        if (!_anntype) {
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
            _anntype = new IriSP.Model.AnnotationType(_anndata.type, _source);
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
            _anntype.title = _anndata.type_title;
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
            _source.getAnnotationTypes().push(_anntype);
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
        }
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
        _ann.setAnnotationType(_anntype.id);
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
        var _tagIds = IriSP._(_anndata.tags).map(function(_title) {
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
            var _tags = _source.getTags(true).searchByTitle(_title, true);
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
            if (_tags.length) {
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
                var _tag = _tags[0];
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
            }
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
            else {
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
                _tag = new IriSP.Model.Tag(_title.replace(/\W/g,'_'),_source);
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
                _tag.title = _title;
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
                _source.getTags().push(_tag);
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
            }
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
            return _tag.id;
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
        });
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
        _ann.setTags(_tagIds);
1068
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1033
diff changeset
    61
        _ann.setBeginEnd(_anndata.begin, _anndata.end);
1033
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
        if (typeof _anndata.content.audio !== "undefined" && _anndata.content.audio.href) {
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
            _ann.audio = _anndata.content.audio;
1068
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1033
diff changeset
    64
        };
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1033
diff changeset
    65
        if (_anndata.content.data) {
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1033
diff changeset
    66
            _ann.content = { data: _anndata.content.data };
7623f9af9272 merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents: 1033
diff changeset
    67
        };
1033
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
        _source.getAnnotations().push(_ann);
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
    },
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
    serialize : function(_source) {
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
        var _this = this;
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
        return JSON.stringify(_source.getAnnotations().map(function (a) { return _this.serializeAnnotation(a, _source); }));
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
    },
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
    deSerialize : function(_data, _source) {
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
        var _this = this;
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
        if (typeof _data == "string") {
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
            _data = JSON.parse(_data);
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
        }
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
        _source.addList('tag', new IriSP.Model.List(_source.directory));
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
        _source.addList('annotationType', new IriSP.Model.List(_source.directory));
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
        _source.addList('annotation', new IriSP.Model.List(_source.directory));
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
        _data.map( function (a) { _this.deserializeAnnotation(a, _source); });
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
    }
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
};
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
/* End ldt_localstorage serializer */