1 | import {DivOverlay} from './DivOverlay';
|
2 | import * as DomEvent from '../dom/DomEvent';
|
3 | import * as DomUtil from '../dom/DomUtil';
|
4 | import {Point, toPoint} from '../geometry/Point';
|
5 | import {Map} from '../map/Map';
|
6 | import {Layer} from './Layer';
|
7 | import * as Util from '../core/Util';
|
8 | import {Path} from './vector/Path';
|
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 | export var Popup = DivOverlay.extend({
|
39 |
|
40 |
|
41 |
|
42 | options: {
|
43 |
|
44 |
|
45 | maxWidth: 300,
|
46 |
|
47 |
|
48 |
|
49 | minWidth: 50,
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | maxHeight: null,
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | autoPan: true,
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | autoPanPaddingTopLeft: null,
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | autoPanPaddingBottomRight: null,
|
70 |
|
71 |
|
72 |
|
73 | autoPanPadding: [5, 5],
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | keepInView: false,
|
79 |
|
80 |
|
81 |
|
82 | closeButton: true,
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | autoClose: true,
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | closeOnEscapeKey: true,
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | className: ''
|
101 | },
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | openOn: function (map) {
|
107 | map.openPopup(this);
|
108 | return this;
|
109 | },
|
110 |
|
111 | onAdd: function (map) {
|
112 | DivOverlay.prototype.onAdd.call(this, map);
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | map.fire('popupopen', {popup: this});
|
119 |
|
120 | if (this._source) {
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | this._source.fire('popupopen', {popup: this}, true);
|
126 |
|
127 |
|
128 | if (!(this._source instanceof Path)) {
|
129 | this._source.on('preclick', DomEvent.stopPropagation);
|
130 | }
|
131 | }
|
132 | },
|
133 |
|
134 | onRemove: function (map) {
|
135 | DivOverlay.prototype.onRemove.call(this, map);
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | map.fire('popupclose', {popup: this});
|
142 |
|
143 | if (this._source) {
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | this._source.fire('popupclose', {popup: this}, true);
|
149 | if (!(this._source instanceof Path)) {
|
150 | this._source.off('preclick', DomEvent.stopPropagation);
|
151 | }
|
152 | }
|
153 | },
|
154 |
|
155 | getEvents: function () {
|
156 | var events = DivOverlay.prototype.getEvents.call(this);
|
157 |
|
158 | if (this.options.closeOnClick !== undefined ? this.options.closeOnClick : this._map.options.closePopupOnClick) {
|
159 | events.preclick = this._close;
|
160 | }
|
161 |
|
162 | if (this.options.keepInView) {
|
163 | events.moveend = this._adjustPan;
|
164 | }
|
165 |
|
166 | return events;
|
167 | },
|
168 |
|
169 | _close: function () {
|
170 | if (this._map) {
|
171 | this._map.closePopup(this);
|
172 | }
|
173 | },
|
174 |
|
175 | _initLayout: function () {
|
176 | var prefix = 'leaflet-popup',
|
177 | container = this._container = DomUtil.create('div',
|
178 | prefix + ' ' + (this.options.className || '') +
|
179 | ' leaflet-zoom-animated');
|
180 |
|
181 | var wrapper = this._wrapper = DomUtil.create('div', prefix + '-content-wrapper', container);
|
182 | this._contentNode = DomUtil.create('div', prefix + '-content', wrapper);
|
183 |
|
184 | DomEvent.disableClickPropagation(wrapper);
|
185 | DomEvent.disableScrollPropagation(this._contentNode);
|
186 | DomEvent.on(wrapper, 'contextmenu', DomEvent.stopPropagation);
|
187 |
|
188 | this._tipContainer = DomUtil.create('div', prefix + '-tip-container', container);
|
189 | this._tip = DomUtil.create('div', prefix + '-tip', this._tipContainer);
|
190 |
|
191 | if (this.options.closeButton) {
|
192 | var closeButton = this._closeButton = DomUtil.create('a', prefix + '-close-button', container);
|
193 | closeButton.href = '#close';
|
194 | closeButton.innerHTML = '×';
|
195 |
|
196 | DomEvent.on(closeButton, 'click', this._onCloseButtonClick, this);
|
197 | }
|
198 | },
|
199 |
|
200 | _updateLayout: function () {
|
201 | var container = this._contentNode,
|
202 | style = container.style;
|
203 |
|
204 | style.width = '';
|
205 | style.whiteSpace = 'nowrap';
|
206 |
|
207 | var width = container.offsetWidth;
|
208 | width = Math.min(width, this.options.maxWidth);
|
209 | width = Math.max(width, this.options.minWidth);
|
210 |
|
211 | style.width = (width + 1) + 'px';
|
212 | style.whiteSpace = '';
|
213 |
|
214 | style.height = '';
|
215 |
|
216 | var height = container.offsetHeight,
|
217 | maxHeight = this.options.maxHeight,
|
218 | scrolledClass = 'leaflet-popup-scrolled';
|
219 |
|
220 | if (maxHeight && height > maxHeight) {
|
221 | style.height = maxHeight + 'px';
|
222 | DomUtil.addClass(container, scrolledClass);
|
223 | } else {
|
224 | DomUtil.removeClass(container, scrolledClass);
|
225 | }
|
226 |
|
227 | this._containerWidth = this._container.offsetWidth;
|
228 | },
|
229 |
|
230 | _animateZoom: function (e) {
|
231 | var pos = this._map._latLngToNewLayerPoint(this._latlng, e.zoom, e.center),
|
232 | anchor = this._getAnchor();
|
233 | DomUtil.setPosition(this._container, pos.add(anchor));
|
234 | },
|
235 |
|
236 | _adjustPan: function () {
|
237 | if (!this.options.autoPan) { return; }
|
238 | if (this._map._panAnim) { this._map._panAnim.stop(); }
|
239 |
|
240 | var map = this._map,
|
241 | marginBottom = parseInt(DomUtil.getStyle(this._container, 'marginBottom'), 10) || 0,
|
242 | containerHeight = this._container.offsetHeight + marginBottom,
|
243 | containerWidth = this._containerWidth,
|
244 | layerPos = new Point(this._containerLeft, -containerHeight - this._containerBottom);
|
245 |
|
246 | layerPos._add(DomUtil.getPosition(this._container));
|
247 |
|
248 | var containerPos = map.layerPointToContainerPoint(layerPos),
|
249 | padding = toPoint(this.options.autoPanPadding),
|
250 | paddingTL = toPoint(this.options.autoPanPaddingTopLeft || padding),
|
251 | paddingBR = toPoint(this.options.autoPanPaddingBottomRight || padding),
|
252 | size = map.getSize(),
|
253 | dx = 0,
|
254 | dy = 0;
|
255 |
|
256 | if (containerPos.x + containerWidth + paddingBR.x > size.x) {
|
257 | dx = containerPos.x + containerWidth - size.x + paddingBR.x;
|
258 | }
|
259 | if (containerPos.x - dx - paddingTL.x < 0) {
|
260 | dx = containerPos.x - paddingTL.x;
|
261 | }
|
262 | if (containerPos.y + containerHeight + paddingBR.y > size.y) {
|
263 | dy = containerPos.y + containerHeight - size.y + paddingBR.y;
|
264 | }
|
265 | if (containerPos.y - dy - paddingTL.y < 0) {
|
266 | dy = containerPos.y - paddingTL.y;
|
267 | }
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 | if (dx || dy) {
|
274 | map
|
275 | .fire('autopanstart')
|
276 | .panBy([dx, dy]);
|
277 | }
|
278 | },
|
279 |
|
280 | _onCloseButtonClick: function (e) {
|
281 | this._close();
|
282 | DomEvent.stop(e);
|
283 | },
|
284 |
|
285 | _getAnchor: function () {
|
286 |
|
287 | return toPoint(this._source && this._source._getPopupAnchor ? this._source._getPopupAnchor() : [0, 0]);
|
288 | }
|
289 |
|
290 | });
|
291 |
|
292 |
|
293 |
|
294 |
|
295 | export var popup = function (options, source) {
|
296 | return new Popup(options, source);
|
297 | };
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 | Map.mergeOptions({
|
306 | closePopupOnClick: true
|
307 | });
|
308 |
|
309 |
|
310 |
|
311 |
|
312 | Map.include({
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 | openPopup: function (popup, latlng, options) {
|
319 | if (!(popup instanceof Popup)) {
|
320 | popup = new Popup(options).setContent(popup);
|
321 | }
|
322 |
|
323 | if (latlng) {
|
324 | popup.setLatLng(latlng);
|
325 | }
|
326 |
|
327 | if (this.hasLayer(popup)) {
|
328 | return this;
|
329 | }
|
330 |
|
331 | if (this._popup && this._popup.options.autoClose) {
|
332 | this.closePopup();
|
333 | }
|
334 |
|
335 | this._popup = popup;
|
336 | return this.addLayer(popup);
|
337 | },
|
338 |
|
339 |
|
340 |
|
341 | closePopup: function (popup) {
|
342 | if (!popup || popup === this._popup) {
|
343 | popup = this._popup;
|
344 | this._popup = null;
|
345 | }
|
346 | if (popup) {
|
347 | this.removeLayer(popup);
|
348 | }
|
349 | return this;
|
350 | }
|
351 | });
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 | Layer.include({
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 | bindPopup: function (content, options) {
|
376 |
|
377 | if (content instanceof Popup) {
|
378 | Util.setOptions(content, options);
|
379 | this._popup = content;
|
380 | content._source = this;
|
381 | } else {
|
382 | if (!this._popup || options) {
|
383 | this._popup = new Popup(options, this);
|
384 | }
|
385 | this._popup.setContent(content);
|
386 | }
|
387 |
|
388 | if (!this._popupHandlersAdded) {
|
389 | this.on({
|
390 | click: this._openPopup,
|
391 | keypress: this._onKeyPress,
|
392 | remove: this.closePopup,
|
393 | move: this._movePopup
|
394 | });
|
395 | this._popupHandlersAdded = true;
|
396 | }
|
397 |
|
398 | return this;
|
399 | },
|
400 |
|
401 |
|
402 |
|
403 | unbindPopup: function () {
|
404 | if (this._popup) {
|
405 | this.off({
|
406 | click: this._openPopup,
|
407 | keypress: this._onKeyPress,
|
408 | remove: this.closePopup,
|
409 | move: this._movePopup
|
410 | });
|
411 | this._popupHandlersAdded = false;
|
412 | this._popup = null;
|
413 | }
|
414 | return this;
|
415 | },
|
416 |
|
417 |
|
418 |
|
419 | openPopup: function (layer, latlng) {
|
420 | if (this._popup && this._map) {
|
421 | latlng = this._popup._prepareOpen(this, layer, latlng);
|
422 |
|
423 |
|
424 | this._map.openPopup(this._popup, latlng);
|
425 | }
|
426 |
|
427 | return this;
|
428 | },
|
429 |
|
430 |
|
431 |
|
432 | closePopup: function () {
|
433 | if (this._popup) {
|
434 | this._popup._close();
|
435 | }
|
436 | return this;
|
437 | },
|
438 |
|
439 |
|
440 |
|
441 | togglePopup: function (target) {
|
442 | if (this._popup) {
|
443 | if (this._popup._map) {
|
444 | this.closePopup();
|
445 | } else {
|
446 | this.openPopup(target);
|
447 | }
|
448 | }
|
449 | return this;
|
450 | },
|
451 |
|
452 |
|
453 |
|
454 | isPopupOpen: function () {
|
455 | return (this._popup ? this._popup.isOpen() : false);
|
456 | },
|
457 |
|
458 |
|
459 |
|
460 | setPopupContent: function (content) {
|
461 | if (this._popup) {
|
462 | this._popup.setContent(content);
|
463 | }
|
464 | return this;
|
465 | },
|
466 |
|
467 |
|
468 |
|
469 | getPopup: function () {
|
470 | return this._popup;
|
471 | },
|
472 |
|
473 | _openPopup: function (e) {
|
474 | var layer = e.layer || e.target;
|
475 |
|
476 | if (!this._popup) {
|
477 | return;
|
478 | }
|
479 |
|
480 | if (!this._map) {
|
481 | return;
|
482 | }
|
483 |
|
484 |
|
485 | DomEvent.stop(e);
|
486 |
|
487 |
|
488 |
|
489 | if (layer instanceof Path) {
|
490 | this.openPopup(e.layer || e.target, e.latlng);
|
491 | return;
|
492 | }
|
493 |
|
494 |
|
495 |
|
496 | if (this._map.hasLayer(this._popup) && this._popup._source === layer) {
|
497 | this.closePopup();
|
498 | } else {
|
499 | this.openPopup(layer, e.latlng);
|
500 | }
|
501 | },
|
502 |
|
503 | _movePopup: function (e) {
|
504 | this._popup.setLatLng(e.latlng);
|
505 | },
|
506 |
|
507 | _onKeyPress: function (e) {
|
508 | if (e.originalEvent.keyCode === 13) {
|
509 | this._openPopup(e);
|
510 | }
|
511 | }
|
512 | });
|