]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/assets/player/peertube-player.ts
Upgrade client dependencies
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / peertube-player.ts
CommitLineData
c6352f2c
C
1import { VideoFile } from '../../../../shared/models/videos'
2
3import 'videojs-hotkeys'
c7b0dacb 4import 'videojs-dock'
960a11e8 5import 'videojs-contextmenu-ui'
c6352f2c
C
6import './peertube-link-button'
7import './resolution-menu-button'
8import './settings-menu-button'
9import './webtorrent-info-button'
10import './peertube-videojs-plugin'
77728efa 11import './peertube-load-progress-bar'
054a103b 12import './theater-button'
16f7022b 13import { VideoJSCaption, videojsUntyped } from './peertube-videojs-typings'
960a11e8 14import { buildVideoEmbed, buildVideoLink, copyToClipboard } from './utils'
9f164722 15import { getCompleteLocale, getShortLocale, is18nLocale, isDefaultLocale } from '../../../../shared/models/i18n/i18n'
c6352f2c
C
16
17// Change 'Playback Rate' to 'Speed' (smaller for our settings menu)
18videojsUntyped.getComponent('PlaybackRateMenuButton').prototype.controlText_ = 'Speed'
16f7022b
C
19// Change Captions to Subtitles/CC
20videojsUntyped.getComponent('CaptionsButton').prototype.controlText_ = 'Subtitles/CC'
21// We just want to display 'Off' instead of 'captions off', keep a space so the variable == true (hacky I know)
22videojsUntyped.getComponent('CaptionsButton').prototype.label_ = ' '
c6352f2c
C
23
24function getVideojsOptions (options: {
25 autoplay: boolean,
26 playerElement: HTMLVideoElement,
27 videoViewUrl: string,
28 videoDuration: number,
29 videoFiles: VideoFile[],
30 enableHotkeys: boolean,
31 inactivityTimeout: number,
33d78552 32 peertubeLink: boolean,
f37bad63 33 poster: string,
1f6824c9 34 startTime: number | string
99941732 35 theaterMode: boolean,
16f7022b 36 videoCaptions: VideoJSCaption[],
95d51135 37 language?: string,
99941732
WL
38 controls?: boolean,
39 muted?: boolean,
40 loop?: boolean
c6352f2c
C
41}) {
42 const videojsOptions = {
16f7022b
C
43 // We don't use text track settings for now
44 textTrackSettings: false,
99941732
WL
45 controls: options.controls !== undefined ? options.controls : true,
46 muted: options.controls !== undefined ? options.muted : false,
47 loop: options.loop !== undefined ? options.loop : false,
33d78552 48 poster: options.poster,
80109b2d 49 autoplay: false,
c6352f2c 50 inactivityTimeout: options.inactivityTimeout,
4ccb6c08 51 playbackRates: [ 0.5, 0.75, 1, 1.25, 1.5, 2 ],
c6352f2c
C
52 plugins: {
53 peertube: {
80109b2d 54 autoplay: options.autoplay, // Use peertube plugin autoplay because we get the file by webtorrent
16f7022b 55 videoCaptions: options.videoCaptions,
c6352f2c
C
56 videoFiles: options.videoFiles,
57 playerElement: options.playerElement,
58 videoViewUrl: options.videoViewUrl,
f37bad63
C
59 videoDuration: options.videoDuration,
60 startTime: options.startTime
c6352f2c
C
61 }
62 },
63 controlBar: {
64 children: getControlBarChildren(options)
65 }
66 }
67
68 if (options.enableHotkeys === true) {
69 Object.assign(videojsOptions.plugins, {
70 hotkeys: {
c4ccb08a
RK
71 enableVolumeScroll: false,
72 enableModifiersForNumbers: false
c6352f2c
C
73 }
74 })
75 }
76
95d51135
C
77 if (options.language && !isDefaultLocale(options.language)) {
78 Object.assign(videojsOptions, { language: options.language })
79 }
80
c6352f2c
C
81 return videojsOptions
82}
83
84function getControlBarChildren (options: {
85 peertubeLink: boolean
16f7022b
C
86 theaterMode: boolean,
87 videoCaptions: VideoJSCaption[]
c6352f2c 88}) {
16f7022b
C
89 const settingEntries = []
90
91 // Keep an order
92 settingEntries.push('playbackRateMenuButton')
93 if (options.videoCaptions.length !== 0) settingEntries.push('captionsButton')
94 settingEntries.push('resolutionMenuButton')
95
c6352f2c
C
96 const children = {
97 'playToggle': {},
98 'currentTimeDisplay': {},
99 'timeDivider': {},
100 'durationDisplay': {},
101 'liveDisplay': {},
102
103 'flexibleWidthSpacer': {},
77728efa
C
104 'progressControl': {
105 children: {
106 'seekBar': {
107 children: {
108 'peerTubeLoadProgressBar': {},
db5529f5 109 'mouseTimeDisplay': {},
77728efa
C
110 'playProgressBar': {}
111 }
112 }
113 }
114 },
c6352f2c
C
115
116 'webTorrentButton': {},
117
118 'muteToggle': {},
119 'volumeControl': {},
120
121 'settingsButton': {
122 setup: {
123 maxHeightOffset: 40
124 },
16f7022b 125 entries: settingEntries
c6352f2c
C
126 }
127 }
128
129 if (options.peertubeLink === true) {
130 Object.assign(children, {
131 'peerTubeLinkButton': {}
132 })
133 }
134
054a103b
C
135 if (options.theaterMode === true) {
136 Object.assign(children, {
137 'theaterButton': {}
138 })
139 }
140
c6352f2c
C
141 Object.assign(children, {
142 'fullscreenToggle': {}
143 })
144
145 return children
146}
147
e945b184 148function addContextMenu (player: any, videoEmbedUrl: string) {
e945b184
C
149 player.contextmenuUI({
150 content: [
151 {
152 label: player.localize('Copy the video URL'),
153 listener: function () {
154 copyToClipboard(buildVideoLink())
155 }
156 },
157 {
158 label: player.localize('Copy the video URL at the current time'),
159 listener: function () {
160 const player = this
161 copyToClipboard(buildVideoLink(player.currentTime()))
162 }
163 },
164 {
165 label: player.localize('Copy embed code'),
166 listener: () => {
167 copyToClipboard(buildVideoEmbed(videoEmbedUrl))
168 }
5511da62
RK
169 },
170 {
171 label: player.localize('Copy magnet URI'),
172 listener: function () {
173 const player = this
174 copyToClipboard(player.peertube().getCurrentVideoFile().magnetUri)
175 }
e945b184
C
176 }
177 ]
178 })
179}
180
3dfa8494
C
181function loadLocaleInVideoJS (serverUrl: string, videojs: any, locale: string) {
182 const path = getLocalePath(serverUrl, locale)
183 // It is the default locale, nothing to translate
184 if (!path) return Promise.resolve(undefined)
e945b184 185
5d128505
C
186 let p: Promise<any>
187
188 if (loadLocaleInVideoJS.cache[path]) {
189 p = Promise.resolve(loadLocaleInVideoJS.cache[path])
190 } else {
191 p = fetch(path + '/player.json')
192 .then(res => res.json())
193 .then(json => {
194 loadLocaleInVideoJS.cache[path] = json
195 return json
196 })
197 }
74b7c6d4 198
5d128505
C
199 const completeLocale = getCompleteLocale(locale)
200 return p.then(json => videojs.addLanguage(getShortLocale(completeLocale), json))
201}
202namespace loadLocaleInVideoJS {
203 export const cache: { [ path: string ]: any } = {}
e945b184
C
204}
205
3dfa8494
C
206function getServerTranslations (serverUrl: string, locale: string) {
207 const path = getLocalePath(serverUrl, locale)
208 // It is the default locale, nothing to translate
209 if (!path) return Promise.resolve(undefined)
210
211 return fetch(path + '/server.json')
212 .then(res => res.json())
213}
214
215// ############################################################################
216
e945b184 217export {
3dfa8494
C
218 getServerTranslations,
219 loadLocaleInVideoJS,
e945b184
C
220 getVideojsOptions,
221 addContextMenu
222}
3dfa8494
C
223
224// ############################################################################
225
226function getLocalePath (serverUrl: string, locale: string) {
227 const completeLocale = getCompleteLocale(locale)
228
229 if (!is18nLocale(completeLocale) || isDefaultLocale(completeLocale)) return undefined
230
231 return serverUrl + '/client/locales/' + completeLocale
232}