src/widgets/AnnotationsList.js
changeset 1037 5c0416e8bba1
parent 1033 c20df1c080e6
child 1046 eb77616c245f
equal deleted inserted replaced
1036:3b88465041d6 1037:5c0416e8bba1
    10 };
    10 };
    11 
    11 
    12 IriSP.Widgets.AnnotationsList.prototype = new IriSP.Widgets.Widget();
    12 IriSP.Widgets.AnnotationsList.prototype = new IriSP.Widgets.Widget();
    13 
    13 
    14 IriSP.Widgets.AnnotationsList.prototype.defaults = {
    14 IriSP.Widgets.AnnotationsList.prototype.defaults = {
    15     /* URL when the annotations are to be reloaded from an LDT-like segment API
    15     /*
    16      * e.g. http://ldt.iri.centrepompidou.fr/ldtplatform/api/ldt/segments/{{media}}/{{begin}}/{{end}}?callback=?
    16      * URL when the annotations are to be reloaded from an LDT-like segment API
       
    17      * e.g.
       
    18      * http://ldt.iri.centrepompidou.fr/ldtplatform/api/ldt/segments/{{media}}/{{begin}}/{{end}}?callback=?
    17      */
    19      */
    18     ajax_url : false,
    20     ajax_url : false,
    19     /* number of milliseconds before/after the current timecode when calling the segment API
    21     /*
       
    22      * number of milliseconds before/after the current timecode when calling the
       
    23      * segment API
    20      */
    24      */
    21     ajax_granularity : 600000, 
    25     ajax_granularity : 600000, 
    22     default_thumbnail : "",
    26     default_thumbnail : "",
    23     /* URL when the annotation is not in the current project,
    27     /*
    24      * e.g. http://ldt.iri.centrepompidou.fr/ldtplatform/ldt/front/player/{{media}}/{{project}}/{{annotationType}}#id={{annotation}}
    28      * URL when the annotation is not in the current project, e.g.
       
    29      * http://ldt.iri.centrepompidou.fr/ldtplatform/ldt/front/player/{{media}}/{{project}}/{{annotationType}}#id={{annotation}}
    25      */
    30      */
    26     foreign_url : "",
    31     foreign_url : "",
    27     annotation_type : false,
    32     annotation_type : false,
    28     refresh_interval : 0,
    33     refresh_interval : 0,
    29     limit_count : 20,
    34     limit_count : 20,
    30     newest_first : false,
    35     newest_first : false,
    31     show_audio: true,
    36     always_visible : false,
    32     show_creator: false,
    37     start_visible: true,
    33     show_controls: false,
    38     show_audio : true,
       
    39     show_filters : false,
       
    40     show_creation_date : false,
       
    41     show_timecode : true, 
       
    42     /*
       
    43      * Only annotation in the current segment will be displayed. Designed to work with the Segments Widget.
       
    44      */
       
    45     filter_by_segments: false,
       
    46     segments_annotation_type: "chap",
       
    47     /*
       
    48      * Set to a username if you only want to display annotations from a given user
       
    49      */
       
    50     show_only_annotation_from_user: false,
       
    51     /*
       
    52      * Show a text field that filter annotations by username
       
    53      */
       
    54     filter_by_user: false,
       
    55     tags : true,
    34     polemics : [{
    56     polemics : [{
    35         keyword: "++",
    57         keyword: "++",
    36         background_color: "#c9ecc6"
    58         background_color: "#c9ecc6"
    37     },{
    59     },{
    38         keyword: "--",
    60         keyword: "--",
    57     }
    79     }
    58 };
    80 };
    59 
    81 
    60 IriSP.Widgets.AnnotationsList.prototype.template =
    82 IriSP.Widgets.AnnotationsList.prototype.template =
    61     '<div class="Ldt-AnnotationsListWidget">'
    83     '<div class="Ldt-AnnotationsListWidget">'
       
    84     + '{{#show_filters}}'
       
    85     + '<div class="Ldt-AnnotationsList-Filters">'
       
    86     +    '<input class="Ldt-AnnotationsList-filter-text" type="text" value="Mot-clés"></input>'
       
    87     +    '<select class="Ldt-AnnotationsList-filter-dropdown"></select>'
       
    88     +    '<label class="Ldt-AnnotationsList-filter-checkbox"><input type="checkbox">Toutes annotations</label>'
       
    89     + '</div>'
       
    90     + '{{/show_filters}}'
    62     + '{{#show_audio}}<div class="Ldt-AnnotationsList-Audio"></div>{{/show_audio}}'
    91     + '{{#show_audio}}<div class="Ldt-AnnotationsList-Audio"></div>{{/show_audio}}'
    63     + '{{#show_controls}}<div class="Ldt-AnnotationsList-Controls"><span class="Ldt-AnnotationsList-Control-Prev">Previous</span> | <span class="Ldt-AnnotationsList-Control-Next">Next</span></div>{{/show_controls}}'
       
    64     + '<ul class="Ldt-AnnotationsList-ul">'
    92     + '<ul class="Ldt-AnnotationsList-ul">'
    65     + '</ul>'
    93     + '</ul>'
    66     + '</div>';
    94     + '</div>';
    67 
    95 
    68 IriSP.Widgets.AnnotationsList.prototype.annotationTemplate = 
    96 IriSP.Widgets.AnnotationsList.prototype.annotationTemplate = 
    69     '<li class="Ldt-AnnotationsList-li Ldt-Highlighter-Annotation Ldt-TraceMe" data-annotation="{{ id }}" data-begin="{{ begin_ms }}" data-end="{{ end_ms }}" trace-info="annotation-id:{{id}}, media-id:{{media_id}}" style="{{specific_style}}">'
    97     '<li class="Ldt-AnnotationsList-li Ldt-TraceMe" trace-info="annotation-id:{{id}}, media-id:{{media_id}}" style="{{specific_style}}">'
    70     + '<div class="Ldt-AnnotationsList-ThumbContainer">'
    98     + '<div class="Ldt-AnnotationsList-ThumbContainer">'
    71     + '<a href="{{url}}" draggable="true">'
    99     + '<a href="{{url}}" draggable="true">'
    72     + '<img title="{{ begin }} - {{ atitle }}" class="Ldt-AnnotationsList-Thumbnail" src="{{thumbnail}}" />'
   100     + '<img class="Ldt-AnnotationsList-Thumbnail" src="{{thumbnail}}" />'
    73     + '</a>'
   101     + '</a>'
    74     + '</div>'
   102     + '</div>'
    75     + '<div class="Ldt-AnnotationsList-Duration">{{begin}} - {{end}}</div>'
   103     + '{{#show_timecode}}<div class="Ldt-AnnotationsList-Duration">{{begin}} - {{end}}</div>{{/show_timecode}}'
    76     + '<h3 class="Ldt-AnnotationsList-Title" draggable="true">'
   104     + '<h3 class="Ldt-AnnotationsList-Title" draggable="true">'
    77     + '<a href="{{url}}">{{{htitle}}}</a>'
   105     + '<a href="{{url}}">{{{htitle}}}</a>'
    78     + '</h3>'
   106     + '</h3>'
    79     + '<p class="Ldt-AnnotationsList-Description">{{{hdescription}}}</p>'
   107     + '<p class="Ldt-AnnotationsList-Description">{{{hdescription}}}</p>'
       
   108     + '{{#created}}'
       
   109     + '<div class="Ldt-AnnotationsList-CreationDate">{{{created}}}</div>'
       
   110     + '{{/created}}'
    80     + '{{#tags.length}}'
   111     + '{{#tags.length}}'
    81     + '<ul class="Ldt-AnnotationsList-Tags">'
   112     + '<ul class="Ldt-AnnotationsList-Tags">'
    82     + '{{#tags}}'
   113     + '{{#tags}}'
    83     + '{{#.}}'
   114     + '{{#.}}'
    84     + '<li class="Ldt-AnnotationsList-Tag-Li">'
   115     + '<li class="Ldt-AnnotationsList-Tag-Li">'
    89     + '</ul>'
   120     + '</ul>'
    90     + '{{/tags.length}}'
   121     + '{{/tags.length}}'
    91     + '{{#audio}}<div class="Ldt-AnnotationsList-Play" data-annotation-id="{{id}}">{{l10n.voice_annotation}}</div>{{/audio}}'
   122     + '{{#audio}}<div class="Ldt-AnnotationsList-Play" data-annotation-id="{{id}}">{{l10n.voice_annotation}}</div>{{/audio}}'
    92     + '</li>';
   123     + '</li>';
    93 
   124 
    94 //obj.url = this.project_url + "/" + media + "/" + annotations[i].meta.project + "/" + annotations[i].meta["id-ref"] + '#id=' + annotations[i].id;
   125 // obj.url = this.project_url + "/" + media + "/" + annotations[i].meta.project
       
   126 // + "/" + annotations[i].meta["id-ref"] + '#id=' + annotations[i].id;
    95 
   127 
    96 IriSP.Widgets.AnnotationsList.prototype.ajaxSource = function() {
   128 IriSP.Widgets.AnnotationsList.prototype.ajaxSource = function() {
    97     var _currentTime = this.media.getCurrentTime(),
   129     var _currentTime = this.media.getCurrentTime(),
    98         _duration = this.media.duration;
   130         _duration = this.media.duration;
    99     this.lastAjaxQuery = _currentTime;
   131     this.lastAjaxQuery = _currentTime;
   143         }
   175         }
   144     }
   176     }
   145     _list = _list.filter(function(_annotation) {
   177     _list = _list.filter(function(_annotation) {
   146         return _annotation.found !== false;
   178         return _annotation.found !== false;
   147     });
   179     });
       
   180     if (this.filter_by_segments) {
       
   181         /*
       
   182          *  A given annotation is considered "in" segment if the middle of it is between the segment beginning and the segment end. 
       
   183          *  Note this is meant to be used for "markings" annotations (not segments)
       
   184          */
       
   185         _segmentsAnnotation = this.currentSource.getAnnotationsByTypeTitle(this.segments_annotation_type)
       
   186         _currentSegments = _segmentsAnnotation.filter(function(_segment){
       
   187             return (_currentTime >= _segment.begin && _currentTime <= _segment.end)
       
   188         });
       
   189         if (_currentSegments.length == 0) {
       
   190             _list = _list.filter(function(_annotation){
       
   191                 return false;
       
   192             });
       
   193         }
       
   194         else {
       
   195             _list = _list.filter(function(_annotation){
       
   196                 _annotation_time = (_annotation.begin+_annotation.end)/2;
       
   197                 return (_currentSegments[0].begin <= _annotation_time && _currentSegments[0].end >= _annotation_time)
       
   198             });
       
   199         }
       
   200     }
       
   201     if (this.show_only_annotation_from_user){
       
   202         _list = _list.filter(function(_annotation){
       
   203            return _annotation.creator == _this.show_only_annotation_from_user;
       
   204         });
       
   205     }
   148     if (this.limit_count) {
   206     if (this.limit_count) {
   149         /* Get the n annotations closest to current timecode */
   207         /* Get the n annotations closest to current timecode */
   150         _list = _list.sortBy(function(_annotation) {
   208         _list = _list.sortBy(function(_annotation) {
   151             return Math.abs((_annotation.begin + _annotation.end) / 2 - _currentTime);
   209             return Math.abs((_annotation.begin + _annotation.end) / 2 - _currentTime);
   152         }).slice(0, this.limit_count);
   210         }).slice(0, this.limit_count);
   188             );
   246             );
   189             var _title = "",
   247             var _title = "",
   190                 _description = _annotation.description,
   248                 _description = _annotation.description,
   191                 _thumbnail = (typeof _annotation.thumbnail !== "undefined" && _annotation.thumbnail ? _annotation.thumbnail : _this.default_thumbnail);
   249                 _thumbnail = (typeof _annotation.thumbnail !== "undefined" && _annotation.thumbnail ? _annotation.thumbnail : _this.default_thumbnail);
   192             // Update : display creator
   250             // Update : display creator
   193             if (_annotation.creator && _this.show_creator) {
   251             if (_annotation.creator) {
   194             	_title = _annotation.creator;
   252                 _title = _annotation.creator;
   195             }
   253             }
   196             if (_annotation.title) {
   254             if (_annotation.title) {
   197             	var tempTitle = _annotation.title;
   255                 var tempTitle = _annotation.title;
   198             	if( tempTitle.substr(0, _title.length + 1) == (_title + ":") ){
   256                 if( tempTitle.substr(0, _title.length + 1) == (_title + ":") ){
   199             		_title = "";
   257                     _title = "";
   200             	}
   258                 }
   201                 _title = _title + ( (_title=="") ? "" : ": ") + _annotation.title;
   259                 _title = _title + ( (_title=="") ? "" : ": ") + _annotation.title;
   202             }
   260             }
   203             var _bgcolor;
   261             var _bgcolor;
   204             IriSP._(_this.polemics).each(function(_polemic) {
   262             IriSP._(_this.polemics).each(function(_polemic) {
   205                 var _rgxp = IriSP.Model.regexpFromTextOrArray(_polemic.keyword, true);
   263                 var _rgxp = IriSP.Model.regexpFromTextOrArray(_polemic.keyword, true);
   206                 if (_rgxp.test(_title + " " + _description)) {
   264                 if (_rgxp.test(_title + " " + _description)) {
   207                     _bgcolor = _polemic.background_color;
   265                     _bgcolor = _polemic.background_color;
   208                 }
   266                 }
   209             });
   267             });
       
   268             var _created = false;
       
   269             if (_this.show_creation_date) {
       
   270                 _created = _annotation.created.toLocaleDateString()+", "+_annotation.created.toLocaleTimeString();
       
   271             }
       
   272             if(this.tags == true){
       
   273                 var _tags = _annotation.getTagTexts();
       
   274             }
       
   275             else {
       
   276                 var _tags = false;
       
   277             }
   210             var _data = {
   278             var _data = {
   211                 id : _annotation.id,
   279                 id : _annotation.id,
   212                 media_id : _annotation.getMedia().id,
   280                 media_id : _annotation.getMedia().id,
   213                 atitle: IriSP.textFieldHtml(_annotation.title),
       
   214                 htitle : IriSP.textFieldHtml(_title),
   281                 htitle : IriSP.textFieldHtml(_title),
   215                 hdescription : IriSP.textFieldHtml(_description),
   282                 hdescription : IriSP.textFieldHtml(_description),
   216                 begin : _annotation.begin.toString(),
   283                 begin : _annotation.begin.toString(),
   217                 end : _annotation.end.toString(),
   284                 end : _annotation.end.toString(),
   218                 begin_ms : _annotation.begin.milliseconds,
   285                 created : _created,
   219                 end_ms : _annotation.end.milliseconds,
   286                 show_timecode : _this.show_timecode,
   220                 thumbnail : _thumbnail,
   287                 thumbnail : _thumbnail,
   221                 url : _url,
   288                 url : _url,
   222                 tags : _annotation.getTagTexts(),
   289                 tags : _tags,
   223                 specific_style : (typeof _bgcolor !== "undefined" ? "background-color: " + _bgcolor : ""),
   290                 specific_style : (typeof _bgcolor !== "undefined" ? "background-color: " + _bgcolor : ""),
   224                 l10n: _this.l10n
   291                 l10n: _this.l10n
   225             };
   292             };
   226             if (_this.show_controls) {
       
   227                 _this.$.find(".Ldt-AnnotationsList-Control-Prev").on("click", function (e) { e.preventDefault(); _this.navigate(-1); });
       
   228                 _this.$.find(".Ldt-AnnotationsList-Control-Next").on("click", function (e) { e.preventDefault(); _this.navigate(+1); });
       
   229            }
       
   230             if (_this.show_audio && _annotation.audio && _annotation.audio.href && _annotation.audio.href != "null") {
   293             if (_this.show_audio && _annotation.audio && _annotation.audio.href && _annotation.audio.href != "null") {
   231                 _data.audio = true;
   294                 _data.audio = true;
   232                 if (!_this.jwplayers[_annotation.id]) {
   295                 if (!_this.jwplayers[_annotation.id]) {
   233                     var _audiofile = _annotation.audio.href;
   296                     var _audiofile = _annotation.audio.href;
   234                     if (_this.audio_url_transform) {
   297                     if (_this.audio_url_transform) {
   271                 .mouseout(function() {
   334                 .mouseout(function() {
   272                     _annotation.trigger("unselect");
   335                     _annotation.trigger("unselect");
   273                 })
   336                 })
   274                 .appendTo(_this.list_$);
   337                 .appendTo(_this.list_$);
   275             IriSP.attachDndData(_el.find("[draggable]"), {
   338             IriSP.attachDndData(_el.find("[draggable]"), {
   276             	title: _title,
   339                 title: _title,
   277             	description: _description,
   340                 description: _description,
   278             	uri: _url,
   341                 uri: _url,
   279             	image: _annotation.thumbnail
   342                 image: _annotation.thumbnail
   280             });
   343             });
   281             _el.on("remove", function() {
   344             _el.on("remove", function() {
   282                 _annotation.off("select", _onselect);
   345                 _annotation.off("select", _onselect);
   283                 _annotation.off("unselect", _onunselect);
   346                 _annotation.off("unselect", _onunselect);
   284             });
   347             });
   323             if (Math.abs(_currentTime - this.lastAjaxQuery) > (this.ajax_granularity)) {
   386             if (Math.abs(_currentTime - this.lastAjaxQuery) > (this.ajax_granularity)) {
   324                 this.ajaxSource();
   387                 this.ajaxSource();
   325             }
   388             }
   326         }
   389         }
   327     }
   390     }
       
   391     
   328     return _list.length;
   392     return _list.length;
       
   393 };
       
   394 
       
   395 IriSP.Widgets.AnnotationsList.prototype.hide = function() {
       
   396     if (this.visible){
       
   397         this.visible = false;
       
   398         this.widget_$.slideUp()
       
   399     }
       
   400 }
       
   401 
       
   402 IriSP.Widgets.AnnotationsList.prototype.show = function() {
       
   403     if(!this.visible){
       
   404         this.visible = true;
       
   405         this.widget_$.slideDown()
       
   406     }
       
   407 }
       
   408 
       
   409 
       
   410 IriSP.Widgets.AnnotationsList.prototype.toggle = function() {
       
   411     if (!this.always_visible) {
       
   412         if (this.visible) {
       
   413             this.hide();
       
   414         } else {
       
   415             this.show();
       
   416         }
       
   417     }
   329 };
   418 };
   330 
   419 
   331 IriSP.Widgets.AnnotationsList.prototype.draw = function() {
   420 IriSP.Widgets.AnnotationsList.prototype.draw = function() {
   332     
   421     
   333     this.jwplayers = {};
   422     this.jwplayers = {};
   336     this.renderTemplate();
   425     this.renderTemplate();
   337     
   426     
   338     var _this = this;
   427     var _this = this;
   339         
   428         
   340     this.list_$ = this.$.find(".Ldt-AnnotationsList-ul");
   429     this.list_$ = this.$.find(".Ldt-AnnotationsList-ul");
       
   430     this.widget_$ = this.$.find(".Ldt-AnnotationsListWidget");
   341     
   431     
   342     
   432     
   343     this.source.getAnnotations().on("search", function(_text) {
   433     this.source.getAnnotations().on("search", function(_text) {
   344         _this.searchString = _text;
   434         _this.searchString = _text;
   345         if (_this.source !== _this.currentSource) {
   435         if (_this.source !== _this.currentSource) {
   388         window.setInterval(function() {
   478         window.setInterval(function() {
   389             _this.currentSource.get();
   479             _this.currentSource.get();
   390         }, this.refresh_interval);
   480         }, this.refresh_interval);
   391     }
   481     }
   392     
   482     
   393     this.onMdpEvent("createAnnotationWidget.addedAnnotation");
   483     this.onMdpEvent("AnnotationsList.toggle","toggle");
       
   484     this.onMdpEvent("AnnotationsList.hide", "hide");
       
   485     this.onMdpEvent("AnnotationsList.show", "show");
       
   486     
       
   487     this.onMdpEvent("createAnnotationWidget.addedAnnotation", "refresh");
   394     var _events = [
   488     var _events = [
   395         "timeupdate",
   489         "timeupdate",
   396         "seeked",
   490         "seeked",
   397         "loadedmetadata"
   491         "loadedmetadata"
   398     ];
   492     ];
   399     for (var _i = 0; _i < _events.length; _i++) {
   493     for (var _i = 0; _i < _events.length; _i++) {
   400         this.onMediaEvent(_events[_i], this.throttledRefresh);
   494         this.onMediaEvent(_events[_i], this.throttledRefresh);
   401     }
   495     }
   402     
   496     
   403     this.throttledRefresh();
   497     this.throttledRefresh();
   404 
   498     
   405 };
   499     this.visible = true;
       
   500     if (!this.start_visible){
       
   501         this.hide();
       
   502     }
       
   503     
       
   504 };