PageSourceSearch

https://www.homewardtrails.org/wp-content/plugins/wp-mapit/wp_mapi…/leaflet.responsive.popup.js?ver=1778582741

js homewardtrails.org collected 2026-09-26 04:01:03 UTC 11,147 bytes, 270 lines download raw bytes

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

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.