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