forked from elidupuis/leaflet.zoomfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleaflet.zoomfs.js
86 lines (73 loc) · 2.71 KB
/
leaflet.zoomfs.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
/*
* L.Control.ZoomFS - default Leaflet.Zoom control with an added fullscreen button
* built to work with Leaflet version 0.3
* https://github.com/elidupuis/leaflet.zoomfs
*/
L.Control.ZoomFS = L.Control.Zoom.extend({
includes: L.Mixin.Events,
onAdd: function (map) {
this._map = map;
this._isFullscreen = false;
this._container = L.DomUtil.create('div', 'leaflet-control-zoom');
this._zoomInButton = this._createButton(
'Zoom in', 'leaflet-control-zoom-in', this._map.zoomIn, this._map);
this._zoomOutButton = this._createButton(
'Zoom out', 'leaflet-control-zoom-out', this._map.zoomOut, this._map);
// ideally the fullscreen function should reside in the Map class, but this will do for now...
this._fullScreenButton = this._createButton(
'Full Screen', 'leaflet-control-fullscreen', this.fullscreen, this);
// reorder this as you like (ie: place fullscreen button below zoom buttons)
this._container.appendChild(this._fullScreenButton);
this._container.appendChild(this._zoomInButton);
this._container.appendChild(this._zoomOutButton);
},
fullscreen: function(){
// call appropriate internal function
if (!this._isFullscreen) {
this._enterFullScreen();
}else{
this._exitFullScreen();
};
// force internal resize
this._map.invalidateSize();
},
_enterFullScreen: function(){
var container = this._map._container;
// apply our fullscreen settings
container.style.position = 'fixed';
container.style.left = 0;
container.style.top = 0;
container.style.width = '100%';
container.style.height = '100%';
// store state
L.DomUtil.addClass(container, 'leaflet-fullscreen');
this._isFullscreen = true;
// add ESC listener
L.DomEvent.addListener(document, 'keyup', this._onKeyUp, this);
// fire fullscreen event on map
this._map.fire('enterFullscreen');
},
_exitFullScreen: function(){
var container = this._map._container;
// update state
L.DomUtil.removeClass(container, 'leaflet-fullscreen');
this._isFullscreen = false;
// remove fullscreen style; make sure we're still position relative for Leaflet core.
container.removeAttribute('style');
// re-apply position:relative; if user does not have it.
var position = L.DomUtil.getStyle(container, 'position');
if (position !== 'absolute' && position !== 'relative') {
container.style.position = 'relative';
}
// remove ESC listener
L.DomEvent.removeListener(document, 'keyup', this._onKeyUp);
// fire fullscreen event
this._map.fire('exitFullscreen');
},
_onKeyUp: function(e){
if (!e) var e = window.event;
if (e.keyCode === 27 && this._isFullscreen === true) {
this._exitFullScreen();
}
}
});