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