1
0
Fork 0
peertube/client/src/assets/player/peertube-player.ts

156 lines
3.8 KiB
TypeScript
Raw Normal View History

import { VideoFile } from '../../../../shared/models/videos'
import 'videojs-hotkeys'
import 'videojs-dock'
2018-05-30 11:10:00 -04:00
import 'videojs-contextmenu'
import 'videojs-contextmenu-ui'
import './peertube-link-button'
import './resolution-menu-button'
import './settings-menu-button'
import './webtorrent-info-button'
import './peertube-videojs-plugin'
2018-05-31 03:51:51 -04:00
import './peertube-load-progress-bar'
import { videojsUntyped } from './peertube-videojs-typings'
2018-05-30 11:10:00 -04:00
import { buildVideoEmbed, buildVideoLink, copyToClipboard } from './utils'
2018-06-06 12:04:33 -04:00
import { getCompleteLocale, getShortLocale, is18nLocale, isDefaultLocale } from '../../../../shared/models/i18n/i18n'
// Change 'Playback Rate' to 'Speed' (smaller for our settings menu)
videojsUntyped.getComponent('PlaybackRateMenuButton').prototype.controlText_ = 'Speed'
function getVideojsOptions (options: {
autoplay: boolean,
playerElement: HTMLVideoElement,
videoViewUrl: string,
videoDuration: number,
videoFiles: VideoFile[],
enableHotkeys: boolean,
inactivityTimeout: number,
2018-04-03 09:11:46 -04:00
peertubeLink: boolean,
2018-04-05 11:06:59 -04:00
poster: string,
startTime: number
}) {
const videojsOptions = {
controls: true,
2018-04-03 09:11:46 -04:00
poster: options.poster,
2018-04-18 04:20:13 -04:00
autoplay: false,
inactivityTimeout: options.inactivityTimeout,
playbackRates: [ 0.5, 1, 1.5, 2 ],
plugins: {
peertube: {
2018-04-18 04:20:13 -04:00
autoplay: options.autoplay, // Use peertube plugin autoplay because we get the file by webtorrent
videoFiles: options.videoFiles,
playerElement: options.playerElement,
videoViewUrl: options.videoViewUrl,
2018-04-05 11:06:59 -04:00
videoDuration: options.videoDuration,
startTime: options.startTime
}
},
controlBar: {
children: getControlBarChildren(options)
}
}
if (options.enableHotkeys === true) {
Object.assign(videojsOptions.plugins, {
hotkeys: {
enableVolumeScroll: false
}
})
}
return videojsOptions
}
function getControlBarChildren (options: {
peertubeLink: boolean
}) {
const children = {
'playToggle': {},
'currentTimeDisplay': {},
'timeDivider': {},
'durationDisplay': {},
'liveDisplay': {},
'flexibleWidthSpacer': {},
2018-05-31 03:51:51 -04:00
'progressControl': {
children: {
'seekBar': {
children: {
'peerTubeLoadProgressBar': {},
'playProgressBar': {}
}
}
}
},
'webTorrentButton': {},
'muteToggle': {},
'volumeControl': {},
'settingsButton': {
setup: {
maxHeightOffset: 40
},
entries: [
'resolutionMenuButton',
'playbackRateMenuButton'
]
}
}
if (options.peertubeLink === true) {
Object.assign(children, {
'peerTubeLinkButton': {}
})
}
Object.assign(children, {
'fullscreenToggle': {}
})
return children
}
2018-06-06 08:23:40 -04:00
function addContextMenu (player: any, videoEmbedUrl: string) {
player.contextmenuUI({
content: [
{
label: player.localize('Copy the video URL'),
listener: function () {
copyToClipboard(buildVideoLink())
}
},
{
label: player.localize('Copy the video URL at the current time'),
listener: function () {
const player = this
copyToClipboard(buildVideoLink(player.currentTime()))
}
},
{
label: player.localize('Copy embed code'),
listener: () => {
copyToClipboard(buildVideoEmbed(videoEmbedUrl))
}
}
]
})
}
function loadLocale (serverUrl: string, videojs: any, locale: string) {
2018-06-06 11:37:13 -04:00
const completeLocale = getCompleteLocale(locale)
2018-06-06 08:23:40 -04:00
2018-06-06 11:37:13 -04:00
if (!is18nLocale(completeLocale) || isDefaultLocale(completeLocale)) return Promise.resolve(undefined)
return fetch(serverUrl + '/client/locales/' + completeLocale + '/player.json')
2018-06-06 08:23:40 -04:00
.then(res => res.json())
2018-06-06 12:04:33 -04:00
.then(json => videojs.addLanguage(getShortLocale(completeLocale), json))
2018-06-06 08:23:40 -04:00
}
export {
loadLocale,
getVideojsOptions,
addContextMenu
}