src/js/widgets/sliderWidget.js
author hamidouk
Mon, 14 Nov 2011 17:19:26 +0100
branchrequire-js
changeset 238 6008172a0592
parent 230 9b86d3c52211
permissions -rw-r--r--
converted all the source files to use the require.js syntax.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
238
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
     1
define(["IriSP", "widgets", "util"], function() {
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
     2
  IriSP.SliderWidget = function(Popcorn, config, Serializer) { 
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
     3
    IriSP.Widget.call(this, Popcorn, config, Serializer);
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
     4
  };
226
d1f0e604bd06 separated the slider from the playerWidget.
hamidouk
parents:
diff changeset
     5
238
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
     6
  IriSP.SliderWidget.prototype = new IriSP.Widget();
226
d1f0e604bd06 separated the slider from the playerWidget.
hamidouk
parents:
diff changeset
     7
238
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
     8
  IriSP.SliderWidget.prototype.draw = function() {
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
     9
    var self = this;
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    10
    
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    11
    this.selector.append("<div class='sliderBackground'></div>");
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    12
    this.sliderBackground = this.selector.children(".sliderBackground");
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    13
    
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    14
    this.selector.append("<div class='sliderForeground' style='position: absolute; top: 0%; width: 0%;'></div>");
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    15
    this.sliderForeground = this.selector.children(".sliderForeground");
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    16
    
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    17
    this.selector.append(Mustache.to_html(IriSP.overlay_marker_template));
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    18
    this.positionMarker = this.selector.children(".positionMarker");
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    19
    
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    20
    this.sliderBackground.click(function(event) { self.clickHandler.call(self, event); });
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    21
    
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    22
    this._Popcorn.listen("timeupdate", IriSP.wrap(this, this.sliderUpdater));
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    23
  };
226
d1f0e604bd06 separated the slider from the playerWidget.
hamidouk
parents:
diff changeset
    24
238
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    25
  /* updates the slider as time passes */
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    26
  IriSP.SliderWidget.prototype.sliderUpdater = function() {  
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    27
    var time = this._Popcorn.currentTime();
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    28
    
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    29
    var duration = this._serializer.currentMedia().meta["dc:duration"] / 1000;
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    30
    var percent = ((time / duration) * 100).toFixed(2);
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    31
    this.sliderForeground.css("width", percent + "%");
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    32
    this.positionMarker.css("left", percent + "%");
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    33
    
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    34
  };
230
9b86d3c52211 added a handler to seek the slider.
hamidouk
parents: 229
diff changeset
    35
238
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    36
  IriSP.SliderWidget.prototype.clickHandler = function(event) {
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    37
    /* this piece of code is a little bit convoluted - here's how it works :
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    38
       we want to handle clicks on the progress bar and convert those to seeks in the media.
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    39
       However, jquery only gives us a global position, and we want a number of pixels relative
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    40
       to our container div, so we get the parent position, and compute an offset to this position,
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    41
       and finally compute the progress ratio in the media.
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    42
       Finally we multiply this ratio with the duration to get the correct time
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    43
    */
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    44
    
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    45
    var parentOffset = this.sliderBackground.parent().offset();
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    46
    var width = this.sliderBackground.width();
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    47
    var relX = event.pageX - parentOffset.left;
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    48
        
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    49
    var duration = this._serializer.currentMedia().meta["dc:duration"] / 1000;
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    50
    var newTime = ((relX / width) * duration).toFixed(2);
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    51
    
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    52
    this._Popcorn.currentTime(newTime);
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    53
  };
6008172a0592 converted all the source files to use the require.js syntax.
hamidouk
parents: 230
diff changeset
    54
});