Layer: refactor openPopup/openTooltip: use common function (#6613)

* Layer: refactor openPopup/openTooltip: use common function DivOverlay._prepareOpen

* More clear error message when trying to attach popup/tooltip to layer
without getLatLng function
This commit is contained in:
johnd0e
2019-04-30 12:56:20 +03:00
committed by Andrew Cherniavskii
parent d9a4c66f8b
commit 1e0d43b11d
3 changed files with 35 additions and 45 deletions

View File

@ -1,4 +1,5 @@
import {Layer} from './Layer';
import {FeatureGroup} from './FeatureGroup';
import * as Util from '../core/Util';
import {toLatLng} from '../geo/LatLng';
import {toPoint} from '../geometry/Point';
@ -158,6 +159,38 @@ export var DivOverlay = Layer.extend({
return this;
},
_prepareOpen: function (parent, layer, latlng) {
if (!(layer instanceof Layer)) {
latlng = layer;
layer = parent;
}
if (layer instanceof FeatureGroup) {
for (var id in parent._layers) {
layer = parent._layers[id];
break;
}
}
if (!latlng) {
if (layer.getCenter) {
latlng = layer.getCenter();
} else if (layer.getLatLng) {
latlng = layer.getLatLng();
} else {
throw new Error('Unable to get source layer LatLng.');
}
}
// set overlay source to this layer
this._source = layer;
// update the overlay (content, layout, ect...)
this.update();
return latlng;
},
_updateContent: function () {
if (!this._content) { return; }

View File

@ -4,7 +4,6 @@ import * as DomUtil from '../dom/DomUtil';
import {Point, toPoint} from '../geometry/Point';
import {Map} from '../map/Map';
import {Layer} from './Layer';
import {FeatureGroup} from './FeatureGroup';
import * as Util from '../core/Util';
import {Path} from './vector/Path';
@ -418,28 +417,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 (!(layer instanceof Layer)) {
latlng = layer;
layer = this;
}
if (layer instanceof FeatureGroup) {
for (var id in this._layers) {
layer = this._layers[id];
break;
}
}
if (!latlng) {
latlng = layer.getCenter ? layer.getCenter() : layer.getLatLng();
}
if (this._popup && this._map) {
// set popup source to this layer
this._popup._source = layer;
// update the popup (content, layout, ect...)
this._popup.update();
latlng = this._popup._prepareOpen(this, layer, latlng);
// open the popup on the map
this._map.openPopup(this._popup, latlng);

View File

@ -4,7 +4,6 @@ import {DivOverlay} from './DivOverlay';
import {toPoint} from '../geometry/Point';
import {Map} from '../map/Map';
import {Layer} from './Layer';
import {FeatureGroup} from './FeatureGroup';
import * as Util from '../core/Util';
import * as DomUtil from '../dom/DomUtil';
@ -312,29 +311,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 (!(layer instanceof Layer)) {
latlng = layer;
layer = this;
}
if (layer instanceof FeatureGroup) {
for (var id in this._layers) {
layer = this._layers[id];
break;
}
}
if (!latlng) {
latlng = layer.getCenter ? layer.getCenter() : layer.getLatLng();
}
if (this._tooltip && this._map) {
// set tooltip source to this layer
this._tooltip._source = layer;
// update the tooltip (content, layout, ect...)
this._tooltip.update();
latlng = this._tooltip._prepareOpen(this, layer, latlng);
// open the tooltip on the map
this._map.openTooltip(this._tooltip, latlng);