]>
Commit | Line | Data |
---|---|---|
63347a0f | 1 | import { Component, ElementRef, Input, ViewChild } from '@angular/core' |
f1c86172 | 2 | import { DomSanitizer, SafeHtml } from '@angular/platform-browser' |
de615445 | 3 | import { HooksService, ServerService } from '@app/core' |
9162fdd3 | 4 | import { VideoDetails } from '@app/shared/shared-main' |
67ed6552 | 5 | import { VideoPlaylist } from '@app/shared/shared-video-playlist' |
82f443de | 6 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' |
57d65032 | 7 | import { buildVideoOrPlaylistEmbed } from '@root-helpers/video' |
15a7eafb | 8 | import { buildPlaylistLink, buildVideoLink, decoratePlaylistLink, decorateVideoLink } from '@shared/core-utils' |
38225867 | 9 | import { VideoCaption, VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models' |
2f4c784a C |
10 | |
11 | type Customizations = { | |
12 | startAtCheckbox: boolean | |
13 | startAt: number | |
14 | ||
15 | stopAtCheckbox: boolean | |
16 | stopAt: number | |
17 | ||
18 | subtitleCheckbox: boolean | |
19 | subtitle: string | |
20 | ||
21 | loop: boolean | |
dd1e2f2f | 22 | originUrl: boolean |
2f4c784a C |
23 | autoplay: boolean |
24 | muted: boolean | |
85302118 C |
25 | |
26 | embedP2P: boolean | |
a871d2a2 | 27 | onlyEmbedUrl: boolean |
2f4c784a C |
28 | title: boolean |
29 | warningTitle: boolean | |
60f013e1 | 30 | controlBar: boolean |
189ab8de | 31 | peertubeLink: boolean |
cadc1a1b | 32 | responsive: boolean |
de615445 C |
33 | |
34 | includeVideoInPlaylist: boolean | |
2f4c784a | 35 | } |
cf02fbfb | 36 | |
951b582f C |
37 | type TabId = 'url' | 'qrcode' | 'embed' |
38 | ||
cf02fbfb C |
39 | @Component({ |
40 | selector: 'my-video-share', | |
c7e1e432 JL |
41 | templateUrl: './video-share.component.html', |
42 | styleUrls: [ './video-share.component.scss' ] | |
cf02fbfb C |
43 | }) |
44 | export class VideoShareComponent { | |
f36da21e | 45 | @ViewChild('modal', { static: true }) modal: ElementRef |
11b8762f | 46 | |
404b54e1 | 47 | @Input() video: VideoDetails = null |
2f4c784a | 48 | @Input() videoCaptions: VideoCaption[] = [] |
3a1fed11 | 49 | @Input() playlist: VideoPlaylist = null |
d142c7b9 | 50 | @Input() playlistPosition: number = null |
cf02fbfb | 51 | |
951b582f C |
52 | activeVideoId: TabId = 'url' |
53 | activePlaylistId: TabId = 'url' | |
54 | ||
2f4c784a C |
55 | customizations: Customizations |
56 | isAdvancedCustomizationCollapsed = true | |
57 | ||
de615445 C |
58 | videoUrl: string |
59 | playlistUrl: string | |
60 | ||
61 | videoEmbedUrl: string | |
62 | playlistEmbedUrl: string | |
63 | ||
64 | videoEmbedHTML: string | |
65 | videoEmbedSafeHTML: SafeHtml | |
66 | playlistEmbedHTML: string | |
67 | playlistEmbedSafeHTML: SafeHtml | |
f1c86172 C |
68 | |
69 | constructor ( | |
70 | private modalService: NgbModal, | |
85302118 | 71 | private sanitizer: DomSanitizer, |
de615445 C |
72 | private server: ServerService, |
73 | private hooks: HooksService | |
f1c86172 | 74 | ) { } |
cf02fbfb | 75 | |
951b582f | 76 | show (currentVideoTimestamp?: number, currentPlaylistPosition?: number) { |
2f4c784a | 77 | let subtitle: string |
82f443de | 78 | if (this.videoCaptions && this.videoCaptions.length !== 0) { |
2f4c784a C |
79 | subtitle = this.videoCaptions[0].language.id |
80 | } | |
81 | ||
f1c86172 | 82 | this.customizations = new Proxy({ |
2f4c784a C |
83 | startAtCheckbox: false, |
84 | startAt: currentVideoTimestamp ? Math.floor(currentVideoTimestamp) : 0, | |
85 | ||
86 | stopAtCheckbox: false, | |
82f443de | 87 | stopAt: this.video?.duration, |
2f4c784a C |
88 | |
89 | subtitleCheckbox: false, | |
90 | subtitle, | |
91 | ||
92 | loop: false, | |
dd1e2f2f | 93 | originUrl: false, |
2f4c784a C |
94 | autoplay: false, |
95 | muted: false, | |
96 | ||
97 | // Embed options | |
a871d2a2 C |
98 | embedP2P: this.server.getHTMLConfig().defaults.p2p.embed.enabled, |
99 | onlyEmbedUrl: false, | |
2f4c784a C |
100 | title: true, |
101 | warningTitle: true, | |
60f013e1 | 102 | controlBar: true, |
de615445 | 103 | peertubeLink: true, |
cadc1a1b | 104 | responsive: false, |
de615445 C |
105 | |
106 | includeVideoInPlaylist: false | |
f1c86172 C |
107 | }, { |
108 | set: (target, prop, value) => { | |
109 | target[prop] = value | |
110 | ||
85302118 C |
111 | if (prop === 'embedP2P') { |
112 | // Auto enabled warning title if P2P is enabled | |
113 | this.customizations.warningTitle = value | |
114 | } | |
115 | ||
de615445 | 116 | this.onUpdate() |
f1c86172 C |
117 | |
118 | return true | |
119 | } | |
120 | }) | |
11b8762f | 121 | |
951b582f C |
122 | this.playlistPosition = currentPlaylistPosition |
123 | ||
de615445 | 124 | this.onUpdate() |
11b8762f | 125 | |
de615445 C |
126 | this.modalService.open(this.modal, { centered: true }).shown.subscribe(() => { |
127 | this.hooks.runAction('action:modal.share.shown', 'video-watch', { video: this.video, playlist: this.playlist }) | |
128 | }) | |
a871d2a2 | 129 | } |
951b582f | 130 | |
de615445 | 131 | // --------------------------------------------------------------------------- |
cf02fbfb | 132 | |
df98563e | 133 | getVideoUrl () { |
ab4001aa C |
134 | const url = this.customizations.originUrl |
135 | ? this.video.url | |
136 | : buildVideoLink(this.video, window.location.origin) | |
d4a8e7a6 | 137 | |
de615445 C |
138 | return this.hooks.wrapFun( |
139 | decorateVideoLink, | |
140 | { url, ...this.getVideoOptions(false) }, | |
141 | 'video-watch', | |
142 | 'filter:share.video-url.build.params', | |
143 | 'filter:share.video-url.build.result' | |
144 | ) | |
145 | } | |
2f4c784a | 146 | |
de615445 C |
147 | getVideoEmbedUrl () { |
148 | return this.hooks.wrapFun( | |
149 | decorateVideoLink, | |
150 | { url: this.video.embedUrl, ...this.getVideoOptions(true) }, | |
151 | 'video-watch', | |
152 | 'filter:share.video-embed-url.build.params', | |
153 | 'filter:share.video-embed-url.build.result' | |
154 | ) | |
155 | } | |
156 | ||
cadc1a1b W |
157 | async getVideoEmbedCode (options: { responsive: boolean }) { |
158 | const { responsive } = options | |
de615445 C |
159 | return this.hooks.wrapFun( |
160 | buildVideoOrPlaylistEmbed, | |
cadc1a1b | 161 | { embedUrl: await this.getVideoEmbedUrl(), embedTitle: this.video.name, responsive }, |
de615445 C |
162 | 'video-watch', |
163 | 'filter:share.video-embed-code.build.params', | |
164 | 'filter:share.video-embed-code.build.result' | |
165 | ) | |
cf02fbfb | 166 | } |
2c8d4697 | 167 | |
de615445 C |
168 | // --------------------------------------------------------------------------- |
169 | ||
3a1fed11 | 170 | getPlaylistUrl () { |
9162fdd3 | 171 | const url = buildPlaylistLink(this.playlist) |
3a1fed11 | 172 | |
de615445 C |
173 | return this.hooks.wrapFun( |
174 | decoratePlaylistLink, | |
175 | { url, ...this.getPlaylistOptions() }, | |
176 | 'video-watch', | |
177 | 'filter:share.video-playlist-url.build.params', | |
178 | 'filter:share.video-playlist-url.build.result' | |
179 | ) | |
180 | } | |
181 | ||
182 | getPlaylistEmbedUrl () { | |
183 | return this.hooks.wrapFun( | |
184 | decoratePlaylistLink, | |
185 | { url: this.playlist.embedUrl, ...this.getPlaylistOptions() }, | |
186 | 'video-watch', | |
187 | 'filter:share.video-playlist-embed-url.build.params', | |
188 | 'filter:share.video-playlist-embed-url.build.result' | |
189 | ) | |
190 | } | |
191 | ||
cadc1a1b W |
192 | async getPlaylistEmbedCode (options: { responsive: boolean }) { |
193 | const { responsive } = options | |
de615445 C |
194 | return this.hooks.wrapFun( |
195 | buildVideoOrPlaylistEmbed, | |
cadc1a1b | 196 | { embedUrl: await this.getPlaylistEmbedUrl(), embedTitle: this.playlist.displayName, responsive }, |
de615445 C |
197 | 'video-watch', |
198 | 'filter:share.video-playlist-embed-code.build.params', | |
199 | 'filter:share.video-playlist-embed-code.build.result' | |
200 | ) | |
2c8d4697 | 201 | } |
c7e1e432 | 202 | |
de615445 C |
203 | // --------------------------------------------------------------------------- |
204 | ||
205 | async onUpdate () { | |
de615445 C |
206 | if (this.playlist) { |
207 | this.playlistUrl = await this.getPlaylistUrl() | |
208 | this.playlistEmbedUrl = await this.getPlaylistEmbedUrl() | |
cadc1a1b W |
209 | this.playlistEmbedHTML = await this.getPlaylistEmbedCode({ responsive: this.customizations.responsive }) |
210 | this.playlistEmbedSafeHTML = this.sanitizer.bypassSecurityTrustHtml(await this.getPlaylistEmbedCode({ responsive: false })) | |
de615445 C |
211 | } |
212 | ||
213 | if (this.video) { | |
214 | this.videoUrl = await this.getVideoUrl() | |
215 | this.videoEmbedUrl = await this.getVideoEmbedUrl() | |
cadc1a1b W |
216 | this.videoEmbedHTML = await this.getVideoEmbedCode({ responsive: this.customizations.responsive }) |
217 | this.videoEmbedSafeHTML = this.sanitizer.bypassSecurityTrustHtml(await this.getVideoEmbedCode({ responsive: false })) | |
de615445 | 218 | } |
f1c86172 C |
219 | } |
220 | ||
3a1fed11 C |
221 | notSecure () { |
222 | return window.location.protocol === 'http:' | |
c7e1e432 | 223 | } |
11b8762f | 224 | |
a871d2a2 | 225 | isInVideoEmbedTab () { |
951b582f | 226 | return this.activeVideoId === 'embed' |
2f4c784a C |
227 | } |
228 | ||
a871d2a2 C |
229 | isInPlaylistEmbedTab () { |
230 | return this.activePlaylistId === 'embed' | |
231 | } | |
232 | ||
dd1e2f2f J |
233 | isLocalVideo () { |
234 | return this.video.isLocal | |
235 | } | |
236 | ||
38225867 NR |
237 | isPrivateVideo () { |
238 | return this.video.privacy.id === VideoPrivacy.PRIVATE | |
239 | } | |
240 | ||
241 | isPrivatePlaylist () { | |
242 | return this.playlist.privacy.id === VideoPlaylistPrivacy.PRIVATE | |
243 | } | |
244 | ||
951b582f C |
245 | private getPlaylistOptions (baseUrl?: string) { |
246 | return { | |
247 | baseUrl, | |
248 | ||
de615445 C |
249 | playlistPosition: this.playlistPosition && this.customizations.includeVideoInPlaylist |
250 | ? this.playlistPosition | |
251 | : undefined | |
951b582f C |
252 | } |
253 | } | |
254 | ||
85302118 C |
255 | private getVideoOptions (forEmbed: boolean) { |
256 | const embedOptions = forEmbed | |
257 | ? { | |
258 | title: this.customizations.title, | |
259 | warningTitle: this.customizations.warningTitle, | |
60f013e1 | 260 | controlBar: this.customizations.controlBar, |
85302118 C |
261 | peertubeLink: this.customizations.peertubeLink, |
262 | ||
263 | // If using default value, we don't need to specify it | |
264 | p2p: this.customizations.embedP2P === this.server.getHTMLConfig().defaults.p2p.embed.enabled | |
265 | ? undefined | |
266 | : this.customizations.embedP2P | |
267 | } | |
268 | : {} | |
269 | ||
2f4c784a | 270 | return { |
2f4c784a C |
271 | startTime: this.customizations.startAtCheckbox ? this.customizations.startAt : undefined, |
272 | stopTime: this.customizations.stopAtCheckbox ? this.customizations.stopAt : undefined, | |
273 | ||
274 | subtitle: this.customizations.subtitleCheckbox ? this.customizations.subtitle : undefined, | |
275 | ||
276 | loop: this.customizations.loop, | |
277 | autoplay: this.customizations.autoplay, | |
278 | muted: this.customizations.muted, | |
11b8762f | 279 | |
85302118 | 280 | ...embedOptions |
2f4c784a | 281 | } |
11b8762f | 282 | } |
cf02fbfb | 283 | } |