]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video-playlist/video-playlist-element-miniature.component.ts
Add search bars for a user's videos and playlist library
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video-playlist / video-playlist-element-miniature.component.ts
CommitLineData
ba430d75 1import { ChangeDetectionStrategy, ChangeDetectorRef, Component, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
e2f01c47 2import { Video } from '@app/shared/video/video.model'
ba430d75 3import { ServerConfig, VideoPlaylistElementType, VideoPlaylistElementUpdate } from '@shared/models'
e2f01c47
C
4import { AuthService, ConfirmService, Notifier, ServerService } from '@app/core'
5import { ActivatedRoute } from '@angular/router'
6import { I18n } from '@ngx-translate/i18n-polyfill'
7import { VideoService } from '@app/shared/video/video.service'
8import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service'
9import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'
10import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model'
11import { secondsToTime } from '../../../assets/player/utils'
bfbd9128 12import { VideoPlaylistElement } from '@app/shared/video-playlist/video-playlist-element.model'
e2f01c47
C
13
14@Component({
15 selector: 'my-video-playlist-element-miniature',
16 styleUrls: [ './video-playlist-element-miniature.component.scss' ],
bce47964
C
17 templateUrl: './video-playlist-element-miniature.component.html',
18 changeDetection: ChangeDetectionStrategy.OnPush
e2f01c47 19})
ba430d75 20export class VideoPlaylistElementMiniatureComponent implements OnInit {
f36da21e 21 @ViewChild('moreDropdown', { static: false }) moreDropdown: NgbDropdown
e2f01c47
C
22
23 @Input() playlist: VideoPlaylist
bfbd9128 24 @Input() playlistElement: VideoPlaylistElement
e2f01c47
C
25 @Input() owned = false
26 @Input() playing = false
27 @Input() rowLink = false
28 @Input() accountLink = true
bfbd9128 29 @Input() position: number // Keep this property because we're in the OnPush change detection strategy
e2f01c47 30
bfbd9128 31 @Output() elementRemoved = new EventEmitter<VideoPlaylistElement>()
e2f01c47
C
32
33 displayTimestampOptions = false
34
35 timestampOptions: {
36 startTimestampEnabled: boolean
37 startTimestamp: number
38 stopTimestampEnabled: boolean
39 stopTimestamp: number
40 } = {} as any
41
ba430d75
C
42 private serverConfig: ServerConfig
43
e2f01c47
C
44 constructor (
45 private authService: AuthService,
46 private serverService: ServerService,
47 private notifier: Notifier,
48 private confirmService: ConfirmService,
49 private route: ActivatedRoute,
50 private i18n: I18n,
51 private videoService: VideoService,
bce47964
C
52 private videoPlaylistService: VideoPlaylistService,
53 private cdr: ChangeDetectorRef
e2f01c47
C
54 ) {}
55
ba430d75
C
56 ngOnInit (): void {
57 this.serverConfig = this.serverService.getTmpConfig()
58 this.serverService.getConfig()
59 .subscribe(config => {
60 this.serverConfig = config
61 this.cdr.detectChanges()
62 })
63 }
64
bfbd9128
C
65 isUnavailable (e: VideoPlaylistElement) {
66 return e.type === VideoPlaylistElementType.UNAVAILABLE
67 }
68
69 isPrivate (e: VideoPlaylistElement) {
70 return e.type === VideoPlaylistElementType.PRIVATE
71 }
72
73 isDeleted (e: VideoPlaylistElement) {
74 return e.type === VideoPlaylistElementType.DELETED
75 }
76
e2f01c47
C
77 buildRouterLink () {
78 if (!this.playlist) return null
79
80 return [ '/videos/watch/playlist', this.playlist.uuid ]
81 }
82
83 buildRouterQuery () {
bfbd9128 84 if (!this.playlistElement || !this.playlistElement.video) return {}
e2f01c47
C
85
86 return {
bfbd9128
C
87 videoId: this.playlistElement.video.uuid,
88 start: this.playlistElement.startTimestamp,
96f6278f
RK
89 stop: this.playlistElement.stopTimestamp,
90 resume: true
e2f01c47
C
91 }
92 }
93
94 isVideoBlur (video: Video) {
ba430d75 95 return video.isVideoNSFWForUser(this.authService.getUser(), this.serverConfig)
e2f01c47
C
96 }
97
bfbd9128
C
98 removeFromPlaylist (playlistElement: VideoPlaylistElement) {
99 this.videoPlaylistService.removeVideoFromPlaylist(this.playlist.id, playlistElement.id)
e2f01c47
C
100 .subscribe(
101 () => {
102 this.notifier.success(this.i18n('Video removed from {{name}}', { name: this.playlist.displayName }))
103
bfbd9128 104 this.elementRemoved.emit(playlistElement)
e2f01c47
C
105 },
106
107 err => this.notifier.error(err.message)
108 )
109
110 this.moreDropdown.close()
111 }
112
bfbd9128 113 updateTimestamps (playlistElement: VideoPlaylistElement) {
e2f01c47
C
114 const body: VideoPlaylistElementUpdate = {}
115
116 body.startTimestamp = this.timestampOptions.startTimestampEnabled ? this.timestampOptions.startTimestamp : null
117 body.stopTimestamp = this.timestampOptions.stopTimestampEnabled ? this.timestampOptions.stopTimestamp : null
118
bfbd9128 119 this.videoPlaylistService.updateVideoOfPlaylist(this.playlist.id, playlistElement.id, body)
e2f01c47
C
120 .subscribe(
121 () => {
122 this.notifier.success(this.i18n('Timestamps updated'))
123
bfbd9128
C
124 playlistElement.startTimestamp = body.startTimestamp
125 playlistElement.stopTimestamp = body.stopTimestamp
bce47964
C
126
127 this.cdr.detectChanges()
e2f01c47
C
128 },
129
130 err => this.notifier.error(err.message)
131 )
132
133 this.moreDropdown.close()
134 }
135
bfbd9128
C
136 formatTimestamp (playlistElement: VideoPlaylistElement) {
137 const start = playlistElement.startTimestamp
138 const stop = playlistElement.stopTimestamp
e2f01c47
C
139
140 const startFormatted = secondsToTime(start, true, ':')
141 const stopFormatted = secondsToTime(stop, true, ':')
142
143 if (start === null && stop === null) return ''
144
145 if (start !== null && stop === null) return this.i18n('Starts at ') + startFormatted
146 if (start === null && stop !== null) return this.i18n('Stops at ') + stopFormatted
147
148 return this.i18n('Starts at ') + startFormatted + this.i18n(' and stops at ') + stopFormatted
149 }
150
151 onDropdownOpenChange () {
152 this.displayTimestampOptions = false
153 }
154
bfbd9128 155 toggleDisplayTimestampsOptions (event: Event, playlistElement: VideoPlaylistElement) {
e2f01c47
C
156 event.preventDefault()
157
158 this.displayTimestampOptions = !this.displayTimestampOptions
159
160 if (this.displayTimestampOptions === true) {
161 this.timestampOptions = {
162 startTimestampEnabled: false,
163 stopTimestampEnabled: false,
164 startTimestamp: 0,
bfbd9128 165 stopTimestamp: playlistElement.video.duration
e2f01c47
C
166 }
167
bfbd9128 168 if (playlistElement.startTimestamp) {
e2f01c47 169 this.timestampOptions.startTimestampEnabled = true
bfbd9128 170 this.timestampOptions.startTimestamp = playlistElement.startTimestamp
e2f01c47
C
171 }
172
bfbd9128 173 if (playlistElement.stopTimestamp) {
e2f01c47 174 this.timestampOptions.stopTimestampEnabled = true
bfbd9128 175 this.timestampOptions.stopTimestamp = playlistElement.stopTimestamp
e2f01c47
C
176 }
177 }
bce47964
C
178
179 // FIXME: why do we have to use setTimeout here?
180 setTimeout(() => {
181 this.cdr.detectChanges()
182 })
e2f01c47
C
183 }
184}