src/js/widgets/sliceWidget.js
author hamidouk
Mon, 09 Jan 2012 11:15:13 +0100
branchpopcorn-port
changeset 595 29d86e6c61a6
parent 581 e13380588930
child 619 2abc7d900f5e
permissions -rw-r--r--
finished going through the widgets to add stricter line checking.
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));
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    39
  this._Popcorn.trigger("IriSP.SliceWidget.position", [57, 24]);
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    40
};
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    41
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    42
IriSP.SliceWidget.prototype.positionSliceHandler = function(params) {
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    43
  left = params[0];
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    44
  width = params[1];
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    45
  
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    46
  this.zoneLeft = left;
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    47
  this.zoneWidth = width;
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    48
  this.sliceZone.css("left", left + "px");
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    49
  this.sliceZone.css("width", width + "px");
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    50
  this.leftHandle.css("left", (left - 7) + "px");
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    51
  this.rightHandle.css("left", left + width + "px");
581
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    52
  
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    53
  this._leftHandleOldLeft = left - 7;
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    54
  this._rightHandleOldLeft = left + width;
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    55
};
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    56
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    57
/** handle a dragging of the left handle */
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    58
IriSP.SliceWidget.prototype.leftHandleDragged = function(event, ui) {
581
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    59
  /* we have a special variable, this._leftHandleOldLeft, to keep the
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    60
     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
    61
     is the handle being dragged
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    62
  */
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    63
  
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    64
  var currentX = this.leftHandle.position()["left"];
581
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    65
  var rightHandleX = this.rightHandle.position()["left"];
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    66
  
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    67
  if (currentX >= rightHandleX - 7 && ui.position.left >= this._leftHandleOldLeft) {
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    68
    /* prevent the handle from moving past the right handle */
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    69
    ui.position.left = this._leftHandleOldLeft;
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    70
  }
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    71
  
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    72
  var increment = this.zoneLeft - (currentX + 7);
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    73
  
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    74
  this.zoneWidth += increment;
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    75
  this.zoneLeft = currentX + 7;
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    76
  this.sliceZone.css("width", this.zoneWidth);
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    77
  this.sliceZone.css("left", this.zoneLeft + "px");
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    78
  this.broadcastChanges();
581
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    79
  
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    80
  this._leftHandleOldLeft = ui.position.left;  
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    81
};
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    82
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    83
/** handle a dragging of the right handle */
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    84
IriSP.SliceWidget.prototype.rightHandleDragged = function(event, ui) { 
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    85
  var currentX = this.rightHandle.position()["left"];
581
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    86
  var leftHandleX = this.leftHandle.position()["left"];
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    87
  
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    88
  if (currentX <= leftHandleX + 7 && ui.position.left <= this._rightHandleOldLeft) {
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    89
    /* prevent the handle from moving past the right handle */
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    90
    ui.position.left = this._rightHandleOldLeft;
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    91
  }
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    92
  
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    93
  var increment = currentX - (this.zoneLeft + this.zoneWidth);  
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    94
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    95
  this.zoneWidth += increment;  
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    96
  this.sliceZone.css("width", this.zoneWidth);
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    97
  this.broadcastChanges();
581
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    98
  
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    99
  this._rightHandleOldLeft = ui.position.left; 
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   100
};
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   101
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   102
/** tell to the world that the coordinates of the slice have
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   103
    changed 
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   104
*/
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   105
IriSP.SliceWidget.prototype.broadcastChanges = function() {
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   106
  var leftPercent = (this.zoneLeft / this.selector.width()) * 100;
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   107
  var zonePercent = (this.zoneWidth / this.selector.width()) * 100;
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   108
  
581
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
   109
  this._Popcorn.trigger("IriSP.SliceWidget.zoneChange", [leftPercent, zonePercent]);  
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   110
};