Skip to content

Commit 4e4b8c8

Browse files
committed
make the whole annotation box a link if that's all the text contains
1 parent aa9fc00 commit 4e4b8c8

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/components/annotations/draw.js

+12
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,18 @@ function drawOne(gd, index) {
188188
}
189189

190190
function drawGraphicalElements() {
191+
// if the text has *only* a link, make the whole box into a link
192+
var anchor = annText.selectAll('a');
193+
if(anchor.size() === 1 && anchor.text() === annText.text()) {
194+
var wholeLink = annTextGroupInner.insert('a', ':first-child').attr({
195+
'xlink:xlink:href': anchor.attr('xlink:href'),
196+
'xlink:xlink:show': anchor.attr('xlink:show')
197+
})
198+
.style({cursor: 'pointer'});
199+
200+
wholeLink.node().appendChild(annTextBG.node());
201+
}
202+
191203

192204
// make sure lines are aligned the way they will be
193205
// at the end, even if their position changes

test/jasmine/tests/annotations_test.js

+34
Original file line numberDiff line numberDiff line change
@@ -1251,4 +1251,38 @@ describe('annotation effects', function() {
12511251
.catch(failTest)
12521252
.then(done);
12531253
});
1254+
1255+
it('makes the whole text box a link if the link is the whole text', function(done) {
1256+
makePlot([
1257+
{x: 20, y: 20, text: '<a href="https://plot.ly">Plot</a>', showarrow: false},
1258+
{x: 50, y: 50, text: '<a href="https://plot.ly">or</a> not', showarrow: false},
1259+
{x: 80, y: 80, text: '<a href="https://plot.ly">arrow</a>'},
1260+
{x: 20, y: 80, text: 'nor <a href="https://plot.ly">this</a>'}
1261+
])
1262+
.then(function() {
1263+
function checkBoxLink(index, isLink) {
1264+
var boxLink = d3.selectAll('.annotation[data-index="' + index + '"] g>a');
1265+
expect(boxLink.size()).toBe(isLink ? 1 : 0);
1266+
1267+
var textLink = d3.selectAll('.annotation[data-index="' + index + '"] text a');
1268+
expect(textLink.size()).toBe(1);
1269+
checkLink(textLink);
1270+
1271+
if(isLink) checkLink(boxLink);
1272+
}
1273+
1274+
function checkLink(link) {
1275+
expect(link.style('cursor')).toBe('pointer');
1276+
expect(link.attr('xlink:href')).toBe('https://plot.ly');
1277+
expect(link.attr('xlink:show')).toBe('new');
1278+
}
1279+
1280+
checkBoxLink(0, true);
1281+
checkBoxLink(1, false);
1282+
checkBoxLink(2, true);
1283+
checkBoxLink(3, false);
1284+
})
1285+
.catch(failTest)
1286+
.then(done);
1287+
});
12541288
});

0 commit comments

Comments
 (0)