integration/js/ldt-serializer.js
changeset 154 60ca7678f074
parent 50 89e152523cb6
child 155 7e8492a5c70f
--- a/integration/js/ldt-serializer.js	Mon Aug 26 13:06:12 2013 +0200
+++ b/integration/js/ldt-serializer.js	Tue Oct 01 16:16:07 2013 +0200
@@ -1,7 +1,7 @@
 /* LDT Platform Serializer */
 
 if (typeof IriSP.serializers === "undefined") {
-    IriSP.serializers = {}
+    IriSP.serializers = {};
 }
 
 IriSP.serializers.ldt = {
@@ -44,7 +44,7 @@
                         "dc:contributor" : _data.contributor || _source.contributor || _data.creator || _source.creator,
                         "dc:duration" : _data.duration.milliseconds
                     }
-                }
+                };
                 _dest.medias.push(_res);
                 var _list = {
                     id: IriSP.Model.getUID(),
@@ -58,26 +58,43 @@
                         "id-ref": _data.id
                     },
                     items: _source.getAnnotationTypes().filter(function(_at) {
-                        return _at.media === _data;
+                        switch (typeof _at.media) {
+                            case "object":
+                                return (_at.media === _data);
+                            case "string":
+                                return (_at.media === _data.id);
+                            default:
+                                var _ann = _at.getAnnotations();
+                                if (_ann) {
+                                    for (var i = 0; i < _ann.length; i++) {
+                                        if (_ann[i].getMedia() === _data) {
+                                            return true;
+                                        }
+                                    }
+                                }
+                        }
+                        return false;
                     }).map(function(_at) {
                         return {
                             "id-ref": _at.id
-                        }
+                        };
                     })
-                }
+                };
                 _dest.lists.push(_list);
                 _dest.views[0].contents.push(_data.id);
             }
         },
         tag : {
             serialized_name : "tags",
-            model_name : "tag",
             deserializer : function(_data, _source) {
                 var _res = new IriSP.Model.Tag(_data.id, _source);
                 _res.title = _data.meta["dc:title"];
                 return _res;        
             },
             serializer : function(_data, _source, _dest) {
+                if (_source.regenerateTags && !_data.regenerated) {
+                    return;
+                }
                 var _res = {
                     id : _data.id,
                     meta : {
@@ -88,7 +105,7 @@
                         "dc:creator" : _data.creator || _source.creator,
                         "dc:contributor" : _data.contributor || _source.contributor || _data.creator || _source.creator,
                     }
-                }
+                };
                 _dest.tags.push(_res);
             }
         },
@@ -109,7 +126,7 @@
                     "dc:modified" : IriSP.Model.dateToIso(_data.modified || _source.modified),
                     "dc:creator" : _data.creator || _source.creator,
                     "dc:contributor" : _data.contributor || _source.contributor || _data.creator || _source.creator,
-                }
+                };
                 _dest["annotation-types"].push(_res);
                 _dest.views[0].annotation_types.push(_data.id);
             }
@@ -131,6 +148,7 @@
                     }
                     _res.color = '#' + _c;
                 }
+                _res.content = _data.content;
                 _res.setMedia(_data.media);
                 _res.setAnnotationType(_data.meta["id-ref"]);
                 _res.setTags(IriSP._(_data.tags).pluck("id-ref"));
@@ -153,14 +171,22 @@
                     id : _data.id,
                     begin : _data.begin.milliseconds,
                     end : _data.end.milliseconds,
-                    content : {
-                        title : _data.title || "",
-                        description : _data.description || "",
+                    content : IriSP._.defaults(
+                        {},
+                        {
+                            title : _data.title,
+                            description : _data.description,
                         audio : _data.audio,
                         img: {
                             src: _data.thumbnail
                         }
                     },
+                        _data.content,
+                        {
+                            title: "",
+                            description: ""
+                        }
+                    ),
                     color: _color,
                     media : _data.media.id,
                     meta : {
@@ -170,13 +196,22 @@
                         "dc:creator" : _data.creator || _source.creator,
                         "dc:contributor" : _data.contributor || _source.contributor || _data.creator || _source.creator,
 //                        project : _source.projectId
-                    },
-                    tags : IriSP._(_data.tag.id).map(function(_id) {
+                    }
+                };
+                if (_source.regenerateTags) {
+                    _res.tags = IriSP._(_data.keywords).map(function(_kw) {
+                        return {
+                            "id-ref": _source.__keywords[_kw.toLowerCase()].id
+                        };
+                    });
+                } else {
+                    _res.tags = IriSP._(_data.tag.id).map(function(_id) {
                        return {
                            "id-ref" : _id
-                       } 
-                    })
+                       };
+                    });
                 }
+                _res.content.title = _data.title || _res.content.title || "";
                 _dest.annotations.push(_res);
             }
         },
@@ -208,7 +243,7 @@
                         return _annotation.annotation.id;
                     }),
                     id: _data.id
-                }
+                };
                 _dest.lists.push(_res);
             }
         }
@@ -238,6 +273,24 @@
                 annotations: []
             },
             _this = this;
+        if (_source.regenerateTags) {
+            _source.__keywords = {};
+            _source.getAnnotations().forEach(function(a) {
+                IriSP._(a.keywords).each(function(kw) {
+                    var lkw = kw.toLowerCase();
+                    if (typeof _source.__keywords[lkw] === "undefined") {
+                        _source.__keywords[lkw] = {
+                            id: IriSP.Model.getUID(),
+                            title: kw,
+                            regenerated: true
+                        };
+                    }
+                });
+            });
+            IriSP._(_source.__keywords).each(function(kw) {
+                _this.types.tag.serializer(kw, _source, _res);
+            });
+        }
         _source.forEach(function(_list, _typename) {
             if (typeof _this.types[_typename] !== "undefined") {
                 _list.forEach(function(_el) {
@@ -275,13 +328,17 @@
         
         if (typeof _data.meta !== "undefined") {
             _source.projectId = _data.meta.id;
+            _source.title = _data.meta["dc:title"] || _data.meta.title || "";
+            _source.description = _data.meta["dc:description"] || _data.meta.description || "";
+            _source.creator = _data.meta["dc:creator"] || _data.meta.creator || "";
+            _source.contributor = _data.meta["dc:contributor"] || _data.meta.contributor || _source.creator;
+            _source.created = IriSP.Model.isoToDate(_data.meta["dc:created"] || _data.meta.created);
         }
         
         if (typeof _data.meta !== "undefined" && typeof _data.meta.main_media !== "undefined" && typeof _data.meta.main_media["id-ref"] !== "undefined") {
             _source.currentMedia = _source.getElement(_data.meta.main_media["id-ref"]);
         }
     }
-}
+};
 
-/* END ldt-serializer.js */
-
+/* End of LDT Platform Serializer */
\ No newline at end of file