From 69188e8fb3629af719058074317f95caa4e256d0 Mon Sep 17 00:00:00 2001 From: johnd0e Date: Tue, 30 Apr 2019 12:56:20 +0300 Subject: [PATCH] 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 --- src/layer/DivOverlay.js | 33 +++++++++++++++++++++++++++++++++ src/layer/Popup.js | 23 +---------------------- src/layer/Tooltip.js | 24 +----------------------- 3 files changed, 35 insertions(+), 45 deletions(-) diff --git a/src/layer/DivOverlay.js b/src/layer/DivOverlay.js index efd76f1bba2..39fc5739c98 100644 --- a/src/layer/DivOverlay.js +++ b/src/layer/DivOverlay.js @@ -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; } diff --git a/src/layer/Popup.js b/src/layer/Popup.js index 4eed622d1ba..caec6cec577 100644 --- a/src/layer/Popup.js +++ b/src/layer/Popup.js @@ -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); diff --git a/src/layer/Tooltip.js b/src/layer/Tooltip.js index 5eb83f9bcbb..de85348cd37 100644 --- a/src/layer/Tooltip.js +++ b/src/layer/Tooltip.js @@ -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);