]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/assets/player/peertube-player.ts
Update translations
[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 71 enableVolumeScroll: false,
52b1ba03
RK
72 enableModifiersForNumbers: false,
73 customKeys: {
74 increasePlaybackRateKey: {
75 key: function (event) {
5363a766 76 return event.key === '>'
52b1ba03 77 },
5363a766
C
78 handler: function (player) {
79 player.playbackRate((player.playbackRate() + 0.1).toFixed(2))
52b1ba03
RK
80 }
81 },
82 decreasePlaybackRateKey: {
83 key: function (event) {
5363a766 84 return event.key === '<'
52b1ba03 85 },
5363a766
C
86 handler: function (player) {
87 player.playbackRate((player.playbackRate() - 0.1).toFixed(2))
52b1ba03
RK
88 }
89 }
90 }
c6352f2c
C
91 }
92 })
93 }
94
95d51135
C
95 if (options.language && !isDefaultLocale(options.language)) {
96 Object.assign(videojsOptions, { language: options.language })
97 }
98
c6352f2c
C
99 return videojsOptions
100}
101
102function getControlBarChildren (options: {
103 peertubeLink: boolean
16f7022b
C
104 theaterMode: boolean,
105 videoCaptions: VideoJSCaption[]
c6352f2c 106}) {
16f7022b
C
107 const settingEntries = []
108
109 // Keep an order
110 settingEntries.push('playbackRateMenuButton')
111 if (options.videoCaptions.length !== 0) settingEntries.push('captionsButton')
112 settingEntries.push('resolutionMenuButton')
113
c6352f2c
C
114 const children = {
115 'playToggle': {},
116 'currentTimeDisplay': {},
117 'timeDivider': {},
118 'durationDisplay': {},
119 'liveDisplay': {},
120
121 'flexibleWidthSpacer': {},
77728efa
C
122 'progressControl': {
123 children: {
124 'seekBar': {
125 children: {
126 'peerTubeLoadProgressBar': {},
db5529f5 127 'mouseTimeDisplay': {},
77728efa
C
128 'playProgressBar': {}
129 }
130 }
131 }
132 },
c6352f2c
C
133
134 'webTorrentButton': {},
135
136 'muteToggle': {},
137 'volumeControl': {},
138
139 'settingsButton': {
140 setup: {
141 maxHeightOffset: 40
142 },
16f7022b 143 entries: settingEntries
c6352f2c
C
144 }
145 }
146
147 if (options.peertubeLink === true) {
148 Object.assign(children, {
149 'peerTubeLinkButton': {}
150 })
151 }
152
054a103b
C
153 if (options.theaterMode === true) {
154 Object.assign(children, {
155 'theaterButton': {}
156 })
157 }
158
c6352f2c
C
159 Object.assign(children, {
160 'fullscreenToggle': {}
161 })
162
163 return children
164}
165
e945b184 166function addContextMenu (player: any, videoEmbedUrl: string) {
e945b184
C
167 player.contextmenuUI({
168 content: [
169 {
170 label: player.localize('Copy the video URL'),
171 listener: function () {
172 copyToClipboard(buildVideoLink())
173 }
174 },
175 {
176 label: player.localize('Copy the video URL at the current time'),
177 listener: function () {
178 const player = this
179 copyToClipboard(buildVideoLink(player.currentTime()))
180 }
181 },
182 {
183 label: player.localize('Copy embed code'),
184 listener: () => {
185 copyToClipboard(buildVideoEmbed(videoEmbedUrl))
186 }
5511da62
RK
187 },
188 {
189 label: player.localize('Copy magnet URI'),
190 listener: function () {
191 const player = this
192 copyToClipboard(player.peertube().getCurrentVideoFile().magnetUri)
193 }
e945b184
C
194 }
195 ]
196 })
197}
198
3dfa8494
C
199function loadLocaleInVideoJS (serverUrl: string, videojs: any, locale: string) {
200 const path = getLocalePath(serverUrl, locale)
201 // It is the default locale, nothing to translate
202 if (!path) return Promise.resolve(undefined)
e945b184 203
5d128505
C
204 let p: Promise<any>
205
206 if (loadLocaleInVideoJS.cache[path]) {
207 p = Promise.resolve(loadLocaleInVideoJS.cache[path])
208 } else {
209 p = fetch(path + '/player.json')
210 .then(res => res.json())
211 .then(json => {
212 loadLocaleInVideoJS.cache[path] = json
213 return json
214 })
215 }
74b7c6d4 216
5d128505
C
217 const completeLocale = getCompleteLocale(locale)
218 return p.then(json => videojs.addLanguage(getShortLocale(completeLocale), json))
219}
220namespace loadLocaleInVideoJS {
221 export const cache: { [ path: string ]: any } = {}
e945b184
C
222}
223
3dfa8494
C
224function getServerTranslations (serverUrl: string, locale: string) {
225 const path = getLocalePath(serverUrl, locale)
226 // It is the default locale, nothing to translate
227 if (!path) return Promise.resolve(undefined)
228
229 return fetch(path + '/server.json')
230 .then(res => res.json())
231}
232
233// ############################################################################
234
e945b184 235export {
3dfa8494
C
236 getServerTranslations,
237 loadLocaleInVideoJS,
e945b184
C
238 getVideojsOptions,
239 addContextMenu
240}
3dfa8494
C
241
242// ############################################################################
243
244function getLocalePath (serverUrl: string, locale: string) {
245 const completeLocale = getCompleteLocale(locale)
246
247 if (!is18nLocale(completeLocale) || isDefaultLocale(completeLocale)) return undefined
248
249 return serverUrl + '/client/locales/' + completeLocale
250}