forked from brunob/leaflet.fullscreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathControl.FullScreen.js
173 lines (145 loc) · 5.24 KB
/
Control.FullScreen.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
(function() {
L.Control.FullScreen = L.Control.extend({
options: {
position: 'topleft',
title: 'Full Screen',
forceSeparateButton: false
},
onAdd: function (map) {
var className = 'leaflet-control-zoom-fullscreen', buttonContainer;
// Do nothing if we can't
if (!fullScreenApi.supportsFullScreen) {
return map.zoomControl ? map.zoomControl._container : L.DomUtil.create('div', '');
}
if (map.zoomControl && !this.options.forceSeparateButton) {
buttonContainer = map.zoomControl._container;
} else {
buttonContainer = L.DomUtil.create('div', 'leaflet-bar');
}
this._map = map;
this._createButton(this.options.title, className, buttonContainer, this.options.container || null, this.toogleFullScreen, this, map);
return buttonContainer;
},
_createButton: function (title, className, buttonContainer, fullscreenContainer, fn, fsContext, mapContext) {
var link = L.DomUtil.create('a', className, buttonContainer);
link.href = '#';
link.title = title;
L.DomEvent
.addListener(link, 'click', L.DomEvent.stopPropagation)
.addListener(link, 'click', L.DomEvent.preventDefault)
.addListener(link, 'click', fn, fsContext);
var fullscreenElContainer = fullscreenContainer || buttonContainer;
L.DomEvent
.addListener(fullscreenElContainer, fullScreenApi.fullScreenEventName, L.DomEvent.stopPropagation)
.addListener(fullscreenElContainer, fullScreenApi.fullScreenEventName, L.DomEvent.preventDefault)
.addListener(fullscreenElContainer, fullScreenApi.fullScreenEventName, this._handleEscKey, fsContext);
L.DomEvent
.addListener(document, fullScreenApi.fullScreenEventName, L.DomEvent.stopPropagation)
.addListener(document, fullScreenApi.fullScreenEventName, L.DomEvent.preventDefault)
.addListener(document, fullScreenApi.fullScreenEventName, this._handleEscKey, fsContext);
return link;
},
toogleFullScreen: function () {
this._exitFired = false;
if (fullScreenApi.supportsFullScreen) {
var container = this.options.container || this._container;
if (fullScreenApi.isFullScreen(container)) {
fullScreenApi.cancelFullScreen(container);
this._map.invalidateSize();
this._map.fire('exitFullscreen');
L.DomUtil.removeClass(container, "fullscreen");
this._exitFired = true;
} else {
fullScreenApi.requestFullScreen(container);
this._map.invalidateSize();
this._map.fire('enterFullscreen');
L.DomUtil.addClass(container, "fullscreen");
}
}
},
_handleEscKey: function () {
if (!fullScreenApi.isFullScreen(this) && !this._exitFired) {
var container = this.options.container || this._container;
this._map.fire('exitFullscreen');
L.DomUtil.removeClass(container, "fullscreen");
this._exitFired = true;
}
}
});
L.Map.addInitHook(function () {
if (this.options.fullscreenControl) {
this.fullscreenControl = L.control.fullscreen(this.options.fullscreenControlOptions);
this.addControl(this.fullscreenControl);
}
});
L.control.fullscreen = function (options) {
return new L.Control.FullScreen(options);
};
/*
Native FullScreen JavaScript API
-------------
Assumes Mozilla naming conventions instead of W3C for now
source : http://johndyer.name/native-fullscreen-javascript-api-plus-jquery-plugin/
*/
var
fullScreenApi = {
supportsFullScreen: false,
isFullScreen: function() { return false; },
requestFullScreen: function() {},
cancelFullScreen: function() {},
fullScreenEventName: '',
prefix: ''
},
browserPrefixes = 'webkit moz o ms khtml'.split(' ');
// check for native support
if (typeof document.exitFullscreen != 'undefined') {
fullScreenApi.supportsFullScreen = true;
} else {
// check for fullscreen support by vendor prefix
for (var i = 0, il = browserPrefixes.length; i < il; i++ ) {
fullScreenApi.prefix = browserPrefixes[i];
if (typeof document[fullScreenApi.prefix + 'CancelFullScreen' ] != 'undefined' ) {
fullScreenApi.supportsFullScreen = true;
break;
}
}
}
var ua = navigator.userAgent;
if ((ua.indexOf('Safari') != -1) && ua.indexOf('Mac') != -1 && ua.indexOf('Chrome') == -1) {
// it's Safari on Mac
fullScreenApi.supportsFullScreen = false;
}
// update methods to do something useful
if (fullScreenApi.supportsFullScreen) {
fullScreenApi.fullScreenEventName = fullScreenApi.prefix + 'fullscreenchange';
fullScreenApi.isFullScreen = function() {
switch (this.prefix) {
case '':
return document.fullScreen;
case 'webkit':
return document.webkitIsFullScreen;
default:
return document[this.prefix + 'FullScreen'];
}
};
fullScreenApi.requestFullScreen = function(el) {
return (this.prefix === '') ? el.requestFullscreen() : el[this.prefix + 'RequestFullScreen'](Element.ALLOW_KEYBOARD_INPUT);
};
fullScreenApi.cancelFullScreen = function(el) {
return (this.prefix === '') ? document.exitFullscreen() : document[this.prefix + 'CancelFullScreen']();
};
}
// jQuery plugin
if (typeof jQuery != 'undefined') {
jQuery.fn.requestFullScreen = function() {
return this.each(function() {
var el = jQuery(this);
if (fullScreenApi.supportsFullScreen) {
fullScreenApi.requestFullScreen(el);
}
});
};
}
// export api
window.fullScreenApi = fullScreenApi;
})();