src/js/widgets/annotationsListWidget.js
branchnew-model
changeset 872 d777d05a16e4
parent 870 2c025db10a10
child 874 38b65761a7d5
equal deleted inserted replaced
870:2c025db10a10 872:d777d05a16e4
    28         } else {
    28         } else {
    29             this.player.popcorn.trigger("IriSP.search.noMatchFound");
    29             this.player.popcorn.trigger("IriSP.search.noMatchFound");
    30         }
    30         }
    31     }
    31     }
    32 }
    32 }
    33 /** effectively redraw the widget - called by drawList */
       
    34 IriSP.AnnotationsListWidget.prototype.do_redraw = function(list) {
       
    35 /*    var _html = IriSP.templToHTML(IriSP.annotationsListWidget_template, {
       
    36         annotations : list
       
    37     }), _this = this;
       
    38 
    33 
    39     this.selector.html(_html);
    34 //obj.url = this.project_url + "/" + media + "/" + annotations[i].meta.project + "/" + annotations[i].meta["id-ref"] + '#id=' + annotations[i].id;
    40 
       
    41     this.selector.find('.Ldt-AnnotationsList-Tag-Li').click(function() {
       
    42         _this.player.popcorn.trigger("IriSP.search.triggeredSearch", IriSP.jQuery(this).text().trim());
       
    43     })
       
    44     if(this.searchRe !== null) {
       
    45         this.selector.find(".Ldt-AnnotationsList-Title a, .Ldt-AnnotationsList-Description").each(function() {
       
    46             var _$ = IriSP.jQuery(this);
       
    47             _$.html(_$.text().trim().replace(_this.searchRe, '<span class="Ldt-AnnotationsList-highlight">$1</span>'))
       
    48         })
       
    49     } */
       
    50 };
       
    51 
       
    52 IriSP.AnnotationsListWidget.prototype.transformAnnotation = function(a) {
       
    53 /*    var _this = this;
       
    54     return {
       
    55         "id" : a.id,
       
    56         "title" : this.cinecast_version ? IriSP.get_aliased(a.meta, ['creator_name', 'creator']) : a.content.title,
       
    57         "desc" : this.cinecast_version ? a.content.data : a.content.description,
       
    58         "begin" : IriSP.msToTime(a.begin),
       
    59         "end" : IriSP.msToTime(a.end),
       
    60         "thumbnail" : ( typeof a.meta == "object" && typeof a.meta.thumbnail == "string") ? a.meta.thumbnail : this.default_thumbnail,
       
    61         "url" : ( typeof a.meta == "object" && typeof a.meta.url == "string") ? a.meta.url : null,
       
    62         "created_at" : ( typeof a.meta == "object" && typeof a.meta.created == "string") ? Date.parse(a.meta.created.replace(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}\:\d{2}\:\d{2}).*$/, "$2/$3/$1 $4 UTC+0000")) : null,
       
    63         "tags" : typeof a.tags == "object" ? IriSP.underscore(a.tags).chain().map(function(_t) {
       
    64             if( typeof _t == "string") {
       
    65                 return _t.replace(/^.*:/, '#');
       
    66             } else {
       
    67                 if( typeof _t['id-ref'] != "undefined") {
       
    68                     var _f = IriSP.underscore.find(_this._serializer._data.tags, function(_tag) {
       
    69                         return _tag.id == _t['id-ref'];
       
    70                     });
       
    71                     if( typeof _f != "undefined") {
       
    72                         return IriSP.get_aliased(_f.meta, ['dc:title', 'title']);
       
    73                     }
       
    74                 }
       
    75             }
       
    76             return null;
       
    77         }).filter(function(_t) {
       
    78             return _t !== null && _t !== ""
       
    79         }).value() : []
       
    80     } */
       
    81 }
       
    82 /** draw the annotation list */
       
    83 IriSP.AnnotationsListWidget.prototype.drawList = function(force_redraw) {
       
    84 /*    var _this = this;
       
    85 
       
    86     //  var view_type = this._serializer.getContributions();
       
    87     var annotations = this._serializer._data.annotations;
       
    88     var currentTime = this.player.popcorn.currentTime();
       
    89     var list = [];
       
    90 
       
    91     for( i = 0; i < annotations.length; i++) {
       
    92         var obj = this.transformAnnotation(annotations[i]);
       
    93         obj.iterator = i;
       
    94         obj.distance = Math.abs((annotations[i].end + annotations[i].begin) / 2000 - currentTime);
       
    95         if(!this.cinecast_version || annotations[i].type == "cinecast:UserAnnotation") {
       
    96             list.push(obj);
       
    97         }
       
    98 
       
    99     }
       
   100 
       
   101     if(this.searchRe !== null) {
       
   102         list = list.filter(function(_a) {
       
   103             return (_this.searchRe.test(_a.desc) || _this.searchRe.test(_a.title));
       
   104         });
       
   105         if(list.length) {
       
   106             this.player.popcorn.trigger("IriSP.search.matchFound");
       
   107         } else {
       
   108             this.player.popcorn.trigger("IriSP.search.noMatchFound");
       
   109         }
       
   110     }
       
   111     list = IriSP.underscore(list).chain().sortBy(function(_o) {
       
   112         return _o.distance;
       
   113     }).first(10).sortBy(function(_o) {
       
   114         return (_this.cinecast_version ? -_o.created_at : _o.iterator);
       
   115     }).value();
       
   116     var idList = IriSP.underscore.pluck(list, "id").sort();
       
   117 
       
   118     if(!IriSP.underscore.isEqual(this.__oldList, idList) || this.lastSearch !== this.searchRe || typeof (force_redraw) !== "undefined") {
       
   119         this.do_redraw(list);
       
   120         this.__oldList = idList;
       
   121         this.lastSearch = this.searchRe;
       
   122     }
       
   123     /* save for next call */
       
   124 
       
   125 };
       
   126 
       
   127 IriSP.AnnotationsListWidget.prototype.ajaxRedraw = function(timecode) {
       
   128 
       
   129     /* the seeked signal sometimes passes an argument - depending on if we're using
       
   130      our popcorn lookalike or the real thing - if it's the case, use it as it's
       
   131      more precise than currentTime which sometimes contains the place we where at */
       
   132     if(IriSP.null_or_undefined(timecode) || typeof (timecode) != "number") {
       
   133         var tcode = this.player.popcorn.currentTime();
       
   134     } else {
       
   135         var tcode = timecode;
       
   136     }
       
   137 
       
   138     /* the platform gives us a special url - of the type : http://path/{{media}}/{{begin}}/{{end}}
       
   139      we double the braces using regexps and we feed it to mustache to build the correct url
       
   140      we have to do that because the platform only knows at run time what view it's displaying.
       
   141      */
       
   142 
       
   143     var media_id = this.currentMedia()["id"];
       
   144     var duration = this.getDuration();
       
   145 
       
   146     var begin_timecode = (Math.floor(tcode) - 300) * 1000;
       
   147     if(begin_timecode < 0)
       
   148         begin_timecode = 0;
       
   149 
       
   150     var end_timecode = (Math.floor(tcode) + 300) * 1000;
       
   151     if(end_timecode > duration)
       
   152         end_timecode = duration;
       
   153 
       
   154     var templ = Mustache.to_html(this.ajax_url, {
       
   155         media : media_id,
       
   156         begin : begin_timecode,
       
   157         end : end_timecode
       
   158     });
       
   159 
       
   160     /* we create on the fly a serializer to get the ajax */
       
   161     var serializer = new IriSP.JSONSerializer(IriSP.__dataloader, templ);
       
   162     serializer.sync(IriSP.wrap(this, function(json) {
       
   163         this.processJson(json, serializer)
       
   164     }));
       
   165 };
       
   166 /** process the received json - it's a bit hackish */
       
   167 IriSP.AnnotationsListWidget.prototype.processJson = function(json, serializer) {
       
   168     /* FIXME: DRY the whole thing */
       
   169 /*    var annotations = serializer._data.annotations;
       
   170     if(IriSP.null_or_undefined(annotations))
       
   171         return;
       
   172 
       
   173     /*
       
   174      commented in case we wanted to discriminate against some annotation types.
       
   175      var view_types = serializer.getIds("Contributions");
       
   176      */
       
   177     var l = [];
       
   178 
       
   179     var media = this.currentMedia()["id"];
       
   180 
       
   181     for( i = 0; i < annotations.length; i++) {
       
   182         var obj = this.transformAnnotation(annotations[i])
       
   183         if( typeof obj.url == "undefined" || !obj.url) {
       
   184             /* only if the annotation isn't present in the document create an
       
   185              external link */
       
   186             if(this.annotations_ids.indexOf(obj.id.toLowerCase()) == -1) {
       
   187                 // braindead url; jacques didn't want to create a new one in the platform,
       
   188                 // so we append the cutting id to the url.
       
   189                 obj.url = this.project_url + "/" + media + "/" + annotations[i].meta.project + "/" + annotations[i].meta["id-ref"] + '#id=' + annotations[i].id;
       
   190 
       
   191                 // obj.url = document.location.href.split("#")[0] + "/" + annotation.meta.project;
       
   192             }
       
   193         }
       
   194         l.push(obj);
       
   195     }
       
   196     this._ajax_cache = l;
       
   197     this.do_redraw(l);
       
   198 };
       
   199 
    35 
   200 IriSP.AnnotationsListWidget.prototype.ajaxSource = function() {
    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);
   201     var _url = Mustache.to_html(this.ajax_url, {
    44     var _url = Mustache.to_html(this.ajax_url, {
   202         media : this.source.currentMedia.namespacedId.name,
    45         media : this.source.currentMedia.namespacedId.name,
   203         begin : 0,
    46         begin : Math.max(0, _currentTime - this.ajax_granularity),
   204         end : 0
    47         end : Math.min(_duration.milliseconds, _currentTime + this.ajax_granularity)
   205     });
    48     });
   206     console.log(_url);
    49     this.currentSource = this.player.loadMetadata(IriSP._.defaults({
       
    50         "url" : _url
       
    51     }, this.metadata));
   207 }
    52 }
   208 
    53 
   209 IriSP.AnnotationsListWidget.prototype.refresh = function(_forceRedraw) {
    54 IriSP.AnnotationsListWidget.prototype.refresh = function(_forceRedraw) {
       
    55     _forceRedraw = (typeof _forceRedraw !== "undefined" && _forceRedraw);
   210     if (this.currentSource.status !== IriSP.Model._SOURCE_STATUS_READY) {
    56     if (this.currentSource.status !== IriSP.Model._SOURCE_STATUS_READY) {
   211         return 0;
    57         return 0;
   212     }
    58     }
   213     var _this = this,
    59     var _this = this,
   214         _list = undefined,
       
   215         _currentTime = this.player.popcorn.currentTime();
    60         _currentTime = this.player.popcorn.currentTime();
   216     if (typeof _currentTime == "undefined") {
    61     if (typeof _currentTime == "undefined") {
   217         _currentTime = 0;
    62         _currentTime = 0;
   218     }
    63     }
   219     if (this.annotation_type) {
    64     var _list = this.annotation_type ? this.currentSource.getAnnotationsByTypeTitle(this.annotation_type, true) : this.currentSource.getAnnotations();
   220         _list = this.currentSource.getAnnotationsByTypeTitle(this.annotation_type);
       
   221     }
       
   222     if (typeof _list === "undefined") {
       
   223         _list = this.currentSource.getAnnotations();
       
   224     }
       
   225     if (this.searchString) {
    65     if (this.searchString) {
   226         _list = _list.searchByTextFields(this.searchString);
    66         _list = _list.searchByTextFields(this.searchString);
   227     }
    67     }
   228     if (this.limit_count) {
    68     if (this.limit_count) {
   229         _list = _list.sortBy(function(_annotation) {
    69         _list = _list.sortBy(function(_annotation) {
   234         _list = _list.sortBy(function(_annotation) {
    74         _list = _list.sortBy(function(_annotation) {
   235             return -_annotation.created.valueOf();
    75             return -_annotation.created.valueOf();
   236         });
    76         });
   237     } else {
    77     } else {
   238         _list = _list.sortBy(function(_annotation) {
    78         _list = _list.sortBy(function(_annotation) {
   239             return _annotation.begin.milliseconds;
    79             return _annotation.begin;
   240         });
    80         });
   241     }
    81     }
       
    82     
   242     var _ids = _list.idIndex;
    83     var _ids = _list.idIndex;
   243     if (!_forceRedraw && IriSP._.isEqual(_ids, this.lastIds)) {
    84     
   244         return _list.length;
    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         }
   245     }
   137     }
   246     
   138     
   247     /* This part only gets executed if the list needs updating */
   139     if (this.ajax_url && this.ajax_granularity) {
   248     this.lastIds = _ids;
   140         if (Math.abs(_currentTime - this.lastAjaxQuery) > this.ajax_granularity / 2) {
   249    
   141             this.ajaxSource();
   250     var _html = IriSP.templToHTML(
   142         }
   251         IriSP.annotationsListWidget_template,
       
   252         {
       
   253             annotations : _list.map(function(_annotation) {
       
   254                 var _res = {
       
   255                     id : _annotation.id,
       
   256                     title : _annotation.title.replace(_annotation.description,''),
       
   257                     description : _annotation.description,
       
   258                     begin : _annotation.begin.toString(),
       
   259                     end : _annotation.end.toString(),
       
   260                     thumbnail : typeof _annotation.thumbnail !== "undefined" ? _annotation.thumbnail : _this.default_thumbnail,
       
   261                     url : typeof _annotation.url !== "undefined" ? _annotation.thumbnail : '#' + _annotation.namespacedId.name,
       
   262                     tags : _annotation.getTagTexts()
       
   263                 }
       
   264                 return _res;
       
   265             })
       
   266         });
       
   267 
       
   268     this.$.html(_html);
       
   269 
       
   270     this.$.find('.Ldt-AnnotationsList-Tag-Li').click(function() {
       
   271         _this.player.popcorn.trigger("IriSP.search.triggeredSearch", IriSP.jQuery(this).text().replace(/(^\s+|\s+$)/g,''));
       
   272     })
       
   273     
       
   274     if(this.searchString) {
       
   275         var _searchRe = new RegExp('(' + this.searchString.replace(/(\W)/gm,'\\$1') + ')','gim');
       
   276         this.$.find(".Ldt-AnnotationsList-Title a, .Ldt-AnnotationsList-Description").each(function() {
       
   277             var _$ = IriSP.jQuery(this);
       
   278             _$.html(_$.text().replace(/(^\s+|\s+$)/g,'').replace(_searchRe, '<span class="Ldt-AnnotationsList-highlight">$1</span>'))
       
   279         })
       
   280     }
   143     }
   281     
       
   282     return _list.length;
   144     return _list.length;
   283 }
   145 }
   284 
   146 
   285 IriSP.AnnotationsListWidget.prototype.draw = function() {
   147 IriSP.AnnotationsListWidget.prototype.draw = function() {
   286     var _this = this;
   148     var _this = this;