Skip to content

Commit 4775a47

Browse files
committed
Test updateevent for sliders
1 parent 6cb6d42 commit 4775a47

File tree

3 files changed

+93
-6
lines changed

3 files changed

+93
-6
lines changed

src/components/sliders/defaults.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,20 @@ function sliderDefaults(sliderIn, sliderOut, layoutOut) {
7676
coerce('transition.duration');
7777
coerce('transition.easing');
7878

79-
if(!Array.isArray(sliderOut.updateevent)) {
80-
sliderOut.updateevent = [sliderOut.updateevent];
79+
if(sliderOut.updateevent) {
80+
if(!Array.isArray(sliderOut.updateevent)) {
81+
sliderOut.updateevent = [sliderOut.updateevent];
82+
}
83+
} else {
84+
sliderOut.updateevent = [];
8185
}
8286

83-
if(!Array.isArray(sliderOut.udpatevalue)) {
84-
sliderOut.udpatevalue = [sliderOut.updatevalue];
87+
if(sliderOut.updatevalue) {
88+
if(!Array.isArray(sliderOut.updatevalue)) {
89+
sliderOut.updatevalue = [sliderOut.updatevalue];
90+
}
91+
} else {
92+
sliderOut.updatevalue = [];
8593
}
8694

8795
Lib.coerceFont(coerce, 'font', layoutOut.font);

src/components/sliders/draw.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ function removeListeners(gd, sliderGroup, sliderOpts) {
292292
}
293293

294294
function attachListeners(gd, sliderGroup, sliderOpts) {
295+
if(!sliderOpts.updateevent || !sliderOpts.updateevent.length) {
296+
return;
297+
}
298+
295299
var listeners = sliderOpts._input.listeners = [];
296300
var eventNames = sliderOpts._input.eventNames = [];
297301

@@ -304,7 +308,7 @@ function attachListeners(gd, sliderGroup, sliderOpts) {
304308

305309
// If it's *currently* invoking a command an event is received,
306310
// then we'll ignore the event in order to avoid complicated
307-
// invinite loops.
311+
// infinite loops.
308312
if(sliderOpts._invokingCommand) return;
309313

310314
setActiveByLabel(gd, sliderGroup, sliderOpts, value, false, true);
@@ -313,7 +317,7 @@ function attachListeners(gd, sliderGroup, sliderOpts) {
313317

314318
for(var i = 0; i < sliderOpts.updateevent.length; i++) {
315319
var updateEventName = sliderOpts.updateevent[i];
316-
var updatevalue = sliderOpts.updatevalue;
320+
var updatevalue = (sliderOpts.updatevalue || [])[i];
317321

318322
var updatelistener = makeListener(updateEventName, updatevalue);
319323

test/jasmine/tests/sliders_test.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,5 +252,80 @@ describe('update sliders interactions', function() {
252252
function assertNodeCount(query, cnt) {
253253
expect(d3.selectAll(query).size()).toEqual(cnt);
254254
}
255+
});
256+
257+
describe('updateevent and updatevalue', function() {
258+
'use strict';
259+
260+
var mock = require('@mocks/sliders.json');
261+
262+
var gd;
263+
264+
beforeEach(function(done) {
265+
gd = createGraphDiv();
266+
267+
var mockCopy = Lib.extendDeep({}, mock);
268+
269+
mockCopy.layout.sliders[0].updateevent = 'plotly_someevent';
270+
mockCopy.layout.sliders[0].updateevent = 'plotly_someevent';
271+
272+
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(done);
273+
});
274+
275+
afterEach(function() {
276+
Plotly.purge(gd);
277+
destroyGraphDiv();
278+
});
279+
280+
it('updates a slider when an event is triggered', function(done) {
281+
Plotly.relayout(gd, {
282+
'sliders[0].updateevent': 'plotly_someevent',
283+
'sliders[0].updatevalue': 'value'
284+
}).then(function() {
285+
expect(gd._fullLayout.sliders[0].active).toEqual(2);
286+
gd.emit('plotly_someevent', {value: 'green'});
287+
}).then(function() {
288+
expect(gd._fullLayout.sliders[0].active).toEqual(3);
289+
}).catch(fail).then(done);
290+
});
291+
292+
it('updates a slider when updatevalue unspecified', function(done) {
293+
Plotly.relayout(gd, {
294+
'sliders[0].updateevent': 'plotly_someevent'
295+
}).then(function() {
296+
expect(gd._fullLayout.sliders[0].active).toEqual(2);
297+
gd.emit('plotly_someevent', 'green');
298+
}).then(function() {
299+
expect(gd._fullLayout.sliders[0].active).toEqual(3);
300+
}).catch(fail).then(done);
301+
});
255302

303+
it('updates a slider when any of multiple updateevents occurs', function(done) {
304+
Plotly.relayout(gd, {
305+
'sliders[0].updateevent': ['plotly_someevent', 'plotly_anotherevent']
306+
}).then(function() {
307+
expect(gd._fullLayout.sliders[0].active).toEqual(2);
308+
gd.emit('plotly_someevent', 'green');
309+
}).then(function() {
310+
expect(gd._fullLayout.sliders[0].active).toEqual(3);
311+
gd.emit('plotly_anotherevent', 'yellow');
312+
}).then(function() {
313+
expect(gd._fullLayout.sliders[0].active).toEqual(2);
314+
}).catch(fail).then(done);
315+
});
316+
317+
it('matches update events with update values', function(done) {
318+
Plotly.relayout(gd, {
319+
'sliders[0].updateevent': ['plotly_someevent', 'plotly_anotherevent'],
320+
'sliders[0].updatevalue': ['foo', 'bar']
321+
}).then(function() {
322+
expect(gd._fullLayout.sliders[0].active).toEqual(2);
323+
gd.emit('plotly_someevent', {foo: 'green'});
324+
}).then(function() {
325+
expect(gd._fullLayout.sliders[0].active).toEqual(3);
326+
gd.emit('plotly_anotherevent', {bar: 'yellow'});
327+
}).then(function() {
328+
expect(gd._fullLayout.sliders[0].active).toEqual(2);
329+
}).catch(fail).then(done);
330+
});
256331
});

0 commit comments

Comments
 (0)