Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow user to define element other than map container for full screen control #7548

Merged
merged 4 commits into from
Nov 13, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions src/ui/control/fullscreen_control.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,40 @@ import window from '../../util/window';

import type Map from '../map';

type Options = {
source?: HTMLElement
};

/**
* A `FullscreenControl` control contains a button for toggling the map in and out of fullscreen mode.
*
* @implements {IControl}
* @param {Object} [options]
* @param {HTMLElement} [options.source] `source` is the [compatible DOM element](https://developer.mozilla.org/en-US/docs/Web/API/Element/requestFullScreen#Compatible_elements) which should be made full screen. By default, the map container element will be made full screen.
*
* @example
* map.addControl(new mapboxgl.FullscreenControl());
* map.addControl(new mapboxgl.FullscreenControl({source: document.querySelector('body')}));
* @see [View a fullscreen map](https://www.mapbox.com/mapbox-gl-js/example/fullscreen/)
*/

class FullscreenControl {
_map: Map;
_mapContainer: HTMLElement;
_container: HTMLElement;
_fullscreen: boolean;
_fullscreenchange: string;
_fullscreenButton: HTMLElement;
_className: string;
_source: HTMLElement;

constructor() {
constructor(options: Options) {
this._fullscreen = false;
if (options && options.source) {
if (options.source instanceof HTMLElement) {
this._source = options.source;
} else {
warnOnce('Full screen control \'source\' must be a DOM element.');
}
}
bindAll([
'_onClickFullscreen',
'_changeIcon'
Expand All @@ -45,7 +59,7 @@ class FullscreenControl {

onAdd(map: Map) {
this._map = map;
this._mapContainer = this._map.getContainer();
if (!this._source) this._source = this._map.getContainer();
ryanhamley marked this conversation as resolved.
Show resolved Hide resolved
this._container = DOM.create('div', `${this._className} mapboxgl-ctrl-group`);
if (this._checkFullscreenSupport()) {
this._setupUI();
Expand Down Expand Up @@ -90,7 +104,7 @@ class FullscreenControl {
(window.document: any).webkitFullscreenElement ||
(window.document: any).msFullscreenElement;

if ((fullscreenElement === this._mapContainer) !== this._fullscreen) {
if ((fullscreenElement === this._source) !== this._fullscreen) {
this._fullscreen = !this._fullscreen;
this._fullscreenButton.classList.toggle(`${this._className}-shrink`);
this._fullscreenButton.classList.toggle(`${this._className}-fullscreen`);
Expand All @@ -108,14 +122,14 @@ class FullscreenControl {
} else if (window.document.webkitCancelFullScreen) {
(window.document: any).webkitCancelFullScreen();
}
} else if (this._mapContainer.requestFullscreen) {
this._mapContainer.requestFullscreen();
} else if ((this._mapContainer: any).mozRequestFullScreen) {
(this._mapContainer: any).mozRequestFullScreen();
} else if ((this._mapContainer: any).msRequestFullscreen) {
(this._mapContainer: any).msRequestFullscreen();
} else if ((this._mapContainer: any).webkitRequestFullscreen) {
(this._mapContainer: any).webkitRequestFullscreen();
} else if (this._source.requestFullscreen) {
this._source.requestFullscreen();
} else if ((this._source: any).mozRequestFullScreen) {
(this._source: any).mozRequestFullScreen();
} else if ((this._source: any).msRequestFullscreen) {
(this._source: any).msRequestFullscreen();
} else if ((this._source: any).webkitRequestFullscreen) {
(this._source: any).webkitRequestFullscreen();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/ui/control/fullscreen.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test('FullscreenControl appears when fullscreen is enabled', (t) => {
t.end();
});

test('FullscreenControl does not appears when fullscreen is not enabled', (t) => {
test('FullscreenControl does not appear when fullscreen is not enabled', (t) => {
window.document.fullscreenEnabled = false;

const consoleWarn = t.stub(console, 'warn');
Expand Down