forked from areichman/leaflet-responsive-attribution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleaflet-responsive-attribution.js
39 lines (31 loc) · 1.39 KB
/
leaflet-responsive-attribution.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
// Use a compact attribution control for small map container widths
L.Control.Attribution.prototype._addTo = L.Control.Attribution.prototype.addTo;
L.Control.Attribution.prototype.addTo = function(map) {
L.Control.Attribution.prototype._addTo.call(this, map);
// use the css checkbox hack to toggle the attribution
var parent = this._container.parentNode;
var checkbox = document.createElement('input');
var label = document.createElement('label');
var checkboxId = map._container.id + '-attribution-toggle'; // unique name if multiple maps are present
checkbox.setAttribute('id', checkboxId);
checkbox.setAttribute('type', 'checkbox');
checkbox.classList.add('leaflet-compact-attribution-toggle');
parent.insertBefore(checkbox, parent.firstChild);
label.setAttribute('for', checkboxId);
label.classList.add('leaflet-control');
label.classList.add('leaflet-compact-attribution-label');
parent.appendChild(label);
// initial setup for map load
if (map._container.offsetWidth <= 600) {
L.DomUtil.addClass(this._container, 'leaflet-compact-attribution');
}
// update on map resize
map.on('resize', function() {
if (map._container.offsetWidth > 600) {
L.DomUtil.removeClass(this._container, 'leaflet-compact-attribution');
} else {
L.DomUtil.addClass(this._container, 'leaflet-compact-attribution');
}
}, this);
return this;
};