Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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