Skip to content

Commit

Permalink
DivOverlay: common functions openOn/close
Browse files Browse the repository at this point in the history
- refactor Map\ open/close-Popup/Tooltip to use DivOverlay's openOn/close

- refactor Layer\ openPopup/Tooltip: use common function DivOverlay\_open
  • Loading branch information
johndoe committed Mar 31, 2021
1 parent f1e7080 commit 85c11e9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 58 deletions.
26 changes: 24 additions & 2 deletions src/layer/DivOverlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,27 @@ export var DivOverlay = Layer.extend({
return this;
},

_prepareOpen: function (parent, layer, latlng) {
// @method openOn(map: Map): this
// Adds the overlay to the map. The same as `map.openPopup(popup)`/`map.openTooltip()`.
openOn: function (map) {
if (!map.hasLayer(this)) {
map.addLayer(this);
}
return this;
},

// @method close(): this
// Closes the overlay. The same as `map.closePopup()`/`map.closeTooltip()`.
close: function () {
if (this._map) {
this._map.removeLayer(this);
}
return this;
},

_open: function (parent, layer, latlng) {
if (!parent._map) { return; }

if (!(layer instanceof Layer)) {
latlng = layer;
layer = parent;
Expand All @@ -184,14 +204,16 @@ export var DivOverlay = Layer.extend({
throw new Error('Unable to get source layer LatLng.');
}
}
this.setLatLng(latlng);

// set overlay source to this layer
this._source = layer;

// update the overlay (content, layout, ect...)
this.update();

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

_updateContent: function () {
Expand Down
53 changes: 20 additions & 33 deletions src/layer/Popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,12 @@ export var Popup = DivOverlay.extend({
// @method openOn(map: Map): this
// Adds the popup to the map and closes the previous one. The same as `map.openPopup(popup)`.
openOn: function (map) {
map.openPopup(this);
return this;
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 +137,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 +163,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 +173,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 +279,7 @@ export var Popup = DivOverlay.extend({
},

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

Expand Down Expand Up @@ -315,30 +317,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 @@ -403,11 +393,8 @@ Layer.include({
// @method openPopup(latlng?: LatLng): this
// Opens the bound popup at the specified `latlng` or at the default popup anchor if no `latlng` is passed.
openPopup: function (layer, latlng) {
if (this._popup && this._map) {
latlng = this._popup._prepareOpen(this, layer, latlng);

// open the popup on the map
this._map.openPopup(this._popup, latlng);
if (this._popup) {
this._popup._open(this, layer, latlng);
}

return this;
Expand All @@ -417,7 +404,7 @@ Layer.include({
// 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 @@ -427,7 +414,7 @@ Layer.include({
togglePopup: function (target) {
if (this._popup) {
if (this._popup._map) {
this.closePopup();
this._popup.close();
} else {
this.openPopup(target);
}
Expand Down
31 changes: 8 additions & 23 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 @@ -323,11 +314,8 @@ Layer.include({
// @method openTooltip(latlng?: LatLng): this
// Opens the bound tooltip at the specified `latlng` or at the default tooltip anchor if no `latlng` is passed.
openTooltip: function (layer, latlng) {
if (this._tooltip && this._map) {
latlng = this._tooltip._prepareOpen(this, layer, latlng);

// open the tooltip on the map
this._map.openTooltip(this._tooltip, latlng);
if (this._tooltip) {
this._tooltip._open(this, layer, latlng);
}

return this;
Expand All @@ -336,18 +324,15 @@ Layer.include({
// @method closeTooltip(): this
// Closes the tooltip bound to this layer if it is open.
closeTooltip: function () {
if (this._tooltip) {
this._tooltip._close();
}
return this;
return this._tooltip.close();
},

// @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 85c11e9

Please sign in to comment.