From 15e9d5ca39e0b792f61453fbf3885a0fc446afa7 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 12 Mar 2019 11:40:42 +0100 Subject: Playlist reorder support --- ...my-account-video-playlist-elements.component.ts | 69 +++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) (limited to 'client/src/app/+my-account/my-account-video-playlists/my-account-video-playlist-elements.component.ts') diff --git a/client/src/app/+my-account/my-account-video-playlists/my-account-video-playlist-elements.component.ts b/client/src/app/+my-account/my-account-video-playlists/my-account-video-playlist-elements.component.ts index 76aff3d4f..4076a3721 100644 --- a/client/src/app/+my-account/my-account-video-playlists/my-account-video-playlist-elements.component.ts +++ b/client/src/app/+my-account/my-account-video-playlists/my-account-video-playlist-elements.component.ts @@ -4,7 +4,7 @@ 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 { Subscription } from 'rxjs' +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' @@ -13,6 +13,8 @@ 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', @@ -42,6 +44,7 @@ export class MyAccountVideoPlaylistElementsComponent implements OnInit, OnDestro private videoPlaylistId: string | number private paramsSub: Subscription + private dragMoveSubject = new Subject() constructor ( private authService: AuthService, @@ -61,12 +64,66 @@ export class MyAccountVideoPlaylistElementsComponent implements OnInit, OnDestro this.loadPlaylistInfo() }) + + this.dragMoveSubject.asObservable() + .pipe(throttleTime(200)) + .subscribe(y => this.checkScroll(y)) } ngOnDestroy () { if (this.paramsSub) this.paramsSub.unsubscribe() } + drop (event: CdkDragDrop) { + 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) { + 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()) } @@ -78,6 +135,7 @@ export class MyAccountVideoPlaylistElementsComponent implements OnInit, OnDestro 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) @@ -173,4 +231,13 @@ export class MyAccountVideoPlaylistElementsComponent implements OnInit, OnDestro this.playlist = playlist }) } + + private reorderClientPositions () { + let i = 1 + + for (const video of this.videos) { + video.playlistElement.position = i + i++ + } + } } -- cgit v1.2.3