[PLUGINS] ~maj globale
[lhc/web/www.git] / www / plugins / gis / lib / leaflet / plugins / Control.FullScreen.js
1 (function () {
2
3 L.Control.FullScreen = L.Control.extend({
4 options: {
5 position: 'topleft',
6 title: 'Full Screen',
7 titleCancel: 'Exit Full Screen',
8 forceSeparateButton: false,
9 forcePseudoFullscreen: false,
10 fullscreenElement: false
11 },
12
13 onAdd: function (map) {
14 var className = 'leaflet-control-zoom-fullscreen', container, content = '';
15
16 if (map.zoomControl && !this.options.forceSeparateButton) {
17 container = map.zoomControl._container;
18 } else {
19 container = L.DomUtil.create('div', 'leaflet-bar');
20 }
21
22 if (this.options.content) {
23 content = this.options.content;
24 } else {
25 className += ' fullscreen-icon';
26 }
27
28 this._createButton(this.options.title, className, content, container, this.toggleFullScreen, this);
29
30 this._map.on('enterFullscreen exitFullscreen', this._toggleTitle, this);
31
32 return container;
33 },
34
35 _createButton: function (title, className, content, container, fn, context) {
36 this.link = L.DomUtil.create('a', className, container);
37 this.link.href = '#';
38 this.link.title = title;
39 this.link.innerHTML = content;
40
41 L.DomEvent
42 .addListener(this.link, 'click', L.DomEvent.stopPropagation)
43 .addListener(this.link, 'click', L.DomEvent.preventDefault)
44 .addListener(this.link, 'click', fn, context);
45
46 L.DomEvent
47 .addListener(container, fullScreenApi.fullScreenEventName, L.DomEvent.stopPropagation)
48 .addListener(container, fullScreenApi.fullScreenEventName, L.DomEvent.preventDefault)
49 .addListener(container, fullScreenApi.fullScreenEventName, this._handleEscKey, context);
50
51 L.DomEvent
52 .addListener(document, fullScreenApi.fullScreenEventName, L.DomEvent.stopPropagation)
53 .addListener(document, fullScreenApi.fullScreenEventName, L.DomEvent.preventDefault)
54 .addListener(document, fullScreenApi.fullScreenEventName, this._handleEscKey, context);
55
56 return this.link;
57 },
58
59 toggleFullScreen: function () {
60 var map = this._map;
61 map._exitFired = false;
62 if (map._isFullscreen) {
63 if (fullScreenApi.supportsFullScreen && !this.options.forcePseudoFullscreen) {
64 fullScreenApi.cancelFullScreen(this.options.fullscreenElement ? this.options.fullscreenElement : map._container);
65 } else {
66 L.DomUtil.removeClass(map._container, 'leaflet-pseudo-fullscreen');
67 }
68 map.invalidateSize();
69 map.fire('exitFullscreen');
70 map._exitFired = true;
71 map._isFullscreen = false;
72 }
73 else {
74 if (fullScreenApi.supportsFullScreen && !this.options.forcePseudoFullscreen) {
75 fullScreenApi.requestFullScreen(this.options.fullscreenElement ? this.options.fullscreenElement : map._container);
76 } else {
77 L.DomUtil.addClass(map._container, 'leaflet-pseudo-fullscreen');
78 }
79 map.invalidateSize();
80 map.fire('enterFullscreen');
81 map._isFullscreen = true;
82 }
83 },
84
85 _toggleTitle: function () {
86 this.link.title = this._map._isFullscreen ? this.options.title : this.options.titleCancel;
87 },
88
89 _handleEscKey: function () {
90 var map = this._map;
91 if (!fullScreenApi.isFullScreen(map) && !map._exitFired) {
92 map.fire('exitFullscreen');
93 map._exitFired = true;
94 map._isFullscreen = false;
95 }
96 }
97 });
98
99 L.Map.addInitHook(function () {
100 if (this.options.fullscreenControl) {
101 this.fullscreenControl = L.control.fullscreen(this.options.fullscreenControlOptions);
102 this.addControl(this.fullscreenControl);
103 }
104 });
105
106 L.control.fullscreen = function (options) {
107 return new L.Control.FullScreen(options);
108 };
109
110 /*
111 Native FullScreen JavaScript API
112 -------------
113 Assumes Mozilla naming conventions instead of W3C for now
114
115 source : http://johndyer.name/native-fullscreen-javascript-api-plus-jquery-plugin/
116
117 */
118
119 var
120 fullScreenApi = {
121 supportsFullScreen: false,
122 isFullScreen: function () { return false; },
123 requestFullScreen: function () {},
124 cancelFullScreen: function () {},
125 fullScreenEventName: '',
126 prefix: ''
127 },
128 browserPrefixes = 'webkit moz o ms khtml'.split(' ');
129
130 // check for native support
131 if (typeof document.exitFullscreen !== 'undefined') {
132 fullScreenApi.supportsFullScreen = true;
133 } else {
134 // check for fullscreen support by vendor prefix
135 for (var i = 0, il = browserPrefixes.length; i < il; i++) {
136 fullScreenApi.prefix = browserPrefixes[i];
137 if (typeof document[fullScreenApi.prefix + 'CancelFullScreen'] !== 'undefined') {
138 fullScreenApi.supportsFullScreen = true;
139 break;
140 }
141 }
142 if (typeof document['msExitFullscreen'] !== 'undefined') {
143 fullScreenApi.prefix = 'ms';
144 fullScreenApi.supportsFullScreen = true;
145 }
146 }
147
148 // update methods to do something useful
149 if (fullScreenApi.supportsFullScreen) {
150 if (fullScreenApi.prefix === 'ms') {
151 fullScreenApi.fullScreenEventName = 'MSFullscreenChange';
152 } else {
153 fullScreenApi.fullScreenEventName = fullScreenApi.prefix + 'fullscreenchange';
154 }
155 fullScreenApi.isFullScreen = function () {
156 switch (this.prefix) {
157 case '':
158 return document.fullScreen;
159 case 'webkit':
160 return document.webkitIsFullScreen;
161 case 'ms':
162 return document.msFullscreenElement;
163 default:
164 return document[this.prefix + 'FullScreen'];
165 }
166 };
167 fullScreenApi.requestFullScreen = function (el) {
168 switch (this.prefix) {
169 case '':
170 return el.requestFullscreen();
171 case 'ms':
172 return el.msRequestFullscreen();
173 default:
174 return el[this.prefix + 'RequestFullScreen']();
175 }
176 };
177 fullScreenApi.cancelFullScreen = function () {
178 switch (this.prefix) {
179 case '':
180 return document.exitFullscreen();
181 case 'ms':
182 return document.msExitFullscreen();
183 default:
184 return document[this.prefix + 'CancelFullScreen']();
185 }
186 };
187 }
188
189 // jQuery plugin
190 if (typeof jQuery !== 'undefined') {
191 jQuery.fn.requestFullScreen = function () {
192 return this.each(function () {
193 var el = jQuery(this);
194 if (fullScreenApi.supportsFullScreen) {
195 fullScreenApi.requestFullScreen(el);
196 }
197 });
198 };
199 }
200
201 // export api
202 window.fullScreenApi = fullScreenApi;
203 })();