forked from ReaverXR/leaflet-responsive-popup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleaflet.responsive.popup.js
270 lines (240 loc) · 11.1 KB
/
leaflet.responsive.popup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
leaflet.responsive.popup 0.6.4
(c) 2019 https://github.com/yafred
*/
L.ResponsivePopup = L.Popup.extend({
options: {
hasTip: true
/*
* Inherited from L.Popup
*
* - offset
*
* - autoPanPadding
* - autoPanPaddingTopLeft
* - autoPanPaddingBottomRight
*/
},
/**
* Overrides https://github.com/Leaflet/Leaflet/blob/v1.3.4/src/layer/Popup.js#L176
* This is to add hasTip option
*/
_initLayout: function () {
var prefix = 'leaflet-popup',
container = this._container = L.DomUtil.create('div',
prefix + ' ' + (this.options.className || '') +
' leaflet-zoom-animated');
var wrapper = this._wrapper = L.DomUtil.create('div', prefix + '-content-wrapper', container);
this._contentNode = L.DomUtil.create('div', prefix + '-content', wrapper);
L.DomEvent.disableClickPropagation(wrapper);
L.DomEvent.disableScrollPropagation(this._contentNode);
L.DomEvent.on(wrapper, 'contextmenu', L.DomEvent.stopPropagation);
this._tipContainer = L.DomUtil.create('div', prefix + '-tip-container', container);
if(!this.options.hasTip) {
this._tipContainer.style.visibility = 'hidden';
}
this._tip = L.DomUtil.create('div', prefix + '-tip', this._tipContainer);
if (this.options.closeButton) {
var closeButton = this._closeButton = L.DomUtil.create('a', prefix + '-close-button', container);
closeButton.href = '#close';
closeButton.innerHTML = '×';
L.DomEvent.on(closeButton, 'click', this._onCloseButtonClick, this);
}
},
/**
* Overrides https://github.com/Leaflet/Leaflet/blob/v1.3.4/src/layer/DivOverlay.js#L178
*/
_updatePosition: function () {
if (!this._map) { return; }
var pos = this._map.latLngToLayerPoint(this._latlng),
basePoint = this._map.layerPointToContainerPoint(pos),
containerWidth = this._container.offsetWidth,
containerHeight = this._container.offsetHeight,
padding = L.point(this.options.autoPanPadding),
paddingTL = L.point(this.options.autoPanPaddingTopLeft || padding),
paddingBR = L.point(this.options.autoPanPaddingBottomRight || padding),
mapSize = this._map.getSize(),
anchor = this._getAnchor(), // popup anchor
offset = L.point(this.options.offset); // offset relative to anchor (option from L.DivOverlay. We only use absolute values).
// Leaflet default dimensions (should not be hard coded in the future)
var tipHeight = 11; //px
var tipWidth = 22; //px
var containerRadius = 12; //px
// Tweak offset to include tip dimensions
var offsetX = Math.abs(offset.x);
var offsetY = Math.abs(offset.y);
if(this.options.hasTip) {
offsetX += tipHeight;
offsetY += tipHeight;
// clear CSS
L.DomUtil.removeClass(this._container, 'leaflet-resp-popup-north');
L.DomUtil.removeClass(this._container, 'leaflet-resp-popup-south');
L.DomUtil.removeClass(this._container, 'leaflet-resp-popup-east');
L.DomUtil.removeClass(this._container, 'leaflet-resp-popup-west');
L.DomUtil.removeClass(this._container, 'leaflet-resp-popup-north-east');
L.DomUtil.removeClass(this._container, 'leaflet-resp-popup-north-west');
L.DomUtil.removeClass(this._container, 'leaflet-resp-popup-south-east');
L.DomUtil.removeClass(this._container, 'leaflet-resp-popup-south-west');
L.DomUtil.removeClass(this._container, 'leaflet-resp-popup-east-north');
L.DomUtil.removeClass(this._container, 'leaflet-resp-popup-east-south');
L.DomUtil.removeClass(this._container, 'leaflet-resp-popup-west-north');
L.DomUtil.removeClass(this._container, 'leaflet-resp-popup-west-south');
// this._container.style.display = 'initial'; // this does not work
}
// Where can we fit the popup ?
var canGoTop = true,
canGoBottom = true,
canGoLeft = true,
canGoRight = true,
containerPos = false;
if(basePoint.y + anchor.y - offsetY - containerHeight - Math.abs(paddingTL.y) < 0) {
canGoTop = false;
}
if(basePoint.y + anchor.y + offsetY + containerHeight + Math.abs(paddingBR.y) > mapSize.y) {
canGoBottom = false;
}
if(basePoint.x + anchor.x - offsetX - containerWidth - Math.abs(paddingTL.x) < 0) {
canGoLeft = false;
}
if(basePoint.x + anchor.x + offsetX + containerWidth + Math.abs(paddingBR.x) > mapSize.x) {
canGoRight = false;
}
// manage overflows
var subtractX = containerWidth / 2 - anchor.x,
subtractY = containerHeight / 2 - anchor.y;
if(canGoTop || canGoBottom) {
var containerLeft = basePoint.x + anchor.x - (containerWidth / 2);
var containerRight = basePoint.x + anchor.x + (containerWidth / 2);
if(containerLeft < Math.abs(paddingTL.x)) { // left overflow
subtractX = containerWidth / 2 - anchor.x - Math.abs(paddingTL.x) + containerLeft;
}
if(containerRight > mapSize.x - Math.abs(paddingBR.x)) { // right overflow
subtractX = containerWidth / 2 - anchor.x + containerRight - mapSize.x + Math.abs(paddingBR.x);
}
}
if(canGoLeft || canGoRight) {
var containerTop = basePoint.y + anchor.y - (containerHeight / 2);
var containerBottom = basePoint.y + anchor.y + (containerHeight / 2);
if(containerTop < Math.abs(paddingTL.y)) { // top overflow
subtractY = containerHeight / 2 - anchor.y - Math.abs(paddingTL.y) + containerTop;
}
if(containerBottom > mapSize.y - Math.abs(paddingBR.y)) { // bottom overflow
subtractY = containerHeight / 2 - anchor.y + containerBottom - mapSize.y + Math.abs(paddingBR.y);
}
}
// position the popup (order of preference is: top, left, bottom, right, centerOnMap)
if(canGoTop) {
containerPos = pos.subtract(L.point(subtractX, -anchor.y + containerHeight + offsetY, true));
if(this.options.hasTip) {
if(basePoint.x + anchor.x < paddingTL.x + containerRadius + tipWidth/2) {
containerPos.x = pos.x + anchor.x;
L.DomUtil.addClass(this._container, 'leaflet-resp-popup-north-east');
this._tipContainer.style.top = containerHeight + 'px';
this._tipContainer.style.left = '0px';
}
else if(basePoint.x + anchor.x > mapSize.x - paddingBR.x - containerRadius - tipWidth/2) {
containerPos.x = pos.x + anchor.x - containerWidth;
L.DomUtil.addClass(this._container, 'leaflet-resp-popup-north-west');
this._tipContainer.style.top = containerHeight + 'px';
this._tipContainer.style.left = containerWidth + 'px';
}
else {
L.DomUtil.addClass(this._container, 'leaflet-resp-popup-north');
this._tipContainer.style.top = containerHeight + 'px';
this._tipContainer.style.left = (pos.x + anchor.x - containerPos.x) + 'px';
}
}
}
else if(canGoLeft) {
containerPos = pos.subtract(L.point(-anchor.x + containerWidth + offsetX, subtractY, true));
if(this.options.hasTip) {
if(basePoint.y + anchor.y < paddingTL.y + containerRadius + tipWidth/2) {
containerPos.y = pos.y + anchor.y;
L.DomUtil.addClass(this._container, 'leaflet-resp-popup-west-south');
this._tipContainer.style.top = '0px';
this._tipContainer.style.left = containerWidth + 'px';
}
else if(basePoint.y + anchor.y > mapSize.y - paddingBR.y - containerRadius - tipWidth/2) {
containerPos.y = pos.y + anchor.y - containerHeight;
L.DomUtil.addClass(this._container, 'leaflet-resp-popup-west-north');
this._tipContainer.style.top = containerHeight + 'px';
this._tipContainer.style.left = containerWidth + 'px';
}
else {
L.DomUtil.addClass(this._container, 'leaflet-resp-popup-west');
this._tipContainer.style.top = (pos.y + anchor.y - containerPos.y) + 'px';
this._tipContainer.style.left = containerWidth + 'px';
}
}
}
else if(canGoBottom) {
containerPos = pos.subtract(L.point(subtractX, -anchor.y - offsetY, true));
if(this.options.hasTip) {
if(basePoint.x + anchor.x < paddingTL.x + containerRadius + tipWidth/2) {
containerPos.x = pos.x + anchor.x;
L.DomUtil.addClass(this._container, 'leaflet-resp-popup-south-east');
this._tipContainer.style.top = '0px';
this._tipContainer.style.left = '0px';
}
else if(basePoint.x + anchor.x > mapSize.x - paddingBR.x - containerRadius - tipWidth/2) {
containerPos.x = pos.x + anchor.x - containerWidth;
L.DomUtil.addClass(this._container, 'leaflet-resp-popup-south-west');
this._tipContainer.style.top = '0px';
this._tipContainer.style.left = containerWidth + 'px';
}
else {
L.DomUtil.addClass(this._container, 'leaflet-resp-popup-south');
this._tipContainer.style.top = '0px';
this._tipContainer.style.left = (pos.x + anchor.x - containerPos.x) + 'px';
}
}
}
else if(canGoRight) {
containerPos = pos.subtract(L.point(-anchor.x - offsetX, subtractY, true));
if(this.options.hasTip) {
if(basePoint.y + anchor.y < paddingTL.y + containerRadius + tipWidth/2) {
containerPos.y = pos.y + anchor.y;
L.DomUtil.addClass(this._container, 'leaflet-resp-popup-east-south');
this._tipContainer.style.top = '0px';
this._tipContainer.style.left = '0px';
}
else if(basePoint.y + anchor.y > mapSize.y - paddingBR.y - containerRadius - tipWidth/2) {
containerPos.y = pos.y + anchor.y - containerHeight;
L.DomUtil.addClass(this._container, 'leaflet-resp-popup-east-north');
this._tipContainer.style.top = containerHeight + 'px';
this._tipContainer.style.left = '0px';
}
else {
L.DomUtil.addClass(this._container, 'leaflet-resp-popup-east');
this._tipContainer.style.top = (pos.y + anchor.y - containerPos.y) + 'px';
this._tipContainer.style.left = '0px';
}
}
}
else {
var pos = this._map.latLngToLayerPoint(this._map.getCenter());
containerPos = pos.subtract(L.point(containerWidth / 2, containerHeight / 2));
if(this.options.hasTip) {
// this._tipContainer.style.display = 'none'; // this does not work
}
}
// if point is not visible, just hide the popup
if(basePoint.x < 0 || basePoint.y < 0 || basePoint.x > mapSize.x || basePoint.y > mapSize.y) {
// this._container.style.display = 'none'; // this does not work
}
// if container is too big, just hide the popup
if(containerWidth - Math.abs(paddingTL.x) - Math.abs(paddingBR.x) > mapSize.x || containerHeight - Math.abs(paddingTL.y) - Math.abs(paddingBR.y) > mapSize.y) {
// this._container.style.display = 'none'; // this does not work
}
L.DomUtil.setPosition(this._container, containerPos);
}
});
//Instantiates a `ResponsivePopup` object given an optional `options` object that describes its appearance and location and an optional `source` object that is used to tag the popup with a reference to the Layer to which it refers.
L.responsivePopup = function (options, source) {
return new L.ResponsivePopup(options, source);
};
//Adds Angular support
if( typeof exports === 'object' && typeof module !== 'undefined') {
exports.responsivePopup = L.responsivePopup;
exports.ResponsivePopup = L.ResponsivePopup;
}