src/widgets/CreateAnnotation.js
changeset 1038 e78b889a75e1
parent 1033 c20df1c080e6
child 1047 c3bf174e0ef8
equal deleted inserted replaced
1037:5c0416e8bba1 1038:e78b889a75e1
     1 /* TODO: Add Social Network Sharing */
     1 /* TODO: Add Social Network Sharing */
     2 
     2 
     3 IriSP.Widgets.CreateAnnotation = function(player, config) {
     3 IriSP.Widgets.CreateAnnotation = function(player, config) {
     4     var _this = this;
       
     5     IriSP.Widgets.Widget.call(this, player, config);
     4     IriSP.Widgets.Widget.call(this, player, config);
     6     if (_this.api_method == 'local' && window.localStorage[_this.api_endpoint_template]) {
       
     7         this.source.onLoad(function () {
       
     8             var _export = _this.player.sourceManager.newLocalSource({serializer: IriSP.serializers[_this.api_serializer]});
       
     9             _export.deSerialize(window.localStorage[_this.api_endpoint_template]);
       
    10             console.log("Loaded personal annotations", _export);
       
    11             _this.source.merge(_export);
       
    12         });
       
    13     };
       
    14 };
     5 };
    15 
     6 
    16 IriSP.Widgets.CreateAnnotation.prototype = new IriSP.Widgets.Widget();
     7 IriSP.Widgets.CreateAnnotation.prototype = new IriSP.Widgets.Widget();
    17 
     8 
    18 IriSP.Widgets.CreateAnnotation.prototype.defaults = {
     9 IriSP.Widgets.CreateAnnotation.prototype.defaults = {
    19     show_title_field : true,
    10     show_title_field : true,
    20     show_creator_field : true,
    11     show_creator_field : true,
       
    12     creator_field_readonly : false,
    21     start_visible : true,
    13     start_visible : true,
    22     always_visible : false,
    14     always_visible : false,
    23     show_slice : true,
    15     show_slice : true,
    24     show_controls: false,
       
    25     show_arrow : true,
    16     show_arrow : true,
    26     show_mic_record: false,
    17     show_mic_record: false,
    27     show_mic_play: false,
    18     show_mic_play: false,
       
    19     show_time: true,
    28     minimize_annotation_widget : true,
    20     minimize_annotation_widget : true,
    29     creator_name : "",
    21     creator_name : "",
    30     creator_avatar : "",
    22     creator_avatar : "",
    31     tags : false,
    23     tags : false,
    32     tag_titles : false,
    24     tag_titles : false,
    49         background_color: "#f0e000",
    41         background_color: "#f0e000",
    50         text_color: "#000000"
    42         text_color: "#000000"
    51     }],
    43     }],
    52     slice_annotation_type: "chap",
    44     slice_annotation_type: "chap",
    53     annotation_type: "Contributions",
    45     annotation_type: "Contributions",
       
    46     post_at_segment_time: false,
       
    47     segment_annotation_type: "chap",
    54     api_serializer: "ldt_annotate",
    48     api_serializer: "ldt_annotate",
    55     api_endpoint_template: "",
    49     api_endpoint_template: "",
    56     api_method: "POST",
    50     api_method: "POST",
       
    51     project_id: "",
    57     after_send_timeout: 0,
    52     after_send_timeout: 0,
    58     close_after_send: false,
    53     close_after_send: false,
    59     tag_prefix: "#",
    54     tag_prefix: "#"
    60     slice_widget: null
       
    61 };
    55 };
    62 
    56 
    63 IriSP.Widgets.CreateAnnotation.prototype.messages = {
    57 IriSP.Widgets.CreateAnnotation.prototype.messages = {
    64     en: {
    58     en: {
    65         from_time: "from",
    59         from_time: "from",
   110     + '{{^show_slice}}{{#show_arrow}}<div class="Ldt-CreateAnnotation-Arrow"></div>{{/show_arrow}}{{/show_slice}}'
   104     + '{{^show_slice}}{{#show_arrow}}<div class="Ldt-CreateAnnotation-Arrow"></div>{{/show_arrow}}{{/show_slice}}'
   111     + '<div class="Ldt-CreateAnnotation"><div class="Ldt-CreateAnnotation-Inner">'
   105     + '<div class="Ldt-CreateAnnotation"><div class="Ldt-CreateAnnotation-Inner">'
   112     + '<form class="Ldt-CreateAnnotation-Screen Ldt-CreateAnnotation-Main">'
   106     + '<form class="Ldt-CreateAnnotation-Screen Ldt-CreateAnnotation-Main">'
   113     + '<h3><span class="Ldt-CreateAnnotation-h3Left">{{l10n.annotate_video}}{{#show_title_field}}</span></h3>'
   107     + '<h3><span class="Ldt-CreateAnnotation-h3Left">{{l10n.annotate_video}}{{#show_title_field}}</span></h3>'
   114     + '<h3><span class="Ldt-CreateAnnotation-h3Left"><input class="Ldt-CreateAnnotation-Title empty" placeholder="{{l10n.type_title}}" />{{/show_title_field}}'
   108     + '<h3><span class="Ldt-CreateAnnotation-h3Left"><input class="Ldt-CreateAnnotation-Title empty" placeholder="{{l10n.type_title}}" />{{/show_title_field}}'
   115     + '<span class="Ldt-CreateAnnotation-Times"> {{#show_slice}}{{l10n.from_time}} {{/show_slice}}{{^show_slice}}{{l10n.at_time}} {{/show_slice}} <span class="Ldt-CreateAnnotation-Begin">00:00</span>'
   109     + '{{#show_time}}<span class="Ldt-CreateAnnotation-Times"> {{#show_slice}}{{l10n.from_time}} {{/show_slice}}{{^show_slice}}{{l10n.at_time}} {{/show_slice}} <span class="Ldt-CreateAnnotation-Begin">00:00</span>{{/show_time}}'
   116     + '{{#show_slice}} {{l10n.to_time}} <span class="Ldt-CreateAnnotation-End">{{end}}</span>{{/show_slice}}</span></span>'
   110     + '{{#show_slice}} {{l10n.to_time}} <span class="Ldt-CreateAnnotation-End">{{end}}</span>{{/show_slice}}</span></span>'
   117     + '{{#show_creator_field}}{{l10n.your_name_}} <input class="Ldt-CreateAnnotation-Creator empty" value="{{creator_name}}" />{{/show_creator_field}}</h3>'
   111     + '{{#show_creator_field}}{{l10n.your_name_}} <input class="Ldt-CreateAnnotation-Creator empty" value="{{creator_name}}" {{#creator_field_readonly}}readonly{{/creator_field_readonly}}/>{{/show_creator_field}}</h3>'
   118     + '{{#show_controls}}<div class="Ldt-CreateAnnotation-Controls">'
       
   119     +   '<span class="Ldt-CreateAnnotation-Control-In">IN</span>'
       
   120     +   '<span class="Ldt-CreateAnnotation-Control-Out">OUT</span>'
       
   121     +   '<span class="Ldt-CreateAnnotation-Control-Play">Play</span>'
       
   122     + '</div>{{/show_controls}}'
       
   123     + '<textarea class="Ldt-CreateAnnotation-Description empty" placeholder="{{l10n.type_description}}"></textarea>'
   112     + '<textarea class="Ldt-CreateAnnotation-Description empty" placeholder="{{l10n.type_description}}"></textarea>'
   124     + '{{#show_creator_field}}<div class="Ldt-CreateAnnotation-Avatar"><img src="{{creator_avatar}}" title="{{creator_name}}"></img></div>{{/show_creator_field}}'
   113     + '<div class="Ldt-CreateAnnotation-Avatar"><img src="{{creator_avatar}}" title="{{creator_name}}"></img></div>'
   125     + '<input type="submit" class="Ldt-CreateAnnotation-Submit" value="{{l10n.submit}}" />'
   114     + '<input type="submit" class="Ldt-CreateAnnotation-Submit" value="{{l10n.submit}}" />'
   126     + '{{#show_mic_record}}<div class="Ldt-CreateAnnotation-RecBlock"><div class="Ldt-CreateAnnotation-RecLabel">Add voice annotation</div>'
   115     + '{{#show_mic_record}}<div class="Ldt-CreateAnnotation-RecBlock"><div class="Ldt-CreateAnnotation-RecLabel">Add voice annotation</div>'
   127     + '    <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="220" height="160">'
   116     + '    <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="220" height="160">'
   128     + '        <param name="movie" value="{{record_swf}}" />'
   117     + '        <param name="movie" value="{{record_swf}}" />'
   129     + '        <param name="quality" value="high" />'
   118     + '        <param name="quality" value="high" />'
   144     + '             type="application/x-shockwave-flash" allowFullScreen="true" wmode="transparent" '
   133     + '             type="application/x-shockwave-flash" allowFullScreen="true" wmode="transparent" '
   145     + '             flashvars="playVisible={{show_mic_play}}"'
   134     + '             flashvars="playVisible={{show_mic_play}}"'
   146     + '             pluginspage="http://www.macromedia.com/go/getflashplayer">'
   135     + '             pluginspage="http://www.macromedia.com/go/getflashplayer">'
   147     + '        </embed>'
   136     + '        </embed>'
   148     + '    </object>'
   137     + '    </object>'
   149     + '</div>{{/show_mic_record}}'
   138     + '</div>{{/show_mic_record}}' 
   150     + '{{#tags.length}}<div class="Ldt-CreateAnnotation-Tags"><div class="Ldt-CreateAnnotation-TagTitle">{{l10n.add_keywords_}}</div><ul class="Ldt-CreateAnnotation-TagList">'
   139     + '{{#tags.length}}<div class="Ldt-CreateAnnotation-Tags"><div class="Ldt-CreateAnnotation-TagTitle">{{l10n.add_keywords_}}</div><ul class="Ldt-CreateAnnotation-TagList">'
   151     + '{{#tags}}<li class="Ldt-CreateAnnotation-TagLi" tag-id="{{id}}" data-text="{{tag_prefix}}{{title}}"><span class="Ldt-CreateAnnotation-TagButton">{{title}}</span></li>{{/tags}}</ul></div>{{/tags.length}}'
   140     + '{{#tags}}<li class="Ldt-CreateAnnotation-TagLi" tag-id="{{id}}" data-text="{{tag_prefix}}{{title}}"><span class="Ldt-CreateAnnotation-TagButton">{{title}}</span></li>{{/tags}}</ul></div>{{/tags.length}}'
   152     + '{{#polemics.length}}<div class="Ldt-CreateAnnotation-Polemics"><div class="Ldt-CreateAnnotation-PolemicTitle">{{l10n.add_polemic_keywords_}}</div><ul class="Ldt-CreateAnnotation-PolemicList">'
   141     + '{{#polemics.length}}<div class="Ldt-CreateAnnotation-Polemics"><div class="Ldt-CreateAnnotation-PolemicTitle">{{l10n.add_polemic_keywords_}}</div><ul class="Ldt-CreateAnnotation-PolemicList">'
   153     + '{{#polemics}}<li class="Ldt-CreateAnnotation-PolemicLi" style="background-color: {{background_color}}; color: {{text_color}}" data-text="{{keyword}}">{{keyword}}</li>{{/polemics}}</ul></div>{{/polemics.length}}'
   142     + '{{#polemics}}<li class="Ldt-CreateAnnotation-PolemicLi" style="background-color: {{background_color}}; color: {{text_color}}" data-text="{{keyword}}">{{keyword}}</li>{{/polemics}}</ul></div>{{/polemics.length}}'
   154     + '<div style="clear: both;"></div></form>'
   143     + '<div style="clear: both;"></div></form>'
   155     + '<div class="Ldt-CreateAnnotation-Screen Ldt-CreateAnnotation-Wait"><div class="Ldt-CreateAnnotation-InnerBox">{{l10n.wait_while_processing}}</div></div>'
   144     + '<div class="Ldt-CreateAnnotation-Screen Ldt-CreateAnnotation-Wait"><div class="Ldt-CreateAnnotation-InnerBox">{{l10n.wait_while_processing}}</div></div>'
   156     + '<div class="Ldt-CreateAnnotation-Screen Ldt-CreateAnnotation-Error">{{^always_visible}}<a title="{{l10n.close_widget}}" class="Ldt-CreateAnnotation-Close" href="#"></a>{{/always_visible}}<div class="Ldt-CreateAnnotation-InnerBox">{{l10n.error_while_contacting}}</div></div>'
   145     + '<div class="Ldt-CreateAnnotation-Screen Ldt-CreateAnnotation-Error">{{^always_visible}}<a title="{{l10n.close_widget}}" class="Ldt-CreateAnnotation-Close" href="#"></a>{{/always_visible}}<div class="Ldt-CreateAnnotation-InnerBox">{{l10n.error_while_contacting}}</div></div>'
   157     + '<div class="Ldt-CreateAnnotation-Screen Ldt-CreateAnnotation-Saved">{{^always_visible}}<a title="{{l10n.close_widget}}" class="Ldt-CreateAnnotation-Close" href="#"></a>{{/always_visible}}<div class="Ldt-CreateAnnotation-InnerBox">{{l10n.annotation_saved}}</div></div>'
   146     + '<div class="Ldt-CreateAnnotation-Screen Ldt-CreateAnnotation-Saved">{{^always_visible}}<a title="{{l10n.close_widget}}" class="Ldt-CreateAnnotation-Close" href="#"></a>{{/always_visible}}<div class="Ldt-CreateAnnotation-InnerBox">{{l10n.annotation_saved}}</div></div>'
   158     + '</div></div>';
   147     + '</div></div>';
   159 
   148     
   160 IriSP.Widgets.CreateAnnotation.prototype.draw = function() {
   149 IriSP.Widgets.CreateAnnotation.prototype.draw = function() {
   161     var _this = this;
   150     var _this = this;
   162 
   151     
   163     this.begin = new IriSP.Model.Time();
   152     this.begin = new IriSP.Model.Time();
   164     this.end = this.source.getDuration();
   153     this.end = this.source.getDuration();
   165 
   154     
   166     this.tag_prefix = this.tag_prefix || "";
   155     this.tag_prefix = this.tag_prefix || "";
   167 
   156     
   168     if (this.tag_titles && !this.tags) {
   157     if (this.tag_titles && !this.tags) {
   169 		if(!(this.tag_titles.length==1 && this.tag_titles[0]=="")){
   158 		if(!(this.tag_titles.length==1 && this.tag_titles[0]=="")){
   170 			this.tags = IriSP._(this.tag_titles).map(function(_tag_title) {
   159 			this.tags = IriSP._(this.tag_titles).map(function(_tag_title) {
   171 				var _tag,
   160 				var _tag,
   172 					_tags = _this.source.getTags().searchByTitle(_tag_title, true);
   161 					_tags = _this.source.getTags().searchByTitle(_tag_title, true);
   198     }
   187     }
   199     this.record_swf = IriSP.getLib("recordMicSwf");
   188     this.record_swf = IriSP.getLib("recordMicSwf");
   200     this.renderTemplate();
   189     this.renderTemplate();
   201     if (this.show_mic_record) {
   190     if (this.show_mic_record) {
   202         this.recorder = this.$.find("embed")[0];
   191         this.recorder = this.$.find("embed")[0];
   203 
   192         
   204         window.setAudioUrl = function(_url) {
   193         window.setAudioUrl = function(_url) {
   205             _this.audio_url = _url;
   194             _this.audio_url = _url;
   206         };
   195         }
   207     }
   196     }
   208     if (this.show_slice) {
   197     if (this.show_slice) {
   209         this.slice_widget = this.insertSubwidget(
   198         this.insertSubwidget(
   210             this.$.find(".Ldt-CreateAnnotation-Slice"),
   199             this.$.find(".Ldt-CreateAnnotation-Slice"),
   211             {
   200             {
   212                 type: "Slice",
   201                 type: "Slice",
   213                 show_arrow: this.show_arrow,
   202                 show_arrow: this.show_arrow,
   214                 annotation_type: this.slice_annotation_type,
   203                 annotation_type: this.slice_annotation_type,
   257         this.$.find(".Ldt-CreateAnnotation-Title").bind("change keyup input paste", this.functionWrapper("onTitleChange"));
   246         this.$.find(".Ldt-CreateAnnotation-Title").bind("change keyup input paste", this.functionWrapper("onTitleChange"));
   258     }
   247     }
   259     if (this.show_creator_field) {
   248     if (this.show_creator_field) {
   260         this.$.find(".Ldt-CreateAnnotation-Creator").bind("change keyup input paste", this.functionWrapper("onCreatorChange"));
   249         this.$.find(".Ldt-CreateAnnotation-Creator").bind("change keyup input paste", this.functionWrapper("onCreatorChange"));
   261     }
   250     }
   262     this.$.find("[class^='Ldt-CreateAnnotation-Control-']").click(function() {
   251     
   263         var action = this.className.replace('Ldt-CreateAnnotation-Control-', '');
       
   264         switch (action) {
       
   265             case "In":
       
   266                // Set In bound to current player time
       
   267                _this.begin = new IriSP.Model.Time(_this.media.getCurrentTime() || 0);
       
   268                _this.$.find(".Ldt-CreateAnnotation-Begin").html(_this.begin.toString());
       
   269                break;
       
   270             case "Out":
       
   271                // Set In bound to current player time
       
   272                _this.end = new IriSP.Model.Time(_this.media.getCurrentTime() || _this.media.duration);
       
   273                _this.$.find(".Ldt-CreateAnnotation-End").html(_this.end.toString());
       
   274                break;
       
   275             case "Play":
       
   276                _this.media.setCurrentTime(_this.begin);
       
   277                _this.media.play()
       
   278                break;
       
   279         }
       
   280         return false;
       
   281     });
       
   282 
       
   283     if (this.start_visible) {
   252     if (this.start_visible) {
   284         this.show();
   253         this.show();
   285     } else {
   254     } else {
   286         this.$.hide();
   255         this.$.hide();
   287         this.hide();
   256         this.hide();
   288     }
   257     }
   289 
   258     
   290     this.onMdpEvent("CreateAnnotation.toggle","toggle");
   259     this.onMdpEvent("CreateAnnotation.toggle","toggle");
       
   260     this.onMdpEvent("CreateAnnotation.hide", "hide");
       
   261     this.onMdpEvent("CreateAnnotation.show", "show");
   291     this.$.find("form").submit(this.functionWrapper("onSubmit"));
   262     this.$.find("form").submit(this.functionWrapper("onSubmit"));
   292 };
   263 };
   293 
   264 
   294 IriSP.Widgets.CreateAnnotation.prototype.showScreen = function(_screenName) {
   265 IriSP.Widgets.CreateAnnotation.prototype.showScreen = function(_screenName) {
   295     this.$.find('.Ldt-CreateAnnotation-' + _screenName).show()
   266     this.$.find('.Ldt-CreateAnnotation-' + _screenName).show()
   296         .siblings().hide();
   267         .siblings().hide();
   297 }
   268 }
   298 
   269 
   299 IriSP.Widgets.CreateAnnotation.prototype.show = function() {
   270 IriSP.Widgets.CreateAnnotation.prototype.show = function() {
   300     this.visible = true;
   271     if (!this.visible){
   301     this.showScreen('Main');
   272         this.visible = true;
   302     this.$.find(".Ldt-CreateAnnotation-Description").val("").css("border-color", "#666666").addClass("empty");
   273         this.showScreen('Main');
   303     if (this.show_title_field) {
   274         this.$.find(".Ldt-CreateAnnotation-Description").val("").css("border-color", "#666666").addClass("empty");
   304         this.$.find(".Ldt-CreateAnnotation-Title").val("").css("border-color", "#666666").addClass("empty");
   275         if (this.show_title_field) {
   305     }
   276             this.$.find(".Ldt-CreateAnnotation-Title").val("").css("border-color", "#666666").addClass("empty");
   306     if (this.show_creator_field) {
   277         }
   307         this.$.find(".Ldt-CreateAnnotation-Creator").val(this.creator_name).css("border-color", "#666666");
   278         if (this.show_creator_field) {
   308         if (!this.creator_name) {
   279             this.$.find(".Ldt-CreateAnnotation-Creator").val(this.creator_name).css("border-color", "#666666");
   309             this.$.find(".Ldt-CreateAnnotation-Creator").addClass("empty");
   280             if (!this.creator_name) {
   310         }
   281                 this.$.find(".Ldt-CreateAnnotation-Creator").addClass("empty");
   311     }
   282             }
   312     this.$.find(".Ldt-CreateAnnotation-TagLi, .Ldt-CreateAnnotation-PolemicLi").removeClass("selected");
   283         }
   313     this.$.slideDown();
   284         this.$.find(".Ldt-CreateAnnotation-TagLi, .Ldt-CreateAnnotation-PolemicLi").removeClass("selected");
   314     if (this.minimize_annotation_widget) {
   285         this.$.slideDown();
   315         this.player.trigger("Annotation.minimize");
   286         if (this.minimize_annotation_widget) {
       
   287             this.player.trigger("Annotation.minimize");
       
   288         }
   316     }
   289     }
   317 };
   290 };
   318 
   291 
   319 IriSP.Widgets.CreateAnnotation.prototype.hide = function() {
   292 IriSP.Widgets.CreateAnnotation.prototype.hide = function() {
   320     if (this.recorder) {
   293     if (this.visible){
   321         this.recorder.stopRecord();
   294         if (this.recorder) {
   322     }
   295             this.recorder.stopRecord();
   323     if (!this.always_visible) {
   296         }
   324         this.visible = false;
   297         if (!this.always_visible) {
   325         this.$.slideUp();
   298             this.visible = false;
   326         if (this.minimize_annotation_widget) {
   299             this.$.slideUp();
   327             this.player.trigger("Annotation.maximize");
   300             if (this.minimize_annotation_widget) {
       
   301                 this.player.trigger("Annotation.maximize");
       
   302             }
   328         }
   303         }
   329     }
   304     }
   330 };
   305 };
   331 
   306 
   332 IriSP.Widgets.CreateAnnotation.prototype.toggle = function() {
   307 IriSP.Widgets.CreateAnnotation.prototype.toggle = function() {
   333     var _this = this;
       
   334     if (!this.always_visible) {
   308     if (!this.always_visible) {
   335         if (this.visible) {
   309         if (this.visible) {
   336             this.hide();
   310             this.hide();
   337         } else {
   311         } else {
   338             _this.begin = new IriSP.Model.Time(_this.media.getCurrentTime() || 0);
       
   339             _this.end = new IriSP.Model.Time(_this.media.getCurrentTime() || 0);
       
   340             _this.$.find(".Ldt-CreateAnnotation-Begin").html(_this.begin.toString());
       
   341             _this.$.find(".Ldt-CreateAnnotation-End").html(_this.end.toString());
       
   342             if (_this.slice_widget) {
       
   343                 _this.slice_widget.setBounds(_this.begin, _this.end);
       
   344             }
       
   345             this.show();
   312             this.show();
   346         }
   313         }
   347     }
   314     }
   348 };
   315 };
   349 
   316 
   417 IriSP.Widgets.CreateAnnotation.prototype.onSubmit = function() {
   384 IriSP.Widgets.CreateAnnotation.prototype.onSubmit = function() {
   418     /* Si les champs obligatoires sont vides, on annule l'envoi */
   385     /* Si les champs obligatoires sont vides, on annule l'envoi */
   419     if (!this.onDescriptionChange() || (this.show_title_field && !this.onTitleChange()) || (this.show_creator_field && !this.onCreatorChange())) {
   386     if (!this.onDescriptionChange() || (this.show_title_field && !this.onTitleChange()) || (this.show_creator_field && !this.onCreatorChange())) {
   420         return false;
   387         return false;
   421     }
   388     }
   422 
   389     
   423     if (this.recorder) {
   390     if (this.recorder) {
   424         this.recorder.stopRecord();
   391         this.recorder.stopRecord();
   425     }
   392     }
   426 
   393     
   427     var _this = this,
   394     var _this = this,
   428         _exportedAnnotations = new IriSP.Model.List(this.player.sourceManager), /* Création d'une liste d'annotations contenant une annotation afin de l'envoyer au serveur */
   395         _exportedAnnotations = new IriSP.Model.List(this.player.sourceManager), /* Création d'une liste d'annotations contenant une annotation afin de l'envoyer au serveur */
   429         _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 */
   396         _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 */
   430         _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) */
   397         _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) */
   431         _annotationTypes = this.source.getAnnotationTypes().searchByTitle(this.annotation_type, true), /* Récupération du type d'annotation dans lequel l'annotation doit être ajoutée */
   398         _annotationTypes = this.source.getAnnotationTypes().searchByTitle(this.annotation_type, true), /* Récupération du type d'annotation dans lequel l'annotation doit être ajoutée */
   432         _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 */
   399         _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 */
   433         _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 */
   400         _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 */
   434 
   401     
   435     /* Si nous avons dû générer un ID d'annotationType à la volée... */
   402     /* Si nous avons dû générer un ID d'annotationType à la volée... */
   436     if (!_annotationTypes.length) {
   403     if (!_annotationTypes.length) {
   437         /* Il ne faudra pas envoyer l'ID généré au serveur */
   404         /* Il ne faudra pas envoyer l'ID généré au serveur */
   438         _annotationType.dont_send_id = true;
   405         _annotationType.dont_send_id = true;
   439         /* Il faut inclure le titre dans le type d'annotation */
   406         /* Il faut inclure le titre dans le type d'annotation */
   440         _annotationType.title = this.annotation_type;
   407         _annotationType.title = this.annotation_type;
   441     }
   408     }
   442 
   409     
   443     /*
   410     /*
   444      * Nous remplissons les données de l'annotation générée à la volée
   411      * Nous remplissons les données de l'annotation générée à la volée
   445      * ATTENTION: Si nous sommes sur un MASHUP, ces éléments doivent se référer AU MEDIA D'ORIGINE
   412      * ATTENTION: Si nous sommes sur un MASHUP, ces éléments doivent se référer AU MEDIA D'ORIGINE
   446      * */
   413      * */
   447     _annotation.setMedia(this.source.currentMedia.id); /* Id du média annoté */
   414     _annotation.setMedia(this.source.currentMedia.id); /* Id du média annoté */
   448     _annotation.setBegin(this.begin); /*Timecode de début */
   415     
   449     _annotation.setEnd(this.end); /* Timecode de fin */
   416     if (this.post_at_segment_time){
   450     _annotation.created = new Date(); /* Date de création de l'annotation */
   417         var _currentTime = this.media.getCurrentTime() 
   451 
   418         var _segmentsAnnotations = this.source.getAnnotationsByTypeTitle(this.segments_annotation_type)
       
   419         var _currentSegments = _segmentsAnnotations.filter(function(_segment){
       
   420             return (_currentTime >= _segment.begin && _currentTime <= _segment.end)
       
   421         });
       
   422         if (_currentSegments.length == 0){
       
   423             _annotation.setBegin(this.begin); /* Timecode de début du widget */
       
   424             _annotation.setEnd(this.end); /* Timecode de fin du widget */
       
   425         }
       
   426         else {
       
   427             _annotation.setBegin(_currentSegments[0].begin); /* Timecode de début du segment */
       
   428             _annotation.setEnd(_currentSegments[0].end); /* Timecode de fin du segment */
       
   429         }
       
   430     }
       
   431     else {
       
   432         _annotation.setBegin(this.begin); /*Timecode de début du widget */
       
   433         _annotation.setEnd(this.end); /* Timecode de fin du widget */
       
   434     }
   452     _annotation.setAnnotationType(_annotationType.id); /* Id du type d'annotation */
   435     _annotation.setAnnotationType(_annotationType.id); /* Id du type d'annotation */
   453     _annotation.description = this.$.find(".Ldt-CreateAnnotation-Description").val(); /* Champ description */
       
   454     if (this.show_title_field) {
   436     if (this.show_title_field) {
   455         /* Champ titre, seulement s'il est visible */
   437         /* Champ titre, seulement s'il est visible */
   456         _annotation.title = this.$.find(".Ldt-CreateAnnotation-Title").val();
   438         _annotation.title = this.$.find(".Ldt-CreateAnnotation-Title").val();
   457     } else {
   439     }if (this.project_id != ""){
   458         _annotation.title = _annotation.description;
   440     	/* Champ id projet, seulement si on l'a renseigné dans la config */
   459     }
   441     	_annotation.project_id = this.project_id;
   460 
   442     }
       
   443     _annotation.created = new Date(); /* Date de création de l'annotation */
       
   444     _annotation.description = this.$.find(".Ldt-CreateAnnotation-Description").val(); /* Champ description */
       
   445    
   461     var tagIds = Array.prototype.map.call(
   446     var tagIds = Array.prototype.map.call(
   462         this.$.find(".Ldt-CreateAnnotation-TagLi.selected"),
   447         this.$.find(".Ldt-CreateAnnotation-TagLi.selected"),
   463         function(el) { return IriSP.jQuery(el).attr("tag-id")}
   448         function(el) { return IriSP.jQuery(el).attr("tag-id")}
   464     );
   449     );
   465 
   450         
   466     IriSP._(_annotation.description.match(/#[^\s#.,;]+/g)).each(function(_tt) {
   451     IriSP._(_annotation.description.match(/#[^\s#.,;]+/g)).each(function(_tt) {
   467         var _tag,
   452         var _tag,
   468             _tag_title = _tt.replace(/^#/,''),
   453             _tag_title = _tt.replace(/^#/,''),
   469             _tags = _this.source.getTags().searchByTitle(_tag_title, true);
   454             _tags = _this.source.getTags().searchByTitle(_tag_title, true);
   470         if (_tags.length) {
   455         if (_tags.length) {
   475             _tag.title = _tag_title;
   460             _tag.title = _tag_title;
   476         }
   461         }
   477         if (tagIds.indexOf(_tag.id) === -1) {
   462         if (tagIds.indexOf(_tag.id) === -1) {
   478             tagIds.push(_tag.id);
   463             tagIds.push(_tag.id);
   479         }
   464         }
   480 
   465         
   481     })
   466     })
   482 
   467    
   483     _annotation.setTags(IriSP._(tagIds).uniq()); /*Liste des ids de tags */
   468     _annotation.setTags(IriSP._(tagIds).uniq()); /*Liste des ids de tags */
   484     if (this.audio_url) {
   469     if (this.audio_url) {
   485         _annotation.audio = {
   470         _annotation.audio = {
   486             src: "mic",
   471             src: "mic",
   487             mimetype: "audio/mp3",
   472             mimetype: "audio/mp3",
   492         _annotation.creator = this.$.find(".Ldt-CreateAnnotation-Creator").val();
   477         _annotation.creator = this.$.find(".Ldt-CreateAnnotation-Creator").val();
   493     } else {
   478     } else {
   494         _annotation.creator = this.creator_name;
   479         _annotation.creator = this.creator_name;
   495     }
   480     }
   496     _exportedAnnotations.push(_annotation); /* Ajout de l'annotation à la liste à exporter */
   481     _exportedAnnotations.push(_annotation); /* Ajout de l'annotation à la liste à exporter */
   497 
   482     _export.addList("annotation",_exportedAnnotations); /* Ajout de la liste à exporter à l'objet Source */
   498     if (this.api_method == 'local') {
   483     var _this = this;
   499         // Append to existing localStorage annotations
   484     /* Envoi de l'annotation via AJAX au serveur ! */
   500         // FIXME: handle movie ids
   485     IriSP.jQuery.ajax({
   501         /* Use localStorage */
   486         url: _url,
   502         /* Data will be serialized in the localStore property designated by api_endpoint_template */
   487         type: this.api_method,
   503         _export.addList("annotation", _exportedAnnotations); /* Ajout de la liste à exporter à l'objet Source */
   488         contentType: 'application/json',
   504         _this.source.merge(_export); /* On ajoute la nouvelle annotation au recueil original */
   489         data: _export.serialize(), /* L'objet Source est sérialisé */
   505         // Import previously saved local annotations
   490         success: function(_data) {
   506         if (window.localStorage[_this.api_endpoint_template]) {
   491             _this.showScreen('Saved'); /* Si l'appel a fonctionné, on affiche l'écran "Annotation enregistrée" */
   507             _export.deSerialize(window.localStorage[_this.api_endpoint_template]);
   492             if (_this.after_send_timeout) { /* Selon les options de configuration, on revient à l'écran principal ou on ferme le widget, ou rien */
   508         }
   493                 window.setTimeout(
   509         // Save everything back
   494                     function() {
   510         window.localStorage[_this.api_endpoint_template] = _export.serialize();
   495                         _this.close_after_send
   511         if (_this.pause_on_write && _this.media.getPaused()) {
   496                         ? _this.hide()
   512             _this.media.play();
   497                         : _this.show();
   513         }
   498                     },
   514         _this.player.trigger("AnnotationsList.refresh"); /* On force le rafraîchissement du widget AnnotationsList */
   499                     _this.after_send_timeout
   515         _this.$.find(".Ldt-CreateAnnotation-Description").val("");
   500                 );
   516     } else {
   501             }
   517         _export.addList("annotation",_exportedAnnotations); /* Ajout de la liste à exporter à l'objet Source */
   502             _export.getAnnotations().removeElement(_annotation, true); /* Pour éviter les doublons, on supprime l'annotation qui a été envoyée */
   518        /* Envoi de l'annotation via AJAX au serveur ! */
   503             _export.deSerialize(_data); /* On désérialise les données reçues pour les réinjecter */
   519         IriSP.jQuery.ajax({
   504             _this.source.merge(_export); /* On récupère les données réimportées dans l'espace global des données */
   520             url: _url,
   505             if (_this.pause_on_write && _this.media.getPaused()) {
   521             type: this.api_method,
   506                 _this.media.play();
   522             contentType: 'application/json',
   507             }
   523             data: _export.serialize(), /* L'objet Source est sérialisé */
   508             _this.player.trigger("AnnotationsList.refresh"); /* On force le rafraîchissement du widget AnnotationsList */
   524             success: function(_data) {
   509         },
   525                 _this.showScreen('Saved'); /* Si l'appel a fonctionné, on affiche l'écran "Annotation enregistrée" */
   510         error: function(_xhr, _error, _thrown) {
   526                 if (_this.after_send_timeout) { /* Selon les options de configuration, on revient à l'écran principal ou on ferme le widget, ou rien */
   511             IriSP.log("Error when sending annotation", _thrown);
   527                     window.setTimeout(
   512             _export.getAnnotations().removeElement(_annotation, true);
   528                         function() {
   513             _this.showScreen('Error');
   529                             _this.close_after_send
   514             window.setTimeout(function(){
   530                                 ? _this.hide()
   515                 _this.showScreen("Main")
   531                                 : _this.show();
       
   532                         },
       
   533                         _this.after_send_timeout
       
   534                     );
       
   535                 }
       
   536                 _export.getAnnotations().removeElement(_annotation, true); /* Pour éviter les doublons, on supprime l'annotation qui a été envoyée */
       
   537                 _export.deSerialize(_data); /* On désérialise les données reçues pour les réinjecter */
       
   538                 _this.source.merge(_export); /* On récupère les données réimportées dans l'espace global des données */
       
   539                 if (_this.pause_on_write && _this.media.getPaused()) {
       
   540                     _this.media.play();
       
   541                 }
       
   542                 _this.player.trigger("AnnotationsList.refresh"); /* On force le rafraîchissement du widget AnnotationsList */
       
   543             },
   516             },
   544             error: function(_xhr, _error, _thrown) {
   517             (_this.after_send_timeout || 5000));
   545                 IriSP.log("Error when sending annotation", _thrown);
   518         }
   546                 _export.getAnnotations().removeElement(_annotation, true);
   519     });
   547                 _this.showScreen('Error');
   520     this.showScreen('Wait');
   548                 window.setTimeout(function(){
   521     
   549                     _this.showScreen("Main")
       
   550                 },
       
   551                                   (_this.after_send_timeout || 5000));
       
   552             }
       
   553         });
       
   554         this.showScreen('Wait');
       
   555     }
       
   556 
       
   557     return false;
   522     return false;
   558 };
   523 };
       
   524