Skip to content

Commit 4a69e03

Browse files
authored
Merge pull request #1391 from OpenGeoscience/fix-svg-feature-visible
fix: Toggling visibility of an svg feature did nothing
2 parents 58a8560 + 8b69084 commit 4a69e03

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

src/svg/object.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var svg_object = function (arg) {
2323
var m_id = 'svg-' + uniqueID(),
2424
s_exit = this._exit,
2525
m_this = this,
26+
s_visible = this.visible,
2627
s_draw = this.draw;
2728

2829
this._svgid = function () {
@@ -59,6 +60,26 @@ var svg_object = function (arg) {
5960
s_exit();
6061
};
6162

63+
/**
64+
* Get/Set the visibility of the feature.
65+
*
66+
* @param {boolean} [val] A boolean to change the visibility, or `undefined`
67+
* to return the visibility.
68+
* @param {boolean} [direct] If `true`, when getting the visibility,
69+
* disregard the visibility of the parent layer, and when setting, refresh
70+
* the state regardless of whether it has changed or not. Otherwise, the
71+
* functional visibility is returned, where both the feature and the layer
72+
* must be visible for a `true` result.
73+
* @returns {boolean|this} Either the visibility (if getting) or the feature
74+
* (if setting).
75+
*/
76+
this.visible = function (val, direct) {
77+
if (val !== undefined && val !== s_visible(undefined, true) && m_this.renderer()) {
78+
m_this.renderer()._scheduleUpdate();
79+
}
80+
return s_visible(val, direct);
81+
};
82+
6283
return this;
6384
};
6485

src/svg/svgRenderer.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,9 +548,16 @@ var svgRenderer = function (arg) {
548548
return m_this;
549549
};
550550

551+
/**
552+
* Schedule an update in the next render frame.
553+
*/
554+
this._scheduleUpdate = function () {
555+
m_this.layer().map().scheduleAnimationFrame(m_this._renderFrame);
556+
};
557+
551558
/**
552559
* Render all features that are marked as needing an update. This should
553-
* only be called duration an animation frame.
560+
* only be called during an animation frame.
554561
*/
555562
this._renderFrame = function () {
556563
var id;
@@ -566,6 +573,11 @@ var svgRenderer = function (arg) {
566573
m_this._renderFeature(id);
567574
}
568575
}
576+
for (id in m_features) {
577+
if (m_features.hasOwnProperty(id) && ids[id] === undefined) {
578+
m_this.select(id).attr('visibility', !m_features[id].visible || m_features[id].visible() ? 'visible' : 'hidden');
579+
}
580+
}
569581
};
570582

571583
/**
@@ -580,6 +592,7 @@ var svgRenderer = function (arg) {
580592
if (!m_features[id]) {
581593
return m_this;
582594
}
595+
m_this.select(id).remove();
583596
var data = m_features[id].data,
584597
index = m_features[id].index,
585598
style = m_features[id].style,
@@ -598,7 +611,7 @@ var svgRenderer = function (arg) {
598611
rendersel.attr('class', classes.concat([id]).join(' '));
599612
setStyles(rendersel, style);
600613
if (visible) {
601-
rendersel.style('visibility', visible() ? 'visible' : 'hidden');
614+
rendersel.attr('visibility', visible() ? 'visible' : 'hidden');
602615
}
603616
if (entries.size() && m_features[id].sortByZ) {
604617
selection.sort(function (a, b) {

tests/cases/lineFeature.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,11 @@ describe('geo.lineFeature', function () {
344344
expect(paths.eq(1).css('stroke-linejoin')).toBe('bevel');
345345
expect(paths.eq(2).css('stroke-linejoin')).toBe('round');
346346
expect(paths.eq(0).css('stroke-miterlimit')).toBe('5');
347+
line.visible(false);
348+
line.draw();
349+
stepAnimationFrame();
350+
expect(layer.node().find('path').length).toBe(9);
351+
expect(layer.node().find('path').attr('visibility')).toBe('hidden');
347352
unmockAnimationFrame();
348353
});
349354
});

0 commit comments

Comments
 (0)