src/js/widgets/sliderWidget.js
author hamidouk
Wed, 16 Nov 2011 17:04:35 +0100
branchpopcorn-port
changeset 259 7d748154f0b5
parent 230 9b86d3c52211
child 260 6603758fe69b
permissions -rw-r--r--
working implementation and tests of the slider.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
226
d1f0e604bd06 separated the slider from the playerWidget.
hamidouk
parents:
diff changeset
     1
IriSP.SliderWidget = function(Popcorn, config, Serializer) { 
d1f0e604bd06 separated the slider from the playerWidget.
hamidouk
parents:
diff changeset
     2
  IriSP.Widget.call(this, Popcorn, config, Serializer);
d1f0e604bd06 separated the slider from the playerWidget.
hamidouk
parents:
diff changeset
     3
};
d1f0e604bd06 separated the slider from the playerWidget.
hamidouk
parents:
diff changeset
     4
d1f0e604bd06 separated the slider from the playerWidget.
hamidouk
parents:
diff changeset
     5
IriSP.SliderWidget.prototype = new IriSP.Widget();
d1f0e604bd06 separated the slider from the playerWidget.
hamidouk
parents:
diff changeset
     6
d1f0e604bd06 separated the slider from the playerWidget.
hamidouk
parents:
diff changeset
     7
IriSP.SliderWidget.prototype.draw = function() {
d1f0e604bd06 separated the slider from the playerWidget.
hamidouk
parents:
diff changeset
     8
  var self = this;
259
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
     9
226
d1f0e604bd06 separated the slider from the playerWidget.
hamidouk
parents:
diff changeset
    10
  
229
808768eb5930 rewriting the slider-port to not use jquery ui slider.
hamidouk
parents: 226
diff changeset
    11
  this.selector.append("<div class='sliderBackground'></div>");
259
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    12
  this.sliderBackground = this.selector.find(".sliderBackground");
229
808768eb5930 rewriting the slider-port to not use jquery ui slider.
hamidouk
parents: 226
diff changeset
    13
  
259
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    14
  this.selector.append("<div class='sliderForeground'></div>");
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    15
  this.sliderForeground = this.selector.find(".sliderForeground");
229
808768eb5930 rewriting the slider-port to not use jquery ui slider.
hamidouk
parents: 226
diff changeset
    16
  
808768eb5930 rewriting the slider-port to not use jquery ui slider.
hamidouk
parents: 226
diff changeset
    17
  this.selector.append(Mustache.to_html(IriSP.overlay_marker_template));
259
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    18
  this.positionMarker = this.selector.find(".positionMarker");
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    19
  this.positionMarker.css("height", "10px");
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    20
  this.positionMarker.css("width", "10px");
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    21
  this.positionMarker.css("top", "0%");
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    22
  
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    23
  // a special variable to stop methods from tinkering
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    24
  // with the positionMarker when the user is dragging it
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    25
  this.draggingOngoing = false;
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    26
  
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    27
  this.positionMarker.draggable({axis: "x", 
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    28
  start: IriSP.wrap(this, this.positionMarkerDraggingStartedHandler),
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    29
  stop: IriSP.wrap(this, this.positionMarkerDraggedHandler)});
226
d1f0e604bd06 separated the slider from the playerWidget.
hamidouk
parents:
diff changeset
    30
  
230
9b86d3c52211 added a handler to seek the slider.
hamidouk
parents: 229
diff changeset
    31
  this.sliderBackground.click(function(event) { self.clickHandler.call(self, event); });
9b86d3c52211 added a handler to seek the slider.
hamidouk
parents: 229
diff changeset
    32
  
259
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    33
  this.selector.hover(IriSP.wrap(this, this.mouseOverHandler), IriSP.wrap(this, this.mouseOutHandler));
226
d1f0e604bd06 separated the slider from the playerWidget.
hamidouk
parents:
diff changeset
    34
  this._Popcorn.listen("timeupdate", IriSP.wrap(this, this.sliderUpdater));
d1f0e604bd06 separated the slider from the playerWidget.
hamidouk
parents:
diff changeset
    35
};
d1f0e604bd06 separated the slider from the playerWidget.
hamidouk
parents:
diff changeset
    36
d1f0e604bd06 separated the slider from the playerWidget.
hamidouk
parents:
diff changeset
    37
/* updates the slider as time passes */
259
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    38
IriSP.SliderWidget.prototype.sliderUpdater = function() {
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    39
  if(this.draggingOngoing)
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    40
    return;
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    41
    
229
808768eb5930 rewriting the slider-port to not use jquery ui slider.
hamidouk
parents: 226
diff changeset
    42
  var time = this._Popcorn.currentTime();
808768eb5930 rewriting the slider-port to not use jquery ui slider.
hamidouk
parents: 226
diff changeset
    43
  
808768eb5930 rewriting the slider-port to not use jquery ui slider.
hamidouk
parents: 226
diff changeset
    44
  var duration = this._serializer.currentMedia().meta["dc:duration"] / 1000;
808768eb5930 rewriting the slider-port to not use jquery ui slider.
hamidouk
parents: 226
diff changeset
    45
  var percent = ((time / duration) * 100).toFixed(2);
808768eb5930 rewriting the slider-port to not use jquery ui slider.
hamidouk
parents: 226
diff changeset
    46
	this.sliderForeground.css("width", percent + "%");
808768eb5930 rewriting the slider-port to not use jquery ui slider.
hamidouk
parents: 226
diff changeset
    47
	this.positionMarker.css("left", percent + "%");
808768eb5930 rewriting the slider-port to not use jquery ui slider.
hamidouk
parents: 226
diff changeset
    48
  
230
9b86d3c52211 added a handler to seek the slider.
hamidouk
parents: 229
diff changeset
    49
};
9b86d3c52211 added a handler to seek the slider.
hamidouk
parents: 229
diff changeset
    50
9b86d3c52211 added a handler to seek the slider.
hamidouk
parents: 229
diff changeset
    51
IriSP.SliderWidget.prototype.clickHandler = function(event) {
9b86d3c52211 added a handler to seek the slider.
hamidouk
parents: 229
diff changeset
    52
  /* this piece of code is a little bit convoluted - here's how it works :
9b86d3c52211 added a handler to seek the slider.
hamidouk
parents: 229
diff changeset
    53
     we want to handle clicks on the progress bar and convert those to seeks in the media.
9b86d3c52211 added a handler to seek the slider.
hamidouk
parents: 229
diff changeset
    54
     However, jquery only gives us a global position, and we want a number of pixels relative
9b86d3c52211 added a handler to seek the slider.
hamidouk
parents: 229
diff changeset
    55
     to our container div, so we get the parent position, and compute an offset to this position,
9b86d3c52211 added a handler to seek the slider.
hamidouk
parents: 229
diff changeset
    56
     and finally compute the progress ratio in the media.
9b86d3c52211 added a handler to seek the slider.
hamidouk
parents: 229
diff changeset
    57
     Finally we multiply this ratio with the duration to get the correct time
9b86d3c52211 added a handler to seek the slider.
hamidouk
parents: 229
diff changeset
    58
  */
9b86d3c52211 added a handler to seek the slider.
hamidouk
parents: 229
diff changeset
    59
  
9b86d3c52211 added a handler to seek the slider.
hamidouk
parents: 229
diff changeset
    60
  var parentOffset = this.sliderBackground.parent().offset();
9b86d3c52211 added a handler to seek the slider.
hamidouk
parents: 229
diff changeset
    61
  var width = this.sliderBackground.width();
9b86d3c52211 added a handler to seek the slider.
hamidouk
parents: 229
diff changeset
    62
  var relX = event.pageX - parentOffset.left;
9b86d3c52211 added a handler to seek the slider.
hamidouk
parents: 229
diff changeset
    63
      
9b86d3c52211 added a handler to seek the slider.
hamidouk
parents: 229
diff changeset
    64
  var duration = this._serializer.currentMedia().meta["dc:duration"] / 1000;
9b86d3c52211 added a handler to seek the slider.
hamidouk
parents: 229
diff changeset
    65
  var newTime = ((relX / width) * duration).toFixed(2);
9b86d3c52211 added a handler to seek the slider.
hamidouk
parents: 229
diff changeset
    66
	
9b86d3c52211 added a handler to seek the slider.
hamidouk
parents: 229
diff changeset
    67
  this._Popcorn.currentTime(newTime);
259
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    68
};
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    69
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    70
/* handles mouse over the slider */
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    71
IriSP.SliderWidget.prototype.mouseOverHandler = function(event) {  
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    72
  this.sliderBackground.animate({"padding-top": "10px"}, 100);
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    73
  this.sliderForeground.animate({"padding-top": "10px"}, 100);  
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    74
};
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    75
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    76
/* handles when the mouse leaves the slider */
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    77
IriSP.SliderWidget.prototype.mouseOutHandler = function(event) {  
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    78
  this.sliderBackground.animate({"padding-top": "5px"}, 50);
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    79
  this.sliderForeground.animate({"padding-top": "5px"}, 50);  
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    80
};
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    81
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    82
// called when the user starts dragging the position indicator
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    83
IriSP.SliderWidget.prototype.positionMarkerDraggingStartedHandler = function(event, ui) {   
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    84
  this.draggingOngoing = true;
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    85
};
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    86
    
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    87
IriSP.SliderWidget.prototype.positionMarkerDraggedHandler = function(event, ui) {  
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    88
  // debugger;
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    89
  console.log("dragging finished");
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    90
  var width = this.sliderBackground.width();
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    91
  var duration = this._serializer.currentMedia().meta["dc:duration"] / 1000;
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    92
  var newTime = ((ui.offset.left / width) * duration).toFixed(2);
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    93
  
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    94
  this._Popcorn.currentTime(newTime);
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    95
  
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    96
  this.draggingOngoing = false;
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    97
};
7d748154f0b5 working implementation and tests of the slider.
hamidouk
parents: 230
diff changeset
    98