955ebca9fcdf8765bb36c658e05484704e00fb59
[oweals/peertube.git] / client / src / app / shared / video / videos-selection.component.ts
1 import {
2   AfterContentInit,
3   Component,
4   ContentChildren,
5   EventEmitter,
6   Input,
7   OnDestroy,
8   OnInit,
9   Output,
10   QueryList,
11   TemplateRef
12 } from '@angular/core'
13 import { ActivatedRoute, Router } from '@angular/router'
14 import { AbstractVideoList } from '@app/shared/video/abstract-video-list'
15 import { AuthService, Notifier, ServerService } from '@app/core'
16 import { ScreenService } from '@app/shared/misc/screen.service'
17 import { MiniatureDisplayOptions } from '@app/shared/video/video-miniature.component'
18 import { Observable } from 'rxjs'
19 import { Video } from '@app/shared/video/video.model'
20 import { PeerTubeTemplateDirective } from '@app/shared/angular/peertube-template.directive'
21 import { VideoSortField } from '@app/shared/video/sort-field.type'
22 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
23
24 export type SelectionType = { [ id: number ]: boolean }
25
26 @Component({
27   selector: 'my-videos-selection',
28   templateUrl: './videos-selection.component.html',
29   styleUrls: [ './videos-selection.component.scss' ]
30 })
31 export class VideosSelectionComponent extends AbstractVideoList implements OnInit, OnDestroy, AfterContentInit {
32   @Input() pagination: ComponentPagination
33   @Input() titlePage: string
34   @Input() miniatureDisplayOptions: MiniatureDisplayOptions
35   @Input() getVideosObservableFunction: (page: number, sort?: VideoSortField) => Observable<{ videos: Video[], totalVideos: number }>
36   @ContentChildren(PeerTubeTemplateDirective) templates: QueryList<PeerTubeTemplateDirective>
37
38   @Output() selectionChange = new EventEmitter<SelectionType>()
39   @Output() videosModelChange = new EventEmitter<Video[]>()
40
41   _selection: SelectionType = {}
42
43   rowButtonsTemplate: TemplateRef<any>
44   globalButtonsTemplate: TemplateRef<any>
45
46   constructor (
47     protected router: Router,
48     protected route: ActivatedRoute,
49     protected notifier: Notifier,
50     protected authService: AuthService,
51     protected screenService: ScreenService,
52     protected serverService: ServerService
53   ) {
54     super()
55   }
56
57   @Input() get selection () {
58     return this._selection
59   }
60
61   set selection (selection: SelectionType) {
62     this._selection = selection
63     this.selectionChange.emit(this._selection)
64   }
65
66   @Input() get videosModel () {
67     return this.videos
68   }
69
70   set videosModel (videos: Video[]) {
71     this.videos = videos
72     this.videosModelChange.emit(this.videos)
73   }
74
75   ngOnInit () {
76     super.ngOnInit()
77   }
78
79   ngAfterContentInit () {
80     {
81       const t = this.templates.find(t => t.name === 'rowButtons')
82       if (t) this.rowButtonsTemplate = t.template
83     }
84
85     {
86       const t = this.templates.find(t => t.name === 'globalButtons')
87       if (t) this.globalButtonsTemplate = t.template
88     }
89   }
90
91   ngOnDestroy () {
92     super.ngOnDestroy()
93   }
94
95   getVideosObservable (page: number) {
96     return this.getVideosObservableFunction(page, this.sort)
97   }
98
99   abortSelectionMode () {
100     this._selection = {}
101   }
102
103   isInSelectionMode () {
104     return Object.keys(this._selection).some(k => this._selection[ k ] === true)
105   }
106
107   generateSyndicationList () {
108     throw new Error('Method not implemented.')
109   }
110
111   protected onMoreVideos () {
112     this.videosModel = this.videos
113   }
114 }