aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+my-library/my-video-playlists/my-video-playlist-elements.component.ts
blob: a8fdf6e2959d4d589cc748ed607b0b7a109db8ec (plain) (blame)
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
import { Subject, Subscription } from 'rxjs'
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({
  templateUrl: './my-video-playlist-elements.component.html',
  styleUrls: [ './my-video-playlist-elements.component.scss' ]
})
export class MyVideoPlaylistElementsComponent implements OnInit, OnDestroy {
  @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

  constructor (
    private notifier: Notifier,
    private router: Router,
    private confirmService: ConfirmService,
    private route: ActivatedRoute,
    private screenService: ScreenService,
    private videoPlaylistService: VideoPlaylistService
  ) {}

  ngOnInit () {
    this.playlistActions = [
      [
        {
          label: $localize`Update playlist`,
          iconName: 'edit',
          linkBuilder: playlist => [ '/my-library', '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()
    })
  }

  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.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(
        () => {
          this.reorderClientPositions()
        },

        err => this.notifier.error(err.message)
      )
  }

  onElementRemoved (element: VideoPlaylistElement) {
    const oldFirst = this.findFirst()

    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.pagination.currentPage += 1
    this.loadElements()
  }

  trackByFn (index: number, elem: VideoPlaylistElement) {
    return elem.id
  }

  isRegularPlaylist (playlist: VideoPlaylist) {
    return playlist?.type.id === VideoPlaylistType.REGULAR
  }

  showShareModal () {
    this.videoShareModal.show()
  }

  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.videoPlaylistService.removeVideoPlaylist(videoPlaylist)
      .subscribe(
        () => {
          this.router.navigate([ '/my-library', '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.videoPlaylistService.getPlaylistVideos(this.videoPlaylistId, this.pagination)
        .subscribe(({ total, data }) => {
          this.playlistElements = this.playlistElements.concat(data)
          this.pagination.totalItems = total

          this.onDataSubject.next(data)
        })
  }

  private loadPlaylistInfo () {
    this.videoPlaylistService.getVideoPlaylist(this.videoPlaylistId)
      .subscribe(playlist => {
        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)
  }
}