Skip to content

Commit 87ccf58

Browse files
committed
Add slider events
1 parent d3c59da commit 87ccf58

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

src/components/sliders/draw.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,18 +361,28 @@ 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+
364366
setActive(gd, sliderGroup, sliderOpts, quantizedPosition, true, doTransition);
367+
368+
return ret;
365369
}
366370
}
367371

368372
function setActive(gd, sliderGroup, sliderOpts, index, doCallback, doTransition) {
373+
var previousActive = sliderOpts.active;
369374
sliderOpts._input.active = sliderOpts.active = index;
370375

371376
var step = sliderOpts.steps[sliderOpts.active];
372377

373378
sliderGroup.call(setGripPosition, sliderOpts, sliderOpts.active / (sliderOpts.steps.length - 1), doTransition);
374379
sliderGroup.call(drawCurrentValue, sliderOpts);
375380

381+
gd.emit('plotly_sliderchange', {
382+
slider: sliderOpts,
383+
previousActive: previousActive
384+
});
385+
376386
if(step && step.method && doCallback) {
377387
if(sliderGroup._nextMethod) {
378388
// If we've already queued up an update, just overwrite it with the most recent:
@@ -405,20 +415,31 @@ function attachGripEvents(item, gd, sliderGroup, sliderOpts) {
405415
d3.event.preventDefault();
406416
grip.call(Color.fill, sliderOpts.activebgcolor);
407417

418+
gd.emit('plotly_sliderdragstart', {slider: sliderOpts});
419+
408420
var normalizedPosition = positionToNormalizedValue(sliderOpts, d3.mouse(node)[0]);
409421
handleInput(gd, sliderGroup, sliderOpts, normalizedPosition, true);
410422
sliderOpts._dragging = true;
411423

412424
$gd.on('mousemove', function() {
413425
var normalizedPosition = positionToNormalizedValue(sliderOpts, d3.mouse(node)[0]);
414-
handleInput(gd, sliderGroup, sliderOpts, normalizedPosition, false);
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+
}
415434
});
416435

417436
$gd.on('mouseup', function() {
418437
sliderOpts._dragging = false;
419438
grip.call(Color.fill, sliderOpts.bgcolor);
420439
$gd.on('mouseup', null);
421440
$gd.on('mousemove', null);
441+
442+
gd.emit('plotly_sliderdragend', {slider: sliderOpts});
422443
});
423444
});
424445
}

test/jasmine/tests/sliders_test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,62 @@ describe('sliders interactions', function() {
319319
}, 100);
320320
});
321321

322+
it('should issue events on interaction', function(done) {
323+
var cntStart = 0;
324+
var cntMove = 0;
325+
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);
338+
}
339+
340+
assertEventCounts(0, 0, 0, 0);
341+
342+
var firstGroup = gd._fullLayout._infolayer.select('.' + constants.railTouchRectClass);
343+
var railNode = firstGroup.node();
344+
var touchRect = railNode.getBoundingClientRect();
345+
346+
// Dispatch a click on the right side of the bar:
347+
railNode.dispatchEvent(new MouseEvent('mousedown', {
348+
clientY: touchRect.top + 5,
349+
clientX: touchRect.left + touchRect.width - 5,
350+
}));
351+
352+
setTimeout(function() {
353+
// One slider received a mousedown, two linked sliders have received a change:
354+
assertEventCounts(1, 0, 0, 2);
355+
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+
}));
362+
363+
setTimeout(function() {
364+
// On move, now two linked sliders have received two changes each:
365+
assertEventCounts(1, 1, 0, 4);
366+
367+
gd.dispatchEvent(new MouseEvent('mouseup'));
368+
369+
// Now all have been receivd:
370+
assertEventCounts(1, 1, 1, 4);
371+
372+
done();
373+
}, 10);
374+
}, 10);
375+
}, 10);
376+
});
377+
322378
function assertNodeCount(query, cnt) {
323379
expect(d3.selectAll(query).size()).toEqual(cnt);
324380
}

0 commit comments

Comments
 (0)