src/js/widgets/annotationsListWidget.js
branchnew-model
changeset 875 43629caa77bc
parent 874 38b65761a7d5
child 876 03967b6ada7c
equal deleted inserted replaced
874:38b65761a7d5 875:43629caa77bc
     1 IriSP.AnnotationsListWidget = function(player, config) {
       
     2     IriSP.Widget.call(this, player, config);
       
     3     this.bindPopcorn("IriSP.search", "searchHandler");
       
     4     this.bindPopcorn("IriSP.search.closed", "searchHandler");
       
     5     this.bindPopcorn("IriSP.search.cleared", "searchHandler");
       
     6     this.searchString = false;
       
     7     this.lastIds = [];
       
     8     var _this = this;
       
     9     this.throttledRefresh = IriSP._.throttle(function() {
       
    10         _this.refresh(false);
       
    11     }, 1500);
       
    12 };
       
    13 
       
    14 IriSP.AnnotationsListWidget.prototype = new IriSP.Widget();
       
    15 
       
    16 IriSP.AnnotationsListWidget.prototype.clear = function() {
       
    17 };
       
    18 
       
    19 IriSP.AnnotationsListWidget.prototype.clearWidget = function() {
       
    20 };
       
    21 
       
    22 IriSP.AnnotationsListWidget.prototype.searchHandler = function(searchString) {
       
    23     this.searchString = typeof searchString !== "undefined" ? searchString : '';
       
    24     var _n = this.refresh(true);
       
    25     if (this.searchString) {
       
    26         if (_n) {
       
    27             this.player.popcorn.trigger("IriSP.search.matchFound");
       
    28         } else {
       
    29             this.player.popcorn.trigger("IriSP.search.noMatchFound");
       
    30         }
       
    31     }
       
    32 }
       
    33 
       
    34 //obj.url = this.project_url + "/" + media + "/" + annotations[i].meta.project + "/" + annotations[i].meta["id-ref"] + '#id=' + annotations[i].id;
       
    35 
       
    36 IriSP.AnnotationsListWidget.prototype.ajaxSource = function() {
       
    37     var _currentTime = this.player.popcorn.currentTime(),
       
    38         _duration = this.source.getDuration();
       
    39     if (typeof _currentTime == "undefined") {
       
    40         _currentTime = 0;
       
    41     }
       
    42     this.lastAjaxQuery = _currentTime;
       
    43     _currentTime = Math.floor(1000 * _currentTime);
       
    44     var _url = Mustache.to_html(this.ajax_url, {
       
    45         media : this.source.currentMedia.namespacedId.name,
       
    46         begin : Math.max(0, _currentTime - this.ajax_granularity),
       
    47         end : Math.min(_duration.milliseconds, _currentTime + this.ajax_granularity)
       
    48     });
       
    49     this.currentSource = this.player.loadMetadata(IriSP._.defaults({
       
    50         "url" : _url
       
    51     }, this.metadata));
       
    52 }
       
    53 
       
    54 IriSP.AnnotationsListWidget.prototype.refresh = function(_forceRedraw) {
       
    55     _forceRedraw = (typeof _forceRedraw !== "undefined" && _forceRedraw);
       
    56     if (this.currentSource.status !== IriSP.Model._SOURCE_STATUS_READY) {
       
    57         return 0;
       
    58     }
       
    59     var _this = this,
       
    60         _currentTime = this.player.popcorn.currentTime();
       
    61     if (typeof _currentTime == "undefined") {
       
    62         _currentTime = 0;
       
    63     }
       
    64     var _list = this.annotation_type ? this.currentSource.getAnnotationsByTypeTitle(this.annotation_type, true) : this.currentSource.getAnnotations();
       
    65     if (this.searchString) {
       
    66         _list = _list.searchByTextFields(this.searchString);
       
    67     }
       
    68     if (this.limit_count) {
       
    69         _list = _list.sortBy(function(_annotation) {
       
    70             return Math.abs(_annotation.begin.getSeconds() - _currentTime);
       
    71         }).slice(0, this.limit_count)
       
    72     }
       
    73     if (this.newest_first) {
       
    74         _list = _list.sortBy(function(_annotation) {
       
    75             return -_annotation.created.valueOf();
       
    76         });
       
    77     } else {
       
    78         _list = _list.sortBy(function(_annotation) {
       
    79             return _annotation.begin;
       
    80         });
       
    81     }
       
    82     
       
    83     var _ids = _list.idIndex;
       
    84     
       
    85     if (_forceRedraw || !IriSP._.isEqual(_ids, this.lastIds)) {
       
    86         /* This part only gets executed if the list needs updating */
       
    87         this.lastIds = _ids;
       
    88        
       
    89         var _html = IriSP.templToHTML(
       
    90             IriSP.annotationsListWidget_template,
       
    91             {
       
    92                 annotations : _list.map(function(_annotation) {
       
    93                     var _url = (
       
    94                         ( typeof _annotation.url !== "undefined" )
       
    95                         ? _annotation.url
       
    96                         : (
       
    97                             ( typeof _this.source.projectId !== "undefined" && typeof _annotation.project !== "undefined" && _this.source.projectId !== _annotation.project )
       
    98                             ? Mustache.to_html(
       
    99                                 this.foreign_url,
       
   100                                 {
       
   101                                     project : _annotation.project,
       
   102                                     media : _annotation.media.id.replace(/^.*:/,''),
       
   103                                     annotation : _annotation.namespacedId.name,
       
   104                                     annotationType : _annotation.annotationType.id.replace(/^.*:/,'')
       
   105                                 }
       
   106                             )
       
   107                             : '#id=' + _annotation.namespacedId.name
       
   108                             )
       
   109                     );
       
   110                     var _res = {
       
   111                         id : _annotation.id,
       
   112                         title : _annotation.title.replace(_annotation.description,''),
       
   113                         description : _annotation.description,
       
   114                         begin : _annotation.begin.toString(),
       
   115                         end : _annotation.end.toString(),
       
   116                         thumbnail : typeof _annotation.thumbnail !== "undefined" ? _annotation.thumbnail : _this.default_thumbnail,
       
   117                         url : _url,
       
   118                         tags : _annotation.getTagTexts()
       
   119                     }
       
   120                     return _res;
       
   121                 })
       
   122             });
       
   123     
       
   124         this.$.html(_html);
       
   125     
       
   126         this.$.find('.Ldt-AnnotationsList-Tag-Li').click(function() {
       
   127             _this.player.popcorn.trigger("IriSP.search.triggeredSearch", IriSP.jQuery(this).text().replace(/(^\s+|\s+$)/g,''));
       
   128         })
       
   129         
       
   130         if(this.searchString) {
       
   131             var _searchRe = new RegExp('(' + this.searchString.replace(/(\W)/gm,'\\$1') + ')','gim');
       
   132             this.$.find(".Ldt-AnnotationsList-Title a, .Ldt-AnnotationsList-Description").each(function() {
       
   133                 var _$ = IriSP.jQuery(this);
       
   134                 _$.html(_$.text().replace(/(^\s+|\s+$)/g,'').replace(_searchRe, '<span class="Ldt-AnnotationsList-highlight">$1</span>'))
       
   135             })
       
   136         }
       
   137     }
       
   138     
       
   139     if (this.ajax_url && this.ajax_granularity) {
       
   140         if (Math.abs(_currentTime - this.lastAjaxQuery) > (this.ajax_granularity / 2000)) {
       
   141             this.ajaxSource();
       
   142         }
       
   143     }
       
   144     return _list.length;
       
   145 }
       
   146 
       
   147 IriSP.AnnotationsListWidget.prototype.draw = function() {
       
   148     var _this = this;
       
   149     
       
   150     if (this.ajax_url && this.ajax_granularity) {
       
   151         this.ajaxSource();
       
   152     } else {
       
   153         this.currentSource = this.source;
       
   154     }
       
   155     
       
   156     if (this.refresh_interval) {
       
   157         window.setInterval(function() {
       
   158             _this.currentSource.get()
       
   159         }, this.refresh_interval);
       
   160     }
       
   161     
       
   162     var _events = [
       
   163         "IriSP.createAnnotationWidget.addedAnnotation",
       
   164         "timeupdate",
       
   165         "seeked",
       
   166         "loadedmetadata"
       
   167     ];
       
   168     for (var _i = 0; _i < _events.length; _i++) {
       
   169         this.player.popcorn.listen(_events[_i], this.throttledRefresh);
       
   170     }
       
   171     
       
   172     this.throttledRefresh();
       
   173 
       
   174 };