src/js/widgets/sliderWidget.js
author hamidouk
Fri, 25 Nov 2011 10:40:08 +0100
branchpopcorn-port
changeset 330 156ea1d74223
parent 329 d37ca170cdb2
child 332 3c40f2e862d2
permissions -rw-r--r--
fixed a bug where clicks on the slider foreground would not be taken into account.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
289
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
     1
IriSP.SliderWidget = function(Popcorn, config, Serializer) {
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
     2
  IriSP.Widget.call(this, Popcorn, config, Serializer);
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
     3
};
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
     4
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
     5
IriSP.SliderWidget.prototype = new IriSP.Widget();
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
     6
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
     7
IriSP.SliderWidget.prototype.draw = function() {
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
     8
  var self = this;
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
     9
316
3b11a798f5e4 WIP - reorganizing the sliderWidget.
hamidouk
parents: 289
diff changeset
    10
  this.selector.append(Mustache.to_html(IriSP.sliderWidget_template, {}));
324
abe961b7f295 a slider that more or less works.
hamidouk
parents: 322
diff changeset
    11
  this.selector.addClass("Ldt-SliderMinimized");
289
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    12
316
3b11a798f5e4 WIP - reorganizing the sliderWidget.
hamidouk
parents: 289
diff changeset
    13
  this.sliderBackground = this.selector.find(".Ldt-sliderBackground");
3b11a798f5e4 WIP - reorganizing the sliderWidget.
hamidouk
parents: 289
diff changeset
    14
  this.sliderForeground = this.selector.find(".Ldt-sliderForeground");
3b11a798f5e4 WIP - reorganizing the sliderWidget.
hamidouk
parents: 289
diff changeset
    15
  this.positionMarker = this.selector.find(".Ldt-sliderPositionMarker");
289
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    16
324
abe961b7f295 a slider that more or less works.
hamidouk
parents: 322
diff changeset
    17
289
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    18
  // a special variable to stop methods from tinkering
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    19
  // with the positionMarker when the user is dragging it
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    20
  this.draggingOngoing = false;
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    21
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    22
  this.positionMarker.draggable({axis: "x",
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    23
  start: IriSP.wrap(this, this.positionMarkerDraggingStartedHandler),
322
6fc87426ecd4 fixed slider bug where the slider could be dragged out of his parent div.
hamidouk
parents: 316
diff changeset
    24
  stop: IriSP.wrap(this, this.positionMarkerDraggedHandler),
6fc87426ecd4 fixed slider bug where the slider could be dragged out of his parent div.
hamidouk
parents: 316
diff changeset
    25
  containment: "parent"
6fc87426ecd4 fixed slider bug where the slider could be dragged out of his parent div.
hamidouk
parents: 316
diff changeset
    26
  });
289
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    27
330
156ea1d74223 fixed a bug where clicks on the slider foreground would not be taken into account.
hamidouk
parents: 329
diff changeset
    28
  this.sliderBackground.click(function(event) { self.backgroundClickHandler.call(self, event); });
156ea1d74223 fixed a bug where clicks on the slider foreground would not be taken into account.
hamidouk
parents: 329
diff changeset
    29
  this.sliderForeground.click(function(event) { self.foregroundClickHandler.call(self, event); });
289
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    30
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    31
  this.selector.hover(IriSP.wrap(this, this.mouseOverHandler), IriSP.wrap(this, this.mouseOutHandler));
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    32
  this._Popcorn.listen("timeupdate", IriSP.wrap(this, this.sliderUpdater));
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    33
};
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    34
322
6fc87426ecd4 fixed slider bug where the slider could be dragged out of his parent div.
hamidouk
parents: 316
diff changeset
    35
/* update the slider and the position marker as time passes */
289
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    36
IriSP.SliderWidget.prototype.sliderUpdater = function() {
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    37
  if(this.draggingOngoing)
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    38
    return;
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    39
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    40
  var time = this._Popcorn.currentTime();
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    41
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    42
  var duration = this._serializer.currentMedia().meta["dc:duration"] / 1000;
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    43
  var percent = ((time / duration) * 100).toFixed(2);
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    44
	this.sliderForeground.css("width", percent + "%");
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    45
	this.positionMarker.css("left", percent + "%");
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    46
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    47
};
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    48
330
156ea1d74223 fixed a bug where clicks on the slider foreground would not be taken into account.
hamidouk
parents: 329
diff changeset
    49
IriSP.SliderWidget.prototype.backgroundClickHandler = function(event) {
289
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    50
  /* this piece of code is a little bit convoluted - here's how it works :
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    51
     we want to handle clicks on the progress bar and convert those to seeks in the media.
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    52
     However, jquery only gives us a global position, and we want a number of pixels relative
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    53
     to our container div, so we get the parent position, and compute an offset to this position,
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    54
     and finally compute the progress ratio in the media.
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    55
     Finally we multiply this ratio with the duration to get the correct time
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    56
  */
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    57
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    58
  var parentOffset = this.sliderBackground.parent().offset();
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    59
  var width = this.sliderBackground.width();
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    60
  var relX = event.pageX - parentOffset.left;
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    61
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    62
  var duration = this._serializer.currentMedia().meta["dc:duration"] / 1000;
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    63
  var newTime = ((relX / width) * duration).toFixed(2);
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    64
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    65
  this._Popcorn.currentTime(newTime);
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    66
};
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    67
330
156ea1d74223 fixed a bug where clicks on the slider foreground would not be taken into account.
hamidouk
parents: 329
diff changeset
    68
/* same function as the previous one, except that it handles clicks
156ea1d74223 fixed a bug where clicks on the slider foreground would not be taken into account.
hamidouk
parents: 329
diff changeset
    69
   on the foreground element */
156ea1d74223 fixed a bug where clicks on the slider foreground would not be taken into account.
hamidouk
parents: 329
diff changeset
    70
IriSP.SliderWidget.prototype.foregroundClickHandler = function(event) {
156ea1d74223 fixed a bug where clicks on the slider foreground would not be taken into account.
hamidouk
parents: 329
diff changeset
    71
  var parentOffset = this.sliderForeground.parent().offset();
156ea1d74223 fixed a bug where clicks on the slider foreground would not be taken into account.
hamidouk
parents: 329
diff changeset
    72
  var width = this.sliderBackground.width();
156ea1d74223 fixed a bug where clicks on the slider foreground would not be taken into account.
hamidouk
parents: 329
diff changeset
    73
  var relX = event.pageX - parentOffset.left;
156ea1d74223 fixed a bug where clicks on the slider foreground would not be taken into account.
hamidouk
parents: 329
diff changeset
    74
156ea1d74223 fixed a bug where clicks on the slider foreground would not be taken into account.
hamidouk
parents: 329
diff changeset
    75
  var duration = this._serializer.currentMedia().meta["dc:duration"] / 1000;
156ea1d74223 fixed a bug where clicks on the slider foreground would not be taken into account.
hamidouk
parents: 329
diff changeset
    76
  var newTime = ((relX / width) * duration).toFixed(2);
156ea1d74223 fixed a bug where clicks on the slider foreground would not be taken into account.
hamidouk
parents: 329
diff changeset
    77
156ea1d74223 fixed a bug where clicks on the slider foreground would not be taken into account.
hamidouk
parents: 329
diff changeset
    78
  this._Popcorn.currentTime(newTime);
156ea1d74223 fixed a bug where clicks on the slider foreground would not be taken into account.
hamidouk
parents: 329
diff changeset
    79
};
156ea1d74223 fixed a bug where clicks on the slider foreground would not be taken into account.
hamidouk
parents: 329
diff changeset
    80
289
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    81
/* handles mouse over the slider */
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    82
IriSP.SliderWidget.prototype.mouseOverHandler = function(event) {
329
d37ca170cdb2 added a one pixel margin to the slider widget for readability.
hamidouk
parents: 324
diff changeset
    83
  this.sliderBackground.animate({"height": "9px"}, 100);
d37ca170cdb2 added a one pixel margin to the slider widget for readability.
hamidouk
parents: 324
diff changeset
    84
  this.sliderForeground.animate({"height": "9px"}, 100);
324
abe961b7f295 a slider that more or less works.
hamidouk
parents: 322
diff changeset
    85
  
abe961b7f295 a slider that more or less works.
hamidouk
parents: 322
diff changeset
    86
//  this.selector.removeClass("Ldt-SliderMinimized");
abe961b7f295 a slider that more or less works.
hamidouk
parents: 322
diff changeset
    87
//  this.selector.addClass("Ldt-SliderMaximized");
289
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    88
};
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    89
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    90
/* handles when the mouse leaves the slider */
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    91
IriSP.SliderWidget.prototype.mouseOutHandler = function(event) {
324
abe961b7f295 a slider that more or less works.
hamidouk
parents: 322
diff changeset
    92
  this.sliderBackground.animate({"height": "5px"}, 100);
abe961b7f295 a slider that more or less works.
hamidouk
parents: 322
diff changeset
    93
  this.sliderForeground.animate({"height": "5px"}, 100);
abe961b7f295 a slider that more or less works.
hamidouk
parents: 322
diff changeset
    94
  
abe961b7f295 a slider that more or less works.
hamidouk
parents: 322
diff changeset
    95
//  this.selector.removeClass("Ldt-SliderMaximized");
abe961b7f295 a slider that more or less works.
hamidouk
parents: 322
diff changeset
    96
//  this.selector.addClass("Ldt-SliderMinimized");
abe961b7f295 a slider that more or less works.
hamidouk
parents: 322
diff changeset
    97
289
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    98
};
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
    99
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
   100
// called when the user starts dragging the position indicator
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
   101
IriSP.SliderWidget.prototype.positionMarkerDraggingStartedHandler = function(event, ui) {
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
   102
  this.draggingOngoing = true;
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
   103
};
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
   104
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
   105
IriSP.SliderWidget.prototype.positionMarkerDraggedHandler = function(event, ui) {
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
   106
  var width = this.sliderBackground.width();
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
   107
  var duration = this._serializer.currentMedia().meta["dc:duration"] / 1000;
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
   108
  var newTime = ((ui.offset.left / width) * duration).toFixed(2);
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
   109
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
   110
  this._Popcorn.currentTime(newTime);
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
   111
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
   112
  this.draggingOngoing = false;
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
   113
};
c0e399fbf3fb due to a np++ crash, some files where created in with windows line endings.
hamidouk
parents: 260
diff changeset
   114