|
| 1 | +(function (factory) { |
| 2 | + if (typeof define === 'function' && define.amd) { |
| 3 | + // AMD |
| 4 | + define(['leaflet'], factory); |
| 5 | + } else if (typeof module !== 'undefined') { |
| 6 | + // Node/CommonJS |
| 7 | + module.exports = factory(require('leaflet')); |
| 8 | + } else { |
| 9 | + // Browser globals |
| 10 | + if (typeof window.L === 'undefined') { |
| 11 | + throw new Error('Leaflet must be loaded first'); |
| 12 | + } |
| 13 | + factory(window.L); |
| 14 | + } |
| 15 | +}(function (L) { |
| 16 | + L.Control.Fullscreen = L.Control.extend({ |
| 17 | + options: { |
| 18 | + position: 'topleft', |
| 19 | + title: { |
| 20 | + 'false': 'View Fullscreen', |
| 21 | + 'true': 'Exit Fullscreen' |
| 22 | + } |
| 23 | + }, |
| 24 | + |
| 25 | + onAdd: function (map) { |
| 26 | + var container = L.DomUtil.create('div', 'leaflet-control-fullscreen leaflet-bar leaflet-control'); |
| 27 | + |
| 28 | + this.link = L.DomUtil.create('a', 'leaflet-control-fullscreen-button leaflet-bar-part', container); |
| 29 | + this.link.href = '#'; |
| 30 | + |
| 31 | + this._map = map; |
| 32 | + this._map.on('fullscreenchange', this._toggleTitle, this); |
| 33 | + this._toggleTitle(); |
| 34 | + |
| 35 | + L.DomEvent.on(this.link, 'click', this._click, this); |
| 36 | + |
| 37 | + return container; |
| 38 | + }, |
| 39 | + |
| 40 | + onRemove: function (map) { |
| 41 | + map.off('fullscreenchange', this._toggleTitle, this); |
| 42 | + }, |
| 43 | + |
| 44 | + _click: function (e) { |
| 45 | + L.DomEvent.stopPropagation(e); |
| 46 | + L.DomEvent.preventDefault(e); |
| 47 | + this._map.toggleFullscreen(this.options); |
| 48 | + }, |
| 49 | + |
| 50 | + _toggleTitle: function() { |
| 51 | + this.link.title = this.options.title[this._map.isFullscreen()]; |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + L.Map.include({ |
| 56 | + isFullscreen: function () { |
| 57 | + return this._isFullscreen || false; |
| 58 | + }, |
| 59 | + |
| 60 | + toggleFullscreen: function (options) { |
| 61 | + var container = this.getContainer(); |
| 62 | + if (this.isFullscreen()) { |
| 63 | + if (options && options.pseudoFullscreen) { |
| 64 | + this._disablePseudoFullscreen(container); |
| 65 | + } else if (document.exitFullscreen) { |
| 66 | + document.exitFullscreen(); |
| 67 | + } else if (document.mozCancelFullScreen) { |
| 68 | + document.mozCancelFullScreen(); |
| 69 | + } else if (document.webkitCancelFullScreen) { |
| 70 | + document.webkitCancelFullScreen(); |
| 71 | + } else if (document.msExitFullscreen) { |
| 72 | + document.msExitFullscreen(); |
| 73 | + } else { |
| 74 | + this._disablePseudoFullscreen(container); |
| 75 | + } |
| 76 | + } else { |
| 77 | + if (options && options.pseudoFullscreen) { |
| 78 | + this._enablePseudoFullscreen(container); |
| 79 | + } else if (container.requestFullscreen) { |
| 80 | + container.requestFullscreen(); |
| 81 | + } else if (container.mozRequestFullScreen) { |
| 82 | + container.mozRequestFullScreen(); |
| 83 | + } else if (container.webkitRequestFullscreen) { |
| 84 | + container.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT); |
| 85 | + } else if (container.msRequestFullscreen) { |
| 86 | + container.msRequestFullscreen(); |
| 87 | + } else { |
| 88 | + this._enablePseudoFullscreen(container); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + }, |
| 93 | + |
| 94 | + _enablePseudoFullscreen: function (container) { |
| 95 | + L.DomUtil.addClass(container, 'leaflet-pseudo-fullscreen'); |
| 96 | + this._setFullscreen(true); |
| 97 | + this.fire('fullscreenchange'); |
| 98 | + }, |
| 99 | + |
| 100 | + _disablePseudoFullscreen: function (container) { |
| 101 | + L.DomUtil.removeClass(container, 'leaflet-pseudo-fullscreen'); |
| 102 | + this._setFullscreen(false); |
| 103 | + this.fire('fullscreenchange'); |
| 104 | + }, |
| 105 | + |
| 106 | + _setFullscreen: function(fullscreen) { |
| 107 | + this._isFullscreen = fullscreen; |
| 108 | + var container = this.getContainer(); |
| 109 | + if (fullscreen) { |
| 110 | + L.DomUtil.addClass(container, 'leaflet-fullscreen-on'); |
| 111 | + } else { |
| 112 | + L.DomUtil.removeClass(container, 'leaflet-fullscreen-on'); |
| 113 | + } |
| 114 | + this.invalidateSize(); |
| 115 | + }, |
| 116 | + |
| 117 | + _onFullscreenChange: function (e) { |
| 118 | + var fullscreenElement = |
| 119 | + document.fullscreenElement || |
| 120 | + document.mozFullScreenElement || |
| 121 | + document.webkitFullscreenElement || |
| 122 | + document.msFullscreenElement; |
| 123 | + |
| 124 | + if ( !this._isFullscreen) { |
| 125 | + this._setFullscreen(true); |
| 126 | + this.fire('fullscreenchange'); |
| 127 | + } else if ( this._isFullscreen) { |
| 128 | + this._setFullscreen(false); |
| 129 | + this.fire('fullscreenchange'); |
| 130 | + } |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + L.Map.mergeOptions({ |
| 135 | + fullscreenControl: false |
| 136 | + }); |
| 137 | + |
| 138 | + L.Map.addInitHook(function () { |
| 139 | + if (this.options.fullscreenControl) { |
| 140 | + this.fullscreenControl = new L.Control.Fullscreen(this.options.fullscreenControl); |
| 141 | + this.addControl(this.fullscreenControl); |
| 142 | + } |
| 143 | + |
| 144 | + var fullscreenchange; |
| 145 | + |
| 146 | + if ('onfullscreenchange' in document) { |
| 147 | + fullscreenchange = 'fullscreenchange'; |
| 148 | + } else if ('onmozfullscreenchange' in document) { |
| 149 | + fullscreenchange = 'mozfullscreenchange'; |
| 150 | + } else if ('onwebkitfullscreenchange' in document) { |
| 151 | + fullscreenchange = 'webkitfullscreenchange'; |
| 152 | + } else if ('onmsfullscreenchange' in document) { |
| 153 | + fullscreenchange = 'MSFullscreenChange'; |
| 154 | + } |
| 155 | + |
| 156 | + if (fullscreenchange) { |
| 157 | + var onFullscreenChange = L.bind(this._onFullscreenChange, this); |
| 158 | + |
| 159 | + this.whenReady(function () { |
| 160 | + L.DomEvent.on(document, fullscreenchange, onFullscreenChange); |
| 161 | + }); |
| 162 | + |
| 163 | + this.on('unload', function () { |
| 164 | + L.DomEvent.off(document, fullscreenchange, onFullscreenChange); |
| 165 | + }); |
| 166 | + } |
| 167 | + }); |
| 168 | + |
| 169 | + L.control.fullscreen = function (options) { |
| 170 | + return new L.Control.Fullscreen(options); |
| 171 | + }; |
| 172 | +})); |
0 commit comments