src/widgets/CreateAnnotation.js
changeset 1067 539c9bee5372
parent 1066 5ef7182907c5
child 1068 7623f9af9272
equal deleted inserted replaced
1066:5ef7182907c5 1067:539c9bee5372
   394     }
   394     }
   395     this.pauseOnWrite();
   395     this.pauseOnWrite();
   396     return !!_contents;
   396     return !!_contents;
   397 };
   397 };
   398 
   398 
   399 /* Fonction effectuant l'envoi des annotations */
       
   400 IriSP.Widgets.CreateAnnotation.prototype.onSubmit = function() {
   399 IriSP.Widgets.CreateAnnotation.prototype.onSubmit = function() {
   401     /* Si les champs obligatoires sont vides, on annule l'envoi */
   400     /* If mandatory fields are empty, we cancel the sending */
   402     if (!this.onDescriptionChange() || (this.show_title_field && !this.onTitleChange()) || (this.show_creator_field && !this.onCreatorChange())) {
   401     if (!this.onDescriptionChange() || (this.show_title_field && !this.onTitleChange()) || (this.show_creator_field && !this.onCreatorChange())) {
   403         return false;
   402         return false;
   404     }
   403     }
   405     
   404     
   406     if (this.recorder) {
   405     if (this.recorder) {
   407         this.recorder.stopRecord();
   406         this.recorder.stopRecord();
   408     }
   407     }
   409     
   408     
   410     var _this = this,
   409     var _this = this,
   411         _exportedAnnotations = new IriSP.Model.List(this.player.sourceManager), /* Création d'une liste d'annotations contenant une annotation afin de l'envoyer au serveur */
   410         _exportedAnnotations = new IriSP.Model.List(this.player.sourceManager), /* We create a List to send to the server that will contains the annotation */
   412         _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 */
   411         _export = this.player.sourceManager.newLocalSource({serializer: IriSP.serializers[this.api_serializer]}), /* We create a source object using a specific serializer for export */
   413         _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) */
   412         _annotation = new IriSP.Model.Annotation(false, _export), /* We create an annotation in the source with a generated ID (param. false) */
   414         _annotationTypes = this.source.getAnnotationTypes().searchByTitle(this.annotation_type, true), /* Récupération du type d'annotation dans lequel l'annotation doit être ajoutée */
   413         _annotationTypes = this.source.getAnnotationTypes().searchByTitle(this.annotation_type, true), /* We get the AnnotationType in which the annotation will be added */
   415         _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 */
   414         _annotationType = (_annotationTypes.length ? _annotationTypes[0] : new IriSP.Model.AnnotationType(false, _export)), /* If it doesn't already exists, we create it */
   416         _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 */
   415         _url = Mustache.to_html(this.api_endpoint_template, {id: this.source.projectId}); /* We make the url to send the request to, must include project id */
   417     
   416     
   418     /* Si nous avons dû générer un ID d'annotationType à la volée... */
   417     /* If we created an AnnotationType on the spot ... */
   419     if (!_annotationTypes.length) {
   418     if (!_annotationTypes.length) {
   420         /* Il ne faudra pas envoyer l'ID généré au serveur */
   419         /* ... We must not send its id to the server ... */
   421         _annotationType.dont_send_id = true;
   420         _annotationType.dont_send_id = true;
   422         /* Il faut inclure le titre dans le type d'annotation */
   421         /* ... And we must include its title. */
   423         _annotationType.title = this.annotation_type;
   422         _annotationType.title = this.annotation_type;
   424     }
   423     }
   425     
   424     
   426     /*
   425     /*
   427      * Nous remplissons les données de l'annotation générée à la volée
   426      * Will fill the generated annotation object's data
   428      * ATTENTION: Si nous sommes sur un MASHUP, ces éléments doivent se référer AU MEDIA D'ORIGINE
   427      * WARNING: If we're on a MASHUP, these datas must refer the ORIGINAL MEDIA
   429      * */
   428      * */
   430     _annotation.setMedia(this.source.currentMedia.id); /* Id du média annoté */
   429     _annotation.setMedia(this.source.currentMedia.id); /* Annotated media ID */
   431     
   430     
   432     if (this.post_at_segment_time){
   431     if (this.post_at_segment_time){
   433         var _currentTime = this.media.getCurrentTime() 
   432         var _currentTime = this.media.getCurrentTime() 
   434         var _segmentsAnnotations = this.source.getAnnotationsByTypeTitle(this.segments_annotation_type)
   433         var _segmentsAnnotations = this.source.getAnnotationsByTypeTitle(this.segments_annotation_type)
   435         var _currentSegments = _segmentsAnnotations.filter(function(_segment){
   434         var _currentSegments = _segmentsAnnotations.filter(function(_segment){
   436             return (_currentTime >= _segment.begin && _currentTime <= _segment.end)
   435             return (_currentTime >= _segment.begin && _currentTime <= _segment.end)
   437         });
   436         });
   438         if (_currentSegments.length == 0){
   437         if (_currentSegments.length == 0){
   439             _annotation.setBegin(this.begin); /* Timecode de début du widget */
   438             _annotation.setBegin(this.begin); /* Widget starting timecode */
   440             _annotation.setEnd(this.end); /* Timecode de fin du widget */
   439             _annotation.setEnd(this.end); /* Widget end timecode */
   441         }
   440         }
   442         else {
   441         else {
   443             _annotation.setBegin(_currentSegments[0].begin); /* Timecode de début du segment */
   442             _annotation.setBegin(_currentSegments[0].begin); /* Segment starting timecode */
   444             _annotation.setEnd(_currentSegments[0].end); /* Timecode de fin du segment */
   443             _annotation.setEnd(_currentSegments[0].end); /* Segment end timecode */
   445         }
   444         }
   446     }
   445     }
   447     else {
   446     else {
   448         _annotation.setBegin(this.begin); /*Timecode de début du widget */
   447         _annotation.setBegin(this.begin); /* Widget starting timecode */
   449         _annotation.setEnd(this.end); /* Timecode de fin du widget */
   448         _annotation.setEnd(this.end); /* Widget end timecode */
   450     }
   449     }
   451     _annotation.setAnnotationType(_annotationType.id); /* Id du type d'annotation */
   450     _annotation.setAnnotationType(_annotationType.id); /* Annotation type ID */
   452     if (this.show_title_field) {
   451     if (this.show_title_field) {
   453         /* Champ titre, seulement s'il est visible */
   452         /* Title field, only if it's visible */
   454         _annotation.title = this.$.find(".Ldt-CreateAnnotation-Title").val();
   453         _annotation.title = this.$.find(".Ldt-CreateAnnotation-Title").val();
   455     }if (this.project_id != ""){
   454     }if (this.project_id != ""){
   456     	/* Champ id projet, seulement si on l'a renseigné dans la config */
   455     	/* Project id, only if it's been specifiec in the config */
   457     	_annotation.project_id = this.project_id;
   456     	_annotation.project_id = this.project_id;
   458     }
   457     }
   459     _annotation.created = new Date(); /* Date de création de l'annotation */
   458     _annotation.created = new Date(); /* Annotation creation date */
   460     _annotation.description = this.$.find(".Ldt-CreateAnnotation-Description").val(); /* Champ description */
   459     _annotation.description = this.$.find(".Ldt-CreateAnnotation-Description").val(); /* Description field */
   461    
   460    
   462     var tagIds = Array.prototype.map.call(
   461     var tagIds = Array.prototype.map.call(
   463         this.$.find(".Ldt-CreateAnnotation-TagLi.selected"),
   462         this.$.find(".Ldt-CreateAnnotation-TagLi.selected"),
   464         function(el) { return IriSP.jQuery(el).attr("tag-id")}
   463         function(el) { return IriSP.jQuery(el).attr("tag-id")}
   465     );
   464     );
   479             tagIds.push(_tag.id);
   478             tagIds.push(_tag.id);
   480         }
   479         }
   481         
   480         
   482     })
   481     })
   483    
   482    
   484     _annotation.setTags(IriSP._(tagIds).uniq()); /*Liste des ids de tags */
   483     _annotation.setTags(IriSP._(tagIds).uniq()); /* Tag ids list */
   485     if (this.audio_url) {
   484     if (this.audio_url) {
   486         _annotation.audio = {
   485         _annotation.audio = {
   487             src: "mic",
   486             src: "mic",
   488             mimetype: "audio/mp3",
   487             mimetype: "audio/mp3",
   489             href: this.audio_url
   488             href: this.audio_url
   492     if (this.show_creator_field) {
   491     if (this.show_creator_field) {
   493         _annotation.creator = this.$.find(".Ldt-CreateAnnotation-Creator").val();
   492         _annotation.creator = this.$.find(".Ldt-CreateAnnotation-Creator").val();
   494     } else {
   493     } else {
   495         _annotation.creator = this.creator_name;
   494         _annotation.creator = this.creator_name;
   496     }
   495     }
   497     _exportedAnnotations.push(_annotation); /* Ajout de l'annotation à la liste à exporter */
   496     _exportedAnnotations.push(_annotation); /* We add the annotation in the list to export */
   498     _export.addList("annotation",_exportedAnnotations); /* Ajout de la liste à exporter à l'objet Source */
   497     _export.addList("annotation",_exportedAnnotations); /* We add the list to the source object */ 
   499     var _this = this;
   498     var _this = this;
   500     /* Envoi de l'annotation via AJAX au serveur ! */
   499     /* We send the AJAX request to the server ! */
   501     IriSP.jQuery.ajax({
   500     IriSP.jQuery.ajax({
   502         url: _url,
   501         url: _url,
   503         type: this.api_method,
   502         type: this.api_method,
   504         contentType: 'application/json',
   503         contentType: 'application/json',
   505         data: _export.serialize(), /* L'objet Source est sérialisé */
   504         data: _export.serialize(), /* Source is serialized */
   506         success: function(_data) {
   505         success: function(_data) {
   507             _this.showScreen('Saved'); /* Si l'appel a fonctionné, on affiche l'écran "Annotation enregistrée" */
   506             _this.showScreen('Saved'); 
   508             if (_this.after_send_timeout) { /* Selon les options de configuration, on revient à l'écran principal ou on ferme le widget, ou rien */
   507             if (_this.after_send_timeout) { 
   509                 window.setTimeout(
   508                 window.setTimeout(
   510                     function() {
   509                     function() {
   511                         _this.close_after_send
   510                         _this.close_after_send
   512                         ? _this.player.trigger("CreateAnnotation.hide")
   511                         ? _this.player.trigger("CreateAnnotation.hide")
   513                         : _this.player.trigger("CreateAnnotation.show");
   512                         : _this.player.trigger("CreateAnnotation.show");
   514                     },
   513                     },
   515                     _this.after_send_timeout
   514                     _this.after_send_timeout
   516                 );
   515                 );
   517             }
   516             }
   518             _export.getAnnotations().removeElement(_annotation, true); /* Pour éviter les doublons, on supprime l'annotation qui a été envoyée */
   517             _export.getAnnotations().removeElement(_annotation, true); /* We delete the sent annotation to avoid redundancy */
   519             _export.deSerialize(_data); /* On désérialise les données reçues pour les réinjecter */
   518             _export.deSerialize(_data); /* Data deserialization */
   520             _this.source.merge(_export); /* On récupère les données réimportées dans l'espace global des données */
   519             _this.source.merge(_export); /* We merge the deserialized data with the current source data */
   521             if (_this.pause_on_write && _this.media.getPaused()) {
   520             if (_this.pause_on_write && _this.media.getPaused()) {
   522                 _this.media.play();
   521                 _this.media.play();
   523             }
   522             }
   524             _this.player.trigger("AnnotationsList.refresh"); /* On force le rafraîchissement du widget AnnotationsList */
   523             _this.player.trigger("AnnotationsList.refresh"); 
   525         },
   524         },
   526         error: function(_xhr, _error, _thrown) {
   525         error: function(_xhr, _error, _thrown) {
   527             IriSP.log("Error when sending annotation", _thrown);
   526             IriSP.log("Error when sending annotation", _thrown);
   528             _export.getAnnotations().removeElement(_annotation, true);
   527             _export.getAnnotations().removeElement(_annotation, true);
   529             _this.showScreen('Error');
   528             _this.showScreen('Error');