]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/standalone/videos/test-embed.ts
Merge branch 'release/1.4.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / standalone / videos / test-embed.ts
CommitLineData
99941732 1import './test-embed.scss'
902aa3a0 2import { PeerTubePlayer } from '../player/player'
c199c427 3import { PeerTubeResolution, PlayerEventType } from '../player/definitions'
99941732
WL
4
5window.addEventListener('load', async () => {
99941732 6 const urlParts = window.location.href.split('/')
902aa3a0
C
7 const lastPart = urlParts[ urlParts.length - 1 ]
8 const videoId = lastPart.indexOf('?') === -1 ? lastPart : lastPart.split('?')[ 0 ]
99941732 9
c4710631 10 const iframe = document.createElement('iframe')
99941732 11 iframe.src = `/videos/embed/${videoId}?autoplay=1&controls=0&api=1`
c4710631 12 const mainElement = document.querySelector('#host')
902aa3a0 13 mainElement.appendChild(iframe)
99941732
WL
14
15 console.log(`Document finished loading.`)
c4710631 16 const player = new PeerTubePlayer(document.querySelector('iframe'))
99941732 17
902aa3a0 18 window[ 'player' ] = player
99941732
WL
19
20 console.log(`Awaiting player ready...`)
21 await player.ready
22 console.log(`Player is ready.`)
23
c4710631 24 const monitoredEvents = [
902aa3a0
C
25 'pause',
26 'play',
27 'playbackStatusUpdate',
99941732
WL
28 'playbackStatusChange'
29 ]
30
31 monitoredEvents.forEach(e => {
902aa3a0 32 player.addEventListener(e as PlayerEventType, () => console.log(`PLAYER: event '${e}' received`))
99941732
WL
33 console.log(`PLAYER: now listening for event '${e}'`)
34 })
35
902aa3a0 36 let playbackRates: number[] = []
99941732
WL
37 let currentRate = await player.getPlaybackRate()
38
c4710631
C
39 const updateRates = async () => {
40 const rateListEl = document.querySelector('#rate-list')
99941732 41 rateListEl.innerHTML = ''
902aa3a0 42
99941732 43 playbackRates.forEach(rate => {
902aa3a0 44 if (currentRate === rate) {
c4710631 45 const itemEl = document.createElement('strong')
99941732
WL
46 itemEl.innerText = `${rate} (active)`
47 itemEl.style.display = 'block'
48 rateListEl.appendChild(itemEl)
49 } else {
c4710631 50 const itemEl = document.createElement('a')
99941732 51 itemEl.href = 'javascript:;'
a7c9cbb5 52 itemEl.innerText = rate.toString()
99941732
WL
53 itemEl.addEventListener('click', () => {
54 player.setPlaybackRate(rate)
55 currentRate = rate
56 updateRates()
57 })
58 itemEl.style.display = 'block'
59 rateListEl.appendChild(itemEl)
60 }
61 })
62 }
63
64 player.getPlaybackRates().then(rates => {
65 playbackRates = rates
66 updateRates()
67 })
68
c4710631
C
69 const updateResolutions = ((resolutions: PeerTubeResolution[]) => {
70 const resolutionListEl = document.querySelector('#resolution-list')
99941732
WL
71 resolutionListEl.innerHTML = ''
72
c199c427 73 resolutions.forEach(resolution => {
99941732 74 if (resolution.active) {
c4710631 75 const itemEl = document.createElement('strong')
99941732
WL
76 itemEl.innerText = `${resolution.label} (active)`
77 itemEl.style.display = 'block'
78 resolutionListEl.appendChild(itemEl)
79 } else {
c4710631 80 const itemEl = document.createElement('a')
99941732
WL
81 itemEl.href = 'javascript:;'
82 itemEl.innerText = resolution.label
83 itemEl.addEventListener('click', () => {
84 player.setResolution(resolution.id)
85 })
86 itemEl.style.display = 'block'
87 resolutionListEl.appendChild(itemEl)
88 }
89 })
244b4ae3 90 })
99941732
WL
91
92 player.getResolutions().then(
93 resolutions => updateResolutions(resolutions))
902aa3a0 94 player.addEventListener('resolutionUpdate',
99941732 95 resolutions => updateResolutions(resolutions))
902aa3a0 96})