Switching to a named filters/single input on video-abuse
[oweals/peertube.git] / client / src / app / +my-account / my-account-video-playlists / my-account-video-playlist-elements.component.ts
index 76aff3d4fc0c3aab8d4f9e772924dfe1daeb4323..366640618d54e3c198e4da9f6ec4e3cad0b9b993 100644 (file)
@@ -1,18 +1,16 @@
-import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
+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 { 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'
 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 { ScreenService } from '@app/shared/misc/screen.service'
+import { CdkDragDrop } from '@angular/cdk/drag-drop'
+import { VideoPlaylistElement } from '@app/shared/video-playlist/video-playlist-element.model'
 
 @Component({
   selector: 'my-account-video-playlist-elements',
@@ -20,9 +18,7 @@ import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'
   styleUrls: [ './my-account-video-playlist-elements.component.scss' ]
 })
 export class MyAccountVideoPlaylistElementsComponent implements OnInit, OnDestroy {
-  @ViewChild('moreDropdown') moreDropdown: NgbDropdown
-
-  videos: Video[] = []
+  playlistElements: VideoPlaylistElement[] = []
   playlist: VideoPlaylist
 
   pagination: ComponentPagination = {
@@ -31,14 +27,7 @@ export class MyAccountVideoPlaylistElementsComponent implements OnInit, OnDestro
     totalItems: null
   }
 
-  displayTimestampOptions = false
-
-  timestampOptions: {
-    startTimestampEnabled: boolean
-    startTimestamp: number
-    stopTimestampEnabled: boolean
-    stopTimestamp: number
-  } = {} as any
+  onDataSubject = new Subject<any[]>()
 
   private videoPlaylistId: string | number
   private paramsSub: Subscription
@@ -50,7 +39,7 @@ export class MyAccountVideoPlaylistElementsComponent implements OnInit, OnDestro
     private confirmService: ConfirmService,
     private route: ActivatedRoute,
     private i18n: I18n,
-    private videoService: VideoService,
+    private screenService: ScreenService,
     private videoPlaylistService: VideoPlaylistService
   ) {}
 
@@ -67,44 +56,37 @@ export class MyAccountVideoPlaylistElementsComponent implements OnInit, OnDestro
     if (this.paramsSub) this.paramsSub.unsubscribe()
   }
 
-  isVideoBlur (video: Video) {
-    return video.isVideoNSFWForUser(this.authService.getUser(), this.serverService.getConfig())
-  }
+  drop (event: CdkDragDrop<any>) {
+    const previousIndex = event.previousIndex
+    const newIndex = event.currentIndex
 
-  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 }))
+    if (previousIndex === newIndex) return
 
-            this.videos = this.videos.filter(v => v.id !== video.id)
-          },
+    const oldPosition = this.playlistElements[previousIndex].position
+    let insertAfter = this.playlistElements[newIndex].position
 
-          err => this.notifier.error(err.message)
-        )
+    if (oldPosition > insertAfter) insertAfter--
 
-    this.moreDropdown.close()
-  }
+    const element = this.playlistElements[previousIndex]
 
-  updateTimestamps (video: Video) {
-    const body: VideoPlaylistElementUpdate = {}
+    this.playlistElements.splice(previousIndex, 1)
+    this.playlistElements.splice(newIndex, 0, element)
 
-    body.startTimestamp = this.timestampOptions.startTimestampEnabled ? this.timestampOptions.startTimestamp : null
-    body.stopTimestamp = this.timestampOptions.stopTimestampEnabled ? this.timestampOptions.stopTimestamp : null
+    this.videoPlaylistService.reorderPlaylist(this.playlist.id, oldPosition, insertAfter)
+      .subscribe(
+        () => {
+          this.reorderClientPositions()
+        },
 
-    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)
+      )
+  }
 
-          err => this.notifier.error(err.message)
-        )
+  onElementRemoved (element: VideoPlaylistElement) {
+    const oldFirst = this.findFirst()
 
-    this.moreDropdown.close()
+    this.playlistElements = this.playlistElements.filter(v => v.id !== element.id)
+    this.reorderClientPositions(oldFirst)
   }
 
   onNearOfBottom () {
@@ -115,55 +97,34 @@ export class MyAccountVideoPlaylistElementsComponent implements OnInit, OnDestro
     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
+  trackByFn (index: number, elem: VideoPlaylistElement) {
+    return elem.id
   }
 
-  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
-      }
+  /**
+   * 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)
         })
   }
 
@@ -173,4 +134,26 @@ export class MyAccountVideoPlaylistElementsComponent implements OnInit, OnDestro
         this.playlist = playlist
       })
   }
+
+  private reorderClientPositions (first?: VideoPlaylistElement) {
+    if (this.playlistElements.length === 0) return
+
+    const oldFirst = first || this.findFirst()
+    let i = 1
+
+    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)
+  }
 }