Skip to content

Commit f025dc6

Browse files
committed
add is-active and is-hovered dynamic props to buttons:
- use update object and current axis range to determine whether a given button is active - fill button with Color.ligthLine on mouse over and when active.
1 parent 4318f45 commit f025dc6

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

src/components/rangeselector/draw.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,28 @@ module.exports = function draw(gd) {
5454

5555
buttons.each(function(d) {
5656
var button = d3.select(this);
57+
var update = getUpdateObject(axisLayout, d);
58+
59+
d.isActive = isActive(axisLayout, d, update);
5760

5861
button.call(drawButtonRect, selectorLayout, d);
5962
button.call(drawButtonText, selectorLayout, d);
6063

6164
button.on('click', function() {
6265
if(gd._dragged) return;
6366

64-
var update = getUpdateObject(axisLayout, d);
65-
6667
Plotly.relayout(gd, update);
6768
});
69+
70+
button.on('mouseover', function() {
71+
d.isHovered = true;
72+
button.call(drawButtonRect, selectorLayout, d);
73+
});
74+
75+
button.on('mouseout', function() {
76+
d.isHovered = false;
77+
button.call(drawButtonRect, selectorLayout, d);
78+
});
6879
});
6980

7081
// N.B. this mutates selectorLayout
@@ -96,7 +107,21 @@ function selectorKeyFunc(d) {
96107
return d._id;
97108
}
98109

99-
function drawButtonRect(button, selectorLayout) {
110+
function isActive(axisLayout, opts, update) {
111+
if(opts.step === 'all') {
112+
return axisLayout.autorange === true;
113+
}
114+
else {
115+
var keys = Object.keys(update);
116+
117+
return (
118+
axisLayout.range[0] === update[keys[0]] &&
119+
axisLayout.range[1] === update[keys[1]]
120+
);
121+
}
122+
}
123+
124+
function drawButtonRect(button, selectorLayout, d) {
100125
var rect = button.selectAll('rect')
101126
.data([0]);
102127

@@ -111,10 +136,16 @@ function drawButtonRect(button, selectorLayout) {
111136
});
112137

113138
rect.call(Color.stroke, selectorLayout.bordercolor)
114-
.call(Color.fill, selectorLayout.bgcolor)
139+
.call(Color.fill, getFillColor(selectorLayout, d))
115140
.style('stroke-width', selectorLayout.borderwidth + 'px');
116141
}
117142

143+
function getFillColor(selectorLayout, d) {
144+
return (d.isActive || d.isHovered) ?
145+
Color.lightLine :
146+
selectorLayout.bgcolor;
147+
}
148+
118149
function drawButtonText(button, selectorLayout, d) {
119150
function textLayout(s) {
120151
svgTextUtils.convertToTspans(s, function() {

test/jasmine/tests/range_selector_test.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
var RangeSelector = require('@src/components/rangeselector');
22
var getUpdateObject = require('@src/components/rangeselector/get_update_object');
33

4+
var d3 = require('d3');
45
var Plotly = require('@lib');
56
var Lib = require('@src/lib');
6-
var d3 = require('d3');
77
var createGraphDiv = require('../assets/create_graph_div');
88
var destroyGraphDiv = require('../assets/destroy_graph_div');
99
var getRectCenter = require('../assets/get_rect_center');
@@ -396,6 +396,12 @@ describe('[range selector suite]', function() {
396396
expect(d3.selectAll(query).size()).toEqual(cnt);
397397
}
398398

399+
function checkActiveButton(activeIndex) {
400+
d3.selectAll('.button').each(function(d, i) {
401+
expect(d.isActive).toBe(activeIndex === i);
402+
});
403+
}
404+
399405
it('should display the correct nodes', function() {
400406
assertNodeCount('.rangeselector', 1);
401407
assertNodeCount('.button', mockCopy.layout.xaxis.rangeselector.buttons.length);
@@ -410,18 +416,37 @@ describe('[range selector suite]', function() {
410416

411417
});
412418

413-
it('should update range when clicked', function() {
419+
it('should update range and active button when clicked', function() {
414420
var range0 = gd.layout.xaxis.range[0];
415421
var buttons = d3.selectAll('.button').select('rect');
416422

423+
checkActiveButton(buttons.size() - 1);
424+
417425
var pos0 = getRectCenter(buttons[0][0]);
418426
var posReset = getRectCenter(buttons[0][buttons.size()-1]);
419427

420428
mouseEvent('click', pos0[0], pos0[1]);
421429
expect(gd.layout.xaxis.range[0]).toBeGreaterThan(range0);
422430

431+
checkActiveButton(0);
432+
423433
mouseEvent('click', posReset[0], posReset[1]);
424434
expect(gd.layout.xaxis.range[0]).toEqual(range0);
435+
436+
checkActiveButton(buttons.size() - 1);
437+
});
438+
439+
it('should change color on mouse over', function() {
440+
var button = d3.select('.button').select('rect');
441+
var pos = getRectCenter(button.node());
442+
443+
expect(button.style('fill')).toEqual('rgb(255, 255, 255)');
444+
445+
mouseEvent('mouseover', pos[0], pos[1]);
446+
expect(button.style('fill')).toEqual('rgb(238, 238, 238)');
447+
448+
mouseEvent('mouseout', pos[0], pos[1]);
449+
expect(button.style('fill')).toEqual('rgb(255, 255, 255)');
425450
});
426451

427452
});

0 commit comments

Comments
 (0)