mirror of
https://github.com/Leaflet/Leaflet.git
synced 2025-08-20 14:34:38 +00:00
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:

committed by
Andrew Cherniavskii

parent
d9a4c66f8b
commit
1e0d43b11d
@ -1,4 +1,5 @@
|
|||||||
import {Layer} from './Layer';
|
import {Layer} from './Layer';
|
||||||
|
import {FeatureGroup} from './FeatureGroup';
|
||||||
import * as Util from '../core/Util';
|
import * as Util from '../core/Util';
|
||||||
import {toLatLng} from '../geo/LatLng';
|
import {toLatLng} from '../geo/LatLng';
|
||||||
import {toPoint} from '../geometry/Point';
|
import {toPoint} from '../geometry/Point';
|
||||||
@ -158,6 +159,38 @@ export var DivOverlay = Layer.extend({
|
|||||||
return this;
|
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 () {
|
_updateContent: function () {
|
||||||
if (!this._content) { return; }
|
if (!this._content) { return; }
|
||||||
|
|
||||||
|
@ -4,7 +4,6 @@ import * as DomUtil from '../dom/DomUtil';
|
|||||||
import {Point, toPoint} from '../geometry/Point';
|
import {Point, toPoint} from '../geometry/Point';
|
||||||
import {Map} from '../map/Map';
|
import {Map} from '../map/Map';
|
||||||
import {Layer} from './Layer';
|
import {Layer} from './Layer';
|
||||||
import {FeatureGroup} from './FeatureGroup';
|
|
||||||
import * as Util from '../core/Util';
|
import * as Util from '../core/Util';
|
||||||
import {Path} from './vector/Path';
|
import {Path} from './vector/Path';
|
||||||
|
|
||||||
@ -418,28 +417,8 @@ Layer.include({
|
|||||||
// @method openPopup(latlng?: LatLng): this
|
// @method openPopup(latlng?: LatLng): this
|
||||||
// Opens the bound popup at the specified `latlng` or at the default popup anchor if no `latlng` is passed.
|
// Opens the bound popup at the specified `latlng` or at the default popup anchor if no `latlng` is passed.
|
||||||
openPopup: function (layer, latlng) {
|
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) {
|
if (this._popup && this._map) {
|
||||||
// set popup source to this layer
|
latlng = this._popup._prepareOpen(this, layer, latlng);
|
||||||
this._popup._source = layer;
|
|
||||||
|
|
||||||
// update the popup (content, layout, ect...)
|
|
||||||
this._popup.update();
|
|
||||||
|
|
||||||
// open the popup on the map
|
// open the popup on the map
|
||||||
this._map.openPopup(this._popup, latlng);
|
this._map.openPopup(this._popup, latlng);
|
||||||
|
@ -4,7 +4,6 @@ import {DivOverlay} from './DivOverlay';
|
|||||||
import {toPoint} from '../geometry/Point';
|
import {toPoint} from '../geometry/Point';
|
||||||
import {Map} from '../map/Map';
|
import {Map} from '../map/Map';
|
||||||
import {Layer} from './Layer';
|
import {Layer} from './Layer';
|
||||||
import {FeatureGroup} from './FeatureGroup';
|
|
||||||
import * as Util from '../core/Util';
|
import * as Util from '../core/Util';
|
||||||
import * as DomUtil from '../dom/DomUtil';
|
import * as DomUtil from '../dom/DomUtil';
|
||||||
|
|
||||||
@ -312,29 +311,8 @@ Layer.include({
|
|||||||
// @method openTooltip(latlng?: LatLng): this
|
// @method openTooltip(latlng?: LatLng): this
|
||||||
// Opens the bound tooltip at the specified `latlng` or at the default tooltip anchor if no `latlng` is passed.
|
// Opens the bound tooltip at the specified `latlng` or at the default tooltip anchor if no `latlng` is passed.
|
||||||
openTooltip: function (layer, latlng) {
|
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) {
|
if (this._tooltip && this._map) {
|
||||||
|
latlng = this._tooltip._prepareOpen(this, layer, latlng);
|
||||||
// set tooltip source to this layer
|
|
||||||
this._tooltip._source = layer;
|
|
||||||
|
|
||||||
// update the tooltip (content, layout, ect...)
|
|
||||||
this._tooltip.update();
|
|
||||||
|
|
||||||
// open the tooltip on the map
|
// open the tooltip on the map
|
||||||
this._map.openTooltip(this._tooltip, latlng);
|
this._map.openTooltip(this._tooltip, latlng);
|
||||||
|
Reference in New Issue
Block a user