]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/+my-account/my-account-video-playlists/my-account-video-playlist-elements.component.ts
Migrate to $localize
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-account / my-account-video-playlists / my-account-video-playlist-elements.component.ts
index dcf470be3748e3f8d7ccfcca025e41fd14cccfdc..f6cdf10674edcbe36414abe6c74e3efddce18afa 100644 (file)
@@ -1,17 +1,12 @@
-import { Component, OnDestroy, OnInit } 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 { CdkDragDrop, CdkDragMove } from '@angular/cdk/drag-drop'
-import { throttleTime } from 'rxjs/operators'
+import { CdkDragDrop } from '@angular/cdk/drag-drop'
+import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
+import { ActivatedRoute, Router } from '@angular/router'
+import { ComponentPagination, ConfirmService, Notifier, ScreenService } from '@app/core'
+import { DropdownAction } from '@app/shared/shared-main'
+import { VideoShareComponent } from '@app/shared/shared-share-modal'
+import { VideoPlaylist, VideoPlaylistElement, VideoPlaylistService } from '@app/shared/shared-video-playlist'
+import { VideoPlaylistType } from '@shared/models'
 
 @Component({
   selector: 'my-account-video-playlist-elements',
@@ -19,41 +14,55 @@ import { throttleTime } from 'rxjs/operators'
   styleUrls: [ './my-account-video-playlist-elements.component.scss' ]
 })
 export class MyAccountVideoPlaylistElementsComponent implements OnInit, OnDestroy {
-  videos: Video[] = []
+  @ViewChild('videoShareModal') videoShareModal: VideoShareComponent
+
+  playlistElements: VideoPlaylistElement[] = []
   playlist: VideoPlaylist
 
+  playlistActions: DropdownAction<VideoPlaylist>[][] = []
+
   pagination: ComponentPagination = {
     currentPage: 1,
     itemsPerPage: 10,
     totalItems: null
   }
 
+  onDataSubject = new Subject<any[]>()
+
   private videoPlaylistId: string | number
   private paramsSub: Subscription
-  private dragMoveSubject = new Subject<number>()
 
   constructor (
-    private authService: AuthService,
-    private serverService: ServerService,
     private notifier: Notifier,
+    private router: Router,
     private confirmService: ConfirmService,
     private route: ActivatedRoute,
-    private i18n: I18n,
-    private videoService: VideoService,
+    private screenService: ScreenService,
     private videoPlaylistService: VideoPlaylistService
   ) {}
 
   ngOnInit () {
+    this.playlistActions = [
+      [
+        {
+          label: $localize`Update playlist`,
+          iconName: 'edit',
+          linkBuilder: playlist => [ '/my-account', 'video-playlists', 'update', playlist.uuid ]
+        },
+        {
+          label: $localize`Delete playlist`,
+          iconName: 'delete',
+          handler: playlist => this.deleteVideoPlaylist(playlist)
+        }
+      ]
+    ]
+
     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 () {
@@ -66,68 +75,95 @@ export class MyAccountVideoPlaylistElementsComponent implements OnInit, OnDestro
 
     if (previousIndex === newIndex) return
 
-    const oldPosition = this.videos[previousIndex].playlistElement.position
-    const insertAfter = newIndex === 0 ? 0 : this.videos[newIndex].playlistElement.position
+    const oldPosition = this.playlistElements[previousIndex].position
+    let insertAfter = this.playlistElements[newIndex].position
+
+    if (oldPosition > insertAfter) insertAfter--
+
+    const element = this.playlistElements[previousIndex]
+
+    this.playlistElements.splice(previousIndex, 1)
+    this.playlistElements.splice(newIndex, 0, element)
 
     this.videoPlaylistService.reorderPlaylist(this.playlist.id, oldPosition, insertAfter)
       .subscribe(
-        () => { /* nothing to do */ },
+        () => {
+          this.reorderClientPositions()
+        },
 
         err => this.notifier.error(err.message)
       )
+  }
 
-    const video = this.videos[previousIndex]
+  onElementRemoved (element: VideoPlaylistElement) {
+    const oldFirst = this.findFirst()
 
-    this.videos.splice(previousIndex, 1)
-    this.videos.splice(newIndex, 0, video)
+    this.playlistElements = this.playlistElements.filter(v => v.id !== element.id)
+    this.reorderClientPositions(oldFirst)
+  }
+
+  onNearOfBottom () {
+    // Last page
+    if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
 
-    this.reorderClientPositions()
+    this.pagination.currentPage += 1
+    this.loadElements()
   }
 
-  onDragMove (event: CdkDragMove<any>) {
-    this.dragMoveSubject.next(event.pointerPosition.y)
+  trackByFn (index: number, elem: VideoPlaylistElement) {
+    return elem.id
   }
 
-  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'
-    //   })
-    // }
+  isRegularPlaylist (playlist: VideoPlaylist) {
+    return playlist?.type.id === VideoPlaylistType.REGULAR
   }
 
-  onElementRemoved (video: Video) {
-    this.videos = this.videos.filter(v => v.id !== video.id)
-    this.reorderClientPositions()
+  showShareModal () {
+    this.videoShareModal.show()
   }
 
-  onNearOfBottom () {
-    // Last page
-    if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
+  async deleteVideoPlaylist (videoPlaylist: VideoPlaylist) {
+    const res = await this.confirmService.confirm(
+      $localize`Do you really want to delete ${videoPlaylist.displayName}?`,
+      $localize`Delete`
+    )
+    if (res === false) return
 
-    this.pagination.currentPage += 1
-    this.loadElements()
+    this.videoPlaylistService.removeVideoPlaylist(videoPlaylist)
+      .subscribe(
+        () => {
+          this.router.navigate([ '/my-account', 'video-playlists' ])
+          this.notifier.success($localize`Playlist ${videoPlaylist.displayName} deleted.`)
+        },
+
+        error => this.notifier.error(error.message)
+      )
+  }
+
+  /**
+   * Returns null to not have drag and drop delay.
+   * In small views, where elements are about 100% wide,
+   * we add a delay to prevent unwanted drag&drop.
+   *
+   * @see {@link https://github.com/Chocobozzz/PeerTube/issues/2078}
+   *
+   * @returns {null|number} Null for no delay, or a number in milliseconds.
+   */
+  getDragStartDelay (): null | number {
+    if (this.screenService.isInTouchScreen()) {
+      return 500
+    }
+
+    return null
   }
 
   private loadElements () {
-    this.videoService.getPlaylistVideos(this.videoPlaylistId, this.pagination)
-        .subscribe(({ totalVideos, videos }) => {
-          this.videos = this.videos.concat(videos)
-          this.pagination.totalItems = totalVideos
+    this.videoPlaylistService.getPlaylistVideos(this.videoPlaylistId, this.pagination)
+        .subscribe(({ total, data }) => {
+          this.playlistElements = this.playlistElements.concat(data)
+          this.pagination.totalItems = total
+
+          this.onDataSubject.next(data)
         })
   }
 
@@ -138,12 +174,25 @@ export class MyAccountVideoPlaylistElementsComponent implements OnInit, OnDestro
       })
   }
 
-  private reorderClientPositions () {
+  private reorderClientPositions (first?: VideoPlaylistElement) {
+    if (this.playlistElements.length === 0) return
+
+    const oldFirst = first || this.findFirst()
     let i = 1
 
-    for (const video of this.videos) {
-      video.playlistElement.position = i
+    for (const element of this.playlistElements) {
+      element.position = i
       i++
     }
+
+    // Reload playlist thumbnail if the first element changed
+    const newFirst = this.findFirst()
+    if (oldFirst && newFirst && oldFirst.id !== newFirst.id) {
+      this.playlist.refreshThumbnail()
+    }
+  }
+
+  private findFirst () {
+    return this.playlistElements.find(e => e.position === 1)
   }
 }