src/js/widgets/sparklineWidget.js
author hamidouk
Wed, 01 Feb 2012 12:40:45 +0100
branchnih-youtube
changeset 757 43e261bcd4ce
parent 688 76953dd536de
child 790 9d2b6f31994d
permissions -rw-r--r--
more work on the youtube player.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
523
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
     1
/** @class The constructor for the sparkline widget */
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
     2
IriSP.SparklineWidget = function(Popcorn, config, Serializer) {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
     3
  IriSP.Widget.call(this, Popcorn, config, Serializer);
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
     4
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
     5
  this._oldAnnotation = null;
644
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
     6
  this._results = [];
523
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
     7
};
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
     8
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
     9
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    10
IriSP.SparklineWidget.prototype = new IriSP.Widget();
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    11
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    12
IriSP.SparklineWidget.prototype.clear = function() {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    13
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    14
};
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    15
524
a06527b99f22 the sparklineWidget height and width are now parametrable.
hamidouk
parents: 523
diff changeset
    16
/** draw the sparkline using jquery sparkline */
523
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    17
IriSP.SparklineWidget.prototype.draw = function() {
524
a06527b99f22 the sparklineWidget height and width are now parametrable.
hamidouk
parents: 523
diff changeset
    18
  var templ = Mustache.to_html(IriSP.SparklineWidget_template, {width: this.width, height: this.height});
523
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    19
  /** this widget uses three divs -
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    20
    the first is the sparkline, which is generated by jquery sparkline,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    21
    the second is an overlay div to display the progression in the video,
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    22
    and the third is a div to react to clicks
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    23
  */
644
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
    24
  
688
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    25
  var views = this._serializer._data.views;
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    26
  var stat_view;
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    27
  if (!IriSP.null_or_undefined(views)) {
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    28
    
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    29
    var i;
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    30
    for (i = 0; i < views.length; i++) {
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    31
      var view = views[i];
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    32
      if (view.id === "stat") {
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    33
          stat_view = view;
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    34
          break;
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    35
      }
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    36
    }
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    37
  }
600
4e669328ab6d fixed config bug.
hamidouk
parents: 532
diff changeset
    38
  
688
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    39
  // If we've found the correct view, feed the directly the data from the view
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    40
  // to jquery sparkline. Otherwise, compute it ourselves.
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    41
  if (!IriSP.null_or_undefined(stat_view)) {
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    42
    console.log("sparklinewidget : using stats embedded in the json");
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    43
    var results = stat_view.meta.stat.split(",");      
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    44
  } else {
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    45
    console.log("sparklinewidget : computing stats ourselves");
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    46
    var num_columns = (this.selector.width()) / IriSP.widgetsDefaults["SparklineWidget"].column_width;
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    47
    var duration = +this._serializer.currentMedia().meta["dc:duration"];
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    48
    var time_step = duration / num_columns; /* the time interval between two columns */
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    49
    var results = [];
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    50
    var i = 0; /* the index in the loop */  
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    51
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    52
    /* this algorithm makes one assumption : that the array is sorted 
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    53
       (it's done for us by the JSONSerializer). We go through the array 
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    54
       and count how many comments fall within a peculiar time piece.
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    55
       As i is preserved between each iteration, it's O(n).
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    56
    */
524
a06527b99f22 the sparklineWidget height and width are now parametrable.
hamidouk
parents: 523
diff changeset
    57
    
688
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    58
    for(var j = 0; j < num_columns && i < this._serializer._data.annotations.length; j++) {    
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    59
      var count = 0;
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    60
      var annotation_begin = +(this._serializer._data.annotations[i].begin);
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    61
      
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    62
      while(annotation_begin >= j * time_step && annotation_begin <= (j + 1) * time_step ) {
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    63
        count++;
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    64
        i++;
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    65
        if (i >= this._serializer._data.annotations.length)
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    66
          break;
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    67
          
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    68
        annotation_begin = +(this._serializer._data.annotations[i].begin);
524
a06527b99f22 the sparklineWidget height and width are now parametrable.
hamidouk
parents: 523
diff changeset
    69
        
688
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    70
      }
524
a06527b99f22 the sparklineWidget height and width are now parametrable.
hamidouk
parents: 523
diff changeset
    71
      
688
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    72
      results.push(count);
524
a06527b99f22 the sparklineWidget height and width are now parametrable.
hamidouk
parents: 523
diff changeset
    73
    }
a06527b99f22 the sparklineWidget height and width are now parametrable.
hamidouk
parents: 523
diff changeset
    74
  }
688
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
    75
  
644
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
    76
  // save the results in an array so that we can re-use them when a new annotation
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
    77
  // is added.
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
    78
  this._results = results;
524
a06527b99f22 the sparklineWidget height and width are now parametrable.
hamidouk
parents: 523
diff changeset
    79
  
523
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    80
  this.selector.append(templ);
524
a06527b99f22 the sparklineWidget height and width are now parametrable.
hamidouk
parents: 523
diff changeset
    81
  this.selector.find(".Ldt-sparkLine").css("background", "#c7c8cc");
a06527b99f22 the sparklineWidget height and width are now parametrable.
hamidouk
parents: 523
diff changeset
    82
  this.selector.find(".Ldt-sparkLine").sparkline(results, {lineColor: "#7492b4", fillColor: "#aeaeb8",
a06527b99f22 the sparklineWidget height and width are now parametrable.
hamidouk
parents: 523
diff changeset
    83
                                                           spotColor: "#b70056",
a06527b99f22 the sparklineWidget height and width are now parametrable.
hamidouk
parents: 523
diff changeset
    84
                                                           width: this.width, height: this.height});
523
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    85
  this._Popcorn.listen("timeupdate", IriSP.wrap(this, this.timeUpdateHandler));
644
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
    86
  this._Popcorn.listen("IriSP.createAnnotationWidget.addedAnnotation", IriSP.wrap(this, this.handleNewAnnotation));
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
    87
  
532
5249bb0cd964 cosmetical change - moved css code from jquery.css to the stylesheet.
hamidouk
parents: 529
diff changeset
    88
  IriSP.jQuery(".Ldt-sparkLineClickOverlay").click(IriSP.wrap(this, this.clickHandler));  
523
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    89
};
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    90
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    91
/** react to a timeupdate event */
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    92
IriSP.SparklineWidget.prototype.timeUpdateHandler = function() {
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    93
  var currentTime = this._Popcorn.currentTime();  
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    94
  var duration = +this._serializer.currentMedia().meta["dc:duration"] / 1000;
524
a06527b99f22 the sparklineWidget height and width are now parametrable.
hamidouk
parents: 523
diff changeset
    95
  var proportion = ((currentTime / duration) * 100).toFixed(4);
a06527b99f22 the sparklineWidget height and width are now parametrable.
hamidouk
parents: 523
diff changeset
    96
  
523
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    97
  IriSP.jQuery(".Ldt-sparkLinePositionMarker").css("width", proportion + "%");                                    
8dfeaec4724b first version of the sparkline widget.
hamidouk
parents:
diff changeset
    98
}
529
3ebf62837492 react to clicks on the curve.
hamidouk
parents: 524
diff changeset
    99
3ebf62837492 react to clicks on the curve.
hamidouk
parents: 524
diff changeset
   100
/** handle clicks on the widget */
3ebf62837492 react to clicks on the curve.
hamidouk
parents: 524
diff changeset
   101
IriSP.SparklineWidget.prototype.clickHandler = function(event) {
3ebf62837492 react to clicks on the curve.
hamidouk
parents: 524
diff changeset
   102
  /* this piece of code is a little bit convoluted - here's how it works :
3ebf62837492 react to clicks on the curve.
hamidouk
parents: 524
diff changeset
   103
     we want to handle clicks on the progress bar and convert those to seeks in the media.
3ebf62837492 react to clicks on the curve.
hamidouk
parents: 524
diff changeset
   104
     However, jquery only gives us a global position, and we want a number of pixels relative
3ebf62837492 react to clicks on the curve.
hamidouk
parents: 524
diff changeset
   105
     to our container div, so we get the parent position, and compute an offset to this position,
3ebf62837492 react to clicks on the curve.
hamidouk
parents: 524
diff changeset
   106
     and finally compute the progress ratio in the media.
3ebf62837492 react to clicks on the curve.
hamidouk
parents: 524
diff changeset
   107
     Finally we multiply this ratio with the duration to get the correct time
3ebf62837492 react to clicks on the curve.
hamidouk
parents: 524
diff changeset
   108
  */
3ebf62837492 react to clicks on the curve.
hamidouk
parents: 524
diff changeset
   109
3ebf62837492 react to clicks on the curve.
hamidouk
parents: 524
diff changeset
   110
  var parentOffset = this.selector.offset();
3ebf62837492 react to clicks on the curve.
hamidouk
parents: 524
diff changeset
   111
  var width = this.selector.width();
3ebf62837492 react to clicks on the curve.
hamidouk
parents: 524
diff changeset
   112
  var relX = event.pageX - parentOffset.left;
3ebf62837492 react to clicks on the curve.
hamidouk
parents: 524
diff changeset
   113
3ebf62837492 react to clicks on the curve.
hamidouk
parents: 524
diff changeset
   114
  var duration = this._serializer.currentMedia().meta["dc:duration"] / 1000;
3ebf62837492 react to clicks on the curve.
hamidouk
parents: 524
diff changeset
   115
  var newTime = ((relX / width) * duration).toFixed(2);
688
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
   116
    
532
5249bb0cd964 cosmetical change - moved css code from jquery.css to the stylesheet.
hamidouk
parents: 529
diff changeset
   117
  this._Popcorn.trigger("IriSP.SparklineWidget.clicked", newTime);
529
3ebf62837492 react to clicks on the curve.
hamidouk
parents: 524
diff changeset
   118
  this._Popcorn.currentTime(newTime);                                 
644
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
   119
};
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
   120
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
   121
/** react when a new annotation is added */
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
   122
IriSP.SparklineWidget.prototype.handleNewAnnotation = function(annotation) {
688
76953dd536de use data from stat view when available.
hamidouk
parents: 645
diff changeset
   123
  var num_columns = this._results.length;
644
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
   124
  var duration = +this._serializer.currentMedia().meta["dc:duration"];
645
0c8ca890c9f1 fixed update bug.
hamidouk
parents: 644
diff changeset
   125
  var time_step = Math.round(duration / num_columns); /* the time interval between two columns */
644
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
   126
  var begin = +annotation.begin;
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
   127
  
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
   128
  var index = Math.floor(begin / time_step);
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
   129
  this._results[index]++;
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
   130
  this.selector.find(".Ldt-sparkLine").sparkline(this._results, {lineColor: "#7492b4", fillColor: "#aeaeb8",
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
   131
                                                           spotColor: "#b70056",
492d3c8d776a WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents: 600
diff changeset
   132
                                                           width: this.width, height: this.height});
529
3ebf62837492 react to clicks on the curve.
hamidouk
parents: 524
diff changeset
   133
};