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