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 Apr 4, 2021
1 parent b3eb92d commit 36de22c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
18 changes: 18 additions & 0 deletions src/layer/DivOverlay.js
Expand Up @@ -61,6 +61,24 @@ 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, target) {
if (this._map) {
this.close();
} else {
layer = arguments.length ? layer : this._source;
var latlng = this._prepareOpen(layer, target);
this.setLatLng(latlng);

// 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 @@ -416,11 +412,7 @@ Layer.include({
// Opens or closes the popup bound to this layer depending on its current state.
togglePopup: function (target) {
if (this._popup) {
if (this._popup._map) {
this._popup.close();
} else {
this.openPopup(target);
}
this._popup.toggle(this, target);
}
return this;
},
Expand Down
6 changes: 1 addition & 5 deletions src/layer/Tooltip.js
Expand Up @@ -333,11 +333,7 @@ Layer.include({
// Opens or closes the tooltip bound to this layer depending on its current state.
toggleTooltip: function (target) {
if (this._tooltip) {
if (this._tooltip._map) {
this._tooltip.close();
} else {
this.openTooltip(target);
}
this._tooltip.toggle(this, target);
}
return this;
},
Expand Down

0 comments on commit 36de22c

Please sign in to comment.