videos-selection.component.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { Observable, Subject } from 'rxjs'
  2. import { AfterContentInit, Component, ContentChildren, EventEmitter, Input, Output, QueryList, TemplateRef } from '@angular/core'
  3. import { ComponentPagination, Notifier, User } from '@app/core'
  4. import { logger } from '@root-helpers/logger'
  5. import { ResultList, VideosExistInPlaylists, VideoSortField } from '@shared/models'
  6. import { PeerTubeTemplateDirective, Video } from '../shared-main'
  7. import { MiniatureDisplayOptions } from './video-miniature.component'
  8. export type SelectionType = { [ id: number ]: boolean }
  9. @Component({
  10. selector: 'my-videos-selection',
  11. templateUrl: './videos-selection.component.html',
  12. styleUrls: [ './videos-selection.component.scss' ]
  13. })
  14. export class VideosSelectionComponent implements AfterContentInit {
  15. @Input() videosContainedInPlaylists: VideosExistInPlaylists
  16. @Input() user: User
  17. @Input() pagination: ComponentPagination
  18. @Input() titlePage: string
  19. @Input() miniatureDisplayOptions: MiniatureDisplayOptions
  20. @Input() noResultMessage = $localize`No results.`
  21. @Input() enableSelection = true
  22. @Input() disabled = false
  23. @Input() getVideosObservableFunction: (page: number, sort?: VideoSortField) => Observable<ResultList<Video>>
  24. @ContentChildren(PeerTubeTemplateDirective) templates: QueryList<PeerTubeTemplateDirective<'rowButtons' | 'globalButtons'>>
  25. @Output() selectionChange = new EventEmitter<SelectionType>()
  26. @Output() videosModelChange = new EventEmitter<Video[]>()
  27. _selection: SelectionType = {}
  28. rowButtonsTemplate: TemplateRef<any>
  29. globalButtonsTemplate: TemplateRef<any>
  30. videos: Video[] = []
  31. sort: VideoSortField = '-publishedAt'
  32. onDataSubject = new Subject<any[]>()
  33. hasDoneFirstQuery = false
  34. private lastQueryLength: number
  35. constructor (
  36. private notifier: Notifier
  37. ) { }
  38. @Input() get selection () {
  39. return this._selection
  40. }
  41. set selection (selection: SelectionType) {
  42. this._selection = selection
  43. this.selectionChange.emit(this._selection)
  44. }
  45. @Input() get videosModel () {
  46. return this.videos
  47. }
  48. set videosModel (videos: Video[]) {
  49. this.videos = videos
  50. this.videosModelChange.emit(this.videos)
  51. }
  52. ngAfterContentInit () {
  53. {
  54. const t = this.templates.find(t => t.name === 'rowButtons')
  55. if (t) this.rowButtonsTemplate = t.template
  56. }
  57. {
  58. const t = this.templates.find(t => t.name === 'globalButtons')
  59. if (t) this.globalButtonsTemplate = t.template
  60. }
  61. this.loadMoreVideos()
  62. }
  63. getVideosObservable (page: number) {
  64. return this.getVideosObservableFunction(page, this.sort)
  65. }
  66. abortSelectionMode () {
  67. this._selection = {}
  68. }
  69. isInSelectionMode () {
  70. return Object.keys(this._selection).some(k => this._selection[k] === true)
  71. }
  72. videoById (index: number, video: Video) {
  73. return video.id
  74. }
  75. onNearOfBottom () {
  76. if (this.disabled) return
  77. // No more results
  78. if (this.lastQueryLength !== undefined && this.lastQueryLength < this.pagination.itemsPerPage) return
  79. this.pagination.currentPage += 1
  80. this.loadMoreVideos()
  81. }
  82. loadMoreVideos (reset = false) {
  83. if (reset) this.hasDoneFirstQuery = false
  84. this.getVideosObservable(this.pagination.currentPage)
  85. .subscribe({
  86. next: ({ data }) => {
  87. this.hasDoneFirstQuery = true
  88. this.lastQueryLength = data.length
  89. if (reset) this.videos = []
  90. this.videos = this.videos.concat(data)
  91. this.videosModel = this.videos
  92. this.onDataSubject.next(data)
  93. },
  94. error: err => {
  95. const message = $localize`Cannot load more videos. Try again later.`
  96. logger.error(message, err)
  97. this.notifier.error(message)
  98. }
  99. })
  100. }
  101. reloadVideos () {
  102. this.pagination.currentPage = 1
  103. this.loadMoreVideos(true)
  104. }
  105. removeVideoFromArray (video: Video) {
  106. this.videos = this.videos.filter(v => v.id !== video.id)
  107. }
  108. }