4cb14e3c0e77ddee4606a2b41a1727080e732024
2 L
.Popup
= L
.Class
.extend({
3 includes
: L
.Mixin
.Events
,
10 offset
: new L
.Point(0, 2),
11 autoPanPadding
: new L
.Point(5, 5)
14 initialize: function(options
) {
15 L
.Util
.setOptions(this, options
);
18 onAdd: function(map
) {
20 if (!this._container
) {
23 this._updateContent();
25 this._container
.style
.opacity
= '0';
27 this._map
._panes
.popupPane
.appendChild(this._container
);
28 this._map
.on('viewreset', this._updatePosition
, this);
29 if (this._map
.options
.closePopupOnClick
) {
30 this._map
.on('preclick', this._close
, this);
34 this._container
.style
.opacity
= '1'; //TODO fix ugly opacity hack
39 onRemove: function(map
) {
40 map
._panes
.popupPane
.removeChild(this._container
);
41 map
.off('viewreset', this._updatePosition
, this);
42 map
.off('click', this._close
, this);
44 this._container
.style
.opacity
= '0';
49 setLatLng: function(latlng
) {
50 this._latlng
= latlng
;
57 setContent: function(content
) {
58 this._content
= content
;
67 this._map
.removeLayer(this);
71 _initLayout: function() {
72 this._container
= L
.DomUtil
.create('div', 'leaflet-popup');
74 this._closeButton
= L
.DomUtil
.create('a', 'leaflet-popup-close-button', this._container
);
75 this._closeButton
.href
= '#close';
76 this._closeButton
.onclick
= L
.Util
.bind(this._onCloseButtonClick
, this);
78 this._wrapper
= L
.DomUtil
.create('div', 'leaflet-popup-content-wrapper', this._container
);
79 L
.DomEvent
.disableClickPropagation(this._wrapper
);
80 this._contentNode
= L
.DomUtil
.create('div', 'leaflet-popup-content', this._wrapper
);
82 this._tipContainer
= L
.DomUtil
.create('div', 'leaflet-popup-tip-container', this._container
);
83 this._tip
= L
.DomUtil
.create('div', 'leaflet-popup-tip', this._tipContainer
);
87 this._container
.style
.visibility
= 'hidden';
89 this._updateContent();
91 this._updatePosition();
93 this._container
.style
.visibility
= '';
98 _updateContent: function() {
99 if (!this._content
) return;
101 if (typeof this._content
== 'string') {
102 this._contentNode
.innerHTML
= this._content
;
104 this._contentNode
.innerHTML
= '';
105 this._contentNode
.appendChild(this._content
);
109 _updateLayout: function() {
110 this._container
.style
.width
= '';
111 this._container
.style
.whiteSpace
= 'nowrap';
113 var width
= this._container
.offsetWidth
;
115 this._container
.style
.width
= (width
> this.options
.maxWidth
? this.options
.maxWidth
: width
) + 'px';
116 this._container
.style
.whiteSpace
= '';
118 this._containerWidth
= this._container
.offsetWidth
;
121 _updatePosition: function() {
122 var pos
= this._map
.latLngToLayerPoint(this._latlng
);
124 this._containerBottom
= -pos
.y
- this.options
.offset
.y
;
125 this._containerLeft
= pos
.x
- Math
.round(this._containerWidth
/2) + this.options
.offset
.x
;
127 this._container
.style
.bottom
= this._containerBottom
+ 'px';
128 this._container
.style
.left
= this._containerLeft
+ 'px';
131 _adjustPan: function() {
132 if (!this.options
.autoPan
) { return; }
134 var containerHeight
= this._container
.offsetHeight
,
135 layerPos
= new L
.Point(
137 -containerHeight
- this._containerBottom
),
138 containerPos
= this._map
.layerPointToContainerPoint(layerPos
),
139 adjustOffset
= new L
.Point(0, 0),
140 padding
= this.options
.autoPanPadding
,
141 size
= this._map
.getSize();
143 if (containerPos
.x
< 0) {
144 adjustOffset
.x
= containerPos
.x
- padding
.x
;
146 if (containerPos
.x
+ this._containerWidth
> size
.x
) {
147 adjustOffset
.x
= containerPos
.x
+ this._containerWidth
- size
.x
+ padding
.x
;
149 if (containerPos
.y
< 0) {
150 adjustOffset
.y
= containerPos
.y
- padding
.y
;
152 if (containerPos
.y
+ containerHeight
> size
.y
) {
153 adjustOffset
.y
= containerPos
.y
+ containerHeight
- size
.y
+ padding
.y
;
156 if (adjustOffset
.x
|| adjustOffset
.y
) {
157 this._map
.panBy(adjustOffset
);
161 _onCloseButtonClick: function(e
) {