1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
import { Notifier, ServerService } from '@app/core'
import { AuthService } from '../../core/auth'
import { ConfirmService } from '../../core/confirm'
import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
import { Video } from '@app/shared/video/video.model'
import { Subject, Subscription } from 'rxjs'
import { ActivatedRoute } from '@angular/router'
import { VideoService } from '@app/shared/video/video.service'
import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service'
import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model'
import { I18n } from '@ngx-translate/i18n-polyfill'
import { secondsToTime } from '../../../assets/player/utils'
import { VideoPlaylistElementUpdate } from '@shared/models'
import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'
import { CdkDragDrop, CdkDragMove } from '@angular/cdk/drag-drop'
import { throttleTime } from 'rxjs/operators'
@Component({
selector: 'my-account-video-playlist-elements',
templateUrl: './my-account-video-playlist-elements.component.html',
styleUrls: [ './my-account-video-playlist-elements.component.scss' ]
})
export class MyAccountVideoPlaylistElementsComponent implements OnInit, OnDestroy {
@ViewChild('moreDropdown') moreDropdown: NgbDropdown
videos: Video[] = []
playlist: VideoPlaylist
pagination: ComponentPagination = {
currentPage: 1,
itemsPerPage: 10,
totalItems: null
}
displayTimestampOptions = false
timestampOptions: {
startTimestampEnabled: boolean
startTimestamp: number
stopTimestampEnabled: boolean
stopTimestamp: number
} = {} as any
private videoPlaylistId: string | number
private paramsSub: Subscription
private dragMoveSubject = new Subject<number>()
constructor (
private authService: AuthService,
private serverService: ServerService,
private notifier: Notifier,
private confirmService: ConfirmService,
private route: ActivatedRoute,
private i18n: I18n,
private videoService: VideoService,
private videoPlaylistService: VideoPlaylistService
) {}
ngOnInit () {
this.paramsSub = this.route.params.subscribe(routeParams => {
this.videoPlaylistId = routeParams[ 'videoPlaylistId' ]
this.loadElements()
this.loadPlaylistInfo()
})
this.dragMoveSubject.asObservable()
.pipe(throttleTime(200))
.subscribe(y => this.checkScroll(y))
}
ngOnDestroy () {
if (this.paramsSub) this.paramsSub.unsubscribe()
}
drop (event: CdkDragDrop<any>) {
const previousIndex = event.previousIndex
const newIndex = event.currentIndex
if (previousIndex === newIndex) return
const oldPosition = this.videos[previousIndex].playlistElement.position
const insertAfter = newIndex === 0 ? 0 : this.videos[newIndex].playlistElement.position
this.videoPlaylistService.reorderPlaylist(this.playlist.id, oldPosition, insertAfter)
.subscribe(
() => { /* nothing to do */ },
err => this.notifier.error(err.message)
)
const video = this.videos[previousIndex]
this.videos.splice(previousIndex, 1)
this.videos.splice(newIndex, 0, video)
this.reorderClientPositions()
}
onDragMove (event: CdkDragMove<any>) {
this.dragMoveSubject.next(event.pointerPosition.y)
}
checkScroll (pointerY: number) {
// FIXME: Uncomment when https://github.com/angular/material2/issues/14098 is fixed
// FIXME: Remove when https://github.com/angular/material2/issues/13588 is implemented
// if (pointerY < 150) {
// window.scrollBy({
// left: 0,
// top: -20,
// behavior: 'smooth'
// })
//
// return
// }
//
// if (window.innerHeight - pointerY <= 50) {
// window.scrollBy({
// left: 0,
// top: 20,
// behavior: 'smooth'
// })
// }
}
isVideoBlur (video: Video) {
return video.isVideoNSFWForUser(this.authService.getUser(), this.serverService.getConfig())
}
removeFromPlaylist (video: Video) {
this.videoPlaylistService.removeVideoFromPlaylist(this.playlist.id, video.id)
.subscribe(
() => {
this.notifier.success(this.i18n('Video removed from {{name}}', { name: this.playlist.displayName }))
this.videos = this.videos.filter(v => v.id !== video.id)
this.reorderClientPositions()
},
err => this.notifier.error(err.message)
)
this.moreDropdown.close()
}
updateTimestamps (video: Video) {
const body: VideoPlaylistElementUpdate = {}
body.startTimestamp = this.timestampOptions.startTimestampEnabled ? this.timestampOptions.startTimestamp : null
body.stopTimestamp = this.timestampOptions.stopTimestampEnabled ? this.timestampOptions.stopTimestamp : null
this.videoPlaylistService.updateVideoOfPlaylist(this.playlist.id, video.id, body)
.subscribe(
() => {
this.notifier.success(this.i18n('Timestamps updated'))
video.playlistElement.startTimestamp = body.startTimestamp
video.playlistElement.stopTimestamp = body.stopTimestamp
},
err => this.notifier.error(err.message)
)
this.moreDropdown.close()
}
onNearOfBottom () {
// Last page
if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
this.pagination.currentPage += 1
this.loadElements()
}
formatTimestamp (video: Video) {
const start = video.playlistElement.startTimestamp
const stop = video.playlistElement.stopTimestamp
const startFormatted = secondsToTime(start, true, ':')
const stopFormatted = secondsToTime(stop, true, ':')
if (start === null && stop === null) return ''
if (start !== null && stop === null) return this.i18n('Starts at ') + startFormatted
if (start === null && stop !== null) return this.i18n('Stops at ') + stopFormatted
return this.i18n('Starts at ') + startFormatted + this.i18n(' and stops at ') + stopFormatted
}
onDropdownOpenChange () {
this.displayTimestampOptions = false
}
toggleDisplayTimestampsOptions (event: Event, video: Video) {
event.preventDefault()
this.displayTimestampOptions = !this.displayTimestampOptions
if (this.displayTimestampOptions === true) {
this.timestampOptions = {
startTimestampEnabled: false,
stopTimestampEnabled: false,
startTimestamp: 0,
stopTimestamp: video.duration
}
if (video.playlistElement.startTimestamp) {
this.timestampOptions.startTimestampEnabled = true
this.timestampOptions.startTimestamp = video.playlistElement.startTimestamp
}
if (video.playlistElement.stopTimestamp) {
this.timestampOptions.stopTimestampEnabled = true
this.timestampOptions.stopTimestamp = video.playlistElement.stopTimestamp
}
}
}
private loadElements () {
this.videoService.getPlaylistVideos(this.videoPlaylistId, this.pagination)
.subscribe(({ totalVideos, videos }) => {
this.videos = this.videos.concat(videos)
this.pagination.totalItems = totalVideos
})
}
private loadPlaylistInfo () {
this.videoPlaylistService.getVideoPlaylist(this.videoPlaylistId)
.subscribe(playlist => {
this.playlist = playlist
})
}
private reorderClientPositions () {
let i = 1
for (const video of this.videos) {
video.playlistElement.position = i
i++
}
}
}
|