src/js/widgets/sparklineWidget.js
branchpopcorn-port
changeset 688 76953dd536de
parent 645 0c8ca890c9f1
child 790 9d2b6f31994d
equal deleted inserted replaced
687:3583e8b447f5 688:76953dd536de
    20     the first is the sparkline, which is generated by jquery sparkline,
    20     the first is the sparkline, which is generated by jquery sparkline,
    21     the second is an overlay div to display the progression in the video,
    21     the second is an overlay div to display the progression in the video,
    22     and the third is a div to react to clicks
    22     and the third is a div to react to clicks
    23   */
    23   */
    24   
    24   
    25   var num_columns = (this.selector.width()) / IriSP.widgetsDefaults["SparklineWidget"].column_width;
    25   var views = this._serializer._data.views;
    26   var duration = +this._serializer.currentMedia().meta["dc:duration"];
    26   var stat_view;
    27   var time_step = duration / num_columns; /* the time interval between two columns */
    27   if (!IriSP.null_or_undefined(views)) {
    28   var results = [];
    28     
    29   var i = 0; /* the index in the loop */  
    29     var i;
       
    30     for (i = 0; i < views.length; i++) {
       
    31       var view = views[i];
       
    32       if (view.id === "stat") {
       
    33           stat_view = view;
       
    34           break;
       
    35       }
       
    36     }
       
    37   }
       
    38   
       
    39   // If we've found the correct view, feed the directly the data from the view
       
    40   // to jquery sparkline. Otherwise, compute it ourselves.
       
    41   if (!IriSP.null_or_undefined(stat_view)) {
       
    42     console.log("sparklinewidget : using stats embedded in the json");
       
    43     var results = stat_view.meta.stat.split(",");      
       
    44   } else {
       
    45     console.log("sparklinewidget : computing stats ourselves");
       
    46     var num_columns = (this.selector.width()) / IriSP.widgetsDefaults["SparklineWidget"].column_width;
       
    47     var duration = +this._serializer.currentMedia().meta["dc:duration"];
       
    48     var time_step = duration / num_columns; /* the time interval between two columns */
       
    49     var results = [];
       
    50     var i = 0; /* the index in the loop */  
    30 
    51 
    31   /* this algorithm makes one assumption : that the array is sorted 
    52     /* this algorithm makes one assumption : that the array is sorted 
    32      (it's done for us by the JSONSerializer). We go through the array 
    53        (it's done for us by the JSONSerializer). We go through the array 
    33      and count how many comments fall within a peculiar time piece.
    54        and count how many comments fall within a peculiar time piece.
    34      As i is preserved between each iteration, it's O(n).
    55        As i is preserved between each iteration, it's O(n).
    35   */
    56     */
       
    57     
       
    58     for(var j = 0; j < num_columns && i < this._serializer._data.annotations.length; j++) {    
       
    59       var count = 0;
       
    60       var annotation_begin = +(this._serializer._data.annotations[i].begin);
       
    61       
       
    62       while(annotation_begin >= j * time_step && annotation_begin <= (j + 1) * time_step ) {
       
    63         count++;
       
    64         i++;
       
    65         if (i >= this._serializer._data.annotations.length)
       
    66           break;
       
    67           
       
    68         annotation_begin = +(this._serializer._data.annotations[i].begin);
       
    69         
       
    70       }
       
    71       
       
    72       results.push(count);
       
    73     }
       
    74   }
    36   
    75   
    37   for(var j = 0; j < num_columns && i < this._serializer._data.annotations.length; j++) {    
       
    38     var count = 0;
       
    39     var annotation_begin = +(this._serializer._data.annotations[i].begin);
       
    40     
       
    41     while(annotation_begin >= j * time_step && annotation_begin <= (j + 1) * time_step ) {
       
    42       count++;
       
    43       i++;
       
    44       if (i >= this._serializer._data.annotations.length)
       
    45         break;
       
    46         
       
    47       annotation_begin = +(this._serializer._data.annotations[i].begin);
       
    48       
       
    49     }
       
    50     
       
    51     results.push(count);
       
    52   }
       
    53 
       
    54   // save the results in an array so that we can re-use them when a new annotation
    76   // save the results in an array so that we can re-use them when a new annotation
    55   // is added.
    77   // is added.
    56   this._results = results;
    78   this._results = results;
    57   
    79   
    58   this.selector.append(templ);
    80   this.selector.append(templ);
    89   var width = this.selector.width();
   111   var width = this.selector.width();
    90   var relX = event.pageX - parentOffset.left;
   112   var relX = event.pageX - parentOffset.left;
    91 
   113 
    92   var duration = this._serializer.currentMedia().meta["dc:duration"] / 1000;
   114   var duration = this._serializer.currentMedia().meta["dc:duration"] / 1000;
    93   var newTime = ((relX / width) * duration).toFixed(2);
   115   var newTime = ((relX / width) * duration).toFixed(2);
    94   
   116     
    95   
       
    96   this._Popcorn.trigger("IriSP.SparklineWidget.clicked", newTime);
   117   this._Popcorn.trigger("IriSP.SparklineWidget.clicked", newTime);
    97   this._Popcorn.currentTime(newTime);                                 
   118   this._Popcorn.currentTime(newTime);                                 
    98 };
   119 };
    99 
   120 
   100 /** react when a new annotation is added */
   121 /** react when a new annotation is added */
   101 IriSP.SparklineWidget.prototype.handleNewAnnotation = function(annotation) {
   122 IriSP.SparklineWidget.prototype.handleNewAnnotation = function(annotation) {
   102   var num_columns = (this.selector.width()) / IriSP.widgetsDefaults["SparklineWidget"].column_width;
   123   var num_columns = this._results.length;
   103   var duration = +this._serializer.currentMedia().meta["dc:duration"];
   124   var duration = +this._serializer.currentMedia().meta["dc:duration"];
   104   var time_step = Math.round(duration / num_columns); /* the time interval between two columns */
   125   var time_step = Math.round(duration / num_columns); /* the time interval between two columns */
   105   var begin = +annotation.begin;
   126   var begin = +annotation.begin;
   106   
   127   
   107   var index = Math.floor(begin / time_step);
   128   var index = Math.floor(begin / time_step);