| author | hamidouk |
| Tue, 17 Jan 2012 11:00:33 +0100 | |
| branch | popcorn-port |
| changeset 645 | 0c8ca890c9f1 |
| parent 644 | 492d3c8d776a |
| child 688 | 76953dd536de |
| permissions | -rw-r--r-- |
| 523 | 1 |
/** @class The constructor for the sparkline widget */ |
2 |
IriSP.SparklineWidget = function(Popcorn, config, Serializer) { |
|
3 |
IriSP.Widget.call(this, Popcorn, config, Serializer); |
|
4 |
||
5 |
this._oldAnnotation = null; |
|
|
644
492d3c8d776a
WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents:
600
diff
changeset
|
6 |
this._results = []; |
| 523 | 7 |
}; |
8 |
||
9 |
||
10 |
IriSP.SparklineWidget.prototype = new IriSP.Widget(); |
|
11 |
||
12 |
IriSP.SparklineWidget.prototype.clear = function() { |
|
13 |
||
14 |
}; |
|
15 |
||
|
524
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
16 |
/** draw the sparkline using jquery sparkline */ |
| 523 | 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 | 19 |
/** this widget uses three divs - |
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, |
|
22 |
and the third is a div to react to clicks |
|
23 |
*/ |
|
|
644
492d3c8d776a
WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents:
600
diff
changeset
|
24 |
|
|
492d3c8d776a
WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents:
600
diff
changeset
|
25 |
var num_columns = (this.selector.width()) / IriSP.widgetsDefaults["SparklineWidget"].column_width; |
|
524
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
26 |
var duration = +this._serializer.currentMedia().meta["dc:duration"]; |
|
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
27 |
var time_step = duration / num_columns; /* the time interval between two columns */ |
|
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
28 |
var results = []; |
| 600 | 29 |
var i = 0; /* the index in the loop */ |
|
524
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
30 |
|
|
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
31 |
/* this algorithm makes one assumption : that the array is sorted |
|
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
32 |
(it's done for us by the JSONSerializer). We go through the array |
|
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
33 |
and count how many comments fall within a peculiar time piece. |
|
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
34 |
As i is preserved between each iteration, it's O(n). |
|
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
35 |
*/ |
| 600 | 36 |
|
37 |
for(var j = 0; j < num_columns && i < this._serializer._data.annotations.length; j++) { |
|
|
524
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
38 |
var count = 0; |
|
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
39 |
var annotation_begin = +(this._serializer._data.annotations[i].begin); |
|
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
40 |
|
|
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
41 |
while(annotation_begin >= j * time_step && annotation_begin <= (j + 1) * time_step ) { |
|
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
42 |
count++; |
|
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
43 |
i++; |
|
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
44 |
if (i >= this._serializer._data.annotations.length) |
|
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
45 |
break; |
|
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
46 |
|
|
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
47 |
annotation_begin = +(this._serializer._data.annotations[i].begin); |
|
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
48 |
|
|
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
49 |
} |
|
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
50 |
|
|
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
51 |
results.push(count); |
|
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
52 |
} |
| 600 | 53 |
|
|
644
492d3c8d776a
WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents:
600
diff
changeset
|
54 |
// 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
|
55 |
// is added. |
|
492d3c8d776a
WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents:
600
diff
changeset
|
56 |
this._results = results; |
|
524
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
57 |
|
| 523 | 58 |
this.selector.append(templ); |
|
524
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
59 |
this.selector.find(".Ldt-sparkLine").css("background", "#c7c8cc"); |
|
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
60 |
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
|
61 |
spotColor: "#b70056", |
|
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
62 |
width: this.width, height: this.height}); |
| 523 | 63 |
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
|
64 |
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
|
65 |
|
|
532
5249bb0cd964
cosmetical change - moved css code from jquery.css to the stylesheet.
hamidouk
parents:
529
diff
changeset
|
66 |
IriSP.jQuery(".Ldt-sparkLineClickOverlay").click(IriSP.wrap(this, this.clickHandler)); |
| 523 | 67 |
}; |
68 |
||
69 |
/** react to a timeupdate event */ |
|
70 |
IriSP.SparklineWidget.prototype.timeUpdateHandler = function() { |
|
71 |
var currentTime = this._Popcorn.currentTime(); |
|
72 |
var duration = +this._serializer.currentMedia().meta["dc:duration"] / 1000; |
|
|
524
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
73 |
var proportion = ((currentTime / duration) * 100).toFixed(4); |
|
a06527b99f22
the sparklineWidget height and width are now parametrable.
hamidouk
parents:
523
diff
changeset
|
74 |
|
| 523 | 75 |
IriSP.jQuery(".Ldt-sparkLinePositionMarker").css("width", proportion + "%"); |
76 |
} |
|
| 529 | 77 |
|
78 |
/** handle clicks on the widget */ |
|
79 |
IriSP.SparklineWidget.prototype.clickHandler = function(event) { |
|
80 |
/* this piece of code is a little bit convoluted - here's how it works : |
|
81 |
we want to handle clicks on the progress bar and convert those to seeks in the media. |
|
82 |
However, jquery only gives us a global position, and we want a number of pixels relative |
|
83 |
to our container div, so we get the parent position, and compute an offset to this position, |
|
84 |
and finally compute the progress ratio in the media. |
|
85 |
Finally we multiply this ratio with the duration to get the correct time |
|
86 |
*/ |
|
87 |
||
88 |
var parentOffset = this.selector.offset(); |
|
89 |
var width = this.selector.width(); |
|
90 |
var relX = event.pageX - parentOffset.left; |
|
91 |
||
92 |
var duration = this._serializer.currentMedia().meta["dc:duration"] / 1000; |
|
93 |
var newTime = ((relX / width) * duration).toFixed(2); |
|
94 |
|
|
|
532
5249bb0cd964
cosmetical change - moved css code from jquery.css to the stylesheet.
hamidouk
parents:
529
diff
changeset
|
95 |
|
|
5249bb0cd964
cosmetical change - moved css code from jquery.css to the stylesheet.
hamidouk
parents:
529
diff
changeset
|
96 |
this._Popcorn.trigger("IriSP.SparklineWidget.clicked", newTime); |
| 529 | 97 |
this._Popcorn.currentTime(newTime); |
|
644
492d3c8d776a
WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents:
600
diff
changeset
|
98 |
}; |
|
492d3c8d776a
WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents:
600
diff
changeset
|
99 |
|
|
492d3c8d776a
WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents:
600
diff
changeset
|
100 |
/** react when a new annotation is added */ |
|
492d3c8d776a
WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents:
600
diff
changeset
|
101 |
IriSP.SparklineWidget.prototype.handleNewAnnotation = function(annotation) { |
|
492d3c8d776a
WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents:
600
diff
changeset
|
102 |
var num_columns = (this.selector.width()) / IriSP.widgetsDefaults["SparklineWidget"].column_width; |
|
492d3c8d776a
WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents:
600
diff
changeset
|
103 |
var duration = +this._serializer.currentMedia().meta["dc:duration"]; |
| 645 | 104 |
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
|
105 |
var begin = +annotation.begin; |
|
492d3c8d776a
WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents:
600
diff
changeset
|
106 |
|
|
492d3c8d776a
WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents:
600
diff
changeset
|
107 |
var index = Math.floor(begin / time_step); |
|
492d3c8d776a
WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents:
600
diff
changeset
|
108 |
this._results[index]++; |
|
492d3c8d776a
WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents:
600
diff
changeset
|
109 |
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
|
110 |
spotColor: "#b70056", |
|
492d3c8d776a
WIP - redrawing the sparkling when an annotation is added.
hamidouk
parents:
600
diff
changeset
|
111 |
width: this.width, height: this.height}); |
| 529 | 112 |
}; |