Skip to content

Commit 653fe76

Browse files
authored
Merge pull request #1126 from plotly/slider-events
Add slider events
2 parents c750179 + ab29acc commit 653fe76

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

src/components/sliders/draw.js

+15
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,21 @@ function handleInput(gd, sliderGroup, sliderOpts, normalizedPosition, doTransiti
366366
}
367367

368368
function setActive(gd, sliderGroup, sliderOpts, index, doCallback, doTransition) {
369+
var previousActive = sliderOpts.active;
369370
sliderOpts._input.active = sliderOpts.active = index;
370371

371372
var step = sliderOpts.steps[sliderOpts.active];
372373

373374
sliderGroup.call(setGripPosition, sliderOpts, sliderOpts.active / (sliderOpts.steps.length - 1), doTransition);
374375
sliderGroup.call(drawCurrentValue, sliderOpts);
375376

377+
gd.emit('plotly_sliderchange', {
378+
slider: sliderOpts,
379+
step: sliderOpts.steps[sliderOpts.active],
380+
interaction: doCallback,
381+
previousActive: previousActive
382+
});
383+
376384
if(step && step.method && doCallback) {
377385
if(sliderGroup._nextMethod) {
378386
// If we've already queued up an update, just overwrite it with the most recent:
@@ -399,6 +407,8 @@ function attachGripEvents(item, gd, sliderGroup, sliderOpts) {
399407
var $gd = d3.select(gd);
400408

401409
item.on('mousedown', function() {
410+
gd.emit('plotly_sliderstart', {slider: sliderOpts});
411+
402412
var grip = sliderGroup.select('.' + constants.gripRectClass);
403413

404414
d3.event.stopPropagation();
@@ -419,6 +429,11 @@ function attachGripEvents(item, gd, sliderGroup, sliderOpts) {
419429
grip.call(Color.fill, sliderOpts.bgcolor);
420430
$gd.on('mouseup', null);
421431
$gd.on('mousemove', null);
432+
433+
gd.emit('plotly_sliderend', {
434+
slider: sliderOpts,
435+
step: sliderOpts.steps[sliderOpts.active]
436+
});
422437
});
423438
});
424439
}

test/jasmine/tests/sliders_test.js

+64
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,70 @@ describe('sliders interactions', function() {
319319
}, 100);
320320
});
321321

322+
it('should issue events on interaction', function(done) {
323+
var cntStart = 0;
324+
var cntInteraction = 0;
325+
var cntNonInteraction = 0;
326+
var cntEnd = 0;
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+
);
346+
}
347+
348+
assertEventCounts(0, 0, 0, 0);
349+
350+
var firstGroup = gd._fullLayout._infolayer.select('.' + constants.railTouchRectClass);
351+
var railNode = firstGroup.node();
352+
var touchRect = railNode.getBoundingClientRect();
353+
354+
// Dispatch a click on the right side of the bar:
355+
railNode.dispatchEvent(new MouseEvent('mousedown', {
356+
clientY: touchRect.top + 5,
357+
clientX: touchRect.left + touchRect.width - 5,
358+
}));
359+
360+
setTimeout(function() {
361+
// One slider received a mousedown, one received an interaction, and one received a change:
362+
assertEventCounts(1, 1, 1, 0);
363+
364+
// Drag to the left side:
365+
gd.dispatchEvent(new MouseEvent('mousemove', {
366+
clientY: touchRect.top + 5,
367+
clientX: touchRect.left + 5,
368+
}));
369+
370+
setTimeout(function() {
371+
// On move, now to changes for the each slider, and no ends:
372+
assertEventCounts(1, 2, 2, 0);
373+
374+
gd.dispatchEvent(new MouseEvent('mouseup'));
375+
376+
setTimeout(function() {
377+
// Now an end:
378+
assertEventCounts(1, 2, 2, 1);
379+
380+
done();
381+
}, 50);
382+
}, 50);
383+
}, 50);
384+
});
385+
322386
function assertNodeCount(query, cnt) {
323387
expect(d3.selectAll(query).size()).toEqual(cnt);
324388
}

0 commit comments

Comments
 (0)