From fd78d2e247b014fd1561cfb5206c9b8b14d6b820 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 26 Apr 2021 10:46:48 +0200 Subject: Refactor next/prev logic --- .../+video-watch/video-watch-playlist.component.ts | 53 ++++++++-------------- 1 file changed, 20 insertions(+), 33 deletions(-) (limited to 'client/src') diff --git a/client/src/app/+videos/+video-watch/video-watch-playlist.component.ts b/client/src/app/+videos/+video-watch/video-watch-playlist.component.ts index cc908b924..a70167da5 100644 --- a/client/src/app/+videos/+video-watch/video-watch-playlist.component.ts +++ b/client/src/app/+videos/+video-watch/video-watch-playlist.component.ts @@ -137,26 +137,8 @@ export class VideoWatchPlaylistComponent { this.onPlaylistVideosNearOfBottom(position) } - findPreviousPlaylistVideo (position = this.currentPlaylistPosition): VideoPlaylistElement { - if (this.currentPlaylistPosition <= 1) { - // we have reached the top of the playlist: either loop or stop - if (this.loopPlaylist) { - this.currentPlaylistPosition = position = this.playlistPagination.totalItems - } else { - return - } - } - const previous = this.playlistElements.find(e => e.position === position) - - if (!previous || !previous.video) { - return this.findPreviousPlaylistVideo(position - 1) - } - - return previous - } - navigateToPreviousPlaylistVideo () { - const previous = this.findPreviousPlaylistVideo(this.currentPlaylistPosition - 1) + const previous = this.findPlaylistVideo(this.currentPlaylistPosition - 1, 'previous') if (!previous) return const start = previous.startTimestamp @@ -164,27 +146,32 @@ export class VideoWatchPlaylistComponent { this.router.navigate([],{ queryParams: { playlistPosition: previous.position, start, stop } }) } - findNextPlaylistVideo (position = this.currentPlaylistPosition): VideoPlaylistElement { - if (this.currentPlaylistPosition >= this.playlistPagination.totalItems) { - // we have reached the end of the playlist: either loop or stop - if (this.loopPlaylist) { - this.currentPlaylistPosition = position = 0 - } else { - return - } + findPlaylistVideo (position: number, type: 'previous' | 'next'): VideoPlaylistElement { + if ( + (type === 'next' && position > this.playlistPagination.totalItems) || + (type === 'previous' && position < 1) + ) { + // End of the playlist: end the recursion if we're not in the loop mode + if (!this.loopPlaylist) return + + // Loop mode + position = type === 'previous' + ? this.playlistPagination.totalItems + : 1 } - const next = this.playlistElements.find(e => e.position === position) + const found = this.playlistElements.find(e => e.position === position) + if (found && found.video) return found - if (!next || !next.video) { - return this.findNextPlaylistVideo(position + 1) - } + const newPosition = type === 'previous' + ? position - 1 + : position + 1 - return next + return this.findPlaylistVideo(newPosition, type) } navigateToNextPlaylistVideo () { - const next = this.findNextPlaylistVideo(this.currentPlaylistPosition + 1) + const next = this.findPlaylistVideo(this.currentPlaylistPosition + 1, 'next') if (!next) return const start = next.startTimestamp -- cgit v1.2.3