]>
Commit | Line | Data |
---|---|---|
99941732 | 1 | import './test-embed.scss' |
902aa3a0 | 2 | import { PeerTubePlayer } from '../player/player' |
1151f521 | 3 | import { PeerTubeResolution, PlayerEventType, PeerTubeTextTrack } from '../player/definitions' |
99941732 WL |
4 | |
5 | window.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') |
6ccdf9d5 | 11 | iframe.src = `/videos/embed/${videoId}?api=1` |
6377a9f2 | 12 | |
c4710631 | 13 | const mainElement = document.querySelector('#host') |
902aa3a0 | 14 | mainElement.appendChild(iframe) |
99941732 WL |
15 | |
16 | console.log(`Document finished loading.`) | |
c4710631 | 17 | const player = new PeerTubePlayer(document.querySelector('iframe')) |
99941732 | 18 | |
902aa3a0 | 19 | window[ 'player' ] = player |
99941732 WL |
20 | |
21 | console.log(`Awaiting player ready...`) | |
22 | await player.ready | |
23 | console.log(`Player is ready.`) | |
24 | ||
c4710631 | 25 | const monitoredEvents = [ |
902aa3a0 C |
26 | 'pause', |
27 | 'play', | |
28 | 'playbackStatusUpdate', | |
99941732 WL |
29 | 'playbackStatusChange' |
30 | ] | |
31 | ||
32 | monitoredEvents.forEach(e => { | |
5ab994fe | 33 | player.addEventListener(e as PlayerEventType, (param) => console.log(`PLAYER: event '${e}' received`, param)) |
99941732 WL |
34 | console.log(`PLAYER: now listening for event '${e}'`) |
35 | }) | |
36 | ||
902aa3a0 | 37 | let playbackRates: number[] = [] |
99941732 WL |
38 | let currentRate = await player.getPlaybackRate() |
39 | ||
c4710631 C |
40 | const updateRates = async () => { |
41 | const rateListEl = document.querySelector('#rate-list') | |
99941732 | 42 | rateListEl.innerHTML = '' |
902aa3a0 | 43 | |
99941732 | 44 | playbackRates.forEach(rate => { |
902aa3a0 | 45 | if (currentRate === rate) { |
c4710631 | 46 | const itemEl = document.createElement('strong') |
99941732 WL |
47 | itemEl.innerText = `${rate} (active)` |
48 | itemEl.style.display = 'block' | |
49 | rateListEl.appendChild(itemEl) | |
50 | } else { | |
c4710631 | 51 | const itemEl = document.createElement('a') |
99941732 | 52 | itemEl.href = 'javascript:;' |
a7c9cbb5 | 53 | itemEl.innerText = rate.toString() |
99941732 WL |
54 | itemEl.addEventListener('click', () => { |
55 | player.setPlaybackRate(rate) | |
56 | currentRate = rate | |
57 | updateRates() | |
58 | }) | |
59 | itemEl.style.display = 'block' | |
60 | rateListEl.appendChild(itemEl) | |
61 | } | |
62 | }) | |
63 | } | |
64 | ||
65 | player.getPlaybackRates().then(rates => { | |
66 | playbackRates = rates | |
67 | updateRates() | |
68 | }) | |
69 | ||
1151f521 C |
70 | const updateCaptions = async () => { |
71 | const captions = await player.getCaptions() | |
72 | ||
73 | const captionEl = document.querySelector('#caption-list') | |
74 | captionEl.innerHTML = '' | |
75 | ||
76 | captions.forEach(c => { | |
77 | console.log(c) | |
78 | ||
79 | if (c.mode === 'showing') { | |
80 | const itemEl = document.createElement('strong') | |
81 | itemEl.innerText = `${c.label} (active)` | |
82 | itemEl.style.display = 'block' | |
83 | captionEl.appendChild(itemEl) | |
84 | } else { | |
85 | const itemEl = document.createElement('a') | |
86 | itemEl.href = 'javascript:;' | |
87 | itemEl.innerText = c.label | |
88 | itemEl.addEventListener('click', () => { | |
89 | player.setCaption(c.id) | |
90 | updateCaptions() | |
91 | }) | |
92 | itemEl.style.display = 'block' | |
93 | captionEl.appendChild(itemEl) | |
94 | } | |
95 | }) | |
96 | } | |
97 | ||
98 | updateCaptions() | |
99 | ||
c4710631 C |
100 | const updateResolutions = ((resolutions: PeerTubeResolution[]) => { |
101 | const resolutionListEl = document.querySelector('#resolution-list') | |
99941732 WL |
102 | resolutionListEl.innerHTML = '' |
103 | ||
c199c427 | 104 | resolutions.forEach(resolution => { |
99941732 | 105 | if (resolution.active) { |
c4710631 | 106 | const itemEl = document.createElement('strong') |
99941732 WL |
107 | itemEl.innerText = `${resolution.label} (active)` |
108 | itemEl.style.display = 'block' | |
109 | resolutionListEl.appendChild(itemEl) | |
110 | } else { | |
c4710631 | 111 | const itemEl = document.createElement('a') |
99941732 WL |
112 | itemEl.href = 'javascript:;' |
113 | itemEl.innerText = resolution.label | |
114 | itemEl.addEventListener('click', () => { | |
115 | player.setResolution(resolution.id) | |
116 | }) | |
117 | itemEl.style.display = 'block' | |
118 | resolutionListEl.appendChild(itemEl) | |
119 | } | |
120 | }) | |
244b4ae3 | 121 | }) |
99941732 WL |
122 | |
123 | player.getResolutions().then( | |
124 | resolutions => updateResolutions(resolutions)) | |
902aa3a0 | 125 | player.addEventListener('resolutionUpdate', |
99941732 | 126 | resolutions => updateResolutions(resolutions)) |
6377a9f2 C |
127 | |
128 | const updateVolume = (volume: number) => { | |
129 | const volumeEl = document.getElementById('volume') | |
130 | volumeEl.innerText = (volume * 100) + '%' | |
131 | } | |
132 | ||
133 | player.getVolume().then(volume => updateVolume(volume)) | |
134 | player.addEventListener('volumeChange', volume => updateVolume(volume)) | |
902aa3a0 | 135 | }) |