Skip to content

Commit

Permalink
DivOverlay: new public method toggle
Browse files Browse the repository at this point in the history
Refactor Layer\ togglePopup/Tooltip: use common method DivOverlay\toggle

Popup: do not clear map._popup on close as it's redundant
  • Loading branch information
johndoe committed Nov 2, 2021
1 parent 1e06ae4 commit 47112b3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
21 changes: 21 additions & 0 deletions src/layer/DivOverlay.js
Expand Up @@ -61,6 +61,27 @@ export var DivOverlay = Layer.extend({
return this;
},

// @method toggle(layer?: Layer): this
// Opens or closes the overlay bound to layer depending on its current state.
// Argument may be omitted only for overlay bound to layer.
// Alternative to `layer.togglePopup()`/`.toggleTooltip()`.
toggle: function (layer) {
if (this._map) {
this.close();
} else {
if (arguments.length) {
this._source = layer;
} else {
layer = this._source;
}
this._prepareOpen();

// open the overlay on the map
this.openOn(layer._map);
}
return this;
},

onAdd: function (map) {
this._zoomAnimated = map._zoomAnimated;

Expand Down
10 changes: 1 addition & 9 deletions src/layer/Popup.js
Expand Up @@ -140,10 +140,6 @@ export var Popup = DivOverlay.extend({
onRemove: function (map) {
DivOverlay.prototype.onRemove.call(this, map);

if (this === map._popup) {
map._popup = null;
}

// @namespace Map
// @section Popup events
// @event popupclose: PopupEvent
Expand Down Expand Up @@ -408,11 +404,7 @@ Layer.include({
// Opens or closes the popup bound to this layer depending on its current state.
togglePopup: function () {
if (this._popup) {
if (this._popup._map) {
this._popup.close();
} else {
this.openPopup();
}
this._popup.toggle(this);
}
return this;
},
Expand Down
6 changes: 1 addition & 5 deletions src/layer/Tooltip.js
Expand Up @@ -324,11 +324,7 @@ Layer.include({
// Opens or closes the tooltip bound to this layer depending on its current state.
toggleTooltip: function () {
if (this._tooltip) {
if (this._tooltip._map) {
this._tooltip.close();
} else {
this.openTooltip();
}
this._tooltip.toggle(this);
}
return this;
},
Expand Down

0 comments on commit 47112b3

Please sign in to comment.