Skip to content

Commit 67f56df

Browse files
committed
Fix mouse click and segment insert at edge of waveform view
See #545
1 parent 2673092 commit 67f56df

6 files changed

+44
-8
lines changed

src/mouse-drag-handler.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ MouseDragHandler.prototype._mouseUp = function(event) {
169169
MouseDragHandler.prototype._getMousePosX = function(clientX) {
170170
const containerPos = this._stage.getContainer().getBoundingClientRect();
171171

172-
return clientX - containerPos.left;
172+
return Math.floor(clientX - containerPos.left);
173173
};
174174

175175
/**

src/scrollbar.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ Scrollbar.prototype._onScrollbarClick = function(event) {
178178
if (event.target === this._stage) {
179179
if (this._zoomview) {
180180
// Centre the scrollbox where the user clicked.
181-
let x = Math.floor(event.evt.layerX - this._scrollboxWidth / 2);
181+
let x = Math.floor(event.evt.offsetX - this._scrollboxWidth / 2);
182182

183183
if (x < 0) {
184184
x = 0;

src/segment.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,26 @@ Segment.prototype.update = function(options) {
220220
* @param {Number} startTime The start of the time region, in seconds.
221221
* @param {Number} endTime The end of the time region, in seconds.
222222
* @returns {Boolean}
223-
*
224-
* @see http://wiki.c2.com/?TestIfDateRangesOverlap
225223
*/
226224

227225
Segment.prototype.isVisible = function(startTime, endTime) {
228-
return this.startTime < endTime && startTime < this.endTime;
226+
// A special case, where the segment has zero duration
227+
// and is at the start of the region.
228+
if (this.startTime === this.endTime && this.startTime === startTime) {
229+
return true;
230+
}
231+
232+
// Segment ends before start of region.
233+
if (this.endTime <= startTime) {
234+
return false;
235+
}
236+
237+
// Segment starts after end of region
238+
if (this.startTime >= endTime) {
239+
return false;
240+
}
241+
242+
return true;
229243
};
230244

231245
Segment.prototype._setStartTime = function(time) {

src/waveform-view.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,12 @@ WaveformView.prototype._onContextMenu = function(event) {
347347
};
348348

349349
WaveformView.prototype._clickHandler = function(event, eventName) {
350+
let offsetX = event.evt.offsetX;
351+
352+
if (offsetX < 0) {
353+
offsetX = 0;
354+
}
355+
350356
let emitViewEvent = true;
351357

352358
if (event.target !== this._stage) {
@@ -387,8 +393,7 @@ WaveformView.prototype._clickHandler = function(event, eventName) {
387393
}
388394

389395
if (emitViewEvent) {
390-
const mousePosX = event.evt.layerX;
391-
const time = this.pixelOffsetToTime(mousePosX);
396+
const time = this.pixelOffsetToTime(offsetX);
392397
const viewName = this.getName();
393398

394399
this._peaks.emit(viewName + '.' + eventName, {

test/helpers/input-controller.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ InputController.prototype._dispatchMouseEvent = function(type, pos) {
3232
const event = new MouseEvent(type, {
3333
bubbles: true,
3434
clientX: this._left + pos.x,
35-
clientY: this._top + pos.y
35+
clientY: this._top + pos.y,
36+
offsetX: pos.x,
37+
offsetY: pos.y
3638
});
3739

3840
this._target.dispatchEvent(event);

test/segment-spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -406,5 +406,20 @@ describe('Segment', function() {
406406

407407
expect(segment.isVisible(10.0, 20.0)).to.equal(true);
408408
});
409+
410+
it('should return true if segment starts at time zero and has zero end time', function() {
411+
const peaks = { emit: function() {} };
412+
const pid = 0;
413+
414+
const segment = new Segment(peaks, pid, {
415+
id: 'segment.1',
416+
labelText: '',
417+
editable: true,
418+
startTime: 0.0,
419+
endTime: 0.0
420+
});
421+
422+
expect(segment.isVisible(0.0, 10.0)).to.equal(true);
423+
});
409424
});
410425
});

0 commit comments

Comments
 (0)