|
|
import {DivOverlay} from './DivOverlay';
|
|
|
import * as DomEvent from '../dom/DomEvent';
|
|
|
import * as DomUtil from '../dom/DomUtil';
|
|
|
import {Point, toPoint} from '../geometry/Point';
|
|
|
import {Map} from '../map/Map';
|
|
|
import {Layer} from './Layer';
|
|
|
import {Path} from './vector/Path';
|
|
|
import {FeatureGroup} from './FeatureGroup';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export var Popup = DivOverlay.extend({
|
|
|
|
|
|
|
|
|
|
|
|
options: {
|
|
|
|
|
|
|
|
|
pane: 'popupPane',
|
|
|
|
|
|
|
|
|
|
|
|
offset: [0, 7],
|
|
|
|
|
|
|
|
|
|
|
|
maxWidth: 300,
|
|
|
|
|
|
|
|
|
|
|
|
minWidth: 50,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
maxHeight: null,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
autoPan: true,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
autoPanPaddingTopLeft: null,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
autoPanPaddingBottomRight: null,
|
|
|
|
|
|
|
|
|
|
|
|
autoPanPadding: [5, 5],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
keepInView: false,
|
|
|
|
|
|
|
|
|
|
|
|
closeButton: true,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
autoClose: true,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
closeOnEscapeKey: true,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
className: ''
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
openOn: function (map) {
|
|
|
map = arguments.length ? map : this._source._map;
|
|
|
|
|
|
if (!map.hasLayer(this) && map._popup && map._popup.options.autoClose) {
|
|
|
map.removeLayer(map._popup);
|
|
|
}
|
|
|
map._popup = this;
|
|
|
|
|
|
return DivOverlay.prototype.openOn.call(this, map);
|
|
|
},
|
|
|
|
|
|
onAdd: function (map) {
|
|
|
DivOverlay.prototype.onAdd.call(this, map);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
map.fire('popupopen', {popup: this});
|
|
|
|
|
|
if (this._source) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._source.fire('popupopen', {popup: this}, true);
|
|
|
|
|
|
|
|
|
if (!(this._source instanceof Path)) {
|
|
|
this._source.on('preclick', DomEvent.stopPropagation);
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
|
|
|
onRemove: function (map) {
|
|
|
DivOverlay.prototype.onRemove.call(this, map);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
map.fire('popupclose', {popup: this});
|
|
|
|
|
|
if (this._source) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._source.fire('popupclose', {popup: this}, true);
|
|
|
if (!(this._source instanceof Path)) {
|
|
|
this._source.off('preclick', DomEvent.stopPropagation);
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
|
|
|
getEvents: function () {
|
|
|
var events = DivOverlay.prototype.getEvents.call(this);
|
|
|
|
|
|
if (this.options.closeOnClick !== undefined ? this.options.closeOnClick : this._map.options.closePopupOnClick) {
|
|
|
events.preclick = this.close;
|
|
|
}
|
|
|
|
|
|
if (this.options.keepInView) {
|
|
|
events.moveend = this._adjustPan;
|
|
|
}
|
|
|
|
|
|
return events;
|
|
|
},
|
|
|
|
|
|
_initLayout: function () {
|
|
|
var prefix = 'leaflet-popup',
|
|
|
container = this._container = DomUtil.create('div',
|
|
|
prefix + ' ' + (this.options.className || '') +
|
|
|
' leaflet-zoom-animated');
|
|
|
|
|
|
var wrapper = this._wrapper = DomUtil.create('div', prefix + '-content-wrapper', container);
|
|
|
this._contentNode = DomUtil.create('div', prefix + '-content', wrapper);
|
|
|
|
|
|
DomEvent.disableClickPropagation(container);
|
|
|
DomEvent.disableScrollPropagation(this._contentNode);
|
|
|
DomEvent.on(container, 'contextmenu', DomEvent.stopPropagation);
|
|
|
|
|
|
this._tipContainer = DomUtil.create('div', prefix + '-tip-container', container);
|
|
|
this._tip = DomUtil.create('div', prefix + '-tip', this._tipContainer);
|
|
|
|
|
|
if (this.options.closeButton) {
|
|
|
var closeButton = this._closeButton = DomUtil.create('a', prefix + '-close-button', container);
|
|
|
closeButton.setAttribute('role', 'button');
|
|
|
closeButton.setAttribute('aria-label', 'Close popup');
|
|
|
closeButton.href = '#close';
|
|
|
closeButton.innerHTML = '<span aria-hidden="true">×</span>';
|
|
|
|
|
|
DomEvent.on(closeButton, 'click', function (ev) {
|
|
|
DomEvent.preventDefault(ev);
|
|
|
this.close();
|
|
|
}, this);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
_updateLayout: function () {
|
|
|
var container = this._contentNode,
|
|
|
style = container.style;
|
|
|
|
|
|
style.width = '';
|
|
|
style.whiteSpace = 'nowrap';
|
|
|
|
|
|
var width = container.offsetWidth;
|
|
|
width = Math.min(width, this.options.maxWidth);
|
|
|
width = Math.max(width, this.options.minWidth);
|
|
|
|
|
|
style.width = (width + 1) + 'px';
|
|
|
style.whiteSpace = '';
|
|
|
|
|
|
style.height = '';
|
|
|
|
|
|
var height = container.offsetHeight,
|
|
|
maxHeight = this.options.maxHeight,
|
|
|
scrolledClass = 'leaflet-popup-scrolled';
|
|
|
|
|
|
if (maxHeight && height > maxHeight) {
|
|
|
style.height = maxHeight + 'px';
|
|
|
DomUtil.addClass(container, scrolledClass);
|
|
|
} else {
|
|
|
DomUtil.removeClass(container, scrolledClass);
|
|
|
}
|
|
|
|
|
|
this._containerWidth = this._container.offsetWidth;
|
|
|
},
|
|
|
|
|
|
_animateZoom: function (e) {
|
|
|
var pos = this._map._latLngToNewLayerPoint(this._latlng, e.zoom, e.center),
|
|
|
anchor = this._getAnchor();
|
|
|
DomUtil.setPosition(this._container, pos.add(anchor));
|
|
|
},
|
|
|
|
|
|
_adjustPan: function () {
|
|
|
if (!this.options.autoPan) { return; }
|
|
|
if (this._map._panAnim) { this._map._panAnim.stop(); }
|
|
|
|
|
|
|
|
|
|
|
|
if (this._autopanning) {
|
|
|
this._autopanning = false;
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
var map = this._map,
|
|
|
marginBottom = parseInt(DomUtil.getStyle(this._container, 'marginBottom'), 10) || 0,
|
|
|
containerHeight = this._container.offsetHeight + marginBottom,
|
|
|
containerWidth = this._containerWidth,
|
|
|
layerPos = new Point(this._containerLeft, -containerHeight - this._containerBottom);
|
|
|
|
|
|
layerPos._add(DomUtil.getPosition(this._container));
|
|
|
|
|
|
var containerPos = map.layerPointToContainerPoint(layerPos),
|
|
|
padding = toPoint(this.options.autoPanPadding),
|
|
|
paddingTL = toPoint(this.options.autoPanPaddingTopLeft || padding),
|
|
|
paddingBR = toPoint(this.options.autoPanPaddingBottomRight || padding),
|
|
|
size = map.getSize(),
|
|
|
dx = 0,
|
|
|
dy = 0;
|
|
|
|
|
|
if (containerPos.x + containerWidth + paddingBR.x > size.x) {
|
|
|
dx = containerPos.x + containerWidth - size.x + paddingBR.x;
|
|
|
}
|
|
|
if (containerPos.x - dx - paddingTL.x < 0) {
|
|
|
dx = containerPos.x - paddingTL.x;
|
|
|
}
|
|
|
if (containerPos.y + containerHeight + paddingBR.y > size.y) {
|
|
|
dy = containerPos.y + containerHeight - size.y + paddingBR.y;
|
|
|
}
|
|
|
if (containerPos.y - dy - paddingTL.y < 0) {
|
|
|
dy = containerPos.y - paddingTL.y;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (dx || dy) {
|
|
|
|
|
|
if (this.options.keepInView) {
|
|
|
this._autopanning = true;
|
|
|
}
|
|
|
|
|
|
map
|
|
|
.fire('autopanstart')
|
|
|
.panBy([dx, dy]);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
_getAnchor: function () {
|
|
|
|
|
|
return toPoint(this._source && this._source._getPopupAnchor ? this._source._getPopupAnchor() : [0, 0]);
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export var popup = function (options, source) {
|
|
|
return new Popup(options, source);
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Map.mergeOptions({
|
|
|
closePopupOnClick: true
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Map.include({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
openPopup: function (popup, latlng, options) {
|
|
|
this._initOverlay(Popup, popup, latlng, options)
|
|
|
.openOn(this);
|
|
|
|
|
|
return this;
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
closePopup: function (popup) {
|
|
|
popup = arguments.length ? popup : this._popup;
|
|
|
if (popup) {
|
|
|
popup.close();
|
|
|
}
|
|
|
return this;
|
|
|
}
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Layer.include({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bindPopup: function (content, options) {
|
|
|
this._popup = this._initOverlay(Popup, this._popup, content, options);
|
|
|
if (!this._popupHandlersAdded) {
|
|
|
this.on({
|
|
|
click: this._openPopup,
|
|
|
keypress: this._onKeyPress,
|
|
|
remove: this.closePopup,
|
|
|
move: this._movePopup
|
|
|
});
|
|
|
this._popupHandlersAdded = true;
|
|
|
}
|
|
|
|
|
|
return this;
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
unbindPopup: function () {
|
|
|
if (this._popup) {
|
|
|
this.off({
|
|
|
click: this._openPopup,
|
|
|
keypress: this._onKeyPress,
|
|
|
remove: this.closePopup,
|
|
|
move: this._movePopup
|
|
|
});
|
|
|
this._popupHandlersAdded = false;
|
|
|
this._popup = null;
|
|
|
}
|
|
|
return this;
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
openPopup: function (latlng) {
|
|
|
if (this._popup) {
|
|
|
if (!(this instanceof FeatureGroup)) {
|
|
|
this._popup._source = this;
|
|
|
}
|
|
|
if (this._popup._prepareOpen(latlng || this._latlng)) {
|
|
|
|
|
|
this._popup.openOn(this._map);
|
|
|
}
|
|
|
}
|
|
|
return this;
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
closePopup: function () {
|
|
|
if (this._popup) {
|
|
|
this._popup.close();
|
|
|
}
|
|
|
return this;
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
togglePopup: function () {
|
|
|
if (this._popup) {
|
|
|
this._popup.toggle(this);
|
|
|
}
|
|
|
return this;
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
isPopupOpen: function () {
|
|
|
return (this._popup ? this._popup.isOpen() : false);
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
setPopupContent: function (content) {
|
|
|
if (this._popup) {
|
|
|
this._popup.setContent(content);
|
|
|
}
|
|
|
return this;
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
getPopup: function () {
|
|
|
return this._popup;
|
|
|
},
|
|
|
|
|
|
_openPopup: function (e) {
|
|
|
if (!this._popup || !this._map) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
DomEvent.stop(e);
|
|
|
|
|
|
var target = e.layer || e.target;
|
|
|
if (this._popup._source === target && !(target instanceof Path)) {
|
|
|
|
|
|
|
|
|
if (this._map.hasLayer(this._popup)) {
|
|
|
this.closePopup();
|
|
|
} else {
|
|
|
this.openPopup(e.latlng);
|
|
|
}
|
|
|
return;
|
|
|
}
|
|
|
this._popup._source = target;
|
|
|
this.openPopup(e.latlng);
|
|
|
},
|
|
|
|
|
|
_movePopup: function (e) {
|
|
|
this._popup.setLatLng(e.latlng);
|
|
|
},
|
|
|
|
|
|
_onKeyPress: function (e) {
|
|
|
if (e.originalEvent.keyCode === 13) {
|
|
|
this._openPopup(e);
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
|