Skip to content

Commit

Permalink
DivOverlay: common functions openOn/close
Browse files Browse the repository at this point in the history
Refactor [Map\Layer] open/close-Popup/Tooltip to use DivOverlay's openOn/close
  • Loading branch information
johndoe committed Apr 3, 2021
1 parent 1ab93a3 commit b3eb92d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 49 deletions.
22 changes: 22 additions & 0 deletions src/layer/DivOverlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@ export var DivOverlay = Layer.extend({
this._source = source;
},

// @method openOn(map: Map): this
// Adds the overlay to the map.
// Alternative to `map.openPopup(popup)`/`.openTooltip(tooltip)`.
openOn: function (map) {
map = arguments.length ? map : this._source._map; // experimental, not the part of public api
if (!map.hasLayer(this)) {
map.addLayer(this);
}
return this;
},

// @method close(): this
// Closes the overlay.
// Alternative to `map.closePopup(popup)`/`.closeTooltip(tooltip)`
// and `layer.closePopup()`/`.closeTooltip()`.
close: function () {
if (this._map) {
this._map.removeLayer(this);
}
return this;
},

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

Expand Down
55 changes: 24 additions & 31 deletions src/layer/Popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,17 @@ export var Popup = DivOverlay.extend({

// @namespace Popup
// @method openOn(map: Map): this
// Adds the popup to the map and closes the previous one. The same as `map.openPopup(popup)`.
// Alternative to `map.openPopup(popup)`.
// Adds the popup to the map and closes the previous one.
openOn: function (map) {
map.openPopup(this);
return this;
map = arguments.length ? map : this._source._map; // experimental, not the part of public api

if (!map.hasLayer(this) && map._popup && map._popup.options.autoClose) {
map.removeLayer(map._popup);
}
map._popup = this;

return DivOverlay.prototype.openOn.call(this, map);
},

onAdd: function (map) {
Expand Down Expand Up @@ -133,6 +140,10 @@ 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 All @@ -155,7 +166,7 @@ export var Popup = DivOverlay.extend({
var events = DivOverlay.prototype.getEvents.call(this);

if (this.options.closeOnClick !== undefined ? this.options.closeOnClick : this._map.options.closePopupOnClick) {
events.preclick = this._close;
events.preclick = this.close;
}

if (this.options.keepInView) {
Expand All @@ -165,12 +176,6 @@ export var Popup = DivOverlay.extend({
return events;
},

_close: function () {
if (this._map) {
this._map.closePopup(this);
}
},

_initLayout: function () {
var prefix = 'leaflet-popup',
container = this._container = DomUtil.create('div',
Expand Down Expand Up @@ -277,7 +282,7 @@ export var Popup = DivOverlay.extend({
},

_onCloseButtonClick: function (e) {
this._close();
this.close();
DomEvent.stop(e);
},

Expand Down Expand Up @@ -315,30 +320,18 @@ Map.include({
// @method openPopup(content: String|HTMLElement, latlng: LatLng, options?: Popup options): this
// Creates a popup with the specified content and options and opens it in the given point on a map.
openPopup: function (popup, latlng, options) {
popup = this._initOverlay(Popup, popup, latlng, options);

if (!this.hasLayer(popup)) {
if (this._popup && this._popup.options.autoClose) {
this.closePopup();
}

this._popup = popup;
this.addLayer(popup);
}
this._initOverlay(Popup, popup, latlng, options)
.openOn(this);

return this;

},

// @method closePopup(popup?: Popup): this
// Closes the popup previously opened with [openPopup](#map-openpopup) (or the given one).
closePopup: function (popup) {
if (!popup || popup === this._popup) {
popup = this._popup;
this._popup = null;
}
popup = arguments.length ? popup : this._popup;
if (popup) {
this.removeLayer(popup);
popup.close();
}
return this;
}
Expand Down Expand Up @@ -402,19 +395,19 @@ Layer.include({
openPopup: function (layer, latlng) {
if (this._popup && this._map) {
latlng = this._popup._prepareOpen(this, layer, latlng);
this._popup.setLatLng(latlng);

// open the popup on the map
this._map.openPopup(this._popup, latlng);
this._popup.openOn(this._map);
}

return this;
},

// @method closePopup(): this
// Closes the popup bound to this layer if it is open.
closePopup: function () {
if (this._popup) {
this._popup._close();
this._popup.close();
}
return this;
},
Expand All @@ -424,7 +417,7 @@ Layer.include({
togglePopup: function (target) {
if (this._popup) {
if (this._popup._map) {
this.closePopup();
this._popup.close();
} else {
this.openPopup(target);
}
Expand Down
26 changes: 8 additions & 18 deletions src/layer/Tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,12 @@ export var Tooltip = DivOverlay.extend({
var events = DivOverlay.prototype.getEvents.call(this);

if (Browser.touch && !this.options.permanent) {
events.preclick = this._close;
events.preclick = this.close;
}

return events;
},

_close: function () {
if (this._map) {
this._map.closeTooltip(this);
}
},

_initLayout: function () {
var prefix = 'leaflet-tooltip',
className = prefix + ' ' + (this.options.className || '') + ' leaflet-zoom-' + (this._zoomAnimated ? 'animated' : 'hide');
Expand Down Expand Up @@ -232,11 +226,8 @@ Map.include({
// @method openTooltip(content: String|HTMLElement, latlng: LatLng, options?: Tooltip options): this
// Creates a tooltip with the specified content and options and open it.
openTooltip: function (tooltip, latlng, options) {
tooltip = this._initOverlay(Tooltip, tooltip, latlng, options);

if (!this.hasLayer(tooltip)) {
this.addLayer(tooltip);
}
this._initOverlay(Tooltip, tooltip, latlng, options)
.openOn(this);

return this;
},
Expand All @@ -245,7 +236,7 @@ Map.include({
// Closes the tooltip given as parameter.
closeTooltip: function (tooltip) {
if (tooltip) {
this.removeLayer(tooltip);
tooltip.close();
}
return this;
}
Expand Down Expand Up @@ -322,29 +313,28 @@ Layer.include({
openTooltip: function (layer, latlng) {
if (this._tooltip && this._map) {
latlng = this._tooltip._prepareOpen(this, layer, latlng);
this._tooltip.setLatLng(latlng);

// open the tooltip on the map
this._map.openTooltip(this._tooltip, latlng);
this._tooltip.openOn(this._map);
}

return this;
},

// @method closeTooltip(): this
// Closes the tooltip bound to this layer if it is open.
closeTooltip: function () {
if (this._tooltip) {
this._tooltip._close();
return this._tooltip.close();
}
return this;
},

// @method toggleTooltip(): this
// 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.closeTooltip();
this._tooltip.close();
} else {
this.openTooltip(target);
}
Expand Down

0 comments on commit b3eb92d

Please sign in to comment.