forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpip_button.js
237 lines (195 loc) · 6.56 KB
/
pip_button.js
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.ui.PipButton');
goog.require('shaka.ui.Constants');
goog.require('shaka.ui.Element');
goog.require('shaka.ui.Enums');
goog.require('shaka.ui.Locales');
goog.require('shaka.ui.Localization');
goog.require('shaka.ui.OverflowMenu');
goog.require('shaka.ui.Utils');
goog.require('shaka.util.Dom');
goog.require('shaka.util.FakeEvent');
goog.requireType('shaka.ui.Controls');
/**
* @extends {shaka.ui.Element}
* @final
* @export
*/
shaka.ui.PipButton = class extends shaka.ui.Element {
/**
* @param {!HTMLElement} parent
* @param {!shaka.ui.Controls} controls
*/
constructor(parent, controls) {
super(parent, controls);
/** @private {HTMLMediaElement} */
this.localVideo_ = this.controls.getLocalVideo();
const LocIds = shaka.ui.Locales.Ids;
/** @private {!HTMLButtonElement} */
this.pipButton_ = shaka.util.Dom.createButton();
this.pipButton_.classList.add('shaka-pip-button');
/** @private {!HTMLElement} */
this.pipIcon_ = shaka.util.Dom.createHTMLElement('i');
this.pipIcon_.classList.add('material-icons-round');
this.pipIcon_.textContent = shaka.ui.Enums.MaterialDesignIcons.PIP;
this.pipButton_.appendChild(this.pipIcon_);
const label = shaka.util.Dom.createHTMLElement('label');
label.classList.add('shaka-overflow-button-label');
this.pipNameSpan_ = shaka.util.Dom.createHTMLElement('span');
this.pipNameSpan_.textContent =
this.localization.resolve(LocIds.PICTURE_IN_PICTURE);
label.appendChild(this.pipNameSpan_);
/** @private {!HTMLElement} */
this.currentPipState_ = shaka.util.Dom.createHTMLElement('span');
this.currentPipState_.classList.add('shaka-current-selection-span');
label.appendChild(this.currentPipState_);
this.pipButton_.appendChild(label);
this.updateLocalizedStrings_();
this.parent.appendChild(this.pipButton_);
// Don't display the button if PiP is not supported or not allowed.
// TODO: Can this ever change? Is it worth creating the button if the below
// condition is true?
if (!this.isPipAllowed_()) {
shaka.ui.Utils.setDisplay(this.pipButton_, false);
}
this.eventManager.listen(
this.localization, shaka.ui.Localization.LOCALE_UPDATED, () => {
this.updateLocalizedStrings_();
});
this.eventManager.listen(
this.localization, shaka.ui.Localization.LOCALE_CHANGED, () => {
this.updateLocalizedStrings_();
});
this.eventManager.listen(this.pipButton_, 'click', () => {
this.onPipClick_();
});
this.eventManager.listen(this.localVideo_, 'enterpictureinpicture', () => {
this.onEnterPictureInPicture_();
});
this.eventManager.listen(this.localVideo_, 'leavepictureinpicture', () => {
this.onLeavePictureInPicture_();
});
this.eventManager.listen(this.controls, 'caststatuschanged', (e) => {
this.onCastStatusChange_(e);
});
this.eventManager.listen(this.player, 'trackschanged', () => {
this.onTracksChanged_();
});
}
/**
* @return {boolean}
* @private
*/
isPipAllowed_() {
return document.pictureInPictureEnabled &&
!this.video.disablePictureInPicture;
}
/**
* @return {!Promise}
* @private
*/
async onPipClick_() {
try {
if (!document.pictureInPictureElement) {
// If you were fullscreen, leave fullscreen first.
if (document.fullscreenElement) {
document.exitFullscreen();
}
await this.video.requestPictureInPicture();
} else {
await document.exitPictureInPicture();
}
} catch (error) {
this.controls.dispatchEvent(new shaka.util.FakeEvent('error', {
detail: error,
}));
}
}
/** @private */
onEnterPictureInPicture_() {
const LocIds = shaka.ui.Locales.Ids;
this.pipIcon_.textContent = shaka.ui.Enums.MaterialDesignIcons.EXIT_PIP;
this.pipButton_.setAttribute(shaka.ui.Constants.ARIA_LABEL,
this.localization.resolve(LocIds.EXIT_PICTURE_IN_PICTURE));
this.currentPipState_.textContent =
this.localization.resolve(LocIds.ON);
}
/** @private */
onLeavePictureInPicture_() {
const LocIds = shaka.ui.Locales.Ids;
this.pipIcon_.textContent = shaka.ui.Enums.MaterialDesignIcons.PIP;
this.pipButton_.setAttribute(shaka.ui.Constants.ARIA_LABEL,
this.localization.resolve(LocIds.ENTER_PICTURE_IN_PICTURE));
this.currentPipState_.textContent =
this.localization.resolve(LocIds.OFF);
}
/**
* @private
*/
updateLocalizedStrings_() {
const LocIds = shaka.ui.Locales.Ids;
this.pipNameSpan_.textContent =
this.localization.resolve(LocIds.PICTURE_IN_PICTURE);
const ariaLabel = document.pictureInPictureElement ?
LocIds.EXIT_PICTURE_IN_PICTURE :
LocIds.ENTER_PICTURE_IN_PICTURE;
this.pipButton_.setAttribute(shaka.ui.Constants.ARIA_LABEL,
this.localization.resolve(ariaLabel));
const currentPipState = document.pictureInPictureElement ?
LocIds.ON : LocIds.OFF;
this.currentPipState_.textContent =
this.localization.resolve(currentPipState);
}
/**
* @param {Event} e
* @private
*/
onCastStatusChange_(e) {
const isCasting = e['newStatus'];
if (isCasting) {
// Picture-in-picture is not applicable if we're casting
if (this.isPipAllowed_()) {
shaka.ui.Utils.setDisplay(this.pipButton_, false);
}
} else {
if (this.isPipAllowed_()) {
shaka.ui.Utils.setDisplay(this.pipButton_, true);
}
}
}
/**
* Display the picture-in-picture button only when the content contains video.
* If it's displaying in picture-in-picture mode, and an audio only content is
* loaded, exit the picture-in-picture display.
* @return {!Promise}
* @private
*/
async onTracksChanged_() {
if (!this.isPipAllowed_()) {
shaka.ui.Utils.setDisplay(this.pipButton_, false);
} else if (this.player && this.player.isAudioOnly()) {
shaka.ui.Utils.setDisplay(this.pipButton_, false);
if (document.pictureInPictureElement) {
await document.exitPictureInPicture();
}
} else {
shaka.ui.Utils.setDisplay(this.pipButton_, true);
}
}
};
/**
* @implements {shaka.extern.IUIElement.Factory}
* @final
*/
shaka.ui.PipButton.Factory = class {
/** @override */
create(rootElement, controls) {
return new shaka.ui.PipButton(rootElement, controls);
}
};
shaka.ui.OverflowMenu.registerElement(
'picture_in_picture', new shaka.ui.PipButton.Factory());