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