src/js/widgets/sliceWidget.js
author hamidouk
Fri, 27 Jan 2012 11:29:03 +0100
branchpopcorn-port
changeset 729 7ba63d0315ad
parent 692 4eca4ee558a3
child 770 14d56cb5d75d
permissions -rw-r--r--
load the widget when the video has finished loading.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
     1
/** A widget to create a new segment */
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
     2
IriSP.SliceWidget = function(Popcorn, config, Serializer) {
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
     3
  IriSP.Widget.call(this, Popcorn, config, Serializer);
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
     4
  
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
     5
};
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
     6
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
     7
IriSP.SliceWidget.prototype = new IriSP.Widget();
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
     8
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
     9
IriSP.SliceWidget.prototype.draw = function() {
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    10
  var templ = Mustache.to_html(IriSP.sliceWidget_template);
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    11
  this.selector.append(templ);
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    12
  
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    13
  this.sliceZone = this.selector.find(".Ldt-sliceZone");
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    14
  
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    15
  /* global variables used to keep the position and width
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    16
     of the zone.
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    17
  */  
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    18
  this.zoneLeft = 0;
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    19
  this.zoneWidth = 0;
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    20
  
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    21
  this.leftHandle = this.selector.find(".Ldt-sliceLeftHandle");
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    22
  this.rightHandle = this.selector.find(".Ldt-sliceRightHandle");
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    23
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    24
  this.leftHandle.draggable({axis: "x",
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    25
  drag: IriSP.wrap(this, this.leftHandleDragged),  
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    26
  containment: "parent"
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    27
  });
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    28
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    29
  this.rightHandle.draggable({axis: "x",
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    30
  drag: IriSP.wrap(this, this.rightHandleDragged),    
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    31
  containment: "parent"
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    32
  });
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    33
551
2f883ad196b2 fixed display bug in webkit because jquery draggable automatically adds
hamidouk
parents: 537
diff changeset
    34
  this.leftHandle.css("position", "absolute");
2f883ad196b2 fixed display bug in webkit because jquery draggable automatically adds
hamidouk
parents: 537
diff changeset
    35
  this.rightHandle.css("position", "absolute");
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    36
  
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    37
  this._Popcorn.listen("IriSP.SliceWidget.position", 
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    38
                        IriSP.wrap(this, this.positionSliceHandler));
619
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
    39
  
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
    40
  this._Popcorn.listen("IriSP.SliceWidget.show", IriSP.wrap(this, this.show));
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
    41
  this._Popcorn.listen("IriSP.SliceWidget.hide", IriSP.wrap(this, this.hide));
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
    42
  this.selector.hide();
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    43
};
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    44
619
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
    45
/** responds to an "IriSP.SliceWidget.position" message
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
    46
    @param params an array with the first element being the left distance in
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
    47
           percents and the second element the width of the slice in pixels
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
    48
*/        
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    49
IriSP.SliceWidget.prototype.positionSliceHandler = function(params) {
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    50
  left = params[0];
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    51
  width = params[1];
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    52
  
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    53
  this.zoneLeft = left;
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    54
  this.zoneWidth = width;
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    55
  this.sliceZone.css("left", left + "px");
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    56
  this.sliceZone.css("width", width + "px");
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    57
  this.leftHandle.css("left", (left - 7) + "px");
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    58
  this.rightHandle.css("left", left + width + "px");
581
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    59
  
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    60
  this._leftHandleOldLeft = left - 7;
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    61
  this._rightHandleOldLeft = left + width;
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    62
};
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    63
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    64
/** handle a dragging of the left handle */
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    65
IriSP.SliceWidget.prototype.leftHandleDragged = function(event, ui) {
581
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    66
  /* we have a special variable, this._leftHandleOldLeft, to keep the
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    67
     previous position of the handle. We do that to know in what direction
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    68
     is the handle being dragged
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    69
  */
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    70
  
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    71
  var currentX = this.leftHandle.position()["left"];
689
dfd601bd2ea8 WIP - redoing the resize slice function. do not use.
hamidouk
parents: 619
diff changeset
    72
  var rightHandleX = Math.floor(this.rightHandle.position()["left"]);
581
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    73
  
692
4eca4ee558a3 this time got it right.
hamidouk
parents: 691
diff changeset
    74
  if (Math.floor(ui.position.left) >= rightHandleX - 7) {
581
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    75
    /* prevent the handle from moving past the right handle */
692
4eca4ee558a3 this time got it right.
hamidouk
parents: 691
diff changeset
    76
    ui.position.left = rightHandleX - 7;
581
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    77
  }
690
b7ac9cfefda4 WIP - nearly fixed all the bugs with the sliceWidget.js. I still have got to
hamidouk
parents: 689
diff changeset
    78
b7ac9cfefda4 WIP - nearly fixed all the bugs with the sliceWidget.js. I still have got to
hamidouk
parents: 689
diff changeset
    79
  this.zoneWidth = rightHandleX - Math.floor(ui.position.left) - 7;  
b7ac9cfefda4 WIP - nearly fixed all the bugs with the sliceWidget.js. I still have got to
hamidouk
parents: 689
diff changeset
    80
  this.zoneLeft = Math.floor(ui.position.left) + 8;
689
dfd601bd2ea8 WIP - redoing the resize slice function. do not use.
hamidouk
parents: 619
diff changeset
    81
  
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    82
  this.sliceZone.css("width", this.zoneWidth);
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    83
  this.sliceZone.css("left", this.zoneLeft + "px");
690
b7ac9cfefda4 WIP - nearly fixed all the bugs with the sliceWidget.js. I still have got to
hamidouk
parents: 689
diff changeset
    84
  this._leftHandleOldLeft = Math.floor(this._leftHandleOldLeft);  
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    85
  this.broadcastChanges();
689
dfd601bd2ea8 WIP - redoing the resize slice function. do not use.
hamidouk
parents: 619
diff changeset
    86
    
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    87
};
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    88
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    89
/** handle a dragging of the right handle */
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    90
IriSP.SliceWidget.prototype.rightHandleDragged = function(event, ui) { 
691
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
    91
  /* we have a special variable, this._leftHandleOldLeft, to keep the
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
    92
     previous position of the handle. We do that to know in what direction
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
    93
     is the handle being dragged
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
    94
  */
581
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    95
  
691
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
    96
  var currentX = this.leftHandle.position()["left"];
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
    97
  var leftHandleX = Math.floor(this.leftHandle.position()["left"]);
581
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    98
  
691
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
    99
  if (Math.floor(ui.position.left) < leftHandleX + 7) {
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
   100
    /* prevent the handle from moving past the right handle */
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
   101
    ui.position.left = leftHandleX + 7;
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
   102
  }
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   103
691
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
   104
  this.zoneWidth = Math.floor(ui.position.left) - (leftHandleX + 7);    
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
   105
  
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   106
  this.sliceZone.css("width", this.zoneWidth);
691
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
   107
  //this.sliceZone.css("left", this.zoneLeft + "px");
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
   108
  this._rightHandleOldLeft = Math.floor(this._rightHandleOldLeft);  
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   109
  this.broadcastChanges();
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   110
};
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   111
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   112
/** tell to the world that the coordinates of the slice have
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   113
    changed 
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   114
*/
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   115
IriSP.SliceWidget.prototype.broadcastChanges = function() {
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   116
  var leftPercent = (this.zoneLeft / this.selector.width()) * 100;
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   117
  var zonePercent = (this.zoneWidth / this.selector.width()) * 100;
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   118
  
581
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
   119
  this._Popcorn.trigger("IriSP.SliceWidget.zoneChange", [leftPercent, zonePercent]);  
619
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
   120
};
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
   121
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
   122
IriSP.SliceWidget.prototype.show = function() {
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
   123
  this.selector.show();
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
   124
};
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
   125
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
   126
IriSP.SliceWidget.prototype.hide = function() {
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
   127
  this.selector.hide();
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   128
};