]>
Commit | Line | Data |
---|---|---|
c6352f2c C |
1 | import { VideoFile } from '../../../../shared/models/videos' |
2 | ||
3 | import 'videojs-hotkeys' | |
c7b0dacb | 4 | import 'videojs-dock' |
960a11e8 C |
5 | import 'videojs-contextmenu' |
6 | import 'videojs-contextmenu-ui' | |
c6352f2c C |
7 | import './peertube-link-button' |
8 | import './resolution-menu-button' | |
9 | import './settings-menu-button' | |
10 | import './webtorrent-info-button' | |
11 | import './peertube-videojs-plugin' | |
77728efa | 12 | import './peertube-load-progress-bar' |
054a103b | 13 | import './theater-button' |
16f7022b | 14 | import { VideoJSCaption, videojsUntyped } from './peertube-videojs-typings' |
960a11e8 | 15 | import { buildVideoEmbed, buildVideoLink, copyToClipboard } from './utils' |
9f164722 | 16 | import { getCompleteLocale, getShortLocale, is18nLocale, isDefaultLocale } from '../../../../shared/models/i18n/i18n' |
c6352f2c C |
17 | |
18 | // Change 'Playback Rate' to 'Speed' (smaller for our settings menu) | |
19 | videojsUntyped.getComponent('PlaybackRateMenuButton').prototype.controlText_ = 'Speed' | |
16f7022b C |
20 | // Change Captions to Subtitles/CC |
21 | videojsUntyped.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) | |
23 | videojsUntyped.getComponent('CaptionsButton').prototype.label_ = ' ' | |
c6352f2c C |
24 | |
25 | function 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 C |
34 | poster: string, |
35 | startTime: number | |
99941732 | 36 | theaterMode: boolean, |
16f7022b | 37 | videoCaptions: VideoJSCaption[], |
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 C |
50 | inactivityTimeout: options.inactivityTimeout, |
51 | playbackRates: [ 0.5, 1, 1.5, 2 ], | |
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 | ||
77 | return videojsOptions | |
78 | } | |
79 | ||
80 | function getControlBarChildren (options: { | |
81 | peertubeLink: boolean | |
16f7022b C |
82 | theaterMode: boolean, |
83 | videoCaptions: VideoJSCaption[] | |
c6352f2c | 84 | }) { |
16f7022b C |
85 | const settingEntries = [] |
86 | ||
87 | // Keep an order | |
88 | settingEntries.push('playbackRateMenuButton') | |
89 | if (options.videoCaptions.length !== 0) settingEntries.push('captionsButton') | |
90 | settingEntries.push('resolutionMenuButton') | |
91 | ||
c6352f2c C |
92 | const children = { |
93 | 'playToggle': {}, | |
94 | 'currentTimeDisplay': {}, | |
95 | 'timeDivider': {}, | |
96 | 'durationDisplay': {}, | |
97 | 'liveDisplay': {}, | |
98 | ||
99 | 'flexibleWidthSpacer': {}, | |
77728efa C |
100 | 'progressControl': { |
101 | children: { | |
102 | 'seekBar': { | |
103 | children: { | |
104 | 'peerTubeLoadProgressBar': {}, | |
db5529f5 | 105 | 'mouseTimeDisplay': {}, |
77728efa C |
106 | 'playProgressBar': {} |
107 | } | |
108 | } | |
109 | } | |
110 | }, | |
c6352f2c C |
111 | |
112 | 'webTorrentButton': {}, | |
113 | ||
114 | 'muteToggle': {}, | |
115 | 'volumeControl': {}, | |
116 | ||
117 | 'settingsButton': { | |
118 | setup: { | |
119 | maxHeightOffset: 40 | |
120 | }, | |
16f7022b | 121 | entries: settingEntries |
c6352f2c C |
122 | } |
123 | } | |
124 | ||
125 | if (options.peertubeLink === true) { | |
126 | Object.assign(children, { | |
127 | 'peerTubeLinkButton': {} | |
128 | }) | |
129 | } | |
130 | ||
054a103b C |
131 | if (options.theaterMode === true) { |
132 | Object.assign(children, { | |
133 | 'theaterButton': {} | |
134 | }) | |
135 | } | |
136 | ||
c6352f2c C |
137 | Object.assign(children, { |
138 | 'fullscreenToggle': {} | |
139 | }) | |
140 | ||
141 | return children | |
142 | } | |
143 | ||
e945b184 | 144 | function addContextMenu (player: any, videoEmbedUrl: string) { |
e945b184 C |
145 | player.contextmenuUI({ |
146 | content: [ | |
147 | { | |
148 | label: player.localize('Copy the video URL'), | |
149 | listener: function () { | |
150 | copyToClipboard(buildVideoLink()) | |
151 | } | |
152 | }, | |
153 | { | |
154 | label: player.localize('Copy the video URL at the current time'), | |
155 | listener: function () { | |
156 | const player = this | |
157 | copyToClipboard(buildVideoLink(player.currentTime())) | |
158 | } | |
159 | }, | |
160 | { | |
161 | label: player.localize('Copy embed code'), | |
162 | listener: () => { | |
163 | copyToClipboard(buildVideoEmbed(videoEmbedUrl)) | |
164 | } | |
5511da62 RK |
165 | }, |
166 | { | |
167 | label: player.localize('Copy magnet URI'), | |
168 | listener: function () { | |
169 | const player = this | |
170 | copyToClipboard(player.peertube().getCurrentVideoFile().magnetUri) | |
171 | } | |
e945b184 C |
172 | } |
173 | ] | |
174 | }) | |
175 | } | |
176 | ||
177 | function loadLocale (serverUrl: string, videojs: any, locale: string) { | |
74b7c6d4 | 178 | const completeLocale = getCompleteLocale(locale) |
e945b184 | 179 | |
74b7c6d4 C |
180 | if (!is18nLocale(completeLocale) || isDefaultLocale(completeLocale)) return Promise.resolve(undefined) |
181 | ||
182 | return fetch(serverUrl + '/client/locales/' + completeLocale + '/player.json') | |
e945b184 | 183 | .then(res => res.json()) |
9f164722 | 184 | .then(json => videojs.addLanguage(getShortLocale(completeLocale), json)) |
e945b184 C |
185 | } |
186 | ||
187 | export { | |
188 | loadLocale, | |
189 | getVideojsOptions, | |
190 | addContextMenu | |
191 | } |