src/js/widgets/sliceWidget.js
author veltr
Wed, 11 Apr 2012 17:06:11 +0200
changeset 857 fa614dc66b0b
parent 775 59927257f43b
child 780 2ae03b1d2797
permissions -rw-r--r--
Bugfix in Sparkline Widget
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
770
14d56cb5d75d fixed the sliceWidget to allow selecting the whole range.
hamidouk
parents: 692
diff changeset
    24
  var left = this.selector.offset().left;
14d56cb5d75d fixed the sliceWidget to allow selecting the whole range.
hamidouk
parents: 692
diff changeset
    25
  var top = this.selector.offset().top;
772
feb3e5194502 discovered a bug in jqueryui. wontfix.
hamidouk
parents: 770
diff changeset
    26
774
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    27
  // a bug in firefox makes it use the wrong format
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    28
  if (!IriSP.jQuery.browser.mozilla) {
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    29
    // contain the handles correctly - we cannot set
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    30
    // containment: parent because it wouldn't allow to select the 
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    31
    // whole slice, so we have to compute a box in which the slice is
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    32
    // allowed to move.
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    33
    var containment = [left - 8, top, this.selector.width() + left, top];
775
59927257f43b temporary fix for ff.
hamidouk
parents: 774
diff changeset
    34
774
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    35
    // var containment = [left - 16, top, this.selector.width() + left - 8, top];
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    36
    this.leftHandle.draggable({axis: "x",
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    37
    drag: IriSP.wrap(this, this.leftHandleDragged),  
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    38
    containment: containment
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    39
    });
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    40
774
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    41
    containment = [left, top, this.selector.width() + left, top];
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    42
    // containment = [left, top, this.selector.width() + left - 8, top];
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    43
    this.rightHandle.draggable({axis: "x",
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    44
    drag: IriSP.wrap(this, this.rightHandleDragged),    
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    45
    containment: containment
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    46
    });
775
59927257f43b temporary fix for ff.
hamidouk
parents: 774
diff changeset
    47
  
59927257f43b temporary fix for ff.
hamidouk
parents: 774
diff changeset
    48
  } else { // firefox
59927257f43b temporary fix for ff.
hamidouk
parents: 774
diff changeset
    49
    // we need to define a containment specific to firefox.
59927257f43b temporary fix for ff.
hamidouk
parents: 774
diff changeset
    50
    
59927257f43b temporary fix for ff.
hamidouk
parents: 774
diff changeset
    51
    var containment = [left - 16, top, this.selector.width() + left - 8, top];
774
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    52
    this.leftHandle.draggable({axis: "x",
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    53
    drag: IriSP.wrap(this, this.leftHandleDragged),  
775
59927257f43b temporary fix for ff.
hamidouk
parents: 774
diff changeset
    54
    containment: containment
774
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    55
    });
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    56
775
59927257f43b temporary fix for ff.
hamidouk
parents: 774
diff changeset
    57
    containment = [left, top, this.selector.width() + left - 8, top];
774
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    58
    this.rightHandle.draggable({axis: "x",
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    59
    drag: IriSP.wrap(this, this.rightHandleDragged),    
775
59927257f43b temporary fix for ff.
hamidouk
parents: 774
diff changeset
    60
    containment: containment
774
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    61
    });
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    62
  }
25cb63882c3c temporary fix for firefox.
hamidouk
parents: 772
diff changeset
    63
  
551
2f883ad196b2 fixed display bug in webkit because jquery draggable automatically adds
hamidouk
parents: 537
diff changeset
    64
  this.leftHandle.css("position", "absolute");
2f883ad196b2 fixed display bug in webkit because jquery draggable automatically adds
hamidouk
parents: 537
diff changeset
    65
  this.rightHandle.css("position", "absolute");
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    66
  
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    67
  this._Popcorn.listen("IriSP.SliceWidget.position", 
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    68
                        IriSP.wrap(this, this.positionSliceHandler));
619
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
    69
  
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
    70
  this._Popcorn.listen("IriSP.SliceWidget.show", IriSP.wrap(this, this.show));
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
    71
  this._Popcorn.listen("IriSP.SliceWidget.hide", IriSP.wrap(this, this.hide));
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
    72
  this.selector.hide();
537
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
619
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
    75
/** responds to an "IriSP.SliceWidget.position" message
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
    76
    @param params an array with the first element being the left distance in
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
    77
           percents and the second element the width of the slice in pixels
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
    78
*/        
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    79
IriSP.SliceWidget.prototype.positionSliceHandler = function(params) {
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    80
  left = params[0];
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    81
  width = params[1];
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
  this.zoneLeft = left;
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    84
  this.zoneWidth = width;
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    85
  this.sliceZone.css("left", left + "px");
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    86
  this.sliceZone.css("width", width + "px");
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    87
  this.leftHandle.css("left", (left - 7) + "px");
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    88
  this.rightHandle.css("left", left + width + "px");
581
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    89
  
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    90
  this._leftHandleOldLeft = left - 7;
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    91
  this._rightHandleOldLeft = left + width;
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    92
};
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    93
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    94
/** handle a dragging of the left handle */
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
    95
IriSP.SliceWidget.prototype.leftHandleDragged = function(event, ui) {
581
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    96
  /* we have a special variable, this._leftHandleOldLeft, to keep the
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    97
     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
    98
     is the handle being dragged
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
    99
  */
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
   100
  
770
14d56cb5d75d fixed the sliceWidget to allow selecting the whole range.
hamidouk
parents: 692
diff changeset
   101
  var currentX = this.leftHandle.offset().left;
689
dfd601bd2ea8 WIP - redoing the resize slice function. do not use.
hamidouk
parents: 619
diff changeset
   102
  var rightHandleX = Math.floor(this.rightHandle.position()["left"]);
581
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
   103
  
770
14d56cb5d75d fixed the sliceWidget to allow selecting the whole range.
hamidouk
parents: 692
diff changeset
   104
  var container_offset = this.selector.offset().left;
14d56cb5d75d fixed the sliceWidget to allow selecting the whole range.
hamidouk
parents: 692
diff changeset
   105
692
4eca4ee558a3 this time got it right.
hamidouk
parents: 691
diff changeset
   106
  if (Math.floor(ui.position.left) >= rightHandleX - 7) {
581
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
   107
    /* prevent the handle from moving past the right handle */
692
4eca4ee558a3 this time got it right.
hamidouk
parents: 691
diff changeset
   108
    ui.position.left = rightHandleX - 7;
581
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
   109
  }
690
b7ac9cfefda4 WIP - nearly fixed all the bugs with the sliceWidget.js. I still have got to
hamidouk
parents: 689
diff changeset
   110
b7ac9cfefda4 WIP - nearly fixed all the bugs with the sliceWidget.js. I still have got to
hamidouk
parents: 689
diff changeset
   111
  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
   112
  this.zoneLeft = Math.floor(ui.position.left) + 8;
689
dfd601bd2ea8 WIP - redoing the resize slice function. do not use.
hamidouk
parents: 619
diff changeset
   113
  
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   114
  this.sliceZone.css("width", this.zoneWidth);
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   115
  this.sliceZone.css("left", this.zoneLeft + "px");
770
14d56cb5d75d fixed the sliceWidget to allow selecting the whole range.
hamidouk
parents: 692
diff changeset
   116
  
14d56cb5d75d fixed the sliceWidget to allow selecting the whole range.
hamidouk
parents: 692
diff changeset
   117
  this._leftHandleOldLeft = ui.position.left;  
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   118
  this.broadcastChanges();
689
dfd601bd2ea8 WIP - redoing the resize slice function. do not use.
hamidouk
parents: 619
diff changeset
   119
    
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   120
};
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   121
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   122
/** handle a dragging of the right handle */
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   123
IriSP.SliceWidget.prototype.rightHandleDragged = function(event, ui) { 
691
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
   124
  /* we have a special variable, this._leftHandleOldLeft, to keep the
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
   125
     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
   126
     is the handle being dragged
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
   127
  */
581
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
   128
  
691
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
   129
  var currentX = this.leftHandle.position()["left"];
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
   130
  var leftHandleX = Math.floor(this.leftHandle.position()["left"]);
770
14d56cb5d75d fixed the sliceWidget to allow selecting the whole range.
hamidouk
parents: 692
diff changeset
   131
14d56cb5d75d fixed the sliceWidget to allow selecting the whole range.
hamidouk
parents: 692
diff changeset
   132
  var container_offset = this.selector.offset().left + this.selector.width();
581
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
   133
  
691
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
   134
  if (Math.floor(ui.position.left) < leftHandleX + 7) {
770
14d56cb5d75d fixed the sliceWidget to allow selecting the whole range.
hamidouk
parents: 692
diff changeset
   135
    /* prevent the handle from moving past the left handle */
691
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
   136
    ui.position.left = leftHandleX + 7;
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
   137
  }
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   138
691
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
   139
  this.zoneWidth = Math.floor(ui.position.left) - (leftHandleX + 7);    
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
   140
  
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   141
  this.sliceZone.css("width", this.zoneWidth);
691
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
   142
  //this.sliceZone.css("left", this.zoneLeft + "px");
2297203d7fa3 made the widget display itself pixel-perfectly.
hamidouk
parents: 690
diff changeset
   143
  this._rightHandleOldLeft = Math.floor(this._rightHandleOldLeft);  
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   144
  this.broadcastChanges();
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   145
};
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   146
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   147
/** tell to the world that the coordinates of the slice have
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   148
    changed 
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   149
*/
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   150
IriSP.SliceWidget.prototype.broadcastChanges = function() {
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   151
  var leftPercent = (this.zoneLeft / this.selector.width()) * 100;
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   152
  var zonePercent = (this.zoneWidth / this.selector.width()) * 100;
770
14d56cb5d75d fixed the sliceWidget to allow selecting the whole range.
hamidouk
parents: 692
diff changeset
   153
581
e13380588930 prevent the handles from moving past each other.
hamidouk
parents: 551
diff changeset
   154
  this._Popcorn.trigger("IriSP.SliceWidget.zoneChange", [leftPercent, zonePercent]);  
619
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
   155
};
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
   156
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
   157
IriSP.SliceWidget.prototype.show = function() {
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
   158
  this.selector.show();
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
   159
};
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
   160
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
   161
IriSP.SliceWidget.prototype.hide = function() {
2abc7d900f5e show or hide the widget.
hamidouk
parents: 581
diff changeset
   162
  this.selector.hide();
537
61fd3968ab06 added a widget to select a slice.
hamidouk
parents:
diff changeset
   163
};