|
1 IriSP.Widgets.Tagger = function(player, config) { |
|
2 IriSP.Widgets.Widget.call(this, player, config); |
|
3 }; |
|
4 |
|
5 IriSP.Widgets.Tagger.prototype = new IriSP.Widgets.Widget(); |
|
6 |
|
7 IriSP.Widgets.Tagger.prototype.defaults = { |
|
8 created_annotation_type: "Contributions", |
|
9 creator_name: 'anonymous', |
|
10 api_endpoint: "/metadataplayer/test/post-test.php", |
|
11 api_method: "PUT", |
|
12 pause_on_write : true, |
|
13 api_serializer: "ldt_annotate", |
|
14 } |
|
15 |
|
16 IriSP.Widgets.Tagger.prototype.messages = { |
|
17 en: { |
|
18 add_a_tag: "Add a tag", |
|
19 submit: "Submit" |
|
20 }, |
|
21 fr: { |
|
22 add_a_tag: "Ajouter un tag", |
|
23 submit: "Envoyer" |
|
24 } |
|
25 } |
|
26 |
|
27 IriSP.Widgets.Tagger.prototype.template = |
|
28 '<form class="Ldt-Tagger"><input class="Ldt-Tagger-Input" placeholder="{{l10n.add_a_tag}}" />' |
|
29 + '<input class="Ldt-Tagger-Submit" type="submit" value="{{l10n.submit}}" /></form>'; |
|
30 |
|
31 IriSP.Widgets.Tagger.prototype.draw = function() { |
|
32 this.renderTemplate(); |
|
33 var _tags = this.source.getTags().getTitles(), |
|
34 _this = this, |
|
35 _input = this.$.find(".Ldt-Tagger-Input"); |
|
36 _input.autocomplete({ |
|
37 source: _tags |
|
38 }); |
|
39 if (this.pause_on_write) { |
|
40 _input.keyup(function() { |
|
41 _this.player.popcorn.pause(); |
|
42 }); |
|
43 } |
|
44 this.$.find(".Ldt-Tagger").submit(function() { |
|
45 var _tagvalue = _input.val(); |
|
46 if (_tagvalue) { |
|
47 |
|
48 /* Création d'une liste d'annotations contenant une annotation afin de l'envoyer au serveur */ |
|
49 var _exportedAnnotations = new IriSP.Model.List(_this.player.sourceManager), |
|
50 /* Création d'un objet source utilisant un sérialiseur spécifique pour l'export */ |
|
51 _export = _this.player.sourceManager.newLocalSource({serializer: IriSP.serializers[_this.api_serializer]}), |
|
52 /* Création d'une annotation dans cette source avec un ID généré à la volée (param. false) */ |
|
53 _annotation = new IriSP.Model.Annotation(false, _export), |
|
54 /* Récupération du type d'annotation dans lequel l'annotation doit être ajoutée */ |
|
55 _annotationTypes = _this.source.getAnnotationTypes().searchByTitle(_this.created_annotation_type), |
|
56 /* Si le Type d'Annotation n'existe pas, il est créé à la volée */ |
|
57 _annotationType = (_annotationTypes.length ? _annotationTypes[0] : new IriSP.Model.AnnotationType(false, _export)), |
|
58 /* L'objet Tag qui sera envoyé */ |
|
59 _tag = new IriSP.Model.Tag(false, _export); |
|
60 /* L'objet Tag doit avoir pour titre le texte du tag envoyé */ |
|
61 _tag.title = _tagvalue; |
|
62 /* Si nous avons dû générer un ID d'annotationType à la volée... */ |
|
63 if (!_annotationTypes.length) { |
|
64 /* Il ne faudra pas envoyer l'ID généré au serveur */ |
|
65 _annotationType.dont_send_id = true; |
|
66 /* Il faut inclure le titre dans le type d'annotation */ |
|
67 _annotationType.title = _this.created_annotation_type; |
|
68 } |
|
69 |
|
70 /* |
|
71 * Nous remplissons les données de l'annotation générée à la volée |
|
72 * ATTENTION: Si nous sommes sur un MASHUP, ces éléments doivent se référer AU MEDIA D'ORIGINE |
|
73 * */ |
|
74 var _now = 1000*_this.player.popcorn.currentTime(), |
|
75 _pilotAnnotation = null; |
|
76 if (_this.source.currentMedia.elementType == "mashup") { |
|
77 /* Si c'est un mashup, on récupère l'annotation d'origine pour caler le temps */ |
|
78 var _pilotAnnotation = _this.source.currentMedia.getAnnotationAtTime(_now).annotation; |
|
79 } else { |
|
80 /* Sinon, on recherche une annotation correspondant au temps */ |
|
81 var _annotations = _this.getWidgetAnnotationsAtTime(_now); |
|
82 if (_annotations.length) { |
|
83 _pilotAnnotation = _annotations[0]; |
|
84 } |
|
85 } |
|
86 if (_pilotAnnotation) { |
|
87 console.log(_pilotAnnotation); |
|
88 _annotation.setBegin(_pilotAnnotation.begin); |
|
89 _annotation.setEnd(_pilotAnnotation.end); |
|
90 /* Id du média annoté */ |
|
91 _annotation.setMedia(_pilotAnnotation.getMedia().id); |
|
92 } else { |
|
93 _annotation.setBegin(_now); |
|
94 _annotation.setEnd(_now); |
|
95 /* Id du média annoté */ |
|
96 _annotation.setMedia(_this.source.currentMedia.id); |
|
97 } |
|
98 |
|
99 /* Id du type d'annotation */ |
|
100 _annotation.setAnnotationType(_annotationType.id); |
|
101 |
|
102 _annotation.title = _tagvalue; |
|
103 _annotation.created = new Date(); /* Date de création de l'annotation */ |
|
104 _annotation.description = _tagvalue; |
|
105 |
|
106 _annotation.setTags([_tag.id]); /*Liste des ids de tags */ |
|
107 |
|
108 /* Les données créateur/date de création sont envoyées non pas dans l'annotation, mais dans le projet */ |
|
109 _export.creator = _this.creator_name; |
|
110 _export.created = new Date(); |
|
111 /* Ajout de l'annotation à la liste à exporter */ |
|
112 _exportedAnnotations.push(_annotation); |
|
113 /* Ajout de la liste à exporter à l'objet Source */ |
|
114 _export.addList("annotation",_exportedAnnotations); |
|
115 |
|
116 IriSP.jQuery.ajax({ |
|
117 url: _this.api_endpoint, |
|
118 type: _this.api_method, |
|
119 contentType: 'application/json', |
|
120 data: _export.serialize(), /* L'objet Source est sérialisé */ |
|
121 success: function(_data) { |
|
122 console.log("success"); |
|
123 /* Pour éviter les doublons, on supprime l'annotation qui a été envoyée */ |
|
124 _export.getAnnotations().removeElement(_annotation, true); |
|
125 /* On désérialise les données reçues pour les réinjecter */ |
|
126 _export.deSerialize(_data); |
|
127 /* On récupère les données réimportées dans l'espace global des données */ |
|
128 _this.source.merge(_export); |
|
129 if (_this.pause_on_write && _this.player.popcorn.media.paused) { |
|
130 _this.player.popcorn.play(); |
|
131 } |
|
132 /* On force le rafraîchissement du widget AnnotationsList */ |
|
133 _this.player.popcorn.trigger("IriSP.AnnotationsList.refresh"); |
|
134 }, |
|
135 error: function(_xhr, _error, _thrown) { |
|
136 console.log("Error when sending annotation", _thrown); |
|
137 _export.getAnnotations().removeElement(_annotation, true); |
|
138 } |
|
139 }); |
|
140 |
|
141 _input.val(""); |
|
142 } |
|
143 return false; |
|
144 }); |
|
145 } |