|
931
|
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', |
|
932
|
10 |
api_endpoint: "", |
|
931
|
11 |
api_method: "PUT", |
|
|
12 |
pause_on_write : true, |
|
|
13 |
api_serializer: "ldt_annotate", |
|
932
|
14 |
tags: false |
|
931
|
15 |
} |
|
|
16 |
|
|
|
17 |
IriSP.Widgets.Tagger.prototype.messages = { |
|
|
18 |
en: { |
|
|
19 |
add_a_tag: "Add a tag", |
|
|
20 |
submit: "Submit" |
|
|
21 |
}, |
|
|
22 |
fr: { |
|
|
23 |
add_a_tag: "Ajouter un tag", |
|
|
24 |
submit: "Envoyer" |
|
|
25 |
} |
|
|
26 |
} |
|
|
27 |
|
|
|
28 |
IriSP.Widgets.Tagger.prototype.template = |
|
|
29 |
'<form class="Ldt-Tagger"><input class="Ldt-Tagger-Input" placeholder="{{l10n.add_a_tag}}" />' |
|
|
30 |
+ '<input class="Ldt-Tagger-Submit" type="submit" value="{{l10n.submit}}" /></form>'; |
|
|
31 |
|
|
|
32 |
IriSP.Widgets.Tagger.prototype.draw = function() { |
|
|
33 |
this.renderTemplate(); |
|
932
|
34 |
var _tags = this.tags || this.source.getTags().getTitles(), |
|
931
|
35 |
_this = this, |
|
|
36 |
_input = this.$.find(".Ldt-Tagger-Input"); |
|
|
37 |
_input.autocomplete({ |
|
|
38 |
source: _tags |
|
|
39 |
}); |
|
|
40 |
if (this.pause_on_write) { |
|
|
41 |
_input.keyup(function() { |
|
|
42 |
_this.player.popcorn.pause(); |
|
|
43 |
}); |
|
|
44 |
} |
|
|
45 |
this.$.find(".Ldt-Tagger").submit(function() { |
|
|
46 |
var _tagvalue = _input.val(); |
|
|
47 |
if (_tagvalue) { |
|
|
48 |
|
|
|
49 |
/* Création d'une liste d'annotations contenant une annotation afin de l'envoyer au serveur */ |
|
|
50 |
var _exportedAnnotations = new IriSP.Model.List(_this.player.sourceManager), |
|
|
51 |
/* Création d'un objet source utilisant un sérialiseur spécifique pour l'export */ |
|
|
52 |
_export = _this.player.sourceManager.newLocalSource({serializer: IriSP.serializers[_this.api_serializer]}), |
|
|
53 |
/* Création d'une annotation dans cette source avec un ID généré à la volée (param. false) */ |
|
|
54 |
_annotation = new IriSP.Model.Annotation(false, _export), |
|
|
55 |
/* Récupération du type d'annotation dans lequel l'annotation doit être ajoutée */ |
|
|
56 |
_annotationTypes = _this.source.getAnnotationTypes().searchByTitle(_this.created_annotation_type), |
|
|
57 |
/* Si le Type d'Annotation n'existe pas, il est créé à la volée */ |
|
|
58 |
_annotationType = (_annotationTypes.length ? _annotationTypes[0] : new IriSP.Model.AnnotationType(false, _export)), |
|
|
59 |
/* L'objet Tag qui sera envoyé */ |
|
|
60 |
_tag = new IriSP.Model.Tag(false, _export); |
|
|
61 |
/* L'objet Tag doit avoir pour titre le texte du tag envoyé */ |
|
|
62 |
_tag.title = _tagvalue; |
|
|
63 |
/* Si nous avons dû générer un ID d'annotationType à la volée... */ |
|
|
64 |
if (!_annotationTypes.length) { |
|
|
65 |
/* Il ne faudra pas envoyer l'ID généré au serveur */ |
|
|
66 |
_annotationType.dont_send_id = true; |
|
|
67 |
/* Il faut inclure le titre dans le type d'annotation */ |
|
|
68 |
_annotationType.title = _this.created_annotation_type; |
|
|
69 |
} |
|
|
70 |
|
|
|
71 |
/* |
|
|
72 |
* Nous remplissons les données de l'annotation générée à la volée |
|
|
73 |
* ATTENTION: Si nous sommes sur un MASHUP, ces éléments doivent se référer AU MEDIA D'ORIGINE |
|
|
74 |
* */ |
|
|
75 |
var _now = 1000*_this.player.popcorn.currentTime(), |
|
|
76 |
_pilotAnnotation = null; |
|
|
77 |
if (_this.source.currentMedia.elementType == "mashup") { |
|
|
78 |
/* Si c'est un mashup, on récupère l'annotation d'origine pour caler le temps */ |
|
|
79 |
var _pilotAnnotation = _this.source.currentMedia.getAnnotationAtTime(_now).annotation; |
|
|
80 |
} else { |
|
|
81 |
/* Sinon, on recherche une annotation correspondant au temps */ |
|
|
82 |
var _annotations = _this.getWidgetAnnotationsAtTime(_now); |
|
|
83 |
if (_annotations.length) { |
|
|
84 |
_pilotAnnotation = _annotations[0]; |
|
|
85 |
} |
|
|
86 |
} |
|
|
87 |
if (_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 |
} |