Merge branch 'feature/audio-upload' into develop
[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
23 export type SelectionType = { [ id: number ]: boolean }
24
25 @Component({
26   selector: 'my-videos-selection',
27   templateUrl: './videos-selection.component.html',
28   styleUrls: [ './videos-selection.component.scss' ]
29 })
30 export class VideosSelectionComponent extends AbstractVideoList implements OnInit, OnDestroy, AfterContentInit {
31   @Input() titlePage: string
32   @Input() miniatureDisplayOptions: MiniatureDisplayOptions
33   @Input() getVideosObservableFunction: (page: number, sort?: VideoSortField) => Observable<{ videos: Video[], totalVideos: number }>
34   @ContentChildren(PeerTubeTemplateDirective) templates: QueryList<PeerTubeTemplateDirective>
35
36   @Output() selectionChange = new EventEmitter<SelectionType>()
37   @Output() videosModelChange = new EventEmitter<Video[]>()
38
39   _selection: SelectionType = {}
40
41   rowButtonsTemplate: TemplateRef<any>
42   globalButtonsTemplate: TemplateRef<any>
43
44   constructor (
45     protected router: Router,
46     protected route: ActivatedRoute,
47     protected notifier: Notifier,
48     protected authService: AuthService,
49     protected screenService: ScreenService,
50     protected serverService: ServerService
51   ) {
52     super()
53   }
54
55   ngAfterContentInit () {
56     {
57       const t = this.templates.find(t => t.name === 'rowButtons')
58       if (t) this.rowButtonsTemplate = t.template
59     }
60
61     {
62       const t = this.templates.find(t => t.name === 'globalButtons')
63       if (t) this.globalButtonsTemplate = t.template
64     }
65   }
66
67   @Input() get selection () {
68     return this._selection
69   }
70
71   set selection (selection: SelectionType) {
72     this._selection = selection
73     this.selectionChange.emit(this._selection)
74   }
75
76   @Input() get videosModel () {
77     return this.videos
78   }
79
80   set videosModel (videos: Video[]) {
81     this.videos = videos
82     this.videosModelChange.emit(this.videos)
83   }
84
85   ngOnInit () {
86     super.ngOnInit()
87   }
88
89   ngOnDestroy () {
90     super.ngOnDestroy()
91   }
92
93   getVideosObservable (page: number) {
94     return this.getVideosObservableFunction(page, this.sort)
95   }
96
97   abortSelectionMode () {
98     this._selection = {}
99   }
100
101   isInSelectionMode () {
102     return Object.keys(this._selection).some(k => this._selection[ k ] === true)
103   }
104
105   generateSyndicationList () {
106     throw new Error('Method not implemented.')
107   }
108
109   protected onMoreVideos () {
110     this.videosModel = this.videos
111   }
112 }