1 import { VideoFile } from '../../../../shared/models/videos'
3 import 'videojs-hotkeys'
5 import 'videojs-contextmenu'
6 import 'videojs-contextmenu-ui'
7 import './peertube-link-button'
8 import './resolution-menu-button'
9 import './settings-menu-button'
10 import './webtorrent-info-button'
11 import './peertube-videojs-plugin'
12 import './peertube-load-progress-bar'
13 import './theater-button'
14 import { VideoJSCaption, videojsUntyped } from './peertube-videojs-typings'
15 import { buildVideoEmbed, buildVideoLink, copyToClipboard } from './utils'
16 import { getCompleteLocale, getShortLocale, is18nLocale, isDefaultLocale } from '../../../../shared/models/i18n/i18n'
18 // Change 'Playback Rate' to 'Speed' (smaller for our settings menu)
19 videojsUntyped.getComponent('PlaybackRateMenuButton').prototype.controlText_ = 'Speed'
20 // Change Captions to Subtitles/CC
21 videojsUntyped.getComponent('CaptionsButton').prototype.controlText_ = 'Subtitles/CC'
22 // We just want to display 'Off' instead of 'captions off', keep a space so the variable == true (hacky I know)
23 videojsUntyped.getComponent('CaptionsButton').prototype.label_ = ' '
25 function getVideojsOptions (options: {
27 playerElement: HTMLVideoElement,
29 videoDuration: number,
30 videoFiles: VideoFile[],
31 enableHotkeys: boolean,
32 inactivityTimeout: number,
33 peertubeLink: boolean,
37 videoCaptions: VideoJSCaption[],
42 const videojsOptions = {
43 // We don't use text track settings for now
44 textTrackSettings: false,
45 controls: options.controls !== undefined ? options.controls : true,
46 muted: options.controls !== undefined ? options.muted : false,
47 loop: options.loop !== undefined ? options.loop : false,
48 poster: options.poster,
50 inactivityTimeout: options.inactivityTimeout,
51 playbackRates: [ 0.5, 1, 1.5, 2 ],
54 autoplay: options.autoplay, // Use peertube plugin autoplay because we get the file by webtorrent
55 videoCaptions: options.videoCaptions,
56 videoFiles: options.videoFiles,
57 playerElement: options.playerElement,
58 videoViewUrl: options.videoViewUrl,
59 videoDuration: options.videoDuration,
60 startTime: options.startTime
64 children: getControlBarChildren(options)
68 if (options.enableHotkeys === true) {
69 Object.assign(videojsOptions.plugins, {
71 enableVolumeScroll: false,
72 enableModifiersForNumbers: false
80 function getControlBarChildren (options: {
83 videoCaptions: VideoJSCaption[]
85 const settingEntries = []
88 settingEntries.push('playbackRateMenuButton')
89 if (options.videoCaptions.length !== 0) settingEntries.push('captionsButton')
90 settingEntries.push('resolutionMenuButton')
94 'currentTimeDisplay': {},
96 'durationDisplay': {},
99 'flexibleWidthSpacer': {},
104 'peerTubeLoadProgressBar': {},
105 'mouseTimeDisplay': {},
106 'playProgressBar': {}
112 'webTorrentButton': {},
121 entries: settingEntries
125 if (options.peertubeLink === true) {
126 Object.assign(children, {
127 'peerTubeLinkButton': {}
131 if (options.theaterMode === true) {
132 Object.assign(children, {
137 Object.assign(children, {
138 'fullscreenToggle': {}
144 function addContextMenu (player: any, videoEmbedUrl: string) {
145 player.contextmenuUI({
148 label: player.localize('Copy the video URL'),
149 listener: function () {
150 copyToClipboard(buildVideoLink())
154 label: player.localize('Copy the video URL at the current time'),
155 listener: function () {
157 copyToClipboard(buildVideoLink(player.currentTime()))
161 label: player.localize('Copy embed code'),
163 copyToClipboard(buildVideoEmbed(videoEmbedUrl))
167 label: player.localize('Copy magnet URI'),
168 listener: function () {
170 copyToClipboard(player.peertube().getCurrentVideoFile().magnetUri)
177 function loadLocale (serverUrl: string, videojs: any, locale: string) {
178 const completeLocale = getCompleteLocale(locale)
180 if (!is18nLocale(completeLocale) || isDefaultLocale(completeLocale)) return Promise.resolve(undefined)
182 return fetch(serverUrl + '/client/locales/' + completeLocale + '/player.json')
183 .then(res => res.json())
184 .then(json => videojs.addLanguage(getShortLocale(completeLocale), json))