1 import { Component, ElementRef, Input, ViewChild } from '@angular/core'
2 import { DomSanitizer, SafeHtml } from '@angular/platform-browser'
3 import { 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 { buildPlaylistLink, buildVideoLink, decoratePlaylistLink, decorateVideoLink } from '@shared/core-utils'
8 import { VideoCaption, VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models'
9 import { buildVideoOrPlaylistEmbed } from '../../../assets/player/utils'
11 type Customizations = {
12 startAtCheckbox: boolean
15 stopAtCheckbox: boolean
18 subtitleCheckbox: boolean
33 type TabId = 'url' | 'qrcode' | 'embed'
36 selector: 'my-video-share',
37 templateUrl: './video-share.component.html',
38 styleUrls: [ './video-share.component.scss' ]
40 export class VideoShareComponent {
41 @ViewChild('modal', { static: true }) modal: ElementRef
43 @Input() video: VideoDetails = null
44 @Input() videoCaptions: VideoCaption[] = []
45 @Input() playlist: VideoPlaylist = null
46 @Input() playlistPosition: number = null
48 activeVideoId: TabId = 'url'
49 activePlaylistId: TabId = 'url'
51 customizations: Customizations
52 isAdvancedCustomizationCollapsed = true
53 includeVideoInPlaylist = false
55 playlistEmbedHTML: SafeHtml
56 videoEmbedHTML: SafeHtml
59 private modalService: NgbModal,
60 private sanitizer: DomSanitizer,
61 private server: ServerService
64 show (currentVideoTimestamp?: number, currentPlaylistPosition?: number) {
66 if (this.videoCaptions && this.videoCaptions.length !== 0) {
67 subtitle = this.videoCaptions[0].language.id
70 this.customizations = new Proxy({
71 startAtCheckbox: false,
72 startAt: currentVideoTimestamp ? Math.floor(currentVideoTimestamp) : 0,
74 stopAtCheckbox: false,
75 stopAt: this.video?.duration,
77 subtitleCheckbox: false,
85 embedP2P: this.server.getHTMLConfig().defaults.p2p.embed.enabled,
93 set: (target, prop, value) => {
96 if (prop === 'embedP2P') {
97 // Auto enabled warning title if P2P is enabled
98 this.customizations.warningTitle = value
101 this.updateEmbedCode()
107 this.playlistPosition = currentPlaylistPosition
109 this.updateEmbedCode()
111 this.modalService.open(this.modal, { centered: true })
114 getVideoIframeCode () {
115 const embedUrl = decorateVideoLink({ url: this.video.embedUrl, ...this.getVideoOptions(true) })
117 return buildVideoOrPlaylistEmbed(embedUrl, this.video.name)
120 getPlaylistIframeCode () {
121 const embedUrl = decoratePlaylistLink({ url: this.playlist.embedUrl, ...this.getPlaylistOptions() })
123 return buildVideoOrPlaylistEmbed(embedUrl, this.playlist.displayName)
127 const url = this.customizations.originUrl
129 : buildVideoLink(this.video, window.location.origin)
131 return decorateVideoLink({
134 ...this.getVideoOptions(false)
139 const url = buildPlaylistLink(this.playlist)
140 if (!this.includeVideoInPlaylist) return url
142 return decoratePlaylistLink({ url, playlistPosition: this.playlistPosition })
146 if (this.playlist) this.playlistEmbedHTML = this.sanitizer.bypassSecurityTrustHtml(this.getPlaylistIframeCode())
148 if (this.video) this.videoEmbedHTML = this.sanitizer.bypassSecurityTrustHtml(this.getVideoIframeCode())
152 return window.location.protocol === 'http:'
155 isVideoInEmbedTab () {
156 return this.activeVideoId === 'embed'
160 return this.video.isLocal
164 return this.video.privacy.id === VideoPrivacy.PRIVATE
167 isPrivatePlaylist () {
168 return this.playlist.privacy.id === VideoPlaylistPrivacy.PRIVATE
171 private getPlaylistOptions (baseUrl?: string) {
175 playlistPosition: this.playlistPosition || undefined
179 private getVideoOptions (forEmbed: boolean) {
180 const embedOptions = forEmbed
182 title: this.customizations.title,
183 warningTitle: this.customizations.warningTitle,
184 controls: this.customizations.controls,
185 peertubeLink: this.customizations.peertubeLink,
187 // If using default value, we don't need to specify it
188 p2p: this.customizations.embedP2P === this.server.getHTMLConfig().defaults.p2p.embed.enabled
190 : this.customizations.embedP2P
195 startTime: this.customizations.startAtCheckbox ? this.customizations.startAt : undefined,
196 stopTime: this.customizations.stopAtCheckbox ? this.customizations.stopAt : undefined,
198 subtitle: this.customizations.subtitleCheckbox ? this.customizations.subtitle : undefined,
200 loop: this.customizations.loop,
201 autoplay: this.customizations.autoplay,
202 muted: this.customizations.muted,