|
|
import {Map} from '../map/Map';
|
|
|
import {Layer} from './Layer';
|
|
|
import {FeatureGroup} from './FeatureGroup';
|
|
|
import * as Util from '../core/Util';
|
|
|
import {toLatLng, LatLng} from '../geo/LatLng';
|
|
|
import {toPoint} from '../geometry/Point';
|
|
|
import * as DomUtil from '../dom/DomUtil';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export var DivOverlay = Layer.extend({
|
|
|
|
|
|
|
|
|
|
|
|
options: {
|
|
|
|
|
|
|
|
|
interactive: false,
|
|
|
|
|
|
|
|
|
|
|
|
offset: [0, 0],
|
|
|
|
|
|
|
|
|
|
|
|
className: '',
|
|
|
|
|
|
|
|
|
|
|
|
pane: undefined,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
content: ''
|
|
|
},
|
|
|
|
|
|
initialize: function (options, source) {
|
|
|
if (options && (options instanceof LatLng || Util.isArray(options))) {
|
|
|
this._latlng = toLatLng(options);
|
|
|
Util.setOptions(this, source);
|
|
|
} else {
|
|
|
Util.setOptions(this, options);
|
|
|
this._source = source;
|
|
|
}
|
|
|
if (this.options.content) {
|
|
|
this._content = this.options.content;
|
|
|
}
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
openOn: function (map) {
|
|
|
map = arguments.length ? map : this._source._map;
|
|
|
if (!map.hasLayer(this)) {
|
|
|
map.addLayer(this);
|
|
|
}
|
|
|
return this;
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
close: function () {
|
|
|
if (this._map) {
|
|
|
this._map.removeLayer(this);
|
|
|
}
|
|
|
return this;
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
toggle: function (layer) {
|
|
|
if (this._map) {
|
|
|
this.close();
|
|
|
} else {
|
|
|
if (arguments.length) {
|
|
|
this._source = layer;
|
|
|
} else {
|
|
|
layer = this._source;
|
|
|
}
|
|
|
this._prepareOpen();
|
|
|
|
|
|
|
|
|
this.openOn(layer._map);
|
|
|
}
|
|
|
return this;
|
|
|
},
|
|
|
|
|
|
onAdd: function (map) {
|
|
|
this._zoomAnimated = map._zoomAnimated;
|
|
|
|
|
|
if (!this._container) {
|
|
|
this._initLayout();
|
|
|
}
|
|
|
|
|
|
if (map._fadeAnimated) {
|
|
|
DomUtil.setOpacity(this._container, 0);
|
|
|
}
|
|
|
|
|
|
clearTimeout(this._removeTimeout);
|
|
|
this.getPane().appendChild(this._container);
|
|
|
this.update();
|
|
|
|
|
|
if (map._fadeAnimated) {
|
|
|
DomUtil.setOpacity(this._container, 1);
|
|
|
}
|
|
|
|
|
|
this.bringToFront();
|
|
|
|
|
|
if (this.options.interactive) {
|
|
|
DomUtil.addClass(this._container, 'leaflet-interactive');
|
|
|
this.addInteractiveTarget(this._container);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
onRemove: function (map) {
|
|
|
if (map._fadeAnimated) {
|
|
|
DomUtil.setOpacity(this._container, 0);
|
|
|
this._removeTimeout = setTimeout(Util.bind(DomUtil.remove, undefined, this._container), 200);
|
|
|
} else {
|
|
|
DomUtil.remove(this._container);
|
|
|
}
|
|
|
|
|
|
if (this.options.interactive) {
|
|
|
DomUtil.removeClass(this._container, 'leaflet-interactive');
|
|
|
this.removeInteractiveTarget(this._container);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getLatLng: function () {
|
|
|
return this._latlng;
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
setLatLng: function (latlng) {
|
|
|
this._latlng = toLatLng(latlng);
|
|
|
if (this._map) {
|
|
|
this._updatePosition();
|
|
|
this._adjustPan();
|
|
|
}
|
|
|
return this;
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
getContent: function () {
|
|
|
return this._content;
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setContent: function (content) {
|
|
|
this._content = content;
|
|
|
this.update();
|
|
|
return this;
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
getElement: function () {
|
|
|
return this._container;
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
update: function () {
|
|
|
if (!this._map) { return; }
|
|
|
|
|
|
this._container.style.visibility = 'hidden';
|
|
|
|
|
|
this._updateContent();
|
|
|
this._updateLayout();
|
|
|
this._updatePosition();
|
|
|
|
|
|
this._container.style.visibility = '';
|
|
|
|
|
|
this._adjustPan();
|
|
|
},
|
|
|
|
|
|
getEvents: function () {
|
|
|
var events = {
|
|
|
zoom: this._updatePosition,
|
|
|
viewreset: this._updatePosition
|
|
|
};
|
|
|
|
|
|
if (this._zoomAnimated) {
|
|
|
events.zoomanim = this._animateZoom;
|
|
|
}
|
|
|
return events;
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
isOpen: function () {
|
|
|
return !!this._map && this._map.hasLayer(this);
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
bringToFront: function () {
|
|
|
if (this._map) {
|
|
|
DomUtil.toFront(this._container);
|
|
|
}
|
|
|
return this;
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
bringToBack: function () {
|
|
|
if (this._map) {
|
|
|
DomUtil.toBack(this._container);
|
|
|
}
|
|
|
return this;
|
|
|
},
|
|
|
|
|
|
|
|
|
_prepareOpen: function (latlng) {
|
|
|
var source = this._source;
|
|
|
if (!source._map) { return false; }
|
|
|
|
|
|
if (source instanceof FeatureGroup) {
|
|
|
source = null;
|
|
|
var layers = this._source._layers;
|
|
|
for (var id in layers) {
|
|
|
if (layers[id]._map) {
|
|
|
source = layers[id];
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
if (!source) { return false; }
|
|
|
|
|
|
|
|
|
this._source = source;
|
|
|
}
|
|
|
|
|
|
if (!latlng) {
|
|
|
if (source.getCenter) {
|
|
|
latlng = source.getCenter();
|
|
|
} else if (source.getLatLng) {
|
|
|
latlng = source.getLatLng();
|
|
|
} else if (source.getBounds) {
|
|
|
latlng = source.getBounds().getCenter();
|
|
|
} else {
|
|
|
throw new Error('Unable to get source layer LatLng.');
|
|
|
}
|
|
|
}
|
|
|
this.setLatLng(latlng);
|
|
|
|
|
|
if (this._map) {
|
|
|
|
|
|
this.update();
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
},
|
|
|
|
|
|
_updateContent: function () {
|
|
|
if (!this._content) { return; }
|
|
|
|
|
|
var node = this._contentNode;
|
|
|
var content = (typeof this._content === 'function') ? this._content(this._source || this) : this._content;
|
|
|
|
|
|
if (typeof content === 'string') {
|
|
|
node.innerHTML = content;
|
|
|
} else {
|
|
|
while (node.hasChildNodes()) {
|
|
|
node.removeChild(node.firstChild);
|
|
|
}
|
|
|
node.appendChild(content);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.fire('contentupdate');
|
|
|
},
|
|
|
|
|
|
_updatePosition: function () {
|
|
|
if (!this._map) { return; }
|
|
|
|
|
|
var pos = this._map.latLngToLayerPoint(this._latlng),
|
|
|
offset = toPoint(this.options.offset),
|
|
|
anchor = this._getAnchor();
|
|
|
|
|
|
if (this._zoomAnimated) {
|
|
|
DomUtil.setPosition(this._container, pos.add(anchor));
|
|
|
} else {
|
|
|
offset = offset.add(pos).add(anchor);
|
|
|
}
|
|
|
|
|
|
var bottom = this._containerBottom = -offset.y,
|
|
|
left = this._containerLeft = -Math.round(this._containerWidth / 2) + offset.x;
|
|
|
|
|
|
|
|
|
this._container.style.bottom = bottom + 'px';
|
|
|
this._container.style.left = left + 'px';
|
|
|
},
|
|
|
|
|
|
_getAnchor: function () {
|
|
|
return [0, 0];
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
Map.include({
|
|
|
_initOverlay: function (OverlayClass, content, latlng, options) {
|
|
|
var overlay = content;
|
|
|
if (!(overlay instanceof OverlayClass)) {
|
|
|
overlay = new OverlayClass(options).setContent(content);
|
|
|
}
|
|
|
if (latlng) {
|
|
|
overlay.setLatLng(latlng);
|
|
|
}
|
|
|
return overlay;
|
|
|
}
|
|
|
});
|
|
|
|
|
|
|
|
|
Layer.include({
|
|
|
_initOverlay: function (OverlayClass, old, content, options) {
|
|
|
var overlay = content;
|
|
|
if (overlay instanceof OverlayClass) {
|
|
|
Util.setOptions(overlay, options);
|
|
|
overlay._source = this;
|
|
|
} else {
|
|
|
overlay = (old && !options) ? old : new OverlayClass(options, this);
|
|
|
overlay.setContent(content);
|
|
|
}
|
|
|
return overlay;
|
|
|
}
|
|
|
});
|
|
|
|