-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathfullscreen-widget.tsx
More file actions
165 lines (145 loc) · 4.27 KB
/
Copy pathfullscreen-widget.tsx
File metadata and controls
165 lines (145 loc) · 4.27 KB
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
// deck.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
/* global document */
import {
_deepEqual as deepEqual,
_applyStyles as applyStyles,
_removeStyles as removeStyles
} from '@deck.gl/core';
import type {Deck, Widget, WidgetPlacement} from '@deck.gl/core';
import {render} from 'preact';
import {IconButton} from './components';
export type FullscreenWidgetProps = {
id?: string;
/**
* Widget positioning within the view. Default 'top-left'.
*/
placement?: WidgetPlacement;
/**
* A [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.
*/
/* eslint-enable max-len */
container?: HTMLElement;
/**
* Tooltip message when out of fullscreen.
*/
enterLabel?: string;
/**
* Tooltip message when fullscreen.
*/
exitLabel?: string;
/**
* CSS inline style overrides.
*/
style?: Partial<CSSStyleDeclaration>;
/**
* Additional CSS class.
*/
className?: string;
};
export class FullscreenWidget implements Widget<FullscreenWidgetProps> {
id = 'fullscreen';
props: FullscreenWidgetProps;
placement: WidgetPlacement = 'top-left';
deck?: Deck<any>;
element?: HTMLDivElement;
fullscreen: boolean = false;
constructor(props: FullscreenWidgetProps) {
this.id = props.id ?? this.id;
this.placement = props.placement ?? this.placement;
this.props = {
...props,
enterLabel: props.enterLabel ?? 'Enter Fullscreen',
exitLabel: props.exitLabel ?? 'Exit Fullscreen',
style: props.style ?? {}
};
}
onAdd({deck}: {deck: Deck<any>}): HTMLDivElement {
const {style, className} = this.props;
const el = document.createElement('div');
el.classList.add('deck-widget', 'deck-widget-fullscreen');
if (className) el.classList.add(className);
applyStyles(el, style);
this.deck = deck;
this.element = el;
this.update();
document.addEventListener('fullscreenchange', this.onFullscreenChange.bind(this));
return el;
}
onRemove() {
this.deck = undefined;
this.element = undefined;
document.removeEventListener('fullscreenchange', this.onFullscreenChange.bind(this));
}
private update() {
const {enterLabel, exitLabel} = this.props;
const element = this.element;
if (!element) {
return;
}
const ui = (
<IconButton
onClick={this.handleClick.bind(this)}
label={this.fullscreen ? exitLabel : enterLabel}
className={this.fullscreen ? 'deck-widget-fullscreen-exit' : 'deck-widget-fullscreen-enter'}
/>
);
render(ui, element);
}
setProps(props: Partial<FullscreenWidgetProps>) {
this.placement = props.placement ?? this.placement;
const oldProps = this.props;
const el = this.element;
if (el) {
if (oldProps.className !== props.className) {
if (oldProps.className) el.classList.remove(oldProps.className);
if (props.className) el.classList.add(props.className);
}
if (!deepEqual(oldProps.style, props.style, 1)) {
removeStyles(el, oldProps.style);
applyStyles(el, props.style);
}
}
Object.assign(this.props, props);
this.update();
}
getContainer() {
return this.props.container || this.deck?.getCanvas()?.parentElement;
}
onFullscreenChange() {
const prevFullscreen = this.fullscreen;
const fullscreen = document.fullscreenElement === this.getContainer();
if (prevFullscreen !== fullscreen) {
this.fullscreen = !this.fullscreen;
}
this.update();
}
async handleClick() {
if (this.fullscreen) {
await this.exitFullscreen();
} else {
await this.requestFullscreen();
}
this.update();
}
async requestFullscreen() {
const container = this.getContainer();
if (container?.requestFullscreen) {
await container.requestFullscreen({navigationUI: 'hide'});
} else {
this.togglePseudoFullscreen();
}
}
async exitFullscreen() {
if (document.exitFullscreen) {
await document.exitFullscreen();
} else {
this.togglePseudoFullscreen();
}
}
togglePseudoFullscreen() {
this.getContainer()?.classList.toggle('deck-pseudo-fullscreen');
}
}