-
-
Notifications
You must be signed in to change notification settings - Fork 634
/
fullscreen_control.ts
186 lines (164 loc) · 6.83 KB
/
fullscreen_control.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import {DOM} from '../../util/dom';
import {warnOnce} from '../../util/util';
import {Event, Evented} from '../../util/evented';
import type {Map} from '../map';
import type {IControl} from './control';
/**
* The {@link FullscreenControl} options object
*/
type FullscreenControlOptions = {
/**
* `container` 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.
*/
container?: HTMLElement;
};
/**
* A `FullscreenControl` control contains a button for toggling the map in and out of fullscreen mode.
* When [requestFullscreen](https://developer.mozilla.org/en-US/docs/Web/API/Element/requestFullscreen) is not supported, fullscreen is handled via CSS properties.
* The map's `cooperativeGestures` option is temporarily disabled while the map
* is in fullscreen mode, and is restored when the map exist fullscreen mode.
*
* @group Markers and Controls
* @param options - the full screen control options
*
* @example
* ```ts
* map.addControl(new FullscreenControl({container: document.querySelector('body')}));
* ```
* @see [View a fullscreen map](https://maplibre.org/maplibre-gl-js/docs/examples/fullscreen/)
*
* ## Events
*
* **Event** `fullscreenstart` of type {@link Event} will be fired when fullscreen mode has started.
*
* **Event** `fullscreenend` of type {@link Event} will be fired when fullscreen mode has ended.
*/
export class FullscreenControl extends Evented implements IControl {
_map: Map;
_controlContainer: HTMLElement;
_fullscreen: boolean;
_fullscreenchange: string;
_fullscreenButton: HTMLButtonElement;
_container: HTMLElement;
_prevCooperativeGesturesEnabled: boolean;
constructor(options: FullscreenControlOptions = {}) {
super();
this._fullscreen = false;
if (options && options.container) {
if (options.container instanceof HTMLElement) {
this._container = options.container;
} else {
warnOnce('Full screen control \'container\' must be a DOM element.');
}
}
if ('onfullscreenchange' in document) {
this._fullscreenchange = 'fullscreenchange';
} else if ('onmozfullscreenchange' in document) {
this._fullscreenchange = 'mozfullscreenchange';
} else if ('onwebkitfullscreenchange' in document) {
this._fullscreenchange = 'webkitfullscreenchange';
} else if ('onmsfullscreenchange' in document) {
this._fullscreenchange = 'MSFullscreenChange';
}
}
/** {@inheritDoc IControl.onAdd} */
onAdd(map: Map): HTMLElement {
this._map = map;
if (!this._container) this._container = this._map.getContainer();
this._controlContainer = DOM.create('div', 'maplibregl-ctrl maplibregl-ctrl-group');
this._setupUI();
return this._controlContainer;
}
/** {@inheritDoc IControl.onRemove} */
onRemove() {
DOM.remove(this._controlContainer);
this._map = null;
window.document.removeEventListener(this._fullscreenchange, this._onFullscreenChange);
}
_setupUI() {
const button = this._fullscreenButton = DOM.create('button', (('maplibregl-ctrl-fullscreen')), this._controlContainer);
DOM.create('span', 'maplibregl-ctrl-icon', button).setAttribute('aria-hidden', 'true');
button.type = 'button';
this._updateTitle();
this._fullscreenButton.addEventListener('click', this._onClickFullscreen);
window.document.addEventListener(this._fullscreenchange, this._onFullscreenChange);
}
_updateTitle() {
const title = this._getTitle();
this._fullscreenButton.setAttribute('aria-label', title);
this._fullscreenButton.title = title;
}
_getTitle() {
return this._map._getUIString(this._isFullscreen() ? 'FullscreenControl.Exit' : 'FullscreenControl.Enter');
}
_isFullscreen() {
return this._fullscreen;
}
_onFullscreenChange = () => {
let fullscreenElement =
window.document.fullscreenElement ||
(window.document as any).mozFullScreenElement ||
(window.document as any).webkitFullscreenElement ||
(window.document as any).msFullscreenElement;
while (fullscreenElement?.shadowRoot?.fullscreenElement) {
fullscreenElement = fullscreenElement.shadowRoot.fullscreenElement;
}
if ((fullscreenElement === this._container) !== this._fullscreen) {
this._handleFullscreenChange();
}
};
_handleFullscreenChange() {
this._fullscreen = !this._fullscreen;
this._fullscreenButton.classList.toggle('maplibregl-ctrl-shrink');
this._fullscreenButton.classList.toggle('maplibregl-ctrl-fullscreen');
this._updateTitle();
if (this._fullscreen) {
this.fire(new Event('fullscreenstart'));
this._prevCooperativeGesturesEnabled = this._map.cooperativeGestures.isEnabled();
this._map.cooperativeGestures.disable();
} else {
this.fire(new Event('fullscreenend'));
if (this._prevCooperativeGesturesEnabled) {
this._map.cooperativeGestures.enable();
}
}
}
_onClickFullscreen = () => {
if (this._isFullscreen()) {
this._exitFullscreen();
} else {
this._requestFullscreen();
}
};
_exitFullscreen() {
if (window.document.exitFullscreen) {
(window.document as any).exitFullscreen();
} else if ((window.document as any).mozCancelFullScreen) {
(window.document as any).mozCancelFullScreen();
} else if ((window.document as any).msExitFullscreen) {
(window.document as any).msExitFullscreen();
} else if ((window.document as any).webkitCancelFullScreen) {
(window.document as any).webkitCancelFullScreen();
} else {
this._togglePseudoFullScreen();
}
}
_requestFullscreen() {
if (this._container.requestFullscreen) {
this._container.requestFullscreen();
} else if ((this._container as any).mozRequestFullScreen) {
(this._container as any).mozRequestFullScreen();
} else if ((this._container as any).msRequestFullscreen) {
(this._container as any).msRequestFullscreen();
} else if ((this._container as any).webkitRequestFullscreen) {
(this._container as any).webkitRequestFullscreen();
} else {
this._togglePseudoFullScreen();
}
}
_togglePseudoFullScreen() {
this._container.classList.toggle('maplibregl-pseudo-fullscreen');
this._handleFullscreenChange();
this._map.resize();
}
}