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