]>
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' |
c6352f2c | 14 | import { 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' | |
20 | ||
21 | function getVideojsOptions (options: { | |
22 | autoplay: boolean, | |
23 | playerElement: HTMLVideoElement, | |
24 | videoViewUrl: string, | |
25 | videoDuration: number, | |
26 | videoFiles: VideoFile[], | |
27 | enableHotkeys: boolean, | |
28 | inactivityTimeout: number, | |
33d78552 | 29 | peertubeLink: boolean, |
f37bad63 C |
30 | poster: string, |
31 | startTime: number | |
054a103b | 32 | theaterMode: boolean |
c6352f2c C |
33 | }) { |
34 | const videojsOptions = { | |
35 | controls: true, | |
33d78552 | 36 | poster: options.poster, |
80109b2d | 37 | autoplay: false, |
c6352f2c C |
38 | inactivityTimeout: options.inactivityTimeout, |
39 | playbackRates: [ 0.5, 1, 1.5, 2 ], | |
40 | plugins: { | |
41 | peertube: { | |
80109b2d | 42 | autoplay: options.autoplay, // Use peertube plugin autoplay because we get the file by webtorrent |
c6352f2c C |
43 | videoFiles: options.videoFiles, |
44 | playerElement: options.playerElement, | |
45 | videoViewUrl: options.videoViewUrl, | |
f37bad63 C |
46 | videoDuration: options.videoDuration, |
47 | startTime: options.startTime | |
c6352f2c C |
48 | } |
49 | }, | |
50 | controlBar: { | |
51 | children: getControlBarChildren(options) | |
52 | } | |
53 | } | |
54 | ||
55 | if (options.enableHotkeys === true) { | |
56 | Object.assign(videojsOptions.plugins, { | |
57 | hotkeys: { | |
58 | enableVolumeScroll: false | |
59 | } | |
60 | }) | |
61 | } | |
62 | ||
63 | return videojsOptions | |
64 | } | |
65 | ||
66 | function getControlBarChildren (options: { | |
67 | peertubeLink: boolean | |
054a103b | 68 | theaterMode: boolean |
c6352f2c C |
69 | }) { |
70 | const children = { | |
71 | 'playToggle': {}, | |
72 | 'currentTimeDisplay': {}, | |
73 | 'timeDivider': {}, | |
74 | 'durationDisplay': {}, | |
75 | 'liveDisplay': {}, | |
76 | ||
77 | 'flexibleWidthSpacer': {}, | |
77728efa C |
78 | 'progressControl': { |
79 | children: { | |
80 | 'seekBar': { | |
81 | children: { | |
82 | 'peerTubeLoadProgressBar': {}, | |
db5529f5 | 83 | 'mouseTimeDisplay': {}, |
77728efa C |
84 | 'playProgressBar': {} |
85 | } | |
86 | } | |
87 | } | |
88 | }, | |
c6352f2c C |
89 | |
90 | 'webTorrentButton': {}, | |
91 | ||
92 | 'muteToggle': {}, | |
93 | 'volumeControl': {}, | |
94 | ||
95 | 'settingsButton': { | |
96 | setup: { | |
97 | maxHeightOffset: 40 | |
98 | }, | |
99 | entries: [ | |
100 | 'resolutionMenuButton', | |
101 | 'playbackRateMenuButton' | |
102 | ] | |
103 | } | |
104 | } | |
105 | ||
106 | if (options.peertubeLink === true) { | |
107 | Object.assign(children, { | |
108 | 'peerTubeLinkButton': {} | |
109 | }) | |
110 | } | |
111 | ||
054a103b C |
112 | if (options.theaterMode === true) { |
113 | Object.assign(children, { | |
114 | 'theaterButton': {} | |
115 | }) | |
116 | } | |
117 | ||
c6352f2c C |
118 | Object.assign(children, { |
119 | 'fullscreenToggle': {} | |
120 | }) | |
121 | ||
122 | return children | |
123 | } | |
124 | ||
e945b184 | 125 | function addContextMenu (player: any, videoEmbedUrl: string) { |
e945b184 C |
126 | player.contextmenuUI({ |
127 | content: [ | |
128 | { | |
129 | label: player.localize('Copy the video URL'), | |
130 | listener: function () { | |
131 | copyToClipboard(buildVideoLink()) | |
132 | } | |
133 | }, | |
134 | { | |
135 | label: player.localize('Copy the video URL at the current time'), | |
136 | listener: function () { | |
137 | const player = this | |
138 | copyToClipboard(buildVideoLink(player.currentTime())) | |
139 | } | |
140 | }, | |
141 | { | |
142 | label: player.localize('Copy embed code'), | |
143 | listener: () => { | |
144 | copyToClipboard(buildVideoEmbed(videoEmbedUrl)) | |
145 | } | |
5511da62 RK |
146 | }, |
147 | { | |
148 | label: player.localize('Copy magnet URI'), | |
149 | listener: function () { | |
150 | const player = this | |
151 | copyToClipboard(player.peertube().getCurrentVideoFile().magnetUri) | |
152 | } | |
e945b184 C |
153 | } |
154 | ] | |
155 | }) | |
156 | } | |
157 | ||
158 | function loadLocale (serverUrl: string, videojs: any, locale: string) { | |
74b7c6d4 | 159 | const completeLocale = getCompleteLocale(locale) |
e945b184 | 160 | |
74b7c6d4 C |
161 | if (!is18nLocale(completeLocale) || isDefaultLocale(completeLocale)) return Promise.resolve(undefined) |
162 | ||
163 | return fetch(serverUrl + '/client/locales/' + completeLocale + '/player.json') | |
e945b184 | 164 | .then(res => res.json()) |
9f164722 | 165 | .then(json => videojs.addLanguage(getShortLocale(completeLocale), json)) |
e945b184 C |
166 | } |
167 | ||
168 | export { | |
169 | loadLocale, | |
170 | getVideojsOptions, | |
171 | addContextMenu | |
172 | } |