1 import { Component, ElementRef, Input, ViewChild } from '@angular/core'
2 import { DomSanitizer, SafeHtml } from '@angular/platform-browser'
3 import { HooksService, ServerService } from '@app/core'
4 import { VideoDetails } from '@app/shared/shared-main'
5 import { VideoPlaylist } from '@app/shared/shared-video-playlist'
6 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
7 import { buildVideoOrPlaylistEmbed } from '@root-helpers/video'
8 import { buildPlaylistLink, buildVideoLink, decoratePlaylistLink, decorateVideoLink } from '@shared/core-utils'
9 import { VideoCaption, VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models'
11 type Customizations = {
12 startAtCheckbox: boolean
15 stopAtCheckbox: boolean
18 subtitleCheckbox: boolean
34 includeVideoInPlaylist: boolean
37 type TabId = 'url' | 'qrcode' | 'embed'
40 selector: 'my-video-share',
41 templateUrl: './video-share.component.html',
42 styleUrls: [ './video-share.component.scss' ]
44 export class VideoShareComponent {
45 @ViewChild('modal', { static: true }) modal: ElementRef
47 @Input() video: VideoDetails = null
48 @Input() videoCaptions: VideoCaption[] = []
49 @Input() playlist: VideoPlaylist = null
50 @Input() playlistPosition: number = null
52 activeVideoId: TabId = 'url'
53 activePlaylistId: TabId = 'url'
55 customizations: Customizations
56 isAdvancedCustomizationCollapsed = true
62 playlistEmbedUrl: string
64 videoEmbedHTML: string
65 videoEmbedSafeHTML: SafeHtml
66 playlistEmbedHTML: string
67 playlistEmbedSafeHTML: SafeHtml
70 private modalService: NgbModal,
71 private sanitizer: DomSanitizer,
72 private server: ServerService,
73 private hooks: HooksService
76 show (currentVideoTimestamp?: number, currentPlaylistPosition?: number) {
78 if (this.videoCaptions && this.videoCaptions.length !== 0) {
79 subtitle = this.videoCaptions[0].language.id
82 this.customizations = new Proxy({
83 startAtCheckbox: false,
84 startAt: currentVideoTimestamp ? Math.floor(currentVideoTimestamp) : 0,
86 stopAtCheckbox: false,
87 stopAt: this.video?.duration,
89 subtitleCheckbox: false,
98 embedP2P: this.server.getHTMLConfig().defaults.p2p.embed.enabled,
106 includeVideoInPlaylist: false
108 set: (target, prop, value) => {
111 if (prop === 'embedP2P') {
112 // Auto enabled warning title if P2P is enabled
113 this.customizations.warningTitle = value
122 this.playlistPosition = currentPlaylistPosition
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 })
131 // ---------------------------------------------------------------------------
134 const url = this.customizations.originUrl
136 : buildVideoLink(this.video, window.location.origin)
138 return this.hooks.wrapFun(
140 { url, ...this.getVideoOptions(false) },
142 'filter:share.video-url.build.params',
143 'filter:share.video-url.build.result'
147 getVideoEmbedUrl () {
148 return this.hooks.wrapFun(
150 { url: this.video.embedUrl, ...this.getVideoOptions(true) },
152 'filter:share.video-embed-url.build.params',
153 'filter:share.video-embed-url.build.result'
157 async getVideoEmbedCode (options: { responsive: boolean }) {
158 const { responsive } = options
159 return this.hooks.wrapFun(
160 buildVideoOrPlaylistEmbed,
161 { embedUrl: await this.getVideoEmbedUrl(), embedTitle: this.video.name, responsive },
163 'filter:share.video-embed-code.build.params',
164 'filter:share.video-embed-code.build.result'
168 // ---------------------------------------------------------------------------
171 const url = buildPlaylistLink(this.playlist)
173 return this.hooks.wrapFun(
174 decoratePlaylistLink,
175 { url, ...this.getPlaylistOptions() },
177 'filter:share.video-playlist-url.build.params',
178 'filter:share.video-playlist-url.build.result'
182 getPlaylistEmbedUrl () {
183 return this.hooks.wrapFun(
184 decoratePlaylistLink,
185 { url: this.playlist.embedUrl, ...this.getPlaylistOptions() },
187 'filter:share.video-playlist-embed-url.build.params',
188 'filter:share.video-playlist-embed-url.build.result'
192 async getPlaylistEmbedCode (options: { responsive: boolean }) {
193 const { responsive } = options
194 return this.hooks.wrapFun(
195 buildVideoOrPlaylistEmbed,
196 { embedUrl: await this.getPlaylistEmbedUrl(), embedTitle: this.playlist.displayName, responsive },
198 'filter:share.video-playlist-embed-code.build.params',
199 'filter:share.video-playlist-embed-code.build.result'
203 // ---------------------------------------------------------------------------
207 this.playlistUrl = await this.getPlaylistUrl()
208 this.playlistEmbedUrl = await this.getPlaylistEmbedUrl()
209 this.playlistEmbedHTML = await this.getPlaylistEmbedCode({ responsive: this.customizations.responsive })
210 this.playlistEmbedSafeHTML = this.sanitizer.bypassSecurityTrustHtml(await this.getPlaylistEmbedCode({ responsive: false }))
214 this.videoUrl = await this.getVideoUrl()
215 this.videoEmbedUrl = await this.getVideoEmbedUrl()
216 this.videoEmbedHTML = await this.getVideoEmbedCode({ responsive: this.customizations.responsive })
217 this.videoEmbedSafeHTML = this.sanitizer.bypassSecurityTrustHtml(await this.getVideoEmbedCode({ responsive: false }))
222 return window.location.protocol === 'http:'
225 isInVideoEmbedTab () {
226 return this.activeVideoId === 'embed'
229 isInPlaylistEmbedTab () {
230 return this.activePlaylistId === 'embed'
234 return this.video.isLocal
238 return this.video.privacy.id === VideoPrivacy.PRIVATE
241 isPrivatePlaylist () {
242 return this.playlist.privacy.id === VideoPlaylistPrivacy.PRIVATE
245 private getPlaylistOptions (baseUrl?: string) {
249 playlistPosition: this.playlistPosition && this.customizations.includeVideoInPlaylist
250 ? this.playlistPosition
255 private getVideoOptions (forEmbed: boolean) {
256 const embedOptions = forEmbed
258 title: this.customizations.title,
259 warningTitle: this.customizations.warningTitle,
260 controlBar: this.customizations.controlBar,
261 peertubeLink: this.customizations.peertubeLink,
263 // If using default value, we don't need to specify it
264 p2p: this.customizations.embedP2P === this.server.getHTMLConfig().defaults.p2p.embed.enabled
266 : this.customizations.embedP2P
271 startTime: this.customizations.startAtCheckbox ? this.customizations.startAt : undefined,
272 stopTime: this.customizations.stopAtCheckbox ? this.customizations.stopAt : undefined,
274 subtitle: this.customizations.subtitleCheckbox ? this.customizations.subtitle : undefined,
276 loop: this.customizations.loop,
277 autoplay: this.customizations.autoplay,
278 muted: this.customizations.muted,