Skip to content

Commit

Permalink
Layer: refactor openPopup/openTooltip: use common function (#6613)
Browse files Browse the repository at this point in the history
* Layer: refactor openPopup/openTooltip: use common function DivOverlay._prepareOpen

* More clear error message when trying to attach popup/tooltip to layer
without getLatLng function
  • Loading branch information
johnd0e authored and cherniavskii committed Apr 30, 2019
1 parent d9a4c66 commit 1e0d43b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 45 deletions.
33 changes: 33 additions & 0 deletions 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';
Expand Down Expand Up @@ -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; }

Expand Down
23 changes: 1 addition & 22 deletions src/layer/Popup.js
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
Expand Down
24 changes: 1 addition & 23 deletions src/layer/Tooltip.js
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1e0d43b

Please sign in to comment.