src/widgets/CreateAnnotation.js
branchnew-model
changeset 928 5aadbc9f27cd
parent 925 28efc97b5d78
child 931 0fdce6e14748
equal deleted inserted replaced
927:977a39c4ee80 928:5aadbc9f27cd
   133             })
   133             })
   134             .slice(0, this.max_tags)
   134             .slice(0, this.max_tags)
   135             .map(function(_tag) {
   135             .map(function(_tag) {
   136                 return _tag;
   136                 return _tag;
   137             });
   137             });
   138         // We have to use the map function because Mustache doesn't like our tags object
   138         /* We have to use the map function because Mustache doesn't like our tags object */
   139     }
   139     }
   140     this.renderTemplate();
   140     this.renderTemplate();
   141     this.$.find(".Ldt-CreateAnnotation-Close").click(function() {
   141     this.$.find(".Ldt-CreateAnnotation-Close").click(function() {
   142         _this.close_after_send
   142         _this.close_after_send
   143         ? _this.hide()
   143         ? _this.hide()
   270     _field.css("border-color", !!_contents ? "#666666" : "#ff0000");
   270     _field.css("border-color", !!_contents ? "#666666" : "#ff0000");
   271     this.pauseOnWrite();
   271     this.pauseOnWrite();
   272     return !!_contents;
   272     return !!_contents;
   273 }
   273 }
   274 
   274 
       
   275 /* Fonction effectuant l'envoi des annotations */
   275 IriSP.Widgets.CreateAnnotation.prototype.onSubmit = function() {
   276 IriSP.Widgets.CreateAnnotation.prototype.onSubmit = function() {
       
   277     /* Si les champs obligatoires sont vides, on annule l'envoi */
   276     if (!this.onDescriptionChange() || (this.show_title_field && !this.onTitleChange()) || (this.show_creator_field && !this.onCreatorChange())) {
   278     if (!this.onDescriptionChange() || (this.show_title_field && !this.onTitleChange()) || (this.show_creator_field && !this.onCreatorChange())) {
   277         return;
   279         return;
   278     }
   280     }
   279     
   281     
   280     var _exportedAnnotations = new IriSP.Model.List(this.player.sourceManager);
   282     var _exportedAnnotations = new IriSP.Model.List(this.player.sourceManager), /* Création d'une liste d'annotations contenant une annotation afin de l'envoyer au serveur */
   281         _export = this.player.sourceManager.newLocalSource({serializer: IriSP.serializers[this.api_serializer]}),
   283         _export = this.player.sourceManager.newLocalSource({serializer: IriSP.serializers[this.api_serializer]}), /* Création d'un objet source utilisant un sérialiseur spécifique pour l'export */
   282         _annotation = new IriSP.Model.Annotation(false, _export),
   284         _annotation = new IriSP.Model.Annotation(false, _export), /* Création d'une annotation dans cette source avec un ID généré à la volée (param. false) */
   283         _annotationTypes = this.source.getAnnotationTypes().searchByTitle(this.annotation_type),
   285         _annotationTypes = this.source.getAnnotationTypes().searchByTitle(this.annotation_type), /* Récupération du type d'annotation dans lequel l'annotation doit être ajoutée */
   284         _annotationType = (_annotationTypes.length ? _annotationTypes[0] : new IriSP.Model.AnnotationType(false, _export)),
   286         _annotationType = (_annotationTypes.length ? _annotationTypes[0] : new IriSP.Model.AnnotationType(false, _export)), /* Si le Type d'Annotation n'existe pas, il est créé à la volée */
   285         _url = Mustache.to_html(this.api_endpoint_template, {id: this.source.projectId});
   287         _url = Mustache.to_html(this.api_endpoint_template, {id: this.source.projectId}); /* Génération de l'URL à laquelle l'annotation doit être envoyée, qui doit inclure l'ID du projet */
   286 
   288     
       
   289     /* Si nous avons dû générer un ID d'annotationType à la volée... */
   287     if (!_annotationTypes.length) {
   290     if (!_annotationTypes.length) {
       
   291         /* Il ne faudra pas envoyer l'ID généré au serveur */
   288         _annotationType.dont_send_id = true;
   292         _annotationType.dont_send_id = true;
   289     }
   293         /* Il faut inclure le titre dans le type d'annotation */
   290     
   294         _annotationType.title = this.annotation_type;
   291     _annotationType.title = this.annotation_type;
   295     }
   292     _annotation.setBegin(this.begin);
   296     
   293     _annotation.setEnd(this.end);
   297     /*
   294     _annotation.setMedia(this.source.currentMedia.id);
   298      * Nous remplissons les données de l'annotation générée à la volée
   295     _annotation.setAnnotationType(_annotationType.id);
   299      * ATTENTION: Si nous sommes sur un MASHUP, ces éléments doivent se référer AU MEDIA D'ORIGINE
       
   300      * */
       
   301     _annotation.setBegin(this.begin); /*Timecode de début */
       
   302     _annotation.setEnd(this.end); /* Timecode de fin */
       
   303     _annotation.setMedia(this.source.currentMedia.id); /* Id du média annoté */
       
   304    
       
   305     _annotation.setAnnotationType(_annotationType.id); /* Id du type d'annotation */
   296     if (this.show_title_field) {
   306     if (this.show_title_field) {
       
   307         /* Champ titre, seulement s'il est visible */
   297         _annotation.title = this.$.find(".Ldt-CreateAnnotation-Title").val();
   308         _annotation.title = this.$.find(".Ldt-CreateAnnotation-Title").val();
   298     }
   309     }
   299     _annotation.created = new Date();
   310     _annotation.created = new Date(); /* Date de création de l'annotation */
   300     _annotation.description = this.$.find(".Ldt-CreateAnnotation-Description").val();
   311     _annotation.description = this.$.find(".Ldt-CreateAnnotation-Description").val(); /* Champ description */
   301     _annotation.setTags(this.$.find(".Ldt-CreateAnnotation-TagLi.selected").map(function() { return IriSP.jQuery(this).attr("tag-id")}));
   312     _annotation.setTags(this.$.find(".Ldt-CreateAnnotation-TagLi.selected")
   302     
   313         .map(function() { return IriSP.jQuery(this).attr("tag-id")})); /*Liste des ids de tags */
       
   314     
       
   315     /* Les données créateur/date de création sont envoyées non pas dans l'annotation, mais dans le projet */
   303     if (this.show_creator_field) {
   316     if (this.show_creator_field) {
   304         _export.creator = this.$.find(".Ldt-CreateAnnotation-Creator").val();
   317         _export.creator = this.$.find(".Ldt-CreateAnnotation-Creator").val();
   305     } else {
   318     } else {
   306         _export.creator = this.creator_name;
   319         _export.creator = this.creator_name;
   307     }
   320     }
   308     _export.created = new Date();
   321     _export.created = new Date();
   309     _exportedAnnotations.push(_annotation);
   322     _exportedAnnotations.push(_annotation); /* Ajout de l'annotation à la liste à exporter */
   310     _export.addList("annotation",_exportedAnnotations);
   323     _export.addList("annotation",_exportedAnnotations); /* Ajout de la liste à exporter à l'objet Source */
   311     
   324     
   312     var _this = this;
   325     var _this = this;
       
   326     /* Envoi de l'annotation via AJAX au serveur ! */
   313     IriSP.jQuery.ajax({
   327     IriSP.jQuery.ajax({
   314         url: _url,
   328         url: _url,
   315         type: this.api_method,
   329         type: this.api_method,
   316         contentType: 'application/json',
   330         contentType: 'application/json',
   317         data: _export.serialize(),
   331         data: _export.serialize(), /* L'objet Source est sérialisé */
   318         success: function(_data) {
   332         success: function(_data) {
   319             _this.showScreen('Saved');
   333             _this.showScreen('Saved'); /* Si l'appel a fonctionné, on affiche l'écran "Annotation enregistrée" */
   320             if (_this.after_send_timeout) {
   334             if (_this.after_send_timeout) { /* Selon les options de configuration, on revient à l'écran principal ou on ferme le widget, ou rien */
   321                 window.setTimeout(
   335                 window.setTimeout(
   322                     function() {
   336                     function() {
   323                         _this.close_after_send
   337                         _this.close_after_send
   324                         ? _this.hide()
   338                         ? _this.hide()
   325                         : _this.showScreen("Main");
   339                         : _this.showScreen("Main");
   326                     },
   340                     },
   327                     _this.after_send_timeout
   341                     _this.after_send_timeout
   328                 );
   342                 );
   329             }
   343             }
   330             _export.getAnnotations().removeElement(_annotation, true);
   344             _export.getAnnotations().removeElement(_annotation, true); /* Pour éviter les doublons, on supprime l'annotation qui a été envoyée */
   331             _export.deSerialize(_data);
   345             _export.deSerialize(_data); /* On désérialise les données reçues pour les réinjecter */
   332             _this.source.merge(_export);
   346             _this.source.merge(_export); /* On récupère les données réimportées dans l'espace global des données */
   333             if (this.pause_on_write && this.player.popcorn.media.paused) {
   347             if (this.pause_on_write && this.player.popcorn.media.paused) {
   334                 this.player.popcorn.play();
   348                 this.player.popcorn.play();
   335             }
   349             }
   336             _this.player.popcorn.trigger("IriSP.AnnotationsList.refresh");
   350             _this.player.popcorn.trigger("IriSP.AnnotationsList.refresh"); /* On force le rafraîchissement du widget AnnotationsList */
   337         },
   351         },
   338         error: function(_xhr, _error, _thrown) {
   352         error: function(_xhr, _error, _thrown) {
   339             IriSP.log("Error when sending annotation", _thrown);
   353             IriSP.log("Error when sending annotation", _thrown);
   340             _export.getAnnotations().removeElement(_annotation, true);
   354             _export.getAnnotations().removeElement(_annotation, true);
   341             _this.showScreen('Error');
   355             _this.showScreen('Error');