Skip to content

Commit

Permalink
filter layout additions if removed by config and vice versa
Browse files Browse the repository at this point in the history
  • Loading branch information
archmoj committed May 17, 2021
1 parent bf3416d commit 3daa2f3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/components/modebar/manage.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,33 @@ function getButtonGroups(gd) {
var fullLayout = gd._fullLayout;
var fullData = gd._fullData;
var context = gd._context;
var buttonsToAdd = context.modeBarButtonsToAdd
.concat(fullLayout.modebar.add.split('+'));
var buttonsToRemove = context.modeBarButtonsToRemove
.concat(fullLayout.modebar.remove.split('+'));

function match(name, B) {
if(typeof B === 'string') {
if(B === name) return true;
} else {
if(B.name === name || B._cat === name) return true;
}
return false;
}

var buttonsToAdd = context.modeBarButtonsToAdd.concat(
fullLayout.modebar.add.split('+').filter(function(e) {
for(var i = 0; i < context.modeBarButtonsToRemove.length; i++) {
if(match(e, context.modeBarButtonsToRemove[i])) return false;
}
return true;
})
);

var buttonsToRemove = context.modeBarButtonsToRemove.concat(
fullLayout.modebar.remove.split('+').filter(function(e) {
for(var i = 0; i < context.modeBarButtonsToAdd.length; i++) {
if(match(e, context.modeBarButtonsToAdd[i])) return false;
}
return true;
})
);

var hasCartesian = fullLayout._has('cartesian');
var hasGL3D = fullLayout._has('gl3d');
Expand Down
40 changes: 40 additions & 0 deletions test/jasmine/tests/modebar_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1726,6 +1726,46 @@ describe('ModeBar', function() {
})
.then(done, done.fail);
});

it('add button if removed by layout and added by config', function(done) {
function countButtons() {
var modeBarEl = gd._fullLayout._modeBar.element;
return d3Select(modeBarEl).selectAll('a.modebar-btn').size();
}

var initial = 10;
Plotly.newPlot(gd, [{y: [1, 2]}], {
modebar: {
remove: 'zoom'
}
}, {
modeBarButtonsToAdd: ['zoom']
})
.then(function() {
expect(countButtons()).toBe(initial);
})
.then(done, done.fail);
});

it('remove button if added by layout and removed by config', function(done) {
function countButtons() {
var modeBarEl = gd._fullLayout._modeBar.element;
return d3Select(modeBarEl).selectAll('a.modebar-btn').size();
}

var initial = 10;
Plotly.newPlot(gd, [{y: [1, 2]}], {
modebar: {
add: 'drawline'
}
}, {
modeBarButtonsToRemove: ['drawline']
})
.then(function() {
expect(countButtons()).toBe(initial);
})
.then(done, done.fail);
});
});

describe('modebar html', function() {
Expand Down

0 comments on commit 3daa2f3

Please sign in to comment.