-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL.Control.ShowAll.js
More file actions
84 lines (67 loc) · 2.55 KB
/
L.Control.ShowAll.js
File metadata and controls
84 lines (67 loc) · 2.55 KB
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
L.Control.ShowAll = L.Control.extend({
options: {
bounds: null, // bounds to show in show-all mode
position: 'topleft',
title: 'Show All', // tooltip
icon: 'icon-show-all', // button icon
},
initialize: function(options) {
for (var i in options) {
this.options[i] = options[i];
}
},
onAdd: function(map) {
var container = L.DomUtil.create('div', 'leaflet-control-showall leaflet-bar leaflet-control');
// make sure bounds were given
if (!(this.options.bounds instanceof L.LatLngBounds)) {
console.error('Leaflet.ShowAll - bounds must be supplied and must be a LatLngBounds');
return container;
}
this.link = L.DomUtil.create('a', 'leaflet-bar-part', container);
L.DomUtil.create('i', 'fa fa-lg ' + this.options.icon , this.link);
this.link.href = '#';
this.link.title = this.options.title;
L.DomEvent.on(this.link, 'click', this.onClick, this)
.on(this.link, 'dblclick', L.DomEvent.stopPropagation);
this._container = container;
// event hooks
map.on('zoomstart', this.onZoom, this);
return container;
},
onClick: function(e) {
L.DomEvent.stopPropagation(e);
L.DomEvent.preventDefault(e);
// start/stop show-all mode
if (L.DomUtil.hasClass(this._container, "active"))
this._endShowAll(true);
else
this._startShowAll();
},
onZoom: function(e) {
// stop show-all mode if on
if (L.DomUtil.hasClass(this._container, "active"))
this._endShowAll(false);
},
_startShowAll: function() {
// save current zoom and bounds
this.oldZoom = this._map.getZoom();
this.oldBounds = this._map.getBounds();
this.oldMaxBounds = this._map.options.maxBounds;
// fit to supplied bounds
this._map.setMaxBounds(this.options.bounds);
this._map.fitBounds(this.options.bounds);
L.DomUtil.addClass(this._container, "active");
},
_endShowAll: function(restore) {
L.DomUtil.removeClass(this._container, "active");
// if not ended by zoom restore zoom and bounds
if (restore) {
this._map.setZoom(this.oldZoom);
this._map.fitBounds(this.oldBounds);
this._map.setMaxBounds(this.oldMaxBounds);
}
},
});
L.control.showAll = function (options) {
return new L.Control.ShowAll(options);
};