src/widgets/LatestAnnotation.js
changeset 1041 423a8c6f9c4d
child 1046 eb77616c245f
equal deleted inserted replaced
1040:be314c4ea4ac 1041:423a8c6f9c4d
       
     1 /* Widget that displays the last annotation that was posted, optionally for current segment, optionally for a given username */
       
     2 
       
     3 IriSP.Widgets.LatestAnnotation = function(player, config){
       
     4     IriSP.Widgets.Widget.call(this, player, config);
       
     5 };
       
     6 
       
     7 IriSP.Widgets.LatestAnnotation.prototype = new IriSP.Widgets.Widget();
       
     8 
       
     9 IriSP.Widgets.LatestAnnotation.prototype.defaults = {
       
    10     from_user: false,
       
    11     filter_by_segment: false,
       
    12     segments_annotation_type: "chap",
       
    13     hide_without_segment: false,
       
    14     annotation_type: "contribution",
       
    15     /*
       
    16      * Set to a username if you only want to display annotations from a given user
       
    17      */
       
    18     show_only_annotation_from_user: false,
       
    19     empty_message: false,
       
    20     starts_hidden: false,
       
    21 };
       
    22 
       
    23 IriSP.Widgets.LatestAnnotation.prototype.template = 
       
    24     "<div class='Ldt-LatestAnnotation'>"
       
    25     + "</div>";
       
    26 
       
    27 IriSP.Widgets.LatestAnnotation.prototype.annotationTemplate =
       
    28     "<div class='Ldt-LatestAnnotation-Box'>"
       
    29     + "    <div class='Ldt-LatestAnnotation-Element Ldt-LatestAnnotation-CreationDate'>{{{annotation_created}}}</div>" 
       
    30     + "    <div class='Ldt-LatestAnnotation-Element Ldt-LatestAnnotation-Title'>{{{annotation_creator}}}{{#annotation_title}}: {{{annotation_title}}}{{/annotation_title}}</div>" 
       
    31     + "    <div class='Ldt-LatestAnnotation-Element Ldt-LatestAnnotation-Content'>"
       
    32     +         "{{{annotation_content}}}"
       
    33     + "    </div>"
       
    34     + "</div>"
       
    35 
       
    36 IriSP.Widgets.LatestAnnotation.prototype.draw = function(){
       
    37     var _this = this;
       
    38     
       
    39     this.renderTemplate();
       
    40     
       
    41     this.annotationContainer_$ = this.$.find('.Ldt-LatestAnnotation');
       
    42     
       
    43     this.onMediaEvent("timeupdate", "refresh");
       
    44     
       
    45     if (this.starts_hidden){
       
    46         this.visible = true;
       
    47         this.hide();
       
    48     }
       
    49     else{
       
    50         this.visible = false;
       
    51         this.show();
       
    52     }
       
    53     
       
    54     this.refresh();
       
    55 }
       
    56 
       
    57 IriSP.Widgets.LatestAnnotation.prototype.messages = {
       
    58     fr : {
       
    59         empty : "Aucune annotation à afficher"
       
    60     },
       
    61     en: {
       
    62         empty: "No annotation to display"
       
    63     }
       
    64 }
       
    65 
       
    66 IriSP.Widgets.LatestAnnotation.prototype.refresh = function(){ 
       
    67     var _currentTime = this.media.getCurrentTime() 
       
    68     var _segmentsAnnotations = this.source.getAnnotationsByTypeTitle(this.segments_annotation_type)
       
    69     var _currentSegments = _segmentsAnnotations.filter(function(_segment){
       
    70         return (_currentTime >= _segment.begin && _currentTime <= _segment.end)
       
    71     });
       
    72     if (this.hide_without_segment){
       
    73         if (_currentSegments.length == 0){
       
    74             if (this.visible){
       
    75                 this.hide()
       
    76             }
       
    77         }
       
    78         else {
       
    79             if (!this.visible){
       
    80                 this.show()
       
    81             }
       
    82         }
       
    83     }
       
    84     if (this.visible){
       
    85         var _list = this.getWidgetAnnotations();
       
    86         if(this.filter_by_segment){
       
    87             if (_currentSegments.length == 0) {
       
    88                 _list = _list.filter(function(_annotation){
       
    89                     return false;
       
    90                 });
       
    91             }
       
    92             else {
       
    93                 _list = _list.filter(function(_annotation){
       
    94                     _annotationTime = (_annotation.begin+_annotation.end)/2;
       
    95                     return (_currentSegments[0].begin <= _annotationTime && _currentSegments[0].end >= _annotationTime);
       
    96                 });
       
    97             }
       
    98             _list.sortBy(function(_annotation){
       
    99                 return _annotation.created;
       
   100             });
       
   101             
       
   102             var _latestAnnotation = false;
       
   103             var _html="";
       
   104             if (_list.length != 0){
       
   105                 _latestAnnotation = _list.pop();
       
   106                 _html = Mustache.to_html(this.annotationTemplate, {
       
   107                     annotation_created: _latestAnnotation.created.toLocaleDateString()+", "+_latestAnnotation.created.toLocaleTimeString(),
       
   108                     annotation_creator: _latestAnnotation.creator,
       
   109                     annotation_title: _latestAnnotation.title,
       
   110                     annotation_content: _latestAnnotation.description,
       
   111                 });
       
   112             }
       
   113             else {
       
   114                 var _empty_message = this.l10n.empty
       
   115                 if (this.empty_message) {
       
   116                     _empty_message = this.empty_message
       
   117                 }
       
   118                 _html = "<div class='Ldt-LatestAnnotation-Element Ldt-LatestAnnotation-NoAnnotation'>"+_empty_message+"</div>";
       
   119             }
       
   120             this.annotationContainer_$.html(_html);
       
   121             
       
   122         }
       
   123     }
       
   124 }
       
   125 
       
   126 IriSP.Widgets.LatestAnnotation.prototype.hide = function() {
       
   127     if (this.visible){
       
   128         this.visible = false;
       
   129         this.annotationContainer_$.hide()
       
   130     }
       
   131 }
       
   132 
       
   133 IriSP.Widgets.LatestAnnotation.prototype.show = function() {
       
   134     if(!this.visible){
       
   135         this.visible = true;
       
   136         this.annotationContainer_$.show()
       
   137     }
       
   138 }