Skip to content

Commit 23d0e48

Browse files
committed
Rewrite slider events
1 parent 87ccf58 commit 23d0e48

File tree

2 files changed

+46
-45
lines changed

2 files changed

+46
-45
lines changed

src/components/sliders/draw.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,7 @@ function handleInput(gd, sliderGroup, sliderOpts, normalizedPosition, doTransiti
361361
var quantizedPosition = Math.round(normalizedPosition * (sliderOpts.steps.length - 1));
362362

363363
if(quantizedPosition !== sliderOpts.active) {
364-
var ret = {previousActive: sliderOpts.active};
365-
366364
setActive(gd, sliderGroup, sliderOpts, quantizedPosition, true, doTransition);
367-
368-
return ret;
369365
}
370366
}
371367

@@ -380,6 +376,7 @@ function setActive(gd, sliderGroup, sliderOpts, index, doCallback, doTransition)
380376

381377
gd.emit('plotly_sliderchange', {
382378
slider: sliderOpts,
379+
interaction: doCallback,
383380
previousActive: previousActive
384381
});
385382

@@ -409,28 +406,21 @@ function attachGripEvents(item, gd, sliderGroup, sliderOpts) {
409406
var $gd = d3.select(gd);
410407

411408
item.on('mousedown', function() {
409+
gd.emit('plotly_sliderstart', {slider: sliderOpts});
410+
412411
var grip = sliderGroup.select('.' + constants.gripRectClass);
413412

414413
d3.event.stopPropagation();
415414
d3.event.preventDefault();
416415
grip.call(Color.fill, sliderOpts.activebgcolor);
417416

418-
gd.emit('plotly_sliderdragstart', {slider: sliderOpts});
419-
420417
var normalizedPosition = positionToNormalizedValue(sliderOpts, d3.mouse(node)[0]);
421418
handleInput(gd, sliderGroup, sliderOpts, normalizedPosition, true);
422419
sliderOpts._dragging = true;
423420

424421
$gd.on('mousemove', function() {
425422
var normalizedPosition = positionToNormalizedValue(sliderOpts, d3.mouse(node)[0]);
426-
var changes = handleInput(gd, sliderGroup, sliderOpts, normalizedPosition, false);
427-
428-
if(changes) {
429-
gd.emit('plotly_sliderdragmove', {
430-
slider: sliderOpts,
431-
previousActive: changes.previousActive
432-
});
433-
}
423+
handleInput(gd, sliderGroup, sliderOpts, normalizedPosition, false);
434424
});
435425

436426
$gd.on('mouseup', function() {
@@ -439,7 +429,10 @@ function attachGripEvents(item, gd, sliderGroup, sliderOpts) {
439429
$gd.on('mouseup', null);
440430
$gd.on('mousemove', null);
441431

442-
gd.emit('plotly_sliderdragend', {slider: sliderOpts});
432+
gd.emit('plotly_sliderend', {
433+
slider: sliderOpts,
434+
step: sliderOpts.steps[sliderOpts.active]
435+
});
443436
});
444437
});
445438
}

test/jasmine/tests/sliders_test.js

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -321,20 +321,28 @@ describe('sliders interactions', function() {
321321

322322
it('should issue events on interaction', function(done) {
323323
var cntStart = 0;
324-
var cntMove = 0;
324+
var cntInteraction = 0;
325+
var cntNonInteraction = 0;
325326
var cntEnd = 0;
326-
var cntChange = 0;
327-
328-
gd.on('plotly_sliderdragstart', function() { cntStart++; });
329-
gd.on('plotly_sliderdragmove', function() { cntMove++; });
330-
gd.on('plotly_sliderdragend', function() { cntEnd++; });
331-
gd.on('plotly_sliderchange', function() { cntChange++; });
332-
333-
function assertEventCounts(starts, moves, ends, changes) {
334-
expect(cntStart).toEqual(starts);
335-
expect(cntMove).toEqual(moves);
336-
expect(cntEnd).toEqual(ends);
337-
expect(cntChange).toEqual(changes);
327+
328+
gd.on('plotly_sliderstart', function() {
329+
cntStart++;
330+
}).on('plotly_sliderchange', function(datum) {
331+
if(datum.interaction) {
332+
cntInteraction++;
333+
} else {
334+
cntNonInteraction++;
335+
}
336+
}).on('plotly_sliderend', function() {
337+
cntEnd++;
338+
});
339+
340+
function assertEventCounts(starts, interactions, noninteractions, ends) {
341+
expect(
342+
[cntStart, cntInteraction, cntNonInteraction, cntEnd]
343+
).toEqual(
344+
[starts, interactions, noninteractions, ends]
345+
);
338346
}
339347

340348
assertEventCounts(0, 0, 0, 0);
@@ -350,29 +358,29 @@ describe('sliders interactions', function() {
350358
}));
351359

352360
setTimeout(function() {
353-
// One slider received a mousedown, two linked sliders have received a change:
354-
assertEventCounts(1, 0, 0, 2);
361+
// One slider received a mousedown, one received an interaction, and one received a change:
362+
assertEventCounts(1, 1, 1, 0);
355363

356-
setTimeout(function() {
357-
// Drag to the left side:
358-
gd.dispatchEvent(new MouseEvent('mousemove', {
359-
clientY: touchRect.top + 5,
360-
clientX: touchRect.left + 5,
361-
}));
364+
// Drag to the left side:
365+
gd.dispatchEvent(new MouseEvent('mousemove', {
366+
clientY: touchRect.top + 5,
367+
clientX: touchRect.left + 5,
368+
}));
362369

363-
setTimeout(function() {
364-
// On move, now two linked sliders have received two changes each:
365-
assertEventCounts(1, 1, 0, 4);
370+
setTimeout(function() {
371+
// On move, now to changes for the each slider, and no ends:
372+
assertEventCounts(1, 2, 2, 0);
366373

367-
gd.dispatchEvent(new MouseEvent('mouseup'));
374+
gd.dispatchEvent(new MouseEvent('mouseup'));
368375

369-
// Now all have been receivd:
370-
assertEventCounts(1, 1, 1, 4);
376+
setTimeout(function() {
377+
// Now an end:
378+
assertEventCounts(1, 2, 2, 1);
371379

372380
done();
373-
}, 10);
374-
}, 10);
375-
}, 10);
381+
}, 50);
382+
}, 50);
383+
}, 50);
376384
});
377385

378386
function assertNodeCount(query, cnt) {

0 commit comments

Comments
 (0)